Vous êtes sur la page 1sur 29

INDEX

SNO. 1. 2.

EXPERIMENT
To find the real roots of the equation f(x)=0 by Newton Raphson method.

DATE
2 Feb11 9th Feb11
nd

REMARK

To find the real roots of equation f(x)=0 by Bisection method.

3. 4. 5. 6. 7. 8. 9. 10. 11.

To find the area under the equation 16th Feb11 f(x) by Trapezoidal rule. To find the area under the equation 23th Feb11 f(x) by Simpsons one- third rule. To find the area under the equation 23th Feb11 f(x) by Simpsons three-eighth rule. To fit a straight line to the given set 9st March11 of values using Least Square method. To fit a parabola to the given set of values using Least Square method. To solve a system of simultaneous algebraic equations given by Gauss Elimination method. To solve a system of simultaneous algebraic equations given by Gauss Jordan method. To solve a system of simultaneous algebraic equations given by Gauss Seidal method. To find the solution of ordinary differential equation by Eulers method 16th March11 16rd March11 23rd March11 30th March11 6th April 11

EXPERIMENT NO.1
Aim:
To find the real roots of the equation f(x)=0 by Newton Raphson method. Here f(x)= x3- 2x- 1

Formula used:
Xn+1=Xn-f(Xn)/f(Xn)

Working Procedure:
Let x0 be the approximate solution of the equation f(x)=0 Let x1=x0+h be the exact solution of equation of f(x)=0 Therefore, f(x1)=0 f(x0+h)=0 By Taylor series f(x0+h)=f(x0)+hf(x0)+hf(x0)+.. Neglecting higher powers f(x0)+hf(x0)=0 h=-f(x0)/f(x0) X1=x0+h X1=x0-f(x0)/f(x0) So in general Xn+1=Xn-f(Xn)/f(Xn)

Implementation using c:
//Newton Raphson Method #include<stdio.h> #include<conio.h> #include<math.h> float f(float x) { return(x*x*x-2*x-1); } float df(float x) { return(3*x*x-2); } void main() { int count=0; float x0=0, x1=1,h=1,err,x2=0; clrscr(); while(f(x0)*f(x1)>0.0) { x0++;

x1=x0+1; } printf("the interval in which root lies is %f %f", x0,x1); printf("enter the allowed error"); scanf("%f", &err); while(fabs(h)>err) { if(df(x0)==0.0) { printf("method fail\n"); break; } else { count++; h=(f(x0)/df(x0)); x1=x0-h; x0=x1; } printf("\nin iteration %d the value of x1= %f",count,x1); printf("\nthe root of equation is %f ",x1);} getch(); }

Output:
the interval in which root lies is 1.000000 2.000000 enter the allowed error0.001 in iteration 1 the value of x1= 3.000000 the root of equation is 3.000000 in iteration 2 the value of x1= 2.200000 the root of equation is 2.200000 in iteration 3 the value of x1= 1.780831 the root of equation is 1.780831 in iteration 4 the value of x1= 1.636303 the root of equation is 1.636303 in iteration 5 the value of x1= 1.618305 the root of equation is 1.618305 in iteration 6 the value of x1= 1.618034 the root of equation is 1.618034

EXPERIMENT NO.2
Aim: To find the real roots of equation f(x)=0 by Bisection method. Here f(x)=ex-x3 Formula used: Xn= (limit1+limit2)/2
limits of the interval in which the function is continous.

Working procedure:
Principle- it is based on repeated application of the intermediate value property for the root to exist in the given interval [a,b]. Intermediate value property if the function is continous in interval [a,b] then there exists a point/root lying between a & b such that 1..x1=(a+b)/2 now if

f(x1)>0
then the root lies between x1 and b now and thus

x2=(x1+b)/2 2.f(x1)<0
Then the root lies between a and x1 And thus

X2=(a+x1)/2
And so on till a converging solution is obtained.

