numerical methods in c

36
PROGRAM /*program for legranges interpolation method*/ #include<stdio.h> #include<conio.h> void main() { float x[20],y[20],unknown,temp,result=0,n,i,j; clrscr(); printf("\n\n\tLEGRANGES INTERPOLATION -FIRST ORDER\ n"); printf("\n\t---------------------------------------\ n"); printf("\nEnter the limit\n"); scanf("%f",&n); printf("Enter the values for x\n"); for(i=0;i<n;i++) { scanf("%f",&x[i]); } printf("Enter the values of y\n"); for(i=0;i<n;i++) { scanf("%f",&y[i]); } printf("Enter the value whose f(x) to be found\n"); scanf("%f",&unknown); for(i=0;i<n;i++) { temp=1; for(j=0;j<n;j++) { if(i!=j) { temp*=((unknown-x[j])/(x[i]-x[j])); } } result+=temp*y[i]; } printf("\f( %f )= ",unknown); St.Mary’scollege,Thrissur

Upload: ambili-baby

Post on 07-Aug-2015

73 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Numerical Methods in C

PROGRAM

/*program for legranges interpolation method*/#include<stdio.h>#include<conio.h>void main(){

float x[20],y[20],unknown,temp,result=0,n,i,j;clrscr();printf("\n\n\tLEGRANGES INTERPOLATION -FIRST ORDER\n");printf("\n\t---------------------------------------\n");printf("\nEnter the limit\n");scanf("%f",&n);printf("Enter the values for x\n");for(i=0;i<n;i++){

scanf("%f",&x[i]);}printf("Enter the values of y\n");for(i=0;i<n;i++){

scanf("%f",&y[i]);}printf("Enter the value whose f(x) to be found\n");scanf("%f",&unknown);for(i=0;i<n;i++){

temp=1;for(j=0;j<n;j++){

if(i!=j){

temp*=((unknown-x[j])/(x[i]-x[j]));}

}result+=temp*y[i];

}printf("\f( %f )= ",unknown);printf("%f",result);getch();

}

St.Mary’scollege,Thrissur

Page 2: Numerical Methods in C

OUTPUT

LEGRANGES INTERPOLATION -FIRST ORDER

------------------------------------------------------------

Enter the limit4Enter the values for x300304305307Enter the values of y2.47712.48292.48432.4871Enter the value whose f(x) to be found301

F(301.000000) = 2.478597

St.Mary’scollege,Thrissur

Page 3: Numerical Methods in C

PROGRAM

/* program to claculate the integral using trezoidal rule*/#define f(x) (1/(x+1))#include<stdio.h>#include<conio.h>void main(){

float h,a,b,result=0,temp=0;int i,j,n;float x[50],y[50];clrscr();printf("Enter the Lower & Upper limit a&b\n");scanf("%f %f",&a,&b);printf("\nEnter the number of intervals\n");scanf("%d", &n);h=(b-a)/n;printf("\nCOMPUTING INTEGRAL USING-TRAPEZOIDAL RULE \n");for(i=0;i<=n;i++){

x[i]=a+(i*h);}for(i=0;i<=n;i++){

y[i]=f(x[i]);}printf("x\t\t\ty");for(i=0;i<=n;i++){printf("\n%f\t\t%f",x[i],y[i]);}result=y[0]+y[n];for(i=1;i<n;i++){

temp+=y[i];}result+=(temp*2);result*=(h/2);printf("\n\n%f",result);getch();

}

St.Mary’scollege,Thrissur

Page 4: Numerical Methods in C

OUTPUT

Enter the Lower & Upper limit a&b01

Enter the number of intervals2

COMPUTING INTEGRAL USING-TRAPEZOIDAL RULE

x y

0.000000 1.0000000.500000 0.6666671.000000 0.500000

0.708333

St.Mary’scollege,Thrissur

Page 5: Numerical Methods in C

PROGRAM

/*program to solve differential equation using Eulers method*/#include<stdio.h>#include<conio.h>#define f(x,y) ((y-x)/(y+x))void main(){

