numerical method mdu programs
TRANSCRIPT
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 1/36
/*to find the roots of non linear equation using bisection
method*/
#include<stdio.h>
#include<math.h>
float f (float x)
{
return (x*x*x-4*x-9);
}
void bisect (float *x,float a,float b,int *itr)
{
*x=(a+b)/2;
++(*itr);
printf("iteration no. %3d x=%7.5\n",*itr,*x);
}
main()
{
int itr=0,maxitr;
float x,a,b,x1,aerr;
printf("enter value of a,b,""allowed error,maximum iteration\n");
scanf("%f%f%f%d",&a,&b,&aerr,&maxitr);
bisect (&x,a,b,&itr);
do
{
if(f(a)*f(x)<0)
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 2/36
b=x;
else
a=x;
bisect (&x1,a,b,&itr);
if (fabs (x1-x)<aerr)
{
printf("after %d iteration ,root""=%6.4f\n",itr,x1);
return 0;
}
x=x1;
}
while (itr<maxitr);
printf("soln not converges,""iteration not sufficient\n");
return (1);
}
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 3/36
/*result*/
enter value of a,b,allowed error,maximum iteration
2 4 .0001 20
iteration no. 1 x=%7.5
iteration no. 2 x=%7.5
iteration no. 3 x=%7.5
iteration no. 4 x=%7.5
iteration no. 5 x=%7.5
iteration no. 6 x=%7.5
iteration no. 7 x=%7.5
iteration no. 8 x=%7.5
iteration no. 9 x=%7.5
iteration no. 10 x=%7.5
iteration no. 11 x=%7.5
iteration no. 12 x=%7.5
iteration no. 13 x=%7.5
iteration no. 14 x=%7.5
iteration no. 15 x=%7.5
after 15 iteration ,root=2.7065
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 4/36
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 5/36
{
printf("Newton algorithm didn't converge after %d steps.\n",iters);
printf("The final estimate was %19.16e\n", x);
printf("f(%19.16e) = %19.16e\n", x, f(x));
}
return 0;
} /* main */
double newton(double x_0, double allowed_error, int max_iterations,
int* iters_p, int* converged_p)
{
double x = x_0;
double x_prev;
int iter = 0;
do
{
iter++;
x_prev = x;
x = x_prev - f(x_prev)/f_prime(x_prev);
}
while (fabs(x - x_prev) > allowed_error && iter < max_iterations);
if (fabs(x - x_prev) <= allowed_error)
*converged_p = 1;
else
*converged_p = 0;
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 6/36
*iters_p = iter;
return x;
} /* newton algorithm */
double f(double x) {
return x*x-2;
} /* f */
double f_prime(double x) {
return 2*x; //the derivative
} /* f_prime */
/*result*/
Enter x_0, allowed_error, and max_iterations
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 7/36
2 0.000001 10
Newton algorithm converged after 5 steps.
The approximate solution is 1.4142135623730951e+00
f(1.4142135623730951e+00) = 2.7343578790084422e-16
Enter x_0, allowed_error, and max_iterations
3 0.000001 10
Newton algorithm converged after 5 steps.
The approximate solution is 1.4142135623731118e+00
f(1.4142135623731118e+00) = 4.7376165490664590e-14
Enter x_0, allowed_error, and max_iterations
4 0.000001 10
Newton algorithm converged after 6 steps.
The approximate solution is 1.4142135623730951e+00
f(1.4142135623730951e+00) = 2.7343578790084422e-16
Enter x_0, allowed_error, and max_iterations
5 0.000001 10
Newton algorithm converged after 6 steps.
The approximate solution is 1.4142135623730951e+00
f(1.4142135623730951e+00) = 2.7343578790084422e-16
Enter x_0, allowed_error, and max_iterations
6 0.000001 10
Newton algorithm converged after 6 steps.
The approximate solution is 1.4142135623732204e+00
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 8/36
/*curve fitting by least square approximation*/
#include <stdio.h>
main()
{
float augm[3][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0}};
float t,a,b,c,x,y,xsq;
int i,j,k,n;
puts("enter the no. of pairs of "" observed values:");
scanf("%d",&n);
augm[0][0]=n;
for (i=0;i<n;i++)
{
printf("pair no.%d\n",i+1);
scanf("%f%f",&x,&y);
xsq=x*x;
augm[0][1]+=x;
augm[0][2]+=xsq;
augm[1][2]+=x*xsq;
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 9/36
augm[2][2]+=xsq*xsq;
augm[0][3]+=y;
augm[1][3]+=x*y;
augm[2][3]+=xsq*y;
}
augm[1][1]=augm[0][2];
augm[2][1]=augm[1][2];
augm[1][0]=augm[0][1];
augm[2][0]=augm[1][1];
puts("the augmented matrix is :-");
for (i=0;i<3;i++)
{
for (j=0;j<4;j++)
printf("%9.4f",augm[i][j]);
printf("\n");
}
for (j=0;j<3;j++)
for(i=0;i<3;i++)
if(i!=j)
{
t=augm[i][j]/augm[j][j];
for (k=0;k<4;k++)
augm[i][k]-=augm[j][k]*t;
}
a=augm[0][3]/augm[0][0];
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 10/36
b=augm[1][3]/augm[1][1];
c=augm[2][3]/augm[2][2];
printf("a = %8.4f b = %8.4f""c = %8.4f\n",a,b,c);}
/*result*/
enter the no. of pairs of observed values:
8
pair no.1
1 1.3
pair no.2
2 2.5
pair no.3
1.1 2.3
pair no.4
2.6 2.8
pair no.5
3.1 3.5
pair no.6
4.1 4.3
pair no.7
6.1 5.2
pair no.8
3.1 3.7
the augmented matrix is :-
8.0000 23.1000 86.2100 25.6000
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 11/36
23.1000 86.2100 383.3910 87.7800
86.2100 383.39101916.0260 367.9780
a = 0.6307 b = 1.1016 c = -0.0568
/*to solve system of linear equation using gauss elimination
method*/
#include<stdio.h>
#define N 4
main()
{
float a[N][N+1],x[N],t,s;
int i,j,k;
printf("enter the elements of the""argumented matrix rowwise\n");
for(i=0;i<N;i++)
for(j=0;j<N+1;j++)
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 12/36
scanf("%f",&a[i][j]);
for(j=0;j<N-1;j++)
for(i=j+1;i<N;i++)
{
t= a[i][j]/a[i][j];
for (k=0;k<N+1;k++)
a[i][k]-=a[j][k]*t;
}
printf("the upper triangular matrix""is :-\n");
for (i=0;i<N;i++)
{
for (j=0;j<N+1;j++)
printf("%8.4f",a[i][j]);
printf("\n");
}
for (i=N-1;i>=0;i--)
{
s=0;
for (j=i+1;j<N;j++)
s+=a[i][j]*x[j];
x[i]=(a[i][N]-s)/a[i][i];
}
printf("the soln is :-\n");
for (i=0;i<N;i++)
printf("x[%3d]=%7.4f\n",i+1,x[i]);
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 13/36
getch();
}
/*result*/
enter the elements of the argumented matrix rowwise
10 -3 7 5 9
-6 8 2 -1 -4
2 5 3 7 6 1
3 -1 -2 5 8
the upper triangular matrixis :-
10.0000 -3.0000 7.0000 5.0000 9.0000
-16.0000 11.0000 -5.0000 -6.0000 -13.0000
8.0000 -3.0000 1.0000 8.0000 10.0000
-1.0000 -2.0000 -4.0000 -9.0000 -1.0000
the soln is :-
x[ 1]=-4.6273
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 14/36
x[ 2]= 3.0202
x[ 3]= 9.1111
x[ 4]= 0.1111
/*to solve system of linear equation using Gauss Seidal
method*/
#include<stdio.h>
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 15/36
#include<math.h>
#define N 4
main()
{
float a[N][N+1],x[N],aerr,maxerr,t,s,err;
int i,j,itr,maxitr;
for(i=0;i<N;i++) x[i]=0;
printf("enter the elements of the""argumented matrix rowwise\n");
for(i=0;i<N;i++)
for(j=0;j<N+1;j++)
scanf("%f",&a[i][j]);
printf("enter the allowed error,""maximum iterations\n");
scanf("%f%d",&aerr,&maxitr);
printf("iterations x[1] x[2]"" x[3]\n");
for (itr=1;itr<=maxitr;itr++)
{
maxerr=0;
for (i=0;i<N;i++)
{
s=0;
for(j=0;j<N;j++)
{
if(j!=i) s+= a[i][j]*x[j];
t=(a[i][N]-s)/a[i][i];
err=fabs(x[i]-t);
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 16/36
if (err>maxerr) maxerr=err;
x[i]=t;
}
printf("%5d",itr);
for (i=0;i<N;i++)
printf("%9.4f",x[i]);
printf("\n");
if (maxerr<aerr)
{
printf("converges in %3d""iteration\n",itr);
for (i=0;i<N;i++)
printf("x[%3d]=%7.4f\n",i+1,x[i]);
return 0;
}
}
printf("the soln does not converges,""iteration not sufficient\n");
return(1);}}
/*RESULT*/
enter the elements of the argumented matrix rowwise
10 -2 1 -1 3
-2 10 -1 -1 15
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 17/36
-1 -1 10 -2 27
-1 -1 -2 10 -9
Enter the allowed error,maximum iterations
.0001 15
Iteration x[1] x[2] x[3] x[4]
1 0.30000 1.5600 2.8860 -0.1368
2 0.8869 1.9523 2.9566 -0.0248
3 0.9836 1.9899 2.9924 -0.0042
4 0.9968 1.9982 2.9987 -0.0008
5 0.9994 1.9997 2.9998 -0.0001
6 0.9999 1.9999 3.0000 -0.0000
7 1.0000 2.0000 3.0000 -0.0000
converges in 7 iterations
x[ 1]= 1.0000
x[ 2]= 2.0000
x[ 3]= 3.0000
x[ 4]= -0.0000
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 18/36
/*to solve system of linear equation using Gauss Jordan
method*/
#include<stdio.h>
int main()
{
double matrix[10][10],a,b;
int i, j, k, n;
printf("Enter the no of variables: ");
scanf("%d", &n);
printf("Enter the agumented matrix:\n");
for(i = 0; i < n ; i++)
{
for(j = 0; j < (n+1); j++)
{
scanf("%lf", &matrix[i][j]);
}
}
for(i = 0; i < n; i++)
{
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 19/36
for(j = 0; j < n; j++)
{
if(i != j)
{
a = matrix[j][i];
b = matrix[i][i];
for(k = 0; k < n+1; k++)
{
matrix[j][k] = matrix[j][k] - (a/b) * matrix[i][k];
}
}
}
}
for(i = 0; i < n; i++)
{
a = matrix[i][i];
for(j = 0; j < n+1; j++)
{
matrix[i][j] /= a;
}
}
printf("The required solution is: \n\n");
for(i = 0; i < n ; i++){
printf("%c => %.2f", i+97, matrix[i][n]);
printf("\n");
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 20/36
}
return 0;}
/*result*/
Enter the no of variables: 3
Enter the agumented matrix:
2 4 5 6
4 6 1 6
4 7 2 8
The required solution is:
a => -1.14
b => 1.71
c => 0.29
Enter the no of variables: 3
Enter the agumented matrix:
1 -2 -4 -1
2 -8 2 -7
-1 2 5 9
The required solution is:
a => 73.50
b => 21.25
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 21/36
c => 8.00
/*integration of given function using Trapezoidal rule*/
#include<stdio.h>
float y(float x)
{
return 1/(1+x*x);
}
int main()
{
float x0,xn,h,s;
int i,n;
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 22/36
printf("Enter x0, xn, no. of subintervals: ");
scanf("%f%f%d",&x0,&xn,&n);
h = (xn-x0)/n;
s = y(x0) + y(xn);
for(i = 1; i < n; i++)
{
s += 2*y(x0+i*h);
}
printf("Value of integral is %6.4f\n",(h/2)*s);
return 0;
}
/*RESULT*/
Enter x0, xn, no. of subintervals:
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 23/36
45 12 10
Value of integral is -0.0619
Enter x0, xn, no. of subintervals:
10 23 1.01
Value of integral is 0.0766
Enter x0, xn, no. of subintervals:
1.234 12 5
Value of integral is 0.7325
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 24/36
/*integration of given function using Simpson's 3/8 rule*/
#include<stdio.h>
float y(float x)
{
return 1/(1+x*x); //function of which integration is to be calculated
}
int main(){
float x0,xn,h,s;
int i,n,j,flag;
printf("Enter x0, xn, no. of subintervals: ");
scanf("%f%f%d",&x0,&xn,&n);
h = (xn-x0)/n;
s = y(x0)+y(xn);
for(i = 1; i<=n-1;i++)
{
for(j=1;j<=n-1;j++)
{
if(i==3*j)
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 25/36
{
flag = 1;
break;
}
else
flag = 0;
}
if(flag==0)
s += 3*y(x0+i*h);
else
s += 2*y(x0+i*h);
}
printf("Value of integral is %6.4f\n",(3*h/8)*s);
return 0;
}
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 26/36
/*RESULT*/
Enter x0, xn, no. of subintervals:
23 10 21
Value of integral is -0.0562
Enter x0, xn, no. of subintervals:
12 13 2
Value of integral is 0.0060
Enter x0, xn, no. of subintervals:
0.02 1.3 3.6
Value of integral is 0.8919
Enter x0, xn, no. of subintervals:
2.45 1.3 -1.2
Value of integral is 0.2219
/*TO FIND LARGEST EIGEN VALUE OF A MATRIX BY POWER
METHOD*/
#include<stdio.h>
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 27/36
#include<math.h>
#include<stdlib.h>
void matrix_mul(float matrix1[3][3],float matrix2[3][1],float matrix3[3][1])
{
int i,j,k;
for(i = 0; i < 3; i++)
{
for(j = 0; j < 1; j++)
{
matrix3[i][j] = 0;
for(k = 0; k < 3; k++)
{
matrix3[i][j] += matrix1[i][k]*matrix2[k][j];
}
}
}
}
float findmax(float array[3][1])
{
int i;
float maximum;
maximum = array[0][0];
for(i = 0; i < 3; i++){
if(maximum < array[i][0])
maximum = array[i][0];
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 28/36
}
return maximum;
}
int isEqual(float matrix1[3][1], float matrix2[3][1])
{
if(matrix1[0][0] == matrix2[0][0]
&& matrix1[1][0] == matrix2[1][0]
&& matrix1[2][0] == matrix2[2][0])
return 1;
return 0;
}
int main()
{
float matrix1[3][3], matrix2[3][1],result[3][1];
float eigenValue;
int i,j,k;
printf("Enter 3x3 matrix:\n");
for(i = 0; i < 3; i++)
{
for(j = 0; j < 3; j++)
{
scanf("%f",&matrix1[i][j]);
}
}
for(i = 0; i < 3; i++){
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 29/36
for(j = 0; j < 1; j++)
{
matrix2[i][j] = 1.0;
}
}
while(1)
{
matrix_mul(matrix1,matrix2,result);
eigenValue = findmax(result);
for(i = 0; i < 3; i++)
{
result[i][0]/=eigenValue;
}
if(isEqual(matrix2,result)==1)
{
break;
}
for(i = 0; i < 3; i++)
{
matrix2[i][0] = result[i][0];
}
}
printf("Greatest Eigen Value = %f", eigenValue);
printf("\nAny one of Eigen Vector: \n");
for(i = 0; i < 3; i++)
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 30/36
{
for(j = 0; j < 1; j++)
{
printf("%.2f",result[i][j]);
printf("\t");
}
printf("\n");
}
return 0;
}
/*RESULT*/
Enter 3x3 matrix:
2 5 8
2 -1 4
3 -5 3
Greatest Eigen Value = 6.274918
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 31/36
Any one of Eigen Vector:
1.00
0.42
0.27
/*TO FIND NUMERICAL SOLUTION OF DIFFERENTIAL
EQUATION BY RUNGE –KUTTA METHOD*/
#include <stdio.h>
float f(float x,float y)
{
return x+y*y;
}
main()
{
float x,x0,y0,h,xn,y,k1,k2,k3,k4,k;
printf("enter the value of x0,y0,""h,xn\n");
scanf("%f%f%f%f",&x0,&y0,&h,&xn);
x=x0;y=y0;
while(1)
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 32/36
{
if(x==xn) break;
k1=h*f(x,y);
k2=h*f(x+h/2,y+k1/2);
k3=h*f(x+h/2,y+k2/2);
k4=h*f(x+h,y+k3);
k=(k1+(k2+k3)*2+k4)/6;
x+=h;y+=k;
printf("when x = %8.4f""y = %8.4f\n",x,y);}}
/*RESULT*/
enter the value of x0,y0,h,xn
0.0 1.0 0.1 0.2
when x = 0.1000y = 1.1165
when x = 0.2000y = 1.2736
enter the value of x0,y0,h,xn
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 33/36
1.0 2.0 1.1 1.2
when x = 2.1000y = 12484.0469
Floating point error: Overflow.
Abnormal program termination
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 34/36
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 35/36
8/2/2019 numerical method mdu programs
http://slidepdf.com/reader/full/numerical-method-mdu-programs 36/36