Sie sind auf Seite 1von 29

NARNAUL (HARYANA)

PRACTICAL FILE OF Numerical Methods


USING C LAB Submitted in Partial Fullfillment of the Requirment in

Electronics & Communication Engineering


(Session 2012-2015)

Submitted to : Mr. Yogesh Gupta ( Lectt. )

Submitted by: Subhash Kumar Yadav ECE 4th Sem. Roll No:12ECEL26

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

NARNAUL (HARYANA)

CERTIFICATE
This to certify that Subhash Kumar Yadav has completed her practical lab file of NUMERICAL METHODS OF COMPUTATIONAL PROGRAMMING LAB under my guidance and completed it to my total satisfaction submitted in partial fullfillment of the requirment in

Electronics & Communication Engineering


(Session 2012-2015)

Submitted to : Mr. Yogesh Gupta ( Lectt. )

Submitted by: Subhash Kumar Yadav ECE 4th Sem. Roll No:12ECEL26

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

NUMERICAL METHODS LAB.


INDEX
Sr.NO
1. 2.

Program Name
Program to implement Bisection method to find the root of given equation. Program to implement Newton-Rapson method to find root of given equation. Program to find solution of system of linear equation using Gauss elimination technique with pivoting. Program to find solution of system of linear equation using Gauss-Jordon technique. Program to find solution of system of linear equation using Gauss-Seidel method. Program to find the largest Eigen value using Power method. Program to fit a polynomial of degree m through a set of points using method of least squares. Program to implement Trapezoidal rule for tabulated function. Program to implement Simpsons 1/3 rule for tabulated function. Program to solve ODE of type dy/dx=xy using Eulers method. Program to solve ODE of type dy/dx=xy using second order Runge-Kutta method(Heuns method)

Page No.
1-2 3-4 5-7

Date

Lect. Sign

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

8-9 10-12 13-15 16-19 20 21-22 23-24 25-26

11.

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Program No: 1 Aim: Program to implement Bisection method to find the root of given equation.

#include<stdio.h> #include<stdlib.h> #include<math.h> #include<conio.h> double f(double x) { return pow((double)x,(double)3.0)-4*x-9; } void main() { float x1,x2,epsilon,x3; clrscr(); printf("Enter first point of the search interval:"); scanf("%f",&x1); printf("Enter second point of the search interval:"); scanf("%f",&x2); if((f(x1)*f(x2))>0) { printf("\n Initial approximations are unsuitable\n"); exit(1); } printf("Enter prescribed tolerance:"); scanf("%f",&epsilon); do { x3=(x1+x2)/2; if(f(x1)*f(x3)<0) x2=x3; else x1=x3;

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

} while( fabs((double)(x1-x2)) > epsilon); printf("\nApproximate root=%8.4f\n",x3); getch(); }

Output: Enter first point of the search interval:2 Enter second point of the search interval:3 Enter prescribed tolerance:.001 Approximate root= 2.7061

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Program No: 2 Aim: Program to implement Newton-Rapson method to find root of given equation. #include<stdio.h> #include<stdlib.h> #include<math.h> #include<conio.h> double f(double x) { return x*x*x-4.0*x-9.0; } double df(double x) { return 3.0*x*x-4.0; } void main() { float x0,x1,epsilon,delta,relativeerror; int i,n; clrscr(); printf("Enter initial approximation:"); scanf("%f",&x0); printf("Enter number of iterations permitted:"); scanf("%d",&n); printf("Enter prescribed tolerance:"); scanf("%f",epsilon); printf("Enter lower bound on slope:"); scanf("%f",&delta); for(i=1;i<=n;i++) { if(fabs(df(x0))<delta) { printf("\nslope too small\n"); exit(1);

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

} x1=x0-f(x0)/df(x0); relativeerror=fabs((x1-x0)/x1); x0=x1; if (relativeerror<=epsilon) { printf("\nApproximate root=%.4f",x1); } } printf("\nDoes not converge in %d iterations\n",n); getch(); }