float x0,y0,h,unknown,result=0,temp=0;printf("\n\n\tEULERS METHOD\n");printf("\nEnter the value for x0\n");scanf("%f",&x0);printf("Enter the value for y0\n");scanf("%f",&y0);printf("Enter the steplength\n");scanf("%f",&h);printf("Enter the value for unknown\n");scanf("%f",&unknown);while(x0<=unknown){

temp=y0+h*f(x0,y0);x0+=h;result=y0;y0=temp;

}printf("Result= %f",result);getch();

}

OUTPUT

EULERS METHOD --------------

Enter the value for x01Enter the value for y02Enter the steplength.5Enter the value for unknown2Result= 2.257576

St.Mary’scollege,Thrissur

Page 6: Numerical Methods in C

PROGRAM

/* program to compute integral using legranges interpolation 2nd order*/#include<stdio.h>#include<conio.h>#define f(x) 1/(1+(x*x))void main(){

float u,l,b,a,h,result=0;clrscr();printf("\n\tLEGRANGES INTERPOLATION - 2nd ORDER\n");printf("\n\t-----------------------------------\n");printf("\nEnter the upper limit:\n\t");scanf("%f",&a);printf("Enter the lower limit:\n\t");scanf("%f",&b);h=(a-b)/2;result=f(-0.57735)+f(0.57735);result*=h;printf("\n\nRESULT = %f",result);getch();

}

LEGRANGES INTERPOLATION - 2nd ORDER

-----------------------------------------------------------

Enter the upper limit: 1Enter the lower limit: -1

RESULT = 1.583338

St.Mary’scollege,Thrissur

Page 7: Numerical Methods in C

PROGRAM

/* program to solve the 3rd order differential equation using Runge Kutta 2nd order*/#define f(x,y) x+y#include<stdio.h>#include<conio.h>void main(){

float x0,y0,y1,k1,k2,h,unknown;clrscr();printf("\n\tRUNGE KUTTA METHOD - 2nd ORDER\n");printf("\n\t------------------------------\n");printf("\nEnter the values for the following:\n");printf("\n\tx0: ") ;scanf("%f",&x0);printf("\n\ty0: ");scanf("%f",&y0);printf("\n\th: ");scanf("%f",&h);printf("\n\tUnknown: ");scanf("%f",&unknown);while(unknown!=x0){

k1=h*(f(x0,y0));k2=h*(f(x0+h,y0+k1));y1=y0+((k1+k2)/2);x0+=h;y0=y1;

}printf("\nUNKNOWN = %f\nRESULT = %f",unknown,y1);getch();

}

St.Mary’scollege,Thrissur

Page 8: Numerical Methods in C

OUTPUT

RUNGE KUTTA METHOD - 2nd ORDER -----------------------------------------------------

Enter the values for the following:

x0: 0

y0: 1

h: .1

Unknown: .2

UNKNOWN = 0.200000RESULT = 1.242050

St.Mary’scollege,Thrissur

Page 9: Numerical Methods in C

PROGRAM

/* program using Runge kutta 3rd order method*/

#define f(x,y) x+y#include<stdio.h>#include<conio.h>void main(){

float y1,x0,y0,k1,k2,k3,h,unknown;clrscr();printf("\n\n\tRUNGE KUTTA - 3rd ORDER\n");printf("\n\t-----------------------\n");printf("Enter the values for the folowing\n");printf("\n\tx0: ");scanf("%f",&x0);printf("\n\ty0: ");scanf("%f",&y0);printf("\n\th: ");scanf("%f",&h);printf("\n\tUnknown: ");scanf("%f",&unknown);while(unknown!=x0){

k1=h*(f(x0,y0));k2=h*(f(x0+h/2,y0+k1/2));k3=h*(f(x0+h,y0+2*k2-k1));y1=y0+(k1+4*k2+k3)/6;x0+=h;y0=y1;

}printf("\n\n Result of %f = %f",unknown,y1);getch();

}

St.Mary’scollege,Thrissur

Page 10: Numerical Methods in C

OUTPUT

RUNGE KUTTA METHOD - 3rd ORDER

--------------------------------------------------

Enter the values for the folowing

x0: 0

y0: 1

h: .1

Unknown: .2

Result of 0.200000 = 1.242787