Implementation using c:
//Bisection Method #include<stdio.h> #include<conio.h> #include<math.h> #include<stdlib.h> float f(float x) { return(exp(x)-x*3); }

void main() { int i=0; float x1,x2,x3,err; clrscr(); printf("enter the first approximation x1\n"); scanf("%f",&x1); printf("enter the second approximation x2\n"); scanf("%f",&x2); if(f(x1)*f(x2)) printf("the approximations are correct"); else { printf("the approximations are wrong "); getch(); exit(0); } printf("\nenter the allowed error for the solution \n"); scanf("%f",&err); while (fabs(x2-x1)>err) { i++; x3=((x1+x2)/2); if(f(x1)*f(x3)<0) { x2=x3; printf("\nin iteration %d x1=%f and x2=%f \n",i,x1,x2); printf("\n"); } else { x1=x3; printf("\nin iteration %d x1=%f and x2=%f \n",i,x1,x2); printf("\n"); } } printf("the solution coverges in iteration %d \n",i); printf("\nthe solution is %f",x3); getch(); }

Output:
enter the first approximation x1 0 enter the second approximation x2 1

the approximations are correct enter the allowed error for the solution 0.001 in iteration 1 x1=0.500000 and x2=1.000000 in iteration 2 x1=0.500000 and x2=0.750000 in iteration 3 x1=0.500000 and x2=0.625000 in iteration 4 x1=0.562500 and x2=0.625000 in iteration 5 x1=0.593750 and x2=0.625000 in iteration 6 x1=0.609375 and x2=0.625000 in iteration 7 x1=0.617188 and x2=0.625000 in iteration 8 x1=0.617188 and x2=0.621094 in iteration 9 x1=0.617188 and x2=0.619141 in iteration 10 x1=0.618164 and x2=0.619141 the solution coverges in iteration 10 the solution is 0.618164

EXPERIMENT NO.3
Aim:
To find the area under the equation f(x) by Trapezoidal rule. Here 6

f(x)=
0

1/(1+x2).dx

Formula used: x0+nh f(x).dx = h/2[(y0+yn)+2(y1+y2+y3++yn-1)] x0 Working procedure:


Putting n=1 in above equation and taking the curve through (x0 ,y0) and (x1 ,y1) as a straight line we get various trapeziums of the area under the curve. These are found separately. Then the area under the ordinates at x0 and x0+nh is approximately equal to the sum of the areas of the n trapeziums.

Implementation using c:
//Trapeziodal Rule #include<stdio.h> #include<conio.h> #include<math.h> float y(float x) { return(1/(1+(x*x))); } void main() { int i,n; float x0,xn,h,s; clrscr(); printf("enter the value of x0,xn,n\n"); scanf("%f%f%d",&x0,&xn,&n); h=(xn-x0)/n; s=y(x0)+y(xn); for(i=1;i<=n-1;i++) s+=2*y(x0+(i*h)); printf("the value of integral is %f", ((h/2)*s));

getch(); }

Output:
enter the value of x0,xn,n 0 6 6 the value of integral is 1.410799

EXPERIMENT NO.4
Aim:
To find the area under the equation f(x) by Simpsons one- third rule. Here 4

f(x) =
0

ex.dx

Formula used: x0+nh x0 f(x).dx=h/3[(y0+yn)+4(y1+y3++yn-1)+ 2(y2+y4+..+yn-2]

Working procedure:
Putting n=2 in above formula and taking the curve through (x0,y0), (x1,y1) and (x2, y2) as a parabola the given interval is divided into even no.of subintervals and we find area of two strips at one time. At end adding these areas we get the approximate area under the curve.

Implementation using c:
//Simpsons 1/3rd rule #include<stdio.h> #include<conio.h> #include<math.h> float y(float x) { return(exp(x)); } void main() { int i,n; float x0,xn,h,s; clrscr(); printf("enter the values of x0,xn,n\n" ); scanf("%f%f%d",&x0,&xn,&n ); h=(xn-x0)/n; s=y(x0)+y(xn)+4*(y(x0+h)); for(i=3;i<=n-1;i+=2)

s+=4*y(x0+i*h)+2*y(x0+(i-1)*h); printf(" the integral is equal to %f \n",(h/3)*s); getch(); }

