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("nntLEGRANGES INTERPOLATION -FIRST ORDERn");
printf("nt---------------------------------------n");
printf("nEnter the limitn");
scanf("%f",&n);
printf("Enter the values for xn");
for(i=0;i<n;i++)
{
scanf("%f",&x[i]);
}
printf("Enter the values of yn");
for(i=0;i<n;i++)
{
scanf("%f",&y[i]);
}
printf("Enter the value whose f(x) to be foundn");
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
OUTPUT
LEGRANGES INTERPOLATION -FIRST ORDER
------------------------------------------------------------
Enter the limit
4
Enter the values for x
300
304
305
307
Enter the values of y
2.4771
2.4829
2.4843
2.4871
Enter the value whose f(x) to be found
301
F(301.000000) = 2.478597
St.Mary’scollege,Thrissur
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&bn");
scanf("%f %f",&a,&b);
printf("nEnter the number of intervalsn");
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("xttty");
for(i=0;i<=n;i++)
{
printf("n%ftt%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("nn%f",result);
getch();
}
St.Mary’scollege,Thrissur
OUTPUT
Enter the Lower & Upper limit a&b
0
1
Enter the number of intervals
2
COMPUTING INTEGRAL USING-TRAPEZOIDAL RULE
x y
0.000000 1.000000
0.500000 0.666667
1.000000 0.500000
0.708333
St.Mary’scollege,Thrissur
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("nntEULERS METHODn");
printf("nEnter the value for x0n");
scanf("%f",&x0);
printf("Enter the value for y0n");
scanf("%f",&y0);
printf("Enter the steplengthn");
scanf("%f",&h);
printf("Enter the value for unknownn");
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 x0
1
Enter the value for y0
2
Enter the steplength
.5
Enter the value for unknown
St.Mary’scollege,Thrissur
2
Result= 2.257576
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("ntLEGRANGES INTERPOLATION - 2nd ORDERn");
printf("nt-----------------------------------n");
printf("nEnter the upper limit:nt");
scanf("%f",&a);
printf("Enter the lower limit:nt");
scanf("%f",&b);
h=(a-b)/2;
result=f(-0.57735)+f(0.57735);
result*=h;
printf("nnRESULT = %f",result);
getch();
}
LEGRANGES INTERPOLATION - 2nd ORDER
-----------------------------------------------------------
Enter the upper limit:
1
Enter the lower limit:
-1
RESULT = 1.583338
St.Mary’scollege,Thrissur
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("ntRUNGE KUTTA METHOD - 2nd ORDERn");
printf("nt------------------------------n");
printf("nEnter the values for the following:n");
printf("ntx0: ") ;
scanf("%f",&x0);
printf("nty0: ");
scanf("%f",&y0);
printf("nth: ");
scanf("%f",&h);
printf("ntUnknown: ");
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 = %fnRESULT = %f",unknown,y1);
getch();
}
St.Mary’scollege,Thrissur
OUTPUT
RUNGE KUTTA METHOD - 2nd ORDER
-----------------------------------------------------
Enter the values for the following:
x0: 0
y0: 1
h: .1
Unknown: .2
UNKNOWN = 0.200000
RESULT = 1.242050
St.Mary’scollege,Thrissur
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("nntRUNGE KUTTA - 3rd ORDERn");
printf("nt-----------------------n");
printf("Enter the values for the folowingn");
printf("ntx0: ");
scanf("%f",&x0);
printf("nty0: ");
scanf("%f",&y0);
printf("nth: ");
scanf("%f",&h);
printf("ntUnknown: ");
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("nn Result of %f = %f",unknown,y1);
getch();
}
St.Mary’scollege,Thrissur
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
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("nntSIMPSONS 1/3rd RULEn");
printf("nt---------------------n");
printf("Enter the Lower and Upper limitsn");
scanf("%f %f",&a,&b);
printf("nEnter the number of intervalsn");
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("nnxttyn");
printf("n-------n");
for(i=0;i<=n;++i)
{
printf("n%ft%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];
St.Mary’scollege,Thrissur
}
result+=(temp1*4);
for(i=2;i<=n-2;i+=2)
{
temp2=temp2+y[i];
}
result+=temp2*2;
result*=h/3;
printf("nntResult = %f",result);
getch();
}
OUTPUT
SIMPSONS 1/3rd RULE
-----------------------------
Enter the Lower and Upper limits
0
1
Enter the number of intervals
2
x y
---------------------------
0.000000 1.000000
0.500000 0.666667
1.000000 0.500000
Result = 0.347222
St.Mary’scollege,Thrissur
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("nntMODIFIED EULERS METHODn");
printf("nt----------------------n");
printf("nEnter the values for the following:n");
printf("ntx0: ");
scanf("%f",&x0);
printf("nty0: ");
scanf("%f",&y0);
printf("ntSteplength: ");
scanf("%f",&h);
printf("ntThe 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;
St.Mary’scollege,Thrissur
result=y0;
y0=y1;
}
printf("nResult:ntf(%f) = %f",unknown,result);
getch();
}
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
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("nntLEGRANGES INTERPOLATION - 3rd ORDERn");
printf("nt-----------------------------------nn");
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:ntf(.6) = %f",result);
getch();
}
OUTPUT
St.Mary’scollege,Thrissur
LEGRANGES INTERPOLATION - 3rd ORDER
----------------------------------------------------------
Enter the Upper limit & Lower limit:
1
-1
Result:
f(.6) = 1.583338
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("ntROOT OF THE (x*x*x-x-1) USING BISECTION METHOD");
printf("nt---------------------------------------------------nn");
while((j-i)>.001)
{
x0=(i+j)/2; /* finding the root of the equation*/
printf("t%f %f %f %fn",i,j,x0,f(x0));
if(f(x0)<0)
i=x0;
else
St.Mary’scollege,Thrissur
j=x0;
}
printf("nntRoot of the equation=%f",x0);
getch();
}
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
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%fn",i,j);
x0=i;
x1=j;
do
{
prev=x2;
fp=f(x0);
St.Mary’scollege,Thrissur
fq=f(x1);
x2=x0-(fp*(x1-x0)/(fq-fp));
printf("Value of x0=%ftValue of x1=%ftValue of x2=%ftValue of
F(x2)=%fn",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();
}
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
St.Mary’scollege,Thrissur
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
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
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("nntNEWTON RAFFSON METHODn");
printf("nt---------------------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%fnn",i,j);
x0=i;
St.Mary’scollege,Thrissur
x1=j;
}
x2=(x0+x1)/2;
printf("x2ttf(x2)ttf'(x2)nnn");
while(1)
{
prev=x2; /*Assigning the current value to the previous value*/
x2=prev-(f(prev)/q(prev));
if(x2==prev)
break;
else
printf("%ft%ft%fn",x2,f(x2),q(x2));
}
printf("nn THE ROOT IS =%f",x2);
getch();
}
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.051013
2.097135 0.028881 11.193929
2.094555 0.000041 11.161484
2.094552 0.000001 11.161439
THE ROOT IS =2.094552
St.Mary’scollege,Thrissur
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 METHODn");
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:nn",m*n,m,n);
for(i=0;i<m;i++) /*…For loop…*/
{
for(j=0;j<n;j++)
{
scanf("%f",&a[i][j]);
}
}
printf("nnInput the raw size of second matrix:");
St.Mary’scollege,Thrissur
scanf("%d",&p);
printf("Input the column size of second matrix:");
scanf("%d",&q);
printf("nInput the %d elements to %d*%d matrix:nn",p*q,p,q);
for(i=0;i<p;i++) /*…For loop…*/
{
for(j=0;j<q;j++)
{
scanf("%f",&b[i][j]);
}
}
printf("ntMatrix A:nn");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%ft",a[i][j]); /*…Print matrix A…*/
}
printf("n");
}
printf("ntMatrix B:nn");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%ft",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;
}
St.Mary’scollege,Thrissur
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("ntThe 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("ntx=%fnty=%fntz=%fn",x,y,z);
getch();
}
OUTPUT:
GAUSS ELIMINATION METHOD
*****************************
Input the raw size of first matrix: 3
Input the column size of first matrix: 3
Input the 9 elements to 3*3 matrix:
4 1 1
3 4 2
2 3 1
Input the raw size of second matrix: 3
Input the column size of second matrix: 1
Input the 3 elements to 3*1 matrix :
11
11
7
St.Mary’scollege,Thrissur
Matrix A:
4.000000 1.000000 1.000000
3.000000 4.000000 2.000000
2.000000 3.000000 1.000000
Matrix B:
11.000000
11.000000
7.000000
The result is:
x=2.333333
y=0.333333
z=1.333333
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 limitn");
scanf("%d %d",&m,&n);
printf("Enter the values of xn");
for(i=0;i<m;i++)
{
scanf("%f",&d[i][0]);
}
printf("Enter the values of yn");
for(i=0;i<n;i++)
{
St.Mary’scollege,Thrissur
scanf("%f",&d[i][1]);
}
printf("Enter the value where f(x) to be foundn");
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("nn");
for(j=0;j<=n-k;j++)
{
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("nnf(%f) = %f",unknown,result);
getch();
}
St.Mary’scollege,Thrissur
OUTPUT
Enter the limit
6
6
Enter the values of x
4
5
7
10
11
13
Enter the values of y
48
100
294
900
1210
2028
Enter the value where f(x) to be found
8
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
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("ntTAYLOR SERIESn");
printf("nEnter the initial valuesn");
scanf("%f %f",&x0,&y0);
printf("Enter the unknown value, whose integral to be foundnn");
scanf("%f",&unknown);
St.Mary’scollege,Thrissur
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:ntf(%f)= %f",unknown,result);
getch();
}
OUTPUT
TAYLOR SERIES
Enter the initial values
0
1
Enter the unknown value, whose integral to be found
.2
Result:
f(0.200000)= 1.271067
St.Mary’scollege,Thrissur