St.Mary’scollege,Thrissur

Page 11: Numerical Methods in C

PROGRAM

/* program to compute integral using Simpsons rule*/#define f(x) 1/(1+x)#include<stdio.h>#include<conio.h>void main(){

float a,b,h,result,temp1=0,temp2=0;float x[20],y[20];int i,j,n;clrscr();printf("\n\n\tSIMPSONS 1/3rd RULE\n");printf("\n\t---------------------\n");printf("Enter the Lower and Upper limits\n");scanf("%f %f",&a,&b);printf("\nEnter the number of intervals\n");scanf("%d",&n);h=(b-a)/n;for(i=0;i<=n;++i){

x[i]=a+(i*h);}for(i=0;i<=n;++i){

y[i]=f(x[i]);}printf("\n\nx\t\ty\n");printf("\n-------\n");for(i=0;i<=n;++i){

printf("\n%f\t%f",x[i],y[i]);}h=(b-a)/(2*n);result=y[0]+y[n];for(i=1;i<=n-1;i+=2){

temp1=temp1+y[i];}result+=(temp1*4);for(i=2;i<=n-2;i+=2){

temp2=temp2+y[i];}result+=temp2*2;result*=h/3;printf("\n\n\tResult = %f",result);getch();

}

St.Mary’scollege,Thrissur

Page 12: Numerical Methods in C

OUTPUT

SIMPSONS 1/3rd RULE -----------------------------Enter the Lower and Upper limits01

Enter the number of intervals2

x y---------------------------

0.000000 1.0000000.500000 0.6666671.000000 0.500000

Result = 0.347222

St.Mary’scollege,Thrissur

Page 13: Numerical Methods in C

PROGRAM

/*program to find the integral at a point using modified Eulers method*/#include<stdio.h>#include<conio.h>#define f(x,y) (y-(x*x))void main(){

float x0,x1,y1,y0,y2,y3,h,unknown,result;clrscr();printf("\n\n\tMODIFIED EULERS METHOD\n");printf("\n\t----------------------\n");printf("\nEnter the values for the following:\n");printf("\n\tx0: ");scanf("%f",&x0);printf("\n\ty0: ");scanf("%f",&y0);printf("\n\tSteplength: ");scanf("%f",&h);printf("\n\tThe value whose f(x) to be found: ");scanf("%f",&unknown);while(x0<=unknown){

y1=y0+(h*f(x0,y0));x1=x0+h;while(1){

y2=y0+(h/2)*(f(x0,y0)+f(x1,y1));y3=y0+(h/2)*(f(x0,y0)+f(x1,y2));if(y2==y3){

break;}y1=y2;

}x0+=h;result=y0;y0=y1;

}printf("\nResult:\n\tf(%f) = %f",unknown,result);getch();

}

St.Mary’scollege,Thrissur

Page 14: Numerical Methods in C

OUTPUT

MODIFIED EULERS METHOD

--------------------------------------

Enter the values for the following:

x0: 1

y0: 2

Steplength: .5

The value whose f(x) to be found: 2

Result: f(0.600000) = 2.180510

St.Mary’scollege,Thrissur

Page 15: Numerical Methods in C

PROGRAM

/*program to compute the integral value at a point using legranges interpolation- 3rd order*/#include<stdio.h>#include<conio.h>#define f(x) (1/(1+(x*x)))void main(){

int a,b;float h,result,r1,r2;clrscr();printf("\n\n\tLEGRANGES INTERPOLATION - 3rd ORDER\n");printf("\n\t-----------------------------------\n\n");printf("Enter the Upper limit & Lower limit:\n");scanf("%d %d",&b,&a);h=(b-a)/2.0;result=(f(-0.77459))+(f(0.77459));r1=result*(5.0/9.0);r2=(8.0/9.0)*f(0);result=(r1+r2)*h;printf("\nResult:\n\tf(.6) = %f",result);getch();

}

OUTPUT

LEGRANGES INTERPOLATION - 3rd ORDER

----------------------------------------------------------

Enter the Upper limit & Lower limit:1-1

Result: f(.6) = 1.583338

St.Mary’scollege,Thrissur