Output:
enter the values of x0,xn,n 0 4 4 the integral is equal to 53.863846

EXPERIMENT NO.5
Aim:
To find the area under the equation f(x) by Simpsons three-eighth rule. Here
4

f(x) =
0

1/(1+x2).dx

Formula used: x0+nh x0 f(x).dx=3h/8[(y0+yn)+3(y1+y2+y3++yn-1)+ 2(y3+y6+..+yn-3]

Working procedure:
Putting n=2 in above formula and taking the curve through (x0,y0), (x1,y1), (x2, y2) and (x3,y3) as a parabola. The number of subintervals should be taken as a multiple of 3. At end adding these areas we get the approximate area under the curve.

Implementation using c:
//Simpson's 3/8th Rule #include<stdio.h> #include<conio.h> #include<math.h> float y(float x) { return(1/(1+x*x)); } void main() { int i,n; float x0,xn,h,s; clrscr(); printf("Enter the values of xo,xn,n\n"); scanf("%f%f%d",&x0,&xn,&n); h=(xn-x0)/n; s=y(x0)+y(xn); for(i=1;i<=n-1;i++);

s+=3*y(x0+i*h); for(i=3;i<=n-3;i+=3) s+=2*y(x0+i*h); s-=3*y(x0+i*h); printf("value of integral is %f ",(3*h/8)*s); getch(); }

Output:
Enter the values of xo,xn,n 0 1 6 value of integral is 0.193750

EXPERIMENT NO.6
Aim:
To fit a straight line to the given set of values using Least Square method. Here the values are given as

x
1 2 3 4 5

y
14 13 9 5 2

Formula used:
Equation of straight line:

y=a+bx
Normal equations are:

y=a x + nb xy=ax+bx Working procedure:


(i) (ii) (iii) (iv) Substitute the observed set of n values in the equation. Form normal equations for each constant(a and b). Solve these normal equations as simultaneous equations for a and b. Substitute the values of a and b in y=a+bx, which is the required line of best fit.

Implementation using c:
//Fit a straight line using Least Square Method #include<stdio.h> #include<conio.h> #include<math.h> void main() { float aug[2][3]={{0,0,0},{0,0,0}}; float multi,a,b,x,y,xsq; int i,j,k,n;

