PRACTICAL FILE
Of Numerical Method Deenbandhu Chhotu Ram University of Sciences & Technology Murthal, Sonepat,
SUBMITTED TO:
Dr. Vijay Tomar 1
SUBMITTED BY :
Jagdish Parsad 11001303008 2nd Sem , ECE (W) DCRUST Murthal
I n d e x
Sl. No. 01 Name of Experiment To find the roots of non linear equation using Bisection Method. To find the roots of non linear equation using
Regula falsi Method.
Page Date of No. Experiment 3 14/01/2012
Date of Submission
Teachers Sign.
02
21/01/2012
03
To find the roots of non linear equation using
Newton Raphson Method
28/01/2012
04
To solve the system of linear equation using
Gauss elimination method.
28/01/2012
05
To solve the system of linear equation using
Gauss Jordan Method.
04/02/2012
06 07 08
To integrate numerically using Simpsons rule. To integrate numerically using trapezoidal rule. To find numerical solution of ordinary differential equation by Euler's method To find numerical solution of ordinary differential equation by Lagrange's Interpolation To find numerical solution of ordinary differential equation by Runge Kutta Method
8 9 10
11/02/2012 18/02/2012 25/02/2012
09
11
25/02/2012
10
12
01/03/2012
SUBMITTED TO:
Dr. Vijay Tomar 2
SUBMITTED BY :
Jagdish Parsad 11001303008 2nd Sem , ECE (W) DCRUST Murthal
Practical = 1
/*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.5f\n",*itr,*x); } main() { int itr = 0, maxitr; float x, a, b, aerr, x1; printf("enter the 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) b=+x; else a=x; bisect (&x1,a,b,&itr); if (fabs(x1-x) < aerr) { printf ("after %d iteration,root<169> = %6.4f\n",itr,x1); return 0; } x=x1; } while (itr < maxitr); printf ("solution does not coverg," "iteration not sufficient "); return 1; }
SUBMITTED TO:
Dr. Vijay Tomar 3
SUBMITTED BY :
Jagdish Parsad 11001303008 2nd Sem , ECE (W) DCRUST Murthal
cout <<"enter the value for x0,x1,"
Practical = 2
/* regula falsi method*/ #include<stdio.h> #include<conio.h> #include<iomanip.h> #include<math.h> float f (float x) { return cos(x)-x*exp(x); } void regula (float *x,float x0,float x1, floatfx0,floatfx1,int *itr) { *x=x0-((x1-x0)/(fx1-fx0))*fx0; ++(*itr); cout<<"iteration no."<<setw(3)<<*itr <<"x="<<setw(7)<<setprecision(5) <<*x<<end1; } int main() { int itr =0, maxitr; floatx0,x1,x2,x3,aerr;
<<"allowed error, maximum iteration "<<end1; cin>>x0>>x1>>aerr>> maxitr; regula (&x2,x0,x1,f(x0),f(X1),& itr); cout << fixed; do { if (f(x0)*f(X2)<0) x1=x2; else x0=x2; regula(&x3,x0,x1,f(x0),f(x1),& itr); if(fabs(x3-x2)< aerr) { cout << "after" <<itr<< " iterations," << "root="<<setw(6)<<setprecision(4) <<x3<<end1; return 0; } x2=x3; } while (itr < maxitr); cout <<" solution does not coverage," <<" iteration not sufficient"<<end; return 1; }
SUBMITTED TO:
Dr. Vijay Tomar 4
SUBMITTED BY :
Jagdish Parsad 11001303008 2nd Sem , ECE (W) DCRUST Murthal
Practical = 3
/* Newton Raphson Method */ #include <stdio.h> #include <math.h> float f (float x) { return x*log10(x)-1.2; } float df (float x) { return log10(x) + 0.43429; } main() { int itr,maxitr; float h,x0,x1,aerr; printf("Enter x0,allowed error," "maximum iteration\n"); scanf ("%f %f %d", &x0 , &aerr, &maxitr); for (itr=1;itr<=maxitr;itr++) { h = f(x0)/df(x0); x1 = x0-h; printf("Iteration no. %3d," "x = %9.6f\n",itr,x1); if (fabs(h) < aerr) { printf("After %3d iteration," "root = %8.6f\n", itr,x1); return 0; } x0 = x1; } printf("Iteration not sufficient," "solution does not converge\n"); return 1; }
SUBMITTED TO:
Dr. Vijay Tomar 5
SUBMITTED BY :
Jagdish Parsad 11001303008 2nd Sem , ECE (W) DCRUST Murthal
Practical = 4
/*Gauss elimination method*/ #include <stdio.h> #define N 4 void main() { float a[N][N+1],x[N],t,s; int i,j,k; printf(" Enter the elements of the" "augmented matrix rowwise"); 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++) { t=a[i][j]/a[j][j]; for (k=0;k<N+1;k++) a[i][k]=a[j][k]*t; } /* now printing the upper triangular matrix */ 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"); } /* now performing back subsitution */ 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]; } /* now printing the results */ printf("The solution is :- \n"); for (i=0;i<N;i++) printf("x[3%d] = %7.4f\n",i+1,x[i]); }
SUBMITTED TO:
Dr. Vijay Tomar 6
SUBMITTED BY :
Jagdish Parsad 11001303008 2nd Sem , ECE (W) DCRUST Murthal
Practical = 5
/* Gauss jordan method */ #include<iostream.h> #include<iomanip.h> #define N 4 int main() { float a[N][N+1],t; int i,j,k,end1,end,fixed; cout<<"Enter the element of the" <<"augmented matrix rowwise" << end1; for (i=0;i<N;i++) for (j=0;j<N+1;j++) cin>>a[i][j]; /* now calculating the values of x1,x2,......,xn */ cout<<fixed; for (j=0;j<N;j++) for (i=0;i<N;i++) if (i!=j) { t=a[i][j]/a[j][j]; } } /* now printing the diagonal matrix */ cout<< "The diagnol matrix is :-"<< end1; for (i=0;i<N;i++) { for (j=0;j<N;j++) cout<< setw(9) << setprecision(4) << a[i][j]; cout << end1; } /* now printing the results */ cout<< "The solutin is:-" << end; for (i=0;i<N;i++) cout<< "x[" <<setw(3)<< i+1<<"] =" << setw(7)<< setprecision(4) << a[i][N]/a[i][i]<< end; return 0; for (k=0;k<N+1;k++) a[i][j] -=a[j][k]*t;
SUBMITTED TO:
Dr. Vijay Tomar 7
SUBMITTED BY :
Jagdish Parsad 11001303008 2nd Sem , ECE (W) DCRUST Murthal
Practical = 6
/*simposon's rule*/ #include<stdio.h> float y(float x) { return 1/(1+x*x); } main() { float x0,xn,h,s; int i,n; puts("enter x0,xn,no. of subintervals"); 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("value of integral is %6.4f\n", (h/3)*s); }
SUBMITTED TO:
Dr. Vijay Tomar 8
SUBMITTED BY :
Jagdish Parsad 11001303008 2nd Sem , ECE (W) DCRUST Murthal
Practical = 7
/*trapezoidal rule*/ #include<stdio.h> float y(float x) { return 1/(1+x*x); } main() { float x0,xn,h,s; int i,n; puts("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++) s+=2*y(x0+i*h); printf ("value of integral is % 6.4f\n", (h/2)*s); }
SUBMITTED TO:
Dr. Vijay Tomar 9
SUBMITTED BY :
Jagdish Parsad 11001303008 2nd Sem , ECE (W) DCRUST Murthal
Practical = 8
/*Euler's method*/ #include<stdio.h> #include<conio.h> #include<iostream.h> #include<iomanip.h> float df(float x,float y) { return x+y; } int main() { float x0,y0,h,x,x1,y1,end1,fixed; cout << "Enter the values of x0,y0,h,x" << end1; cin >> x0 >> y0 >> h >> x; cout << fixed; x1=x0;y1=y0; while(1) { if(x1>x) return 0; } y1+=h*df(x1,y1); x1+=h; cout << "when x=" << setw(3) << setprecision(1) << x1 << "y=" << setw(4) << setprecision(2) << y1 << end1;
SUBMITTED TO:
Dr. Vijay Tomar 10
SUBMITTED BY :
Jagdish Parsad 11001303008 2nd Sem , ECE (W) DCRUST Murthal
Practical = 9
/*Lagrange's Interpolation*/ #include<iostream.h> #include<iomanip.h> #define MAX 100 int main() { float ax [MAX+1],ay[MAX+1],nr,dr,x,y=0,end1,fixed; int i,j,n; cout << "Enter the value of n" << end1; cin >> n; cout << "Enter the set of values" << end1; for (i=0;i<=n;i++) cin >> ax[i] >> ay[i]; cout << "Enter the value of x for which" << "Value of y is wanted" << end1; cin >> x; cout << fixed; { nr=dr=1; for(j=0;j<=n;j++) if (j!=i) { nr *=x-ax[j]; dr *=ax[i]-ax[j]; } y+= (nr/dr)*ay[i]; } cout << "when x=" << setw(4) << setprecision(1) << x << "y=" << setw(7) << setprecision(1) << y << end1; return 0; }
SUBMITTED TO:
Dr. Vijay Tomar 11
SUBMITTED BY :
Jagdish Parsad 11001303008 2nd Sem , ECE (W) DCRUST Murthal
Practical = 10
/*Runge Kutta Method*/ #include<iostream.h> #include<iomanip.h> float f(float x,float y) { return x+y*y; } int main() { float x0,y0,h,xn,x,y,k1,k2,k3,k4,k,end1,fixed; cout << "Enter the value of x0,y0," << "h,xn" << end1; cin >> x0 >> y0 >> h >> xn; x=x0; y=y0; cout << fixed; while (1) { if (x == xn) break; k1=h*f(x,y); k2=h*f(x+h/2,y+k1/2); } return 0; } 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; cout << "when x=" << setprecision(4) << setw(8) << x << "y=" << setw(8) << y << end1;
SUBMITTED TO:
Dr. Vijay Tomar 12
SUBMITTED BY :
Jagdish Parsad 11001303008 2nd Sem , ECE (W) DCRUST Murthal