Page 16: Numerical Methods in C

PROGRAM

/*program for Bisection method*/#define f(x) (x*x*x-x-1)#include<stdio.h>#include<conio.h>void main(){

float i=0,j=1,f1,f2,x0;clrscr();while(1){

f1=f(i); /* initial guess*/f2=f(j);if((f1<0&&f2>0)||(f1>0&&f2<0))

break;else{

i++;j=i+1;

}}printf("\n\tROOT OF THE (x*x*x-x-1) USING BISECTION METHOD");printf("\n\t---------------------------------------------------\n\n");while((j-i)>.001){

x0=(i+j)/2; /* finding the root of the equation*/printf("\t%f %f %f %f\n",i,j,x0,f(x0));if(f(x0)<0)

i=x0;else

j=x0;}printf("\n\n\tRoot of the equation=%f",x0);getch();

}

St.Mary’scollege,Thrissur

Page 17: Numerical Methods in C

OUTPUT

ROOT OF (x*x*x-x-1) USING BISECTION METHOD --------------------------------------------------------------------

1.000000 2.000000 1.500000 0.875000 1.000000 1.500000 1.250000 -0.296875 1.250000 1.500000 1.375000 0.224609 1.250000 1.375000 1.312500 -0.051514 1.312500 1.375000 1.343750 0.082611 1.312500 1.343750 1.328125 0.014576 1.312500 1.328125 1.320312 -0.018711 1.320312 1.328125 1.324219 -0.002128 1.324219 1.328125 1.326172 0.006209 1.324219 1.326172 1.325195 0.002037

Root of the equation=1.325195

St.Mary’scollege,Thrissur

Page 18: Numerical Methods in C

PROGRAM

/*Program for find the root of the equation(x*x*x-x-1) by False position method*/

#define f(x) (x*x*x-x-1)#include<stdio.h>#include<conio.h>void main(){

float i=0,j=1,f1,f2,x0,fp,fq,x1,x2=0,prev;clrscr();while(1){

f1=f(i);f2=f(j);if((f1<0&&f2>0)||(f1>0&&f2<0)) /* Finding the intervals*/

break;else{

i++;j=i+1;

}printf("\n The Intervals are:\t%f,\t%f\n",i,j);x0=i;x1=j;do{

prev=x2;fp=f(x0);fq=f(x1);x2=x0-(fp*(x1-x0)/(fq-fp));printf("Value of x0=%f\tValue of x1=%f\tValue of x2=%f\tValue of

F(x2)=%f\n",x0,x1,x2,f(x2));if(f(x2)<0)

x0=x2;else

x1=x2;

}while((x2-prev)>.001);}

printf("\nRoot of the equation=%f",x2);getch();

}

St.Mary’scollege,Thrissur

Page 19: Numerical Methods in C

OUTPUT

The Intervals are: 1.000000, 2.000000

Value of x0=1.000000 Value of x1=2.000000 Value of x2=1.166667 Value of F(x2)=-0.578704

Value of x0=1.166667 Value of x1=2.000000 Value of x2=1.253112 Value of F(x2)=-0.285363

Value of x0=1.253112 Value of x1=2.000000 Value of x2=1.293437 Value of F(x2)=-0.129542

Value of x0=1.293437 Value of x1=2.000000 Value of x2=1.311281 Value of F(x2)=-0.056589

Value of x0=1.311281 Value of x1=2.000000 Value of x2=1.318988 Value of F(x2)=-0.024304

Value of x0=1.318988 Value of x1=2.000000 Value of x2=1.322283 Value of F(x2)=-0.010362

Value of x0=1.322283 Value of x1=2.000000 Value of x2=1.323684 Value of F(x2)=-0.004404

St.Mary’scollege,Thrissur

Page 20: Numerical Methods in C

Value of x0=1.323684 Value of x1=2.000000 Value of x2=1.324279 Value of F(x2)=-0.001869

Root of the equation=1.324279

St.Mary’scollege,Thrissur

Page 21: Numerical Methods in C

PROGRAM