clrscr(); printf("enter the pair of observed values\n"); scanf("%d",&n); aug[0][0]=n; for(i=0;i<n;i++) { printf("pair no.%d\n",i+1); scanf("%f\n%f",&x,&y); xsq=x*x; //summation of x,y,xsq,x*y; aug[0][1]+=x; aug[0][2]+=y; aug[1][1]+=xsq; aug[1][2]+=x*y; } aug[1][0]=aug[0][1]; printf("enter value of aug matrix\n"); for (i=0;i<2;i++) { for(j=0;j<3;j++) printf("%f\t",aug[i][j]); printf("\n"); } //applying Gauss Jordan method to reduce the coefficient matrix to diagonal matrix for(j=0;j<2;j++) for(i=0;i<2;i++) if(i!=j) { multi=aug[i][j]/aug[j][j]; for(k=0;k<3;k++) aug[i][k]-=aug[j][k]*multi; } a=aug[0][2]/aug[0][0]; b=aug[1][2]/aug[1][1]; printf("\na=%f b=%f",a,b); getch(); }

Output:
enter the pair of observed values 5 pair no.1 1 14 pair no.2

2 13 pair no.3 3 9 pair no.4 4 5 pair no.5 5 2 enter value of aug matrix 5.000000 15.000000 15.000000 55.000000 a= 18.200001 b= -3.200000

43.000000 97.000000

EXPERIMENT NO.7
Aim:
To fit a parabola to the given set of values using Least Square method. Here the values are given as

x
1.00 2.00 3.00 4.00 5.00

y
5.00 12.00 26.00 60.00 97.00

Formula used:
Equation of parabola:

y=a+bx+cx2
Normal equations are:

y=na+b x +cx2 xy=ax+bx2+cx3 x2y=ax2+bx3+cx4 Working procedure:


(i) (ii) (iii) Form the normal equations. Solve these as simultaneous equations for a,b and c. 2 Substitute the values of a,b,c in y=a+bx+cx , which is the required parabola of best fit.

Implementation using c:
//Fit a Parabola by least square method #include<stdio.h> #include<conio.h> #include<math.h> void main() { float aug[3][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0}}; float multi,a,b,c,x,y,xsq; int i,j,k,n; clrscr(); printf("enter the no. of observation "); scanf("%d",&n);

aug[0][0]=n; for(i=0;i<n;i++) { printf("pair no.%d\n",i+1); scanf("%f%f",&x,&y); xsq=x*x; aug[0][1]+=x; aug[0][2]+=xsq; aug[1][2]+=x*xsq; aug[2][2]+=xsq*xsq; aug[0][3]+=y; aug[1][3]+=x*y; aug[2][3]+=xsq*y; } aug[1][1]=aug[0][2]; aug[2][1]=aug[1][2]; aug[1][0]=aug[0][1]; aug[2][0]=aug[1][1]; printf("the augmented matrix is\n"); for (i=0;i<3;i++) { for(j=0;j<4;j++) printf("\t%9.4f",aug[i][j]); printf("\n"); } for(j=0;j<3;j++) for(i=0;i<3;i++) { if(i!=j) { multi=aug[i][j]/aug[j][j]; for(k=0;k<4;k++) aug[i][k]-=aug[j][k]*multi; }} a=aug[0][3]/aug[0][0]; b=aug[1][3]/aug[1][1]; c=aug[2][3]/aug[2][2]; printf("\n a=%8.4f b=%8.4f c=%8.4f",a,b,c); getch(); }

Output:

enter the no. of observation 7 pair no.1 1 1.1 pair no.2 1.5 1.3 pair no.3 2 1.6 pair no.4 2.5 2 pair no.5 3.0 2.7 pair no.6 3.5 3.4 pair no.7 4.0 4.1 the augmented matrix is 7.0000 17.5000 17.5000 50.7500 50.7500 161.8750 a= 1.0357 b= -0.1929

50.7500 161.8750 548.1875 c= 0.2429

16.2000 47.6500 154.4750

EXPERIMENT NO.8
Aim:
To solve a system of simultaneous algebraic equations given by Gauss Elimination method.

a1x+b1y+c1z = d1 a2x+b2y+c2z = d2 a3x+b3y+c3z = d3 Formula used: AX=B a1 b1 c1 : d1 a2 b2 c2 : d2 a3 b3 c3 : d3

[A:B]=

z=d3/c3 y=1/b2c3[d2c2-c2d3] x=1/a1b2c3[d1b2c3-b1d2c3+b1c2d3-b2c1d3] Working procedure: stepI To eliminate x from second and third equations a1x+b1y+c1z = d1 b2y+c2z = d2 b3y+c3z = d3 stepII To eliminate y from third equation a1x+b1y+c1z = d1 b2y+c2z = d2 c3z = d3 stepIII To evaluate the unknowns a1 b1 c1 x = d1 a2 b2 c2 y = d2 a3 b3 c3 z = d3 Implementation using c:
//Gauss elimination #include<stdio.h> #include<conio.h> #include<math.h> #define n 4