Output: Enter initial approximation:2 Enter number of iterations permitted:9 Enter prescribed tolerance:.01 Enter lower bound on slope:1 Approximate root=2.7065

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Program No: 3 Aim: Program to find solution of system of linear equation using Gauss elimination technique with pivoting. #include<stdio.h> #include<math.h> #include<conio.h> #define MAX 20 void main() { float a[MAX][MAX],x[MAX],max,temp,sum; int i,j,k,m,n,p,q; clrscr(); printf("Enter number of equations:"); scanf("%d",&n); printf("\nEnter coefficients equation wise\n"); for(i=1;i<=n;i++) { for(j=1;j<=(n+1);j++) { scanf("%f",&a[i][j]); } } for(k=1;k<n;k++) { max=fabs(a[k][k]); p=k; for(m=k+1;m<=n;m++) { if(fabs(a[m][k])>max) { max=fabs(a[m][k]);p=m; } }

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

if(p!=k) { for(q=k;q<=(n+1);q++) { temp=a[k][q];a[k][q]=a[p][q];a[p][q]=temp; } } for(i=k+1;i<=n;i++) { temp=a[i][k]/a[k][k]; for(j=k;j<=(n+1);j++) a[i][j]=a[i][j]-temp*a[k][j]; } } x[n]=a[n][n+1]/a[n][n]; for(i=(n-1);i>=1;i--) { for(sum=0.0,j=(i+1);j<=n;j++) sum=sum+a[i][j]*x[j]; } printf("\n\nRequired solution is\n"); for(i=1;i<=n;i++) { printf("\nx(%d)=%.3f",i,x[i]); } getch(); }

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Output: Enter number of equations:2 Enter coefficients equation wise 3 2 5 8 6 9

Required solution is x(1)=56403.301 x(2)=-6.500

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Program No: 4 Aim: Program to find solution of system of linear equation using GaussJordon technique

#include<stdio.h> #include<math.h> #include<conio.h> #define MAX 20 void main() { float a[MAX][MAX],temp,comm; int i,j,k,m,n,p,q; clrscr(); printf("Enter number of equations:"); scanf("%d",&n); printf("\nEnter coefficients equation wise\n"); for(i=1;i<=n;i++) { for(j=1;j<=(n+1);j++) { scanf("%f",&a[i][j]); } } for(k=1;k<=n;k++) { temp=a[k][k]; for(j=k;j<=(n+1);j++) a[k][j]=a[k][j]/temp; for(i=1;i<=n;i++) { if(i!=k) { comm=a[i][k]; for(j=k;j<=(n+1);j++)

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

a[i][j]=a[i][j]-comm*a[k][j]; } } } printf("\n\nRequired solution is\n"); for(i=1;i<=n;i++) { printf("\nx(%d)=%.3f",i,a[i][n+1]); } getch(); }

Output: Enter number of equations:2 Enter coefficients equation wise 7 6 9 7 8 3 Required solution is x(1)=3.857 x(2)=-3.000

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Program No: 5

Aim: Program to find solution of system of linear equation using Gauss-Seidel method. #include<stdio.h> #include<math.h> #include<conio.h> #define MAX 20 void main() { float a[MAX][MAX],x[MAX],max,temp,sum; int i,j,k,m,n,p,q; clrscr(); printf("Enter number of equations:"); scanf("%d",&n); printf("\nEnter coefficients equation wise\n"); for(i=1;i<=n;i++) { for(j=1;j<=(n+1);j++) { scanf("%f",&a[i][j]); } } for(k=1;k<n;k++) { max=fabs(a[k][k]); p=k; for(m=k+1;m<=n;m++) { if(fabs(a[m][k])>max) { max=fabs(a[m][k]);p=m;

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

} } if(p!=k) { for(q=k;q<=(n+1);q++) { temp=a[k][q];a[k][q]=a[p][q];a[p][q]=temp; } } for(i=k+1;i<=n;i++) { temp=a[i][k]/a[k][k]; for(j=k;j<=(n+1);j++) a[i][j]=a[i][j]-temp*a[k][j]; } } x[n]=a[n][n+1]/a[n][n]; for(i=(n-1);i>=1;i--) { for(sum=0.0,j=(i+1);j<=n;j++) sum=sum+a[i][j]*x[j]; } printf("\n\nRequired solution is\n"); for(i=1;i<=n;i++) { printf("\nx(%d)=%.3f",i,x[i]); } getch(); }

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Output: Enter number of equations:2 Enter coefficients equation wise 4 9 6 7 8 2

