• API
• FAQ
• Tools
• Archive
SHARE
TWEET

# NM_10

a guest Feb 26th, 2020 171 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. #include<stdio.h>
2. #include<conio.h>
3. #include<math.h>
4.
5. #define f1(x,y,z)  (17-y+2*z)/20
6. #define f2(x,y,z)  (-18-3*x+z)/20
7. #define f3(x,y,z)  (25-2*x+3*y)/20
8. #define EPS 0.001
9.
10. int main()
11. {
12.     float x0=0, y0=0, z0=0, x1, y1, z1, e1, e2, e3, e;
13.     int count=1;
14.     printf("\nCount\tx\ty\tz\n");
15.     do
16.         {
17.             /* Calculation */
18.             x1 = f1(x0,y0,z0);
19.             y1 = f2(x0,y0,z0);
20.             z1 = f3(x0,y0,z0);
21.             printf("%d\t%0.4f\t%0.4f\t%0.4f\n",count, x1,y1,z1);
22.
23.             /* Error */
24.             e1 = fabs(x0-x1);
25.             e2 = fabs(y0-y1);
26.             e3 = fabs(z0-z1);
27.
28.             count++;
29.
30.             /* Set value for next iteration */
31.               x0 = x1;
32.               y0 = y1;
33.               z0 = z1;
34.         }while(e1>e && e2>e && e3>e);
35.
36.         printf("\nSolution: x=%0.3f, y=%0.3f and z = %0.3f\n",x1,y1,z1);
37.
38.     getch();
39.     return 0;
40. }
41.
42.
43.
44. #include<stdio.h>
45. #include<conio.h>
46. #include<math.h>
47.
48. /* Arrange systems of linear
49.    equations to be solved in
50.    diagonally dominant form
51.    and form equation for each
52.    unknown and define here
53. */
54. /* In this example we are solving
55.    3x + 20y - z = -18
56.    2x - 3y + 20z = 25
57.    20x + y - 2z = 17
58. */
59. /* Arranging given system of linear
60.    equations in diagonally dominant
61.    form:
62.    20x + y - 2z = 17
63.    3x + 20y -z = -18
64.    2x - 3y + 20z = 25
65. */
66. /* Equations:
67.    x = (17-y+2z)/20
68.    y = (-18-3x+z)/20
69.    z = (25-2x+3y)/20
70. */
71. /* Defining function */
72. #define f1(x,y,z)  (17-y+2*z)/20
73. #define f2(x,y,z)  (-18-3*x+z)/20
74. #define f3(x,y,z)  (25-2*x+3*y)/20
75. #define e 0.001
76. /* Main function */
77. int main()
78. {
79.  float x0=0, y0=0, z0=0, x1, y1, z1, e1, e2, e3;
80.  int count=1;
81.
82.  printf("\nCount\tx\ty\tz\n");
83.  do
84.  {
85.   /* Calculation */
86.   x1 = f1(x0,y0,z0);
87.   y1 = f2(x1,y0,z0);
88.   z1 = f3(x1,y1,z0);
89.   printf("%d\t%0.4f\t%0.4f\t%0.4f\n",count, x1,y1,z1);
90.
91.   /* Error */
92.   e1 = fabs(x0-x1);
93.   e2 = fabs(y0-y1);
94.   e3 = fabs(z0-z1);
95.
96.   count++;
97.
98.   /* Set value for next iteration */
99.   x0 = x1;
100.   y0 = y1;
101.   z0 = z1;
102.
103.  }while(e1>e && e2>e && e3>e);
104.
105.  printf("\nSolution: x=%0.3f, y=%0.3f and z = %0.3f\n",x1,y1,z1);
106.
107.  getch();
108.  return 0;
109. }
110.
111.
112.
113. #include<stdio.h>
114. #include<conio.h>
115. #include<math.h>
116. #define MAXIT 10
117. #define EPS 0.001
118. int main()
119. {
120.     int i,j,n,A[20][20],X[20][2],count=1;
121.     float Xo[20][2],Xn[20][2],Xmax=0;
122.     printf("Enter the size of the matrix A : ");scanf("%d",&n);
123.     printf("\nEnter the elements of the %d*%d matrix A\n",n,n);
124.     for(i=1;i<=n;i++)
125.     {for(j=1;j<=n;j++)
126.         {printf("\nA[%d][%d] : ",n,n);scanf("%d",&A[i][j]);}
127.     }
128.
129.     printf("\nEnter the initial Guess Vector X : \n");
130.     for(i=1;i<=n;i++)
131.     {
132.         printf("Enter X[%d][1] : ",i);scanf("%d",&X[i][1]);
133.     }
134.
135.     Xo[1][1]=A[1][1]*X[1][1]+A[1][2]*X[2][1]+A[1][3]*X[3][1];
136.     Xo[2][1]=A[2][1]*X[1][1]+A[2][2]*X[2][1]+A[2][3]*X[3][1];
137.     Xo[3][1]=A[3][1]*X[1][1]+A[3][2]*X[2][1]+A[3][3]*X[3][1];
138.
139.     for(i=1;i<=n;i++)
140.     {
141.         if(Xo[i][1]>Xmax)
142.         {Xmax = Xo[i][1];}
143.     }
144.
145.     Xn[1][1] = Xo[1][1]/Xmax;
146.     Xn[2][1] = Xo[2][1]/Xmax;
147.     Xn[3][1] = Xo[3][1]/Xmax;
148.     count++;
149.     if((fabs(Xn[1][1]-Xo[1][1])<EPS)&&(fabs(Xn[1][1]-Xo[1][1])<EPS)&&(fabs(Xn[1][1]-Xo[1][1])<EPS)||count<MAXIT)
150.     printf("\nThe Required Eigen Value is : %f",Xmax);
151.     printf("\nThe Required Eigen Vector is :\n");
152.     printf("\n|%.3f|",Xn[1][1]);
153.     printf("\n|%.3f|",Xn[2][1]);
154.     printf("\n|%.3f|",Xn[3][1]);
155.     getch();
156.     return 0;
157. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.
Top