Numerical Analysis
Numerical Analysis
}
Out-Put:
Enter two initial guesses:
0 1
Enter tolerable error:
0.0001
Step x0 x1 x2 f(x2)
0 0.0000 1.0000 0.5000 0.0532
1 0.5000 1.0000 0.7500 -0.8561
2 0.5000 0.7500 0.6250 -0.3567
3 0.5000 0.6250 0.5625 -0.1413
4 0.5000 0.5625 0.5313 -0.0415
5 0.5000 0.5313 0.5156 0.0065
6 0.5156 0.5313 0.5234 -0.0174
7 0.5156 0.5234 0.5195 -0.0054
8 0.5156 0.5195 0.5176 0.0005
9 0.5176 0.5195 0.5186 -0.0024
10 0.5176 0.5186 0.5181 -0.0009
11 0.5176 0.5181 0.5178 -0.0002
12 0.5176 0.5178 0.5177 0.0002
13 0.5177 0.5178 0.5178 -0.0000
}
Out-Put:
Enter two initial guesses:
2
3
Enter tolerable error:
0.0001
Step x0 x1 x2 f(x2)
0 2.000000 3.000000 2.721014 -0.017091
1 2.721014 3.000000 2.740206 -0.000384
2 2.740206 3.000000 2.740636 -0.000009
for(k=1;k<=n+1;k++)
{
a[j][k] = a[j][k] - R*a[i][k];
}
}
}
x[n] = a[n][n+1]/a[n][n];
for(i=n-1;i>=1;i--)
{
x[i] = a[i][n+1];
for(j=i+1;j<=n;j++)
{
x[i] = x[i] - a[i][j]*x[j];
}
x[i] = x[i]/a[i][i];
}
printf("\nSolution:\n");
for(i=1;i<=n;i++)
{
printf("x[%d] = %0.3f\n",i, x[i]);
}
}
Out-Put:
Enter number of unknowns: 3
Enter Elements of Augmented Matrix:
a[1][1] = 1
a[1][2] = 2
a[1][3] = -1
a[1][4] = 3
a[2][1] = 2
a[2][2] = 5
a[2][3] = 2
a[2][4] = -3
a[3][1] = 4
a[3][2] = -2
a[3][3] = 1
a[3][4] = 12
Solution:
x[1] = 3.000
x[2] = -1.000
x[3] = -2.000
Question5: Write and run the program in any software to solve the following system of
linear equations 𝟑𝒙 + 𝟐𝟎𝒚 − 𝒛 = −𝟏𝟖, 𝟐𝒙 − 𝟑𝒚 + 𝟐𝟎𝒛 = 𝟐𝟓, 𝟐𝟎𝒙 + 𝒚 − 𝟐𝒛 = 𝟏𝟕 by
Gauss Seidel Method correct up to 3D places.
C-Code:
/* Gauss-Seidel Iteration Method */
#include<stdio.h>
#include<math.h>
/* In this example we are solving
3x + 20y - z = -18, 2x - 3y + 20z = 25, 20x + y - 2z = 17 */
/* Arranging given system of linear equations in diagonally dominant form:
20x + y - 2z = 17, 3x + 20y -z = -18, 2x - 3y + 20z = 25 */
/* Equations: x = (17-y+2z)/20, y = (-18-3x+z)/20, z = (25-2x+3y)/20 */
/* Defining function */
#define f1(x,y,z) (17-y+2*z)/20
#define f2(x,y,z) (-18-3*x+z)/20
#define f3(x,y,z) (25-2*x+3*y)/20
int main()
{
float x0=0, y0=0, z0=0, x1, y1, z1, e1, e2, e3, e;
int count=0;
printf("Enter tolerable error:\n");
scanf("%f", &e);
printf("\nCount\t\tx\t\ty\t\tz\n");
do
{
/* Calculation */
x1 = f1(x0,y0,z0);
y1 = f2(x1,y0,z0);
z1 = f3(x1,y1,z0);
printf("%d\t\t%0.4f\t\t%0.4f\t\t%0.4f\n",count, x1,y1,z1);
/* Errors */
e1 = fabs(x0-x1);
e2 = fabs(y0-y1);
e3 = fabs(z0-z1);
count++;
/* Set value for next iteration */
x0 = x1;
y0 = y1;
z0 = z1;
}while(e1>e || e2>e || e3>e);
printf("\nSolution: x=%0.3f, y=%0.3f and z = %0.3f\n",x1,y1,z1);
}
Out-Put:
Enter tolerable error:
0.0001
Count x y z
0 0.8500 -1.0275 1.0109
1 1.0025 -0.9998 0.9998
2 1.0000 -1.0000 1.0000
3 1.0000 -1.0000 1.0000
Out-Put:
Enter lower limit (a) of integration: 0
Enter upper limit (b) of integration: 1
Enter number (n) of sub intervals: 12
Out-Put:
Enter lower limit (a) of integration: 1
Enter upper limit (b) of integration: 2
Enter number (n) of sub intervals: 14
Step x0 y0 slope yn
-----------------------------------------------
0 0.0000 1.0000 1.0000 1.1000
1 0.1000 1.1000 1.2000 1.2200
2 0.2000 1.2200 1.4200 1.3620
3 0.3000 1.3620 1.6620 1.5282
4 0.4000 1.5282 1.9282 1.7210
5 0.5000 1.7210 2.2210 1.9431
6 0.6000 1.9431 2.5431 2.1974
7 0.7000 2.1974 2.8974 2.4872
8 0.8000 2.4872 3.2872 2.8159
9 0.9000 2.8159 3.7159 3.1875
-----------------------------
step x0 y0 yn
0 0.0000 1.0000 1.1155
1 0.1000 1.1155 1.2708
-----------------------------
step x0 y0 yn
0 0.0000 1.0000 1.1165
1 0.1000 1.1165 1.2736