/*Program for find the root of the eqn(x*x*x-2x-5) by Newton Raffson Method method*/#define f(x) (x*x*x-2x-5)#define q(x) (3*x*x-2)#include<stdio.h>#include<conio.h>void main(){

float i=0,j=1,f1,f2,fp,fq,x1,x2,prev,x0;clrscr();printf("\n\n\tNEWTON RAFFSON METHOD\n");printf("\n\t---------------------\n");while(1){

f1=f(i);f2=f(j);if((f1<0&&f2>0)||(f1>0&&f2<0)) /* Finding the intervals*/

break;else{

i++;j=i+1;

}printf("\n The Intervals are:\t%f,\t%f\n\n",i,j);x0=i;x1=j;}x2=(x0+x1)/2;printf("x2\t\tf(x2)\t\tf'(x2)\n\n\n");while(1){ prev=x2; /*Assigning the current value to the previous value*/ x2=prev-(f(prev)/q(prev)); if(x2==prev) break; else printf("%f\t%f\t%f\n",x2,f(x2),q(x2)); } printf("\n\n THE ROOT IS =%f",x2); getch(); }

St.Mary’scollege,Thrissur

Page 22: Numerical Methods in C

OUTPUT

NEWTON RAFFSON METHOD

-------------------------------------------

The Intervals are: 1.000000, 2.000000

The Intervals are: 2.000000, 3.000000

x2 f(x2) f'(x2)

2.164179 0.807945 12.0510132.097135 0.028881 11.1939292.094555 0.000041 11.1614842.094552 0.000001 11.161439

THE ROOT IS =2.094552

St.Mary’scollege,Thrissur

Page 23: Numerical Methods in C

PROGRAM:

/*program for gauss elimination method…*/#include<conio.h>#include<stdio.h>void main(){ int m,n,p,q,i,j; float a[10][10],b[10][10],x,y,z,t; /*…Declaration…*/ clrscr(); printf("\nGAUSS ELIMINATION METHOD\n"); printf("********************************\n"); printf("\nInput the raw size of first matrix:"); scanf("%d",&m); printf("Input the column size of first matrix:"); scanf("%d",&n); printf("\nInput the %d elements to %d*%d matrix:\n\n",m*n,m,n); for(i=0;i<m;i++) /*…For loop…*/ { for(j=0;j<n;j++) { scanf("%f",&a[i][j]); } } printf("\n\nInput the raw size of second matrix:"); scanf("%d",&p); printf("Input the column size of second matrix:"); scanf("%d",&q); printf("\nInput the %d elements to %d*%d matrix:\n\n",p*q,p,q); for(i=0;i<p;i++) /*…For loop…*/ { for(j=0;j<q;j++) { scanf("%f",&b[i][j]); } } printf("\n\tMatrix A:\n\n"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%f\t",a[i][j]); /*…Print matrix A…*/ } printf("\n"); } printf("\n\tMatrix B:\n\n"); for(i=0;i<p;i++) {

St.Mary’scollege,Thrissur

Page 24: Numerical Methods in C

for(j=0;j<q;j++) { printf("%f\t",b[i][j]); /*…Print matrix B…*/ } printf("\n"); } t=a[0][0]; for(j=0;j<m;j++) { a[0][j]/=t; } b[0][0]/=t; for(i=1;i<p;i++) { t=a[i][0]; for(j=0;j<m;j++) { a[i][j]-=(t*a[0][j]); /*…Calculations…*/ } b[i][0]-=(t*b[0][0]); } t=a[1][1]; for(j=1;j<=m;j++) { a[1][j]/=t; } b[1][0]/=t; t=a[2][1]; for(j=1;j<m;j++) { a[2][j]-=(t*a[1][j]); } b[2][0]-=(t*b[1][0]); printf("\n\tThe result is:\n"); z=b[2][0]/a[2][2]; y=b[1][0]-(a[1][2]*z); /*…Printing the output…*/ x=b[0][0]-((a[0][1]*y)+(a[0][2]*z)); printf("\n\tx=%f\n\ty=%f\n\tz=%f\n",x,y,z); getch();

}

St.Mary’scollege,Thrissur

Page 25: Numerical Methods in C

OUTPUT:

GAUSS ELIMINATION METHOD*****************************