Required solution is x(1)=56403.301 x(2)=1.097

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Program No: 6 Aim: Program to find the largest Eigen value using Power method. #include<stdio.h> #include<math.h> #include<conio.h> #define MAX 20 void main() { float a[MAX][MAX],x[MAX],s[MAX],epsilon; float error,big_error,temp,lamda; int i,j,k,n; clrscr(); printf("Enter number of eqautions:"); scanf("%d",&n); printf("\nEnter coefficients equation wise\n"); for (i=1;i<=n;i++) { for(j=1;j<=n;j++) { scanf("%f",&a[i][j]); } } printf("\nEnter required precision:"); scanf("%f",&epsilon); for(i=1;i<=n;i++) { x[i]=1.0; } do { big_error=0.0; for(i=1;i<=n;i++) {

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

s[i]=0.0; for(j=1;j<=n;j++) { s[i]=s[i]+a[i][j]*x[j]; } } lamda=fabs(s[1]); for(i=1;i<=n;i++) { temp=s[i]/lamda; error=fabs((temp-x[i])/temp); x[i]=temp; if(error>big_error) { big_error=error; } } } while(big_error>epsilon); printf("\n\nLargest eigen value=%.4f\n",lamda); printf("\nAssociated eigen vector is\n"); for(i=1;i<=n;i++) { printf("\nx(%d)=%.4f",i,x[i]); } getch(); }

Output: Enter number of eqautions:2 Enter coefficients equation wise 3

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

9 6 8 Enter required precision:2 Largest eigen value=12.0000 Associated eigen vector is x(1)=1.0000 x(2)=1.1667

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Program No: 7 Aim: Program to fit a polynomial of degree m through a set of points usi ng method of least squares.

#include<stdio.h> #include<math.h> #include<conio.h> void main() { float x[20],y[20],a[20],c[20][20]; int i,j,n,m,k,m1,m2,l; float p,q,max,temp,sum; clrscr(); printf("Enter degree of polynomial:"); scanf("%d",&m); printf("Enter number of points:"); scanf("%d",&n); printf("Enter %d pair of values as x,y\n",n); for(i=1;i<=n;i++) { printf("Enter value for x[%d]:"); scanf("%f",&x[i]); printf("Enter value for y[%d]:"); scanf("%f",&y[i]); } m1=m+1; m2=m+2; for(i=1;i<=m1;i++) { for(j=1;j<=m1;j++) { l=i+j-2; c[i][j]=0.0; for(k=1;k<=n;k++)

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