void main() { float a[n][n+1],x[n],multi,s; int i,j,k; clrscr(); printf("enter the elements of aug matrix row wise\n "); for(i=0;i<n;i++) for(j=0;j<n+1;j++) scanf("%f",&a[i][j]); for(j=0;j<n-1;j++) for(i=j+1;i<n;i++) { multi=a[i][j]/a[j][j]; for(k=0;k<n+1;k++) a[i][k]-=a[j][k]*multi; } printf("upper triangular matrix \n\n"); for(i=0;i<n;i++) { for(j=0;j<n+1;j++) printf("%f\t",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 solution is \n") for(i=0;i<n;i++) printf(" x(%d)=%f\n",i+1,x[i]); getch(); }

Output:
enter the elements of aug matrix row wise 10 -7 3 5 6 -6

8 -1 -4 5 3 1 4 11 2 5 -9 -2 4 7 upper triangular matrix 10.000000 0.000000 -0.000000 0.000000 The solution is x(1)=5.000001 x(2)=4.000001 x(3)=-7.000001 x(4)=1.000000 -7.000000 3.800000 -0.000000 -0.000000 3.000000 0.800000 2.447368 -0.000000 5.000000 -1.00000 10.315789 9.924731 6.000000 8.600000 -3.815791 9.924732

EXPERIMENT NO.9
Aim:
To solve a system of simultaneous algebraic equations given by Gauss Jordan method.

a1x+b1y+c1z = d1 a2x+b2y+c2z = d2 a3x+b3y+c3z = d3 Formula used: AX=B a1 b1 c1 : d1 a2 b2 c2 : d2 a3 b3 c3 : d3 1 0 0 : d1 0 1 0 : d2 0 0 1 : d3

[A:B]=

reducing this matrix A into a diagonal matrix so that each row has only one unknown .

[A:B]=

Working procedure: stepI To eliminate x stepII To eliminate y stepIII To eliminate z


From these the unknowns are readily obtained.

Implementation using c:
//Gauss Jordan method #include<stdio.h> #include<conio.h> #define N 4 void main() { float a[N][N+1],t; int i,j,k; clrscr(); printf("enter the elements of the augmented matrix rowwise \n"); for(i=0;i<N;i++) for(j=0;j<N+1;j++)

scanf("%f",&a[i][j]); //now calculating the values of x1,x2,....,xN for(j=0;j<N;j++) for(i=0;i<N;i++) if(i!=j) { t=a[i][j]/a[j][j]; for(k=0;k<N+1;k++) a[i][k]-=a[j][k]*t; } //now printing the diagonal matrix printf("the diagonal matrix is :- \n"); for(i=0;i<N;i++) { for(j=0;j<N+1;j++) printf("%9.4f ",a[i][j]); printf("\n"); } //now printing the result printf("\nthe solution is :- \n"); for(i=0;i<N;i++) printf("x[%d]= %7.4f\n",i+1,a[i][N]/a[i][i]); getch(); }

Output:
enter the elements of the augmented matrix rowwise 10 -7 3 5 6 -6 8 -1 -4 5 3 1 4 11 2 5 -9

-2 4 7 the diagonal matrix is :10.0000 -0.0000 -0.0000 0.0000 3.8000 -0.0000 -0.0000 0.0000 2.4474 0.0000 -0.0000 0.0000 the solution is :x[1]= 5.0000 x[2]= 4.0000 x[3]= -7.0000 x[4]= 1.0000

-0.0000 0.0000 0.0000 9.9247

50.0000 15.2000 -17.1316 9.9247

EXPERIMENT NO.10
Aim:
To solve a system of simultaneous algebraic equations given by Gauss Seidal method.

a1x+b1y+c1z = d1 a2x+b2y+c2z = d2 a3x+b3y+c3z = d3 Formula used: xk+1 = 1/a1.(d1-b1yk-c1zk) yk+1 = 1/b2.(d2-a2xk+1-c2zk) zk+1 = 1/c3.(d3-a3xk+1-b3yk+1) Working procedure:
Here we start with initial approximations x0,y0,z0 for x,y,z respectively. 1.Substituting y=y0 and z=z0 for finding x1. 2.Substituting x=x1 and z=z0 for finding y1. 3.Substituting x=x1 and y=y1 for finding z1. And so on i.e. as soon as a new approximation for an unknown is found, it is immediately used in the next step. This process of iteration is repeated till the values of x.y.z are obtained to the desired degree of accuracy.

Implementation using c:
//Gauss Seidal method #include<stdio.h> #include<math.h> #include<conio.h> #define S 3 #define T 4 void main () { float a[S][T],x[S],e,relerr,temp,sum,big; int i,j,n,itr,maxitr; clrscr(); printf("enter the no.of unknowns <= %d\n",S); scanf("%d",&n); printf("enter the coefficients \n");

for(i=0;i<n;i++) { for(j=0;j<=n;j++) scanf("%f",&a[i][j]); } printf("enter the maximum allowed error\n "); scanf("%f",&e); printf("enter the maximum no of iteration\n "); scanf("%d",&maxitr); for(i=0;i<n;i++) x[i]=0; for(itr=1;itr<=maxitr;itr++) { big=0; for(i=0;i<n;i++) { sum=0; for(j=0;j<n;j++) { if(j!=i) sum+=a[i][j]*x[j]; } temp=(a[i][n]-sum)/a[i][i]; relerr=fabs(x[i]-temp); if(relerr> big) big=relerr; x[i]=temp; } if(big<e) { printf("\n converges to solution\n\n"); for(i=0;i<n;i++) printf("\nx[%d] = %8.3f\n",i+1,x[i]); getch(); } } printf("\n doesnt converge in %d iterations ",maxitr); for(i=0;i<n;i++) printf("\nx[%d]=%8.3f\n",i+1,x[i]); getch(); }

Output:
enter the no.of unknowns <= 3 3 enter the coefficients 20 1 -2 17 3 20 -1 -18 2 -3 20 25 enter the maximum allowed error 0.0001 enter the maximum no of iteration 20 converges to solution

x[1] = 1.000 x[2]= -1.000 x[3]= 1.000

EXPERIMENT NO.11
Aim:
To find the solution of ordinary differential equation by Eulers method. Here to find the approximate value of y when x=0.1 if

dy/dx = x+y
given that y=0 at x=0 and h=0.02

Formula used:
Consider the differential equation

dy/dx = f(x,y)
Where y=y0 at x=x0 Its solution is

ym+1= ym + hf(xm,ym) Working procedure:


Here the ordinate is divided in n subintervals of width h i.e. at

x0, x0+h, x0+2h.x0+nh


We get the approximate value of y by the length of line from each of these ordinate points to the tangent of the curve. By repeating this process n no. of times we finally reach the closest approximation.

Implementation using c:
//Euler Method #include<stdio.h> #include<conio.h> void main() { float f(float x,float y); float x,y,h,xn,dy; int i,n; clrscr(); printf("Enter initial values of x, y\n"); scanf("%f%f",&x,&y); printf("enter x at which y is required : "); scanf("%f",&xn); printf("\n enter stepsize, h: ");

scanf("%f",&h); n=(int)((xn-x)/h+0.5); printf("\n\n step x y \n\n"); for(i=1;i<=n;i++) { dy=h*f(x,y); x=x+h; y=y+dy; printf("%3d%10.3f %f\n\n",i,x,y); } printf("\n value of y at x= %f is %f \n\n",x,y); getch(); } float f(float x, float y) { return(x+y); }

Output:
Enter initial values of x, y 0 1 enter x at which y is required : 0.1 enter stepsize, h: 0.02

step 1 2 3 4 5

0.020 1.020000 0.040 1.040800 0.060 1.062416 0.080 1.084864 0.100 1.108162

value of y at x= 0.100000 is 1.108162

Vous aimerez peut-être aussi