Input the raw size of first matrix: 3Input the column size of first matrix: 3

Input the 9 elements to 3*3 matrix:

4 1 13 4 22 3 1

Input the raw size of second matrix: 3Input the column size of second matrix: 1

Input the 3 elements to 3*1 matrix :

11117

Matrix A:

4.000000 1.000000 1.0000003.000000 4.000000 2.0000002.000000 3.000000 1.000000

Matrix B:

11.00000011.0000007.000000

The result is:

x=2.333333 y=0.333333 z=1.333333

St.Mary’scollege,Thrissur

Page 26: Numerical Methods in C

PROGRAM

/* program using Newtons divided difference formula*/#include<stdio.h>#include<conio.h>#include<math.h>void main(){

float d[100][100],result,temp,unknown;int m,n,i,j,k;clrscr();printf("Enter the limit\n");scanf("%d %d",&m,&n);printf("Enter the values of x\n");for(i=0;i<m;i++){

scanf("%f",&d[i][0]);}printf("Enter the values of y\n");for(i=0;i<n;i++){

scanf("%f",&d[i][1]);}printf("Enter the value where f(x) to be found\n");scanf("%f",&unknown);printf(" x \t y");for(i=0;i<=(n-2);i++){

printf(" \ty%d",i);printf(“---------------------------------------------------------------------“);

}printf("\n");for(j=2;j<(n+1);j++){

k=j-1;for(i=0;i<(n+1)-j;i++){

d[i][j]=(d[i+1][j-1]-d[i][j-1])/(d[k][0]-d[k-(j-1)][0]);k++;

}}k=0;for(i=0;i<n;i++){

printf("\n\n");for(j=0;j<=n-k;j++){

St.Mary’scollege,Thrissur

Page 27: Numerical Methods in C

printf(" %f ",d[i][j]);}

k++;}result=d[0][1];for(j=2;j<n+1;j++){

temp=1;for(i=0;i<=j-2;i++){

temp=temp*(unknown-d[i][0]);}

result+=temp*d[0][j];}printf("\n\nf(%f) = %f",unknown,result);getch();

}

OUTPUT

Enter the limit66Enter the values of x457101113Enter the values of y4810029490012102028Enter the value where f(x) to be found8

St.Mary’scollege,Thrissur

Page 28: Numerical Methods in C

x y y0 y1 y2 y3 y4

--------------------------------------------------------------------------------------

4.000 48.000 52.000 15.000 1.000 0.000 0.000

5.000 100.000 97.000 21.000 1.000 0.000

7.000 294.000 202.000 27.000 1.000

10.000 900.000 310.000 33.000

11.000 1210.000 409.000

13.000 2028.000

f(8.000000) = 448.000000

St.Mary’scollege,Thrissur

Page 29: Numerical Methods in C

PROGRAM

/*find the integral using taylor series*/#include<stdio.h>#include<conio.h>#define f1(x,y) (x+(y*y))#define f2(y,y1) (1+(2*y*y1))#define f3(y,y1,y2) (2*(y*y2+y1*y1))#define f4(y,y1,y2,y3) (2*((y*y3+y2*y1)+(2*y1*y2)))void main(){

float x0,y0,y1,y2,y3,y4,unknown,h,result=0;clrscr();printf("\n\tTAYLOR SERIES\n");printf("\nEnter the initial values\n");scanf("%f %f",&x0,&y0);printf("Enter the unknown value, whose integral to be found\n\n");scanf("%f",&unknown);h=unknown-x0;/*first*/y1=f1(x0,y0);y2=f2(y0,y1);y3=f3(y0,y1,y2);y4=f4(y0,y1,y2,y3);result=y0+(h*y1)+((h*h)/2)*y2+((h*h*h)/6)*y3+((h*h*h*h)/24)*y4;printf("\nResult:\n\tf(%f)= %f",unknown,result);getch();

}

OUTPUT

TAYLOR SERIES

Enter the initial values01Enter the unknown value, whose integral to be found

.2

Result: f(0.200000)= 1.271067

St.Mary’scollege,Thrissur

Page 30: Numerical Methods in C

St.Mary’scollege,Thrissur