Numerical Methods in C

  • 1.
    PROGRAM /*program for legrangesinterpolation method*/ #include<stdio.h> #include<conio.h> void main() { float x[20],y[20],unknown,temp,result=0,n,i,j; clrscr(); printf("nntLEGRANGES INTERPOLATION -FIRST ORDERn"); printf("nt---------------------------------------n"); printf("nEnter the limitn"); scanf("%f",&n); printf("Enter the values for xn"); for(i=0;i<n;i++) { scanf("%f",&x[i]); } printf("Enter the values of yn"); for(i=0;i<n;i++) { scanf("%f",&y[i]); } printf("Enter the value whose f(x) to be foundn"); 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
  • 2.
    OUTPUT LEGRANGES INTERPOLATION -FIRSTORDER ------------------------------------------------------------ Enter the limit 4 Enter the values for x 300 304 305 307 Enter the values of y 2.4771 2.4829 2.4843 2.4871 Enter the value whose f(x) to be found 301 F(301.000000) = 2.478597 St.Mary’scollege,Thrissur
  • 3.
    PROGRAM /* program toclaculate 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&bn"); scanf("%f %f",&a,&b); printf("nEnter the number of intervalsn"); 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("xttty"); for(i=0;i<=n;i++) { printf("n%ftt%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("nn%f",result); getch(); } St.Mary’scollege,Thrissur
  • 4.
    OUTPUT Enter the Lower& Upper limit a&b 0 1 Enter the number of intervals 2 COMPUTING INTEGRAL USING-TRAPEZOIDAL RULE x y 0.000000 1.000000 0.500000 0.666667 1.000000 0.500000 0.708333 St.Mary’scollege,Thrissur
  • 5.
    PROGRAM /*program to solvedifferential 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("nntEULERS METHODn"); printf("nEnter the value for x0n"); scanf("%f",&x0); printf("Enter the value for y0n"); scanf("%f",&y0); printf("Enter the steplengthn"); scanf("%f",&h); printf("Enter the value for unknownn"); 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 x0 1 Enter the value for y0 2 Enter the steplength .5 Enter the value for unknown St.Mary’scollege,Thrissur
  • 6.
    2 Result= 2.257576 PROGRAM /* programto 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("ntLEGRANGES INTERPOLATION - 2nd ORDERn"); printf("nt-----------------------------------n"); printf("nEnter the upper limit:nt"); scanf("%f",&a); printf("Enter the lower limit:nt"); scanf("%f",&b); h=(a-b)/2; result=f(-0.57735)+f(0.57735); result*=h; printf("nnRESULT = %f",result); getch(); } LEGRANGES INTERPOLATION - 2nd ORDER ----------------------------------------------------------- Enter the upper limit: 1 Enter the lower limit: -1 RESULT = 1.583338 St.Mary’scollege,Thrissur
  • 7.
    PROGRAM /* program tosolve 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("ntRUNGE KUTTA METHOD - 2nd ORDERn"); printf("nt------------------------------n"); printf("nEnter the values for the following:n"); printf("ntx0: ") ; scanf("%f",&x0); printf("nty0: "); scanf("%f",&y0); printf("nth: "); scanf("%f",&h); printf("ntUnknown: "); 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 = %fnRESULT = %f",unknown,y1); getch(); } St.Mary’scollege,Thrissur
  • 8.
    OUTPUT RUNGE KUTTA METHOD- 2nd ORDER ----------------------------------------------------- Enter the values for the following: x0: 0 y0: 1 h: .1 Unknown: .2 UNKNOWN = 0.200000 RESULT = 1.242050 St.Mary’scollege,Thrissur
  • 9.
    PROGRAM /* program usingRunge 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("nntRUNGE KUTTA - 3rd ORDERn"); printf("nt-----------------------n"); printf("Enter the values for the folowingn"); printf("ntx0: "); scanf("%f",&x0); printf("nty0: "); scanf("%f",&y0); printf("nth: "); scanf("%f",&h); printf("ntUnknown: "); 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("nn Result of %f = %f",unknown,y1); getch(); } St.Mary’scollege,Thrissur
  • 10.
    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
  • 11.
    PROGRAM /* program tocompute 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("nntSIMPSONS 1/3rd RULEn"); printf("nt---------------------n"); printf("Enter the Lower and Upper limitsn"); scanf("%f %f",&a,&b); printf("nEnter the number of intervalsn"); 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("nnxttyn"); printf("n-------n"); for(i=0;i<=n;++i) { printf("n%ft%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]; St.Mary’scollege,Thrissur
  • 12.
    } result+=(temp1*4); for(i=2;i<=n-2;i+=2) { temp2=temp2+y[i]; } result+=temp2*2; result*=h/3; printf("nntResult = %f",result); getch(); } OUTPUT SIMPSONS1/3rd RULE ----------------------------- Enter the Lower and Upper limits 0 1 Enter the number of intervals 2 x y --------------------------- 0.000000 1.000000 0.500000 0.666667 1.000000 0.500000 Result = 0.347222 St.Mary’scollege,Thrissur
  • 13.
    PROGRAM /*program to findthe 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("nntMODIFIED EULERS METHODn"); printf("nt----------------------n"); printf("nEnter the values for the following:n"); printf("ntx0: "); scanf("%f",&x0); printf("nty0: "); scanf("%f",&y0); printf("ntSteplength: "); scanf("%f",&h); printf("ntThe 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; St.Mary’scollege,Thrissur
  • 14.
    result=y0; y0=y1; } printf("nResult:ntf(%f) = %f",unknown,result); getch(); } OUTPUT MODIFIEDEULERS 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
  • 15.
    PROGRAM /*program to computethe 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("nntLEGRANGES INTERPOLATION - 3rd ORDERn"); printf("nt-----------------------------------nn"); 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:ntf(.6) = %f",result); getch(); } OUTPUT St.Mary’scollege,Thrissur
  • 16.
    LEGRANGES INTERPOLATION -3rd ORDER ---------------------------------------------------------- Enter the Upper limit & Lower limit: 1 -1 Result: f(.6) = 1.583338 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("ntROOT OF THE (x*x*x-x-1) USING BISECTION METHOD"); printf("nt---------------------------------------------------nn"); while((j-i)>.001) { x0=(i+j)/2; /* finding the root of the equation*/ printf("t%f %f %f %fn",i,j,x0,f(x0)); if(f(x0)<0) i=x0; else St.Mary’scollege,Thrissur
  • 17.
    j=x0; } printf("nntRoot of theequation=%f",x0); getch(); } 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
  • 18.
    PROGRAM /*Program for findthe 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%fn",i,j); x0=i; x1=j; do { prev=x2; fp=f(x0); St.Mary’scollege,Thrissur
  • 19.
    fq=f(x1); x2=x0-(fp*(x1-x0)/(fq-fp)); printf("Value of x0=%ftValueof x1=%ftValue of x2=%ftValue of F(x2)=%fn",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(); } 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 St.Mary’scollege,Thrissur
  • 20.
    Value of x0=1.311281 Valueof 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 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
  • 21.
    PROGRAM /*Program for findthe 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("nntNEWTON RAFFSON METHODn"); printf("nt---------------------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%fnn",i,j); x0=i; St.Mary’scollege,Thrissur
  • 22.
    x1=j; } x2=(x0+x1)/2; printf("x2ttf(x2)ttf'(x2)nnn"); while(1) { prev=x2; /*Assigning thecurrent value to the previous value*/ x2=prev-(f(prev)/q(prev)); if(x2==prev) break; else printf("%ft%ft%fn",x2,f(x2),q(x2)); } printf("nn THE ROOT IS =%f",x2); getch(); } 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.051013 2.097135 0.028881 11.193929 2.094555 0.000041 11.161484 2.094552 0.000001 11.161439 THE ROOT IS =2.094552 St.Mary’scollege,Thrissur
  • 23.
    PROGRAM: /*program for gausselimination 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 METHODn"); 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:nn",m*n,m,n); for(i=0;i<m;i++) /*…For loop…*/ { for(j=0;j<n;j++) { scanf("%f",&a[i][j]); } } printf("nnInput the raw size of second matrix:"); St.Mary’scollege,Thrissur
  • 24.
    scanf("%d",&p); printf("Input the columnsize of second matrix:"); scanf("%d",&q); printf("nInput the %d elements to %d*%d matrix:nn",p*q,p,q); for(i=0;i<p;i++) /*…For loop…*/ { for(j=0;j<q;j++) { scanf("%f",&b[i][j]); } } printf("ntMatrix A:nn"); for(i=0;i<m;i++) { for(j=0;j<n;j++) { printf("%ft",a[i][j]); /*…Print matrix A…*/ } printf("n"); } printf("ntMatrix B:nn"); for(i=0;i<p;i++) { for(j=0;j<q;j++) { printf("%ft",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; } St.Mary’scollege,Thrissur
  • 25.
    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("ntThe 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("ntx=%fnty=%fntz=%fn",x,y,z); getch(); } OUTPUT: GAUSS ELIMINATION METHOD ***************************** Input the raw size of first matrix: 3 Input the column size of first matrix: 3 Input the 9 elements to 3*3 matrix: 4 1 1 3 4 2 2 3 1 Input the raw size of second matrix: 3 Input the column size of second matrix: 1 Input the 3 elements to 3*1 matrix : 11 11 7 St.Mary’scollege,Thrissur
  • 26.
    Matrix A: 4.000000 1.0000001.000000 3.000000 4.000000 2.000000 2.000000 3.000000 1.000000 Matrix B: 11.000000 11.000000 7.000000 The result is: x=2.333333 y=0.333333 z=1.333333 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 limitn"); scanf("%d %d",&m,&n); printf("Enter the values of xn"); for(i=0;i<m;i++) { scanf("%f",&d[i][0]); } printf("Enter the values of yn"); for(i=0;i<n;i++) { St.Mary’scollege,Thrissur
  • 27.
    scanf("%f",&d[i][1]); } printf("Enter the valuewhere f(x) to be foundn"); 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("nn"); for(j=0;j<=n-k;j++) { 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("nnf(%f) = %f",unknown,result); getch(); } St.Mary’scollege,Thrissur
  • 28.
    OUTPUT Enter the limit 6 6 Enterthe values of x 4 5 7 10 11 13 Enter the values of y 48 100 294 900 1210 2028 Enter the value where f(x) to be found 8 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
  • 29.
    PROGRAM /*find the integralusing 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("ntTAYLOR SERIESn"); printf("nEnter the initial valuesn"); scanf("%f %f",&x0,&y0); printf("Enter the unknown value, whose integral to be foundnn"); scanf("%f",&unknown); St.Mary’scollege,Thrissur
  • 30.