{ c[i][j]=c[i][j]+pow((double)x[k],(double)l); } } l=i-1; c[i][m2]=0.0; for(k=1;k<=n;k++) { c[i][m2]=c[i][m2]+y[k]*pow((double)x[k],(double)l); } } for(k=1;k<=(m1-1);k++) { max=abs(c[k][k]); p=k; for(q=k+1;q<=m1;q++) { if(abs(c[q][k])>max) { max=abs(c[q][k]); p=q; } } if(p!=k) { for(q=k;q<=m2;q++) { temp=c[k][q]; c[k][q]=c[p][q]; c[p][q]=temp; } } for(i=k+1;i<=m1;i++) { temp=c[i][k]/c[k][k];

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

for(j=k;j<=m2;j++) { c[i][j]=c[i][j]-temp*c[k][j]; } } } a[m]=c[m1][m2]/c[m1][m1]; for(i=(m1-1);i>=1;i--) { sum=0.0; for(j=(i+1);j<=m1;j++) { sum=sum+c[i][j]*a[j]; } a[i]=(c[i][m2]-sum)/c[i][i]; } printf("\nRegression coefficients\n\n"); for(i=1;i<=m1;i++) { printf("a[%d]=%8.4f\n",i,a[i]); } getch(); }

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Output: Enter degree of polynomial:3 Enter number of points:2 Enter 2 pair of values as x,y Enter value for x[2154]:23 Enter value for y[2154]:43 Enter value for x[2154]:23 Enter value for y[2154]:65 Regression coefficients a[1]= a[2]= a[3]= a[4]= 0.0000 0.0000 0.1021 0.0000

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Program No: 8 Aim: Program to implement Trapezoidal rule for tabulated function. #include<stdio.h> #include<stdlib.h> #include<math.h> #include<conio.h> void main(){ int i,n; float x[20],y[20],h,sum,integral; clrscr(); printf("Enter number of intervals:"); scanf("2%d",&n); printf("Enter size of interval:"); scanf("%f",&h); printf("Enter %d pair of (x,y)\n",n+1); for(i=1;i<=(n+1);i++){ scanf("%f,%f",&x[i],&y[i]); } sum=(y[1]+y[n+1])/2; for(i=2;i<=n;i++){ sum=sum+y[i]; } integral=h*sum; printf("\nValue of the integral=%9.4f\n",integral); getch(); } Output: Enter number of intervals:4,5,6,3 Enter size of interval:Enter -28883 pair of (x,y) Value of the integral= -0.0000

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Program No: 9 Aim: Program to implement Simpsons 1/3 rule for tabulated function #include<stdio.h> #include<stdlib.h> #include<math.h> #include<conio.h> void main(){ int i,n; float x[20],y[20],h,sum,integral,s2,s4; clrscr(); printf("Enter number of intervals:"); scanf("%d",&n); printf("Enter size of interval:"); scanf("%f",&h); printf("Enter %d pair of (x,y)\n",n+1); for (i=1;i<=(n+1);i++){ scanf("%f,%f",&x[i],&y[i]); } sum=y[1]+y[n+1]; s2=s4=0.0; for(i=2;i<=n;i+=2){ s4=s4+y[i]; } for(i=3;i<=(n-1);i+=2){ s2=s2+y[i]; } integral=(h/3.0)*(sum+2*s2+4*s4); printf("\nValue of the integral=%9.4f\n",integral); getch(); }

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Output: Enter number of intervals:3 Enter size of interval:2 Enter 4 pair of (x,y) 4,3 3,4 2,7 9,5 Value of the integral= 16.0000

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Program No: 10 Aim: Program to solve ODE of type dy/dx=xy using Eulers method. #include<stdio.h> #include<conio.h> float f(float x,float y) { return(x*y); } void main() { int i; float x,y,x1,y1,xf,h,s,s1,s2,s3,s4; clrscr(); printf("Enter value of x1,y1,xf:"); scanf("%f,%f,%f",&x1,&y1,&xf); printf("Enter size of interval:"); scanf("%f",&h); x=x1; y=y1; i=1; printf("\n\nPoints constituting solution curve are\n\n"); printf("\nPoint x y"); printf("\n================================="); printf("\n%3d\t%8.3f\t%8.3f",i,x,y); while(x<xf) { y=y+h*f(x,y); x=x+h; i=i+1; printf("\n%3d\t%8.3f\t%8.3f",i,x,y); } printf("\n===============================\n"); getch();

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Output: Enter value of x1,y1,xf:2,3,4 Enter size of interval:2

Points constituting solution curve are

Point x y ================================= 1 2.000 3.000 2 4.000 15.000 ===============================

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Program No: 11 Aim: Program to solve ODE of type dy/dx=xy using second order Runge-Kutta method(Heuns method). #include<stdio.h> #include<conio.h> float f(float x,float y) { return(x*y); } void main() { int i; float x,y,x1,y1,xf,h,s,s1,s2,s3,s4; clrscr(); printf("Enter value of x1,y1,xf:"); scanf("%f,%f,%f",&x1,&y1,&xf); printf("Enter size of interval:"); scanf("%f",&h); x=x1; y=y1; i=1; printf("\n\nPoints constituting solution curve are\n\n"); printf("\nPoint x y"); printf("\n================================="); printf("\n%3d\t%8.3f\t%8.3f",i,x,y); while(x<xf) { s1=f(x,y); x=x+h; s2=f(x,y+h*s1); y=y+h*(s1+s2)/2; i=i+1; printf("\n %3d\t%8.3f\t%8.3f",i,x,y);

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

} printf("\n===============================\n"); getch(); }

Output: Enter value of x1,y1,xf:4,5,3 Enter size of interval:3

Points constituting solution curve are

Point x y ================================= 1 4.000 5.000 ===============================

Department of Electronics & Communication Engineering, YCET, Narnaul, Haryana

Das könnte Ihnen auch gefallen