LAB # 01
Object:
To find the root of f(x)=0 using Bisection Method (using CPP).
Program:
// To find the root of f(x)=0 using Bisection Method
#include <iostream.h>
#include<constream.h>
#include <conio.h>
#include <math.h>
#include<stdio.h>
#define f(x) (pow(x,3)-pow(x,2)+x-7)
void main ()
{
clrscr();
float a,b,xn,xo=a,et,error;
cout<<endl<<"Enter the value of a, b, and error tolerance value in % = "<<endl;
cin>>a>>b>>et;
int i=1;
cout<<endl<<"*************Bisection Meth-od**************\n"<<endl;
cout<<"it#"<<setw(10)<<"a"<<setw(17)<<"b"<<setw(17)<<"Root"<<setw(16)<<"error in %"<<endl;
do
{ cout<<i;
xn=(a+b)/2;
error=fabs((xn-xo)/xn)*100;
printf("\t%.5f\t\t%.5f\t\t%.5f\t\t%.5f",a,b,xn,error);
cout<<"\n............................................................................\n";
if (f(a)*f(xn)<0)
{b=xn; xo=xn;}
Else
{a=xn; xo=xn;}
i++;
}
while (error>et);
cout<<"\n The approximate root containing "<<et<<" % error is = "<<xn;
getch();
}
JUNAID ABBASI (11ES16)
1
RESULT:Enter the value of a, b, and error tolerance value in % =
015
*************Bisection Meth-od**************
it#
1
a
0.00000
b
Root
1.00000
error in %
0.50000
99.20654
............................................................................
2
0.50000
1.00000
0.75000
33.33333
............................................................................
3
0.75000
1.00000
0.87500
14.28571
............................................................................
4
0.87500
1.00000
0.93750
6.66667
............................................................................
5
0.93750
1.00000
0.96875
3.22581
............................................................................
The approximate root containing 5 % error is = 0.96875
JUNAID ABBASI (11ES16)
2
LAB # 02
Object: To find the root of f(x)=0 using Regula Falsi Method.
Program:
// to find the root of the f(x)=0 using regula falsi method
#include <constream.h>
#include <math.h>
#include <iomanip.h>
#include <stdio.h>
#define f(x) (pow(x,3)-4*x-9)
void main()
{
clrscr();
float a,b,xn,xo=a,et,error;
int iteration=1;
cout<<"enter the value of a,b and error tolerance value in %=";
cin>>a>>b>>et;
cout<<" regula falsi method";
cout<<"it#"<<setw(11)<<"a"<<setw(18)<<"b"<<setw(18)<<"root"<<setw(23)<<"error in %\n"<<endl;
do
{
cout<<iteration;
xn=(a*f(b)-b*f(a))/f(b)-f(a);
error=fabs((xn-xo)/xn)*100;
printf("\t\t%.5f\t\t%.5f\t\t%.5f\t\t%.5f",a,b,xn,error);
cout<<"\n.........................................................................\n";
if (f(a)*f(xn)<0)
{
b=xn; xo=xn; }
else
{
a=xn; xo=xn; }
iteration++;
}
while (error>et);
cout<<"\n the approximate root containing "<<et<<" %error is ="<<xn;
getche();
}
JUNAID ABBASI (11ES16)
3
RESULT:Enter the value of a,b and error tolerance value in %=
014
regula falsi methodit#
a
b
root
error in %
1
0.00000
1.00000
8.25000
99.91048
.........................................................................
2
0.00000
8.25000
9.14292
9.76626
.........................................................................
3
0.00000
9.14292
9.11449
0.31192
.........................................................................
the approximate root containing 4 %error is =9.114491
JUNAID ABBASI (11ES16)
4
LAB # 03
Object: To find out the root of f(x)=0 using Newton Raphson Method.
Program:
// To find the root of y=f(x) using Newton-Raphson Method
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <iomanip.h>
#include <stdio.h>
#define f(x) (2*x+sin(x)-exp(x))
#define df(x) (2+cos(x)-exp(x))
void main()
{ clrscr();
float xo,et,error,xn;
int iteration=1;
cout<<endl<<"Enter initial guess and error tolerance value in % = ";
cin>>xo>>et;
cout<<endl<<"*******************NEWTON-RAPHSON METHOD*********************\n"<<endl;
cout<<"\tit#"<<setw(18)<<"Root"<<setw(20)<<" Error in % "<<endl;
do
{ cout<<endl<<"\t"<<iteration;
xn=xo-f(xo)/df(xo);
error=fabs((xn-xo)/xn)*100;
printf ("\t\t%.5f\t\t%.5f",xn,error);
cout<<"\n..........................................................";
xo=xn;
iteration++;
}
while (error>et);
cout<<"\n\n The approximate root is = "<<xn<<" containing "<<et<<" % error.";
getch();
}
JUNAID ABBASI (11ES16)
5
RESULT:Enter initial guess and error tolerance value in % = 0 0.01
*******************NEWTON-RAPHSON METHOD*********************
it#
1
Root
0.50000
Error in %
100.00000
..........................................................
2
0.63777
21.60138
..........................................................
3
0.66116
3.53806
..........................................................
4
0.66196
0.12166
..........................................................
5
0.66196
0.00015
..........................................................
The approximate root is = 0.661965 containing 0.01 % error.
JUNAID ABBASI (11ES16)
6
LAB # 04
Object: To find the root of f(x)=0 using Modified Newton-Raphson Method.
Program:
// To find the root of y=f(x) using Modified Newton-Raphson Method
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <iomanip.h>
#include <stdio.h>
#define f(x) (3*x+sin(x)-exp(x))
#define d1f(x) (3+cos(x)-exp(x))
#define d2f(x) (-sin(x)-exp(x))
void main()
{ clrscr();
float xo,et,error,xn;
int iteration=1;
cout<<endl<<"Enter initial guess and error tolerance value in % = ";
cin>>xo>>et;
cout<<endl<<"*************MODIFIED NEWTON-RAPHSON METHOD***************\n"<<endl;
cout<<"\tit#"<<setw(18)<<"Root"<<setw(20)<<" Error in % "<<endl;
do
{ cout<<endl<<"\t"<<iteration;
xn=xo-(f(xo)*d1f(xo))/(d1f(xo)*d1f(xo)-f(xo)*d2f(xo));
error=fabs((xn-xo)/xn)*100;
printf ("\t\t%.5f\t\t%.5f",xn,error);
cout<<"\n..........................................................";
xo=xn;
iteration++;
}
while (error>et);
cout<<"\n\n The approximate root is = "<<xn<<" containing "<<et<<" % error.";
getch();
}
JUNAID ABBASI (11ES16)
7
Enter initial guess and error tolerance value in % = 0 5
****************Modified Newton-Raphson Method*****************
it#
1
Root
0.37500
Error in %
100.00000
..........................................................
2
0.36050
4.02209
..........................................................
The approximate root is = 0.3605 containing 5 % error.
JUNAID ABBASI (11ES16)
8
LAB # 05
Object: To find root of f(x)=0 using Fixed Point Iteration Method.
Program:
// To find root of f(x)=0 using Fixed Point Iteration Method
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <iomanip.h>
#include <stdio.h>
#define g(x) (1/((x*x)+1))
void main ()
{ clrscr();
float x0,xn,error,et;
int iteration=1;
cout<<endl<<"What is initial point (x0) and error tolerance (et) in % ?"<<endl;
cin>>x0>>et;
cout<<"*******************Fixed Point Iteration Method*************************"<<endl;
cout<<"\tit#"<<setw(18)<<"Root"<<setw(20)<<"error in %"<<endl;
do
{ cout<<"\t"<<iteration;
xn=g(x0);
error=fabs((xn-x0)/xn)*100;
printf("\t\t%.5f\t\t%.5f",xn,error);
cout<<"\n................................................................\n";
x0=xn;
iteration++;
}
while (error>et);
cout<<"\n\n Approximate Root is = "<<xn<<" containing "<<et<<" % error";
getch();
}
JUNAID ABBASI (11ES16)
9
RESULT:-
What is initial point (x0) and error tolerance (et) in % ?
05
*******************Fixed Point Iteration Method*************************
it#
1
Root
1.00000
error in %
100.00000
................................................................
2
0.50000
100.00000
................................................................
3
0.80000
37.50000
................................................................
4
0.60976
31.20000
................................................................
5
0.72897
16.35350
................................................................
6
0.65300
11.63373
................................................................
7
0.70106
6.85556
................................................................
8
0.67047
4.56240
................................................................
Approximate Root is = 0.670472 containing 5 % error
JUNAID ABBASI (11ES16)
10
LAB # 06
Object: To find the approximate solution of linear system using Jacobi’s Method.
Program:
// The Jacobi's Method for Linear Systems
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include <constream.h>
#include <iostream.h>
#define ESP 0.001
#define X1(x2,x3) ((21-x2+2*x3)/30)
#define X2(x1,x3) ((17-3*x1+x3)/30)
#define X3(x1,x2) ((19-2*x1+3*x2)/30)
void main()
{
float x1=0,x2=0,x3=0,y1,y2,y3,errx1,errx2,errx3;
int iteration=1;
clrscr();
printf("\n************************The Jacobi Method***************************** \n");
printf("\nit# \t x1\t x2\t
x3\t
errx1\t errx2\t errx3 \n");
do
{ cout<<endl<< iteration;
y1=X1(x2,x3);y2=X2(x1,x3);y3=X3(x1,x2);
errx1=fabs((y1-x1)/y1);errx2=fabs((y2-x2)/y2);errx3=fabs((y3-x3)/y3);
printf("\t%.4f\t %.4f\t %.4f\t %.4f\t %.4f\t %.4f ",y1,y2,y3,errx1,errx2,errx3);
x1=y1;x2=y2;x3=y3;
iteration++;
}
while (errx1>ESP||errx2>ESP||errx3>ESP);
cout<<"\n\n The Approximate solution is x1 = "<<y1<<" , x2 = "<<y2<<" and x3 = "<<y3<<" . ";
getch();
}
JUNAID ABBASI (11ES16)
11
RESULT:************************The Jacobi Method*****************************
it#
x1
x2
x3
errx1
errx2
errx3
1
0.7000
0.5667
0.6333 1.0000 1.0000
1.0000
2
0.7233
0.5178
0.6433 0.0323 0.0944
0.0155
3
0.7256
0.5158
0.6369 0.0032 0.0039
0.0101
4
0.7253
0.5153
0.6365 0.0005 0.0009
0.0006
The Approximate solution is x1 = 0.725267 , x2 = 0.515333 and x3 = 0.636536 .
JUNAID ABBASI (11ES16)
12
Lab # 07
Program:
// Gauss's Seidal Method for Linear Systems
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include <constream.h>
#include <iostream.h>
#define ESP 0.001
#define X1(x2,x3) ((13-x2+x3)/4)
#define X2(x1,x3) ((21-3*x1-2*x3)/3)
#define X3(x1,x2) ((14-2*x1-x2)/6)
void main()
{
float x1=0,x2=0,x3=0,y1,y2,y3,errx1,errx2,errx3;
int iteration=1;
clrscr();
printf("\n************************Gauss Seidal Method***************************** \n");
printf("\nit# \t x1\t x2\t
x3\t
errx1\t errx2\t errx3 \n");
do
{ cout<<endl<< iteration;
y1=X1(x2,x3);errx1=fabs((y1-x1)/y1);x1=y1;
y2=X2(x1,x3);errx2=fabs((y2-x2)/y2);x2=y2;
y3=X3(x1,x2);errx3=fabs((y3-x3)/y3);x3=y3;
printf("\t%.4f\t %.4f\t %.4f\t %.4f\t %.4f\t %.4f ",y1,y2,y3,errx1,errx2,errx3);
iteration++;
}
while (errx1>ESP||errx2>ESP||errx3>ESP);
cout<<"\n\n The Approximate solution is x1 = "<<y1<<" , x2 = "<<y2<<" and x3 = "<<y3<<" . ";
getch();
}
JUNAID ABBASI (11ES16)
13
RESULT:************************Gauss Seidal Method*****************************
it#
x1
x2
x3
errx1
errx2
errx3
1
3.2500
3.7500
0.6250 1.0000 1.0000
1.0000
2
2.4688
4.1146
0.8247 0.3165 0.0886
0.2421
3
2.4275
4.0227
0.8537 0.0170 0.0228
0.0340
4
2.4577
3.9731
0.8519 0.0123 0.0125
0.0021
5
2.4697
3.9624
0.8497 0.0048 0.0027
0.0026
6
2.4718
3.9617
0.8491 0.0009 0.0002
0.0007
The Approximate solution is x1 = 2.471834 , x2 = 3.961696 and x3 = 0.849106 .
JUNAID ABBASI (11ES16)
14
Lab # 08
Program:
// Power Method to find Dominant Eigenvalue and its Corresponding Eigenvector
#include<constream.h>
#include <math.h>
#include <stdio.h>
#include <iostream.h>
void main()
{
clrscr();
float a[10][10],x[10],y[10],d[10],ibig=0.0,big,error,et;
int i,j,n,iteration=1;
cout<<"\n enter order matrix = ";cin>>n;
cout<<"\n enter initial vector = ";
for (i=1;i<=n;i++)
cin>>x[i];
cout<<"\n enter error tolerance in % = ";cin>>et;
cout<<"\n enter coefficients of the matrix = ";
for (i=1;i<=n;i++)
{cout<<" \n row "<<i<<" = ";
for (j=1;j<=n;j++)
cin>>a[i][j];
}
clrscr();
cout<<"\n ---------------------------------Power Method-------------------------------\n";
cout<<"\n it #"<<setw(10)<<" E-Value "<<setw(15)<<" Error "<<setw(30)<<"
"<<endl;
ab:
for (i=1;i<=n;i++)
{
y[i]=0.0;
for (j=1;j<=n;j++)
y[i]+=a[i][j]*x[j];
}
for (i=1;i<=n;i++)
d[i]=fabs(y[i]);
big=d[1];
for(i=2;i<=n;i++)
if (d[i]>big)
big=d[i];
cout<<endl<<iteration;
error=fabs((big-ibig)/big)*100;
printf(" \t % .4f \t % .4f ",big,error);
for (i=1;i<=n;i++)
x[i]=(y[i]/big);
E-Vector
JUNAID ABBASI (11ES16)
15
for (i=1;i<=n;i++)
printf(" \t % .4f ",x[i]);
if (error>et)
{
ibig=big;
iteration++;
goto ab;
}
cout<<"\n Thus the dominant eigenvalue is = "<<big<<" containing "<<et<<" % error. ";
getch();
}
RESULT:-
---------------------------------Power Method-------------------------------
it #
E-Value
Error
E-Vector
1
24.0000
100.0000
0.6667
0.1250
1.0000
2
5.7083
320.4380
0.6861
0.1752
1.0000
3
5.8978
3.2127
0.6844
0.1696
1.0000
4
5.8775
0.3460
0.6846
0.1701
1.0000
5
5.8796
0.0354
0.6845
0.1701
1.0000
Thus the dominant eigenvalue is = 5.879554 containing 0.1 % error.
JUNAID ABBASI (11ES16)
16
Lab # 09
Program:
// Forward Difference Table
#include<stdio.h>
#include<math.h>
#include <constream.h>
void main()
{ clrscr();
float x[10],y[10][10];
int n,i,j;
printf("Enter total number of values n = ");
scanf("%d",&n);
printf("X\tY\n");
for(i = 1;i<=n;i++)
{
scanf("%f %f",&x[i],&y[i][0]);
}
//Forward Difference Table
for(j=1;j<n;j++)
for(i=1;i<=(n-j);i++)
y[i][j] = y[i+1][j-1] - y[i][j-1];
printf("\n***********Forward Difference Table ***********\n");
printf("\n------------------------------------------------------\n\n");
//Display Forward Difference Table
for(i=1;i<=n;i++)
{
printf("\t%.2f \t%.2f",x[i],y[i][0]);
for(j=1;j<=(n-i);j++)
printf("\t%.2f",y[i][j]);
printf("\n");
}
getch();
}
JUNAID ABBASI (11ES16)
17
RESULT:Enter total number of values n = 5
X
Y
1
2
2
4
3
5
4
7
5
9
***********Forward Difference Table ***********
------------------------------------------------------
1.00 2.00 2.00 -1.00 2.00 -3.00
2.00 4.00 1.00 1.00 -1.00
3.00 5.00 2.00 0.00
4.00 7.00 2.00
5.00 9.00
JUNAID ABBASI (11ES16)
18
Lab # 10
Program:
// Backward Difference Table
#include <stdio.h>
#include <math.h>
#include <constream.h>
void main()
{ clrscr();
float x[10],y[10][10];
int n,i,j;
printf("Enter n : ");
scanf("%d",&n);
printf("X\tY\n");
for(i = 1;i<=n;i++)
{
scanf("%f %f",&x[i],&y[i][0]);
}
//Backward Difference Table
for(j=1;j<n;j++)
for(i=n;i>(j-1);i--)
y[i][j] = y[i][j-1] - y[i-1][j-1];
printf("\n***********Backward Difference Table ***********\n\n");
//Display Backward Difference Table
for(i=1;i<=n;i++)
{
printf("\t%.2f\t%.2f",x[i],y[i][0]);
for(j=1;j<i;j++)
printf("\t%.2f",y[i][j]);
printf("\n");
}
getch();
}
JUNAID ABBASI (11ES16)
19
Enter n : 5
X
Y
1
9
2
16
3
33
4
36
5
60
***********Backward Difference Table ***********
1.00 9.00
2.00 16.00 7.00
3.00 33.00 17.00 10.00
4.00 36.00 3.00 -14.00 -24.00
5.00 60.00 24.00 21.00 35.00 59.00
JUNAID ABBASI (11ES16)
20
Lab # 11
Program:
// Divided Difference Table
#include <stdio.h>
#include <conio.h>
#include <constream.h>
#include <math.h>
void main()
{
clrscr();
float x[10],y[10][10];
int i,n,j,k;
printf("\n How many data values are there? n = ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\n\n enter the value of x%d: ",i);
scanf("%f",&x[i]);
printf("\n\n enter the value of f(x%d): ",i);
scanf("%f",&y[k][i]);
}
clrscr();
for(i=1;i<n;i++)
{
k=i;
for(j=0;j<n-i;j++)
{
y[i][j]=(y[i-1][j+1]-y[i-1][j])/(x[k]-x[j]);
k++;
}
}
printf("\n ----------------------Divided Difference Table----------------------------");
printf("\n__________________________________________________________________________\n")
;
printf("\n x(i)\t
y(i)\t
y1(i)
y2(i)
y3(i) ");
printf("\n__________________________________________________________________________\n")
;
for(i=0;i<n;i++)
{
printf("\n %.4f",x[i]);
for(j=0;j<n-i;j++)
{
printf("
");
JUNAID ABBASI (11ES16)
21
printf(" %.4f",y[j][i]);
}
printf("\n");
}
getch();
}
Result:-
----------------------Divided Difference Table---------------------------__________________________________________________________________________
x(i)
y(i)
y1(i)
y2(i)
y3(i)
__________________________________________________________________________
1.0000
2.0000
-1.0000
0.3000
0.0000
3.0000
1.7000
-0.0233
10.0000
20.0000
30.0000
40.0000
-0.0111
1.0000
JUNAID ABBASI (11ES16)
22
Lab # 12
Program:
// Newton's Forward Interpolation Formula For Equally - Spaced Data
#include <stdio.h>
#include <math.h>
#include <constream.h>
#include <iostream.h>
void main()
{ clrscr();
float x[10],y[10][10],xn,p,h,f;
int n,i,j;
printf("Enter total number of values n = ");
scanf("%d",&n);
printf("X\tY\n");
for(i=1;i<=n;i++)
{
scanf("%f %f",&x[i],&y[i][0]);
}
//Forward Difference Table
for(j=1;j<n;j++)
for(i=1;i<=(n-j);i++)
y[i][j] = y[i+1][j-1] - y[i][j-1];
printf("\n***********Forward Difference Table ***********\n");
printf("\n------------------------------------------------------\n\n");
//Display Forward Difference Table
for(i=1;i<=n;i++)
{
printf("\t%.2f \t%.2f",x[i],y[i][0]);
for(j=1;j<=(n-i);j++)
printf("\t%.2f",y[i][j]);
printf("\n");
}
// Newton's Forward Interpolation Formula's Calculation
printf("\n enter xn at which y is needed? = ");
cin>>xn;
h=x[2]-x[1];
p=(xn-x[1])/h;
cout<<"\nThe value of p is = "<<p<<endl;
f=y[1][0]+p*y[1][1]+(p*(p-1)*y[1][2])/2+(p*(p-1)*(p-2)*y[1][3])/6;
cout<<"\n ---------Newton Forward Interpolation Formula--------"<<endl;
cout<<"\n The required value is f("<<xn<<") = "<<f;
getch();
}
JUNAID ABBASI (11ES16)
23
RESULT:Enter total number of values n = 4
X
Y
1.5
1.36
2
2.5
3
0.58
0.34
0.2
***********Forward Difference Table ***********
------------------------------------------------------
1.50 1.36 -0.78 0.54 -0.44
2.00 0.58 -0.24 0.10
2.50 0.34 -0.14
3.00 0.20
enter xn at which y is needed? = 1.6
The value of p is = 0.2
---------Newton Forward Interpolation Formula--------
The required value is f(1.6) = 1.13968
JUNAID ABBASI (11ES16)
24
Lab # 13
Program:
// Newton's Backward Interpolation Formula For Equally - Spaced Data
#include <stdio.h>
#include <math.h>
#include <constream.h>
#include <iostream.h>
void main()
{ clrscr();
float x[10],y[10][10],xn,p,h,f;
int n,i,j;
printf("Enter n : ");
scanf("%d",&n);
printf("X\tY\n");
for(i = 1;i<=n;i++)
{
scanf("%f %f",&x[i],&y[i][0]);
}
//Backward Difference Table
for(j=1;j<n;j++)
for(i=n;i>(j-1);i--)
y[i][j] = y[i][j-1] - y[i-1][j-1];
printf("\n***********Backward Difference Table ***********\n\n");
//Display Backward Difference Table
for(i=1;i<=n;i++)
{
printf("\t%.2f\t%.2f",x[i],y[i][0]);
for(j=1;j<i;j++)
printf("\t%.2f",y[i][j]);
printf("\n");
}
// Newton's Backward Interpolation Formula's Calculation for four data points
printf("\n enter xn at which y is needed? = ");cin>>xn;
h=x[2]-x[1];p=(xn-x[4])/h;
cout<<"\nThe value of p is = "<<p<<endl;
f=y[4][0]+p*y[4][1]+(p*(p+1)*y[4][2])/2+(p*(p+1)*(p+2)*y[4][3])/6;
cout<<"\n ---------Newton Backward Interpolation Formula--------"<<endl;
cout<<"\n The required value is f("<<xn<<") = "<<f;
getch();
}
JUNAID ABBASI (11ES16)
25
Result:Enter n : 4
X
Y
5
8
8
16
11
24
14
32
***********Backward Difference Table ***********
5.00 8.00
8.00 16.00 8.00
11.00 24.00 8.00 0.00
14.00 32.00 8.00 0.00 0.00
enter xn at which y is needed? = 12
The value of p is = -0.666667
---------Newton Backward Interpolation Formula--------
The required value is f(12) = 26.666666
JUNAID ABBASI (11ES16)
26
Lab # 14
Program:
// Newton's Divided Difference Interpolation Formula
# include <stdio.h>
# include <conio.h>
#include <math.h>
# define MAX 20
void main()
{
clrscr();
int i,j,n;
float x[MAX],y[MAX][MAX],xp,u,sum=0;
printf("\n*********Newton Divided Difference formula********** \n");
printf("Enter No. of data points : ");
scanf("%d",&n);
printf("Enter values of X and Y set by set each at once \n");
for(i=1;i<=n;i++)
scanf("%f %f",&x[i],&y[i][1]);
printf("Enter value of X at which interpolation is required : ");
scanf("%f",&xp);
for(i=2;i<=n;i++)
for(j=1;j<=n-i+1;j++)
y[j][i]=(y[j+1][i-1]-y[j][i-1])/(x[j+i-1]-x[j]);
printf("-------------------------------------------------------------------\n");
printf("X\t F(x) ");
for(i=1;i<n;i++)
printf(" y(%d) ",i);
printf("\n-----------------------------------------------------------------\n");
for(i=1;i<=n;i++)
{
printf("%4.3f\t ",x[i]);
for(j=1;j<=n-i+1;j++)
printf(" %4.3f ",y[i][j]);
printf("\n");
}
printf("\n-----------------------------------------------------------------\n");
sum=y[1][1];
for(i=2;i<=n;i++)
{
u=1;
for(j=1;j<i;j++)
u*=(xp-x[j]);
u*=y[1][i];
sum+=u;
}
printf("For %3.3f Interpolated value = %4.3f",xp,sum);
JUNAID ABBASI (11ES16)
27
getch();
}
Result:
*********Newton Divided Difference formula**********
Enter No. of data points : 4
Enter values of X and Y set by set each at once
-2
0.033
0
0
3
0.0822
5
0.1718
Enter value of X at which interpolation is required : 1.265
------------------------------------------------------------------X
F(x)
y(1)
y(2)
y(3)
-----------------------------------------------------------------2.000
0.033 -0.016 0.009 -0.001
0.000
0.000 0.027 0.003
3.000
0.082 0.045
5.000
0.172
----------------------------------------------------------------For 1.265 Interpolated value = 0.021
JUNAID ABBASI (11ES16)
28
Lab # 15
Program:
/********** Lagrange's Interpolation ***************/
#include <constream.h>
#include <iostream.h>
#include <conio.h>
#include <stdio.h>
#include <math.h>
void main()
{ clrscr();
int n,i,j;
float mult,sum=0,x[10],f[10],a;
cout<<"\n Enter total number of data points = ";cin>>n;
cout<<"\n Enter all values of x and corresponding funtional value = "<<endl;
printf("\n x \t y \n");
for(i=0;i<n;i++)
cin>>x[i]>>f[i];
cout<<"\n Enter your x for calculation = ";cin>>a;
for(i=0;i<n;i++)
{
mult=1;
for(j=0;j<n;j++)
{
if(j!=i)
mult*=(a-x[j])/(x[i]-x[j]);
}
sum+=mult*f[i];
}
cout<<"\n The estimated value of f(x) = "<<sum;
getch();
}
JUNAID ABBASI (11ES16)
29
RESULT:Enter total number of data points = 4
Enter all values of x and corresponding funtional value =
x
y
0
5
3
7
6
10
9
13
Enter your x for calculation = 1.25
The estimated value of f(x) = 5.647666
JUNAID ABBASI (11ES16)
30
Lab # 16
Program:
/**** Least-Squares Curve Fitting By Straight Line ****/
#include <constream.h>
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main()
{ clrscr();
int n,i,j;
float p,a,b,x[10],y[10],sumx=0,sumy=0,sumxy=0,sumx2=0;
cout<<"\n Enter number of data points = ";cin>>n;
cout<<"\n Enter all x and y points = "<<endl;
printf("X\tY\n");
for(i = 1;i<=n;i++)
{
scanf("%f %f",&x[i],&y[i]);
sumx+=x[i];sumy+=y[i];sumxy+=x[i]*y[i];sumx2+=x[i]*x[i];
}
clrscr();
cout<<"\n ******
Curve Fitting by Straight Line *********"<<endl;
printf("\n \tx\ty\tx2\txy");
cout<<"\n------------------------------------------------\n";
for(i=1;i<=n;i++)
{
printf("\n \t%.2f \t%.2f\t%.2f \t%.2f",x[i],y[i],x[i]*y[i],x[i]*x[i]);
printf("\n");
}
cout<<"\n-------------------------------------------------";
cout<<"\n Enter x at which y is required = ";
cin>>p;
a=(n*sumxy-sumx*sumy)/(n*sumx2-sumx*sumx);
b=(1/n)*(sumy-a*sumx);
cout<<"\n Line of the best fit is = "<<a<<"x"<<"+"<<b<<endl;
cout<<"\n The predicted value of y at given x is = "<<a*p+b<<endl;
getch();
}
JUNAID ABBASI (11ES16)
31
RESULT:******
x
Curve Fitting by Straight Line
y
x2
*********
xy
------------------------------------------------
1.00 5.00 5.00 1.00
2.00 6.00 12.00 4.00
3.00 7.00 21.00 9.00
4.00 8.00 32.00 16.00
5.00 9.00 45.00 25.00
------------------------------------------------Enter x at which y is required = 3.4
Line of the best fit is = 1x+0
The predicted value of y at given x is = 3.4
JUNAID ABBASI (11ES16)
32
Lab # 17
Program:
// Curve Fitting by Parabolic Curve
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float x[10],y[10],sum[10],mat[3][4],temp=0,temp1=0,a1,a2,a3;
int i,n,j;
float fact(int);
clrscr();
printf("\nHow many record you will be enter: ");
scanf("%d",&n);
for(i=0; i<n; i++)
{
printf("\n\nenter the value of x%d: ",i);
scanf("%f",&x[i]);
temp=temp+x[i];
printf("\n\nenter the value of y%d: ",i);
scanf("%f",&y[i]);
temp1=temp1+y[i];
}
sum[0]=temp;
sum[1]=temp1;
sum[2]=sum[3]=sum[4]=sum[5]=sum[6]=0;
for(i=0;i<n;i++)
{
sum[2]=sum[2]+(x[i]*x[i]);
sum[3]=sum[3]+(x[i]*x[i]*x[i]);
sum[4]=sum[4]+(x[i]*x[i]*x[i]*x[i]);
sum[5]=sum[5]+(x[i]*y[i]);
sum[6]=sum[6]+(x[i]*x[i]*y[i]);
}
mat[0][0]=n;
mat[0][1]=mat[1][0]=sum[0];
mat[0][2]=mat[1][1]=mat[2][0]=sum[2];
mat[1][2]=mat[2][1]=sum[3];
mat[2][2]=sum[4];
mat[0][3]=sum[1];
mat[1][3]=sum[5];
mat[2][3]=sum[6];
temp=mat[1][0]/mat[0][0];
JUNAID ABBASI (11ES16)
33
temp1=mat[2][0]/mat[0][0];
for(i=0,j=0;j<3+1;j++)
{
mat[i+1][j]=mat[i+1][j]-(mat[i][j]*temp);
mat[i+2][j]=mat[i+2][j]-(mat[i][j]*temp1);
}
temp=mat[2][1]/mat[1][1];
temp1=mat[0][1]/mat[1][1];
for(i=1,j=0;j<3+1;j++)
{
mat[i+1][j]=mat[i+1][j]-(mat[i][j]*temp);
mat[i-1][j]=mat[i-1][j]-(mat[i][j]*temp1);
}
temp=mat[0][2]/mat[2][2];
temp1=mat[1][2]/mat[2][2];
for(i=0,j=0;j<3+1;j++)
{
mat[i][j]=mat[i][j]-(mat[i+2][j]*temp);
mat[i+1][j]=mat[i+1][j]-(mat[i+2][j]*temp1);
}
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf(" %.3f ",mat[i][j]);
}
printf("\n\n");
}
a3 = mat[2][3]/mat[2][2];
a2 = mat[1][3]/mat[1][1];
a1 = mat[0][3]/mat[0][0];
printf("\n\nx = %.3f",a1);
printf("\n\ny = %.3f",a2);
printf("\n\nz = %.3f",a3);
getch();
}
JUNAID ABBASI (11ES16)
34
RESULT:How many record you will be enter: 4
enter the value of x0: 2
enter the value of y0: 1
enter the value of x1: 2.5
enter the value of y1: 1.3
enter the value of x2: 4
enter the value of y2: 2
enter the value of x3: 5
enter the value of y3: 2.3
4.000 -0.000 -0.000 -2.133
0.000 5.688 0.000 5.119
0.000 -0.000 2.918 -0.195
x = -0.533
y = 0.900
z = -0.067
JUNAID ABBASI (11ES16)
35
Lab # 18
Program:
/**** Least-Squares Curve Fitting By Exponential Curve ****/
#include <constream.h>
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main()
{ clrscr();
int n,i,j;
float p,A,B,x[10],y[10],sumx=0,sumy=0,sumx2=0,sumY=0,sumxY=0;
cout<<"\n Enter number of data points = ";cin>>n;
cout<<"\n Enter all x and y points = "<<endl;
printf("X\tY\n");
for(i = 1;i<=n;i++)
{
scanf("%f %f",&x[i],&y[i]);
sumx+=x[i];sumy+=y[i];sumY+=log(y[i]);sumxY+=x[i]*log(y[i]);sumx2+=x[i]*x[i];
}
clrscr();
cout<<"\n ****** Curve Fitting by Exponential Curve *********"<<endl;
printf("\n x\ty\tY=log(y)\txY\tx2");
cout<<"\n------------------------------------------------\n";
for(i=1;i<=n;i++)
{
printf("\n %.3f\t%.3f\t%.3f\t%.3f\t%.3f",x[i],y[i],log(y[i]),x[i]*log(y[i]),x[i]*x[i]);
printf("\n");
}
cout<<"\n-------------------------------------------------";
cout<<"\n Enter x at which y is required = ";
cin>>p;
A=(sumx2*sumY-sumx*sumxY)/(n*sumx2-sumx*sumx);
B=(n*sumxY-sumx*sumY)/(n*sumx2-sumx*sumx);
cout<<"\n Exponential Curve of the best fit is = "<<exp(A)<<" e ^"<<"("<<B<<" t )"<<endl;
cout<<"\n The predicted value of y at given x is = "<<exp(A)*exp(B*p)<<endl;
getch();
}
JUNAID ABBASI (11ES16)
36
Result:-
****** Curve Fitting by Exponential Curve *********
x
y
Y=log(y)
xY
x2
------------------------------------------------
0.000 150.000 5.011 0.000 0.000
2.000 63.000 4.143 8.286 4.000
4.000 28.000 3.332 13.329 16.000
6.000 12.000 2.485 14.909 36.000
8.000 5.600 1.723 13.782 64.000
------------------------------------------------Enter x at which y is required = 3
Exponential Curve of the best fit is = 146.279953 e ^(-0.411698 t )
The predicted value of y at given x is = 42.539264
JUNAID ABBASI (11ES16)
37
Lab # 19
Program:
/**** Least-Squares Curve Fitting By Logarithmic Curve ****/
#include <constream.h>
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main()
{ clrscr();
int n,i,j;
float p,a,b,x[10],y[10],sumx=0,sumy=0,sumX=0,sumX2=0,sumXy=0;
cout<<"\n Enter number of data points = ";cin>>n;
cout<<"\n Enter all x and y points = "<<endl;
printf("X\tY\n");
for(i = 1;i<=n;i++)
{
scanf("%f %f",&x[i],&y[i]);
sumx+=x[i];sumy+=y[i];sumX+=log(x[i]),sumXy+=log(x[i])*y[i];sumX2+=log(x[i])*log(x[i]);
}
clrscr();
cout<<"\n ****** Curve Fitting by Logarithmic Curve *********"<<endl;
printf("\n x \ty\tX=log(x)\tXy\tX^2");
cout<<"\n------------------------------------------------\n";
for(i=1;i<=n;i++)
{
printf("\n %.3f \t%.3f\t%.3f\t%.3f\t%.3f",x[i],y[i],log(x[i]),log(x[i])*y[i],log(x[i])*log(x[i]));
printf("\n");
}
cout<<"\n-------------------------------------------------";
cout<<"\n Enter x at which y is required = ";
cin>>p;
a=(sumX2*sumy-sumX*sumXy)/(n*sumX2-sumX*sumX);
b=(n*sumXy-sumX*sumy)/(n*sumX2-sumX*sumX);
cout<<"\n Logarithmic Curve of the best fit is y = "<<a<<" + "<<b<<" ln(x) "<<endl;
cout<<"\n The predicted value of y at given x is = "<<a+b*log(p)<<endl;
getch();
}
JUNAID ABBASI (11ES16)
38
Result:
****** Curve Fitting by Logarithmic Curve *********
x
y
X=log(x)
Xy
X^2
------------------------------------------------
10.000
4.000 2.303 9.210 5.302
20.000
5.000 2.996 14.979 8.974
40.000
6.000 3.689 22.133 13.608
100.000
7.000 4.605 32.236 21.208
------------------------------------------------Enter x at which y is required = 12
Logarithmic Curve of the best fit is y = 1.052357 + 1.308865 ln(x)
The predicted value of y at given x is = 4.304764
JUNAID ABBASI (11ES16)
39
Lab # 20
Program:
/**** Least-Squares Curve Fitting By Power Function Curve ****/
#include <constream.h>
#include <stdio.h>
#include <iostream.h>
#include <conio.h>
#include <math.h>
void main()
{ clrscr();
int n,i,j;
float p,A,B,x[10],y[10],sumx=0,sumy=0,sumX=0,sumY=0,sumX2=0,sumXY=0;
cout<<"\n Enter number of data points = ";cin>>n;
cout<<"\n Enter all x and y points = "<<endl;
printf("X\tY\n");
for(i = 1;i<=n;i++)
{
scanf("%f %f",&x[i],&y[i]);
sumx+=x[i];sumy+=y[i];sumX+=log(x[i]),sumY+=log(y[i]);sumXY+=log(x[i])*log(y[i]);sumX2+=log(x
[i])*log(x[i]);
}
clrscr();
cout<<"\n ****** Curve Fitting by Power Function *********"<<endl;
printf("\n x\ty\tX=log(x)\tY=log(y)\tXY\tX^2");
cout<<"\n------------------------------------------------\n";
for(i=1;i<=n;i++)
{
printf("\n %.3f \t%.3f \t%.3f \t%.3f \t%.3f
\t%.3f",x[i],y[i],log(x[i]),log(y[i]),log(x[i])*log(y[i]),log(x[i])*log(x[i]));
printf("\n");
}
cout<<"\n-------------------------------------------------";
cout<<"\n Enter x at which y is required = ";
cin>>p;
A=(sumX2*sumY-sumX*sumXY)/(n*sumX2-sumX*sumX);
B=(n*sumXY-sumX*sumY)/(n*sumX2-sumX*sumX);
cout<<"\n Power Function Curve of the best fit is = "<<exp(A)<<" x ^"<<B<<endl;
cout<<"\n The predicted value of y at given x is = "<<exp(A)*pow(p,B)<<endl;
getch();
}
JUNAID ABBASI (11ES16)
40
Result:
****** Curve Fitting by Power Function *********
x
y
X=log(x)
Y=log(y)
XY
X^2
------------------------------------------------
2.000 4.000 0.693 1.386 0.961 0.480
4.000 6.000 1.386 1.792 2.484 1.922
6.000 10.000 1.792 2.303 4.126 3.210
------------------------------------------------Enter x at which y is required = 3
Power Function Curve of the best fit is = 2.192933 x ^0.807222
The predicted value of y at given x is = 5.323144
JUNAID ABBASI (11ES16)
41
Lab # 21
Program:
// First, Second and Third Derivatives Using Newton Forward Interpolation Formula
#include <stdio.h>
#include <math.h>
#include <constream.h>
#include <iostream.h>
void main()
{ clrscr();
float x[10],y[10][10],xn,p,h,d1f,d2f,d3f;
int n,i,j;
printf("Enter total number of values n = ");
scanf("%d",&n);
printf("X\tY\n");
for(i=1;i<=n;i++)
{
scanf("%f %f",&x[i],&y[i][0]);
}
//Forward Difference Table
for(j=1;j<n;j++)
for(i=1;i<=(n-j);i++)
y[i][j] = y[i+1][j-1] - y[i][j-1];
printf("\n***********Forward Difference Table ***********\n");
printf("\n------------------------------------------------------\n\n");
//Display Forward Difference Table
for(i=1;i<=n;i++)
{
printf("\t%.2f \t%.2f",x[i],y[i][0]);
for(j=1;j<=(n-i);j++)
printf("\t%.2f",y[i][j]);
printf("\n");
}
// Calculation of First, Second and Third Derivative For 5 Given Tabular data Points Using NFDIF:
printf("\n enter xn at which all 3 derivatives are needed? = ");
cin>>xn;
h=x[2]-x[1];
p=(xn-x[1])/h;
cout<<"\nThe value of p is = "<<p<<endl;
d1f=(1/h)*(y[1][1]+(2*p-1)*y[1][2]/2+(3*pow(p,2)-6*p+2)*y[1][3]/6+(4*pow(p,3)-18*pow(p,2)+22*p6)*y[1][4]/24);
d2f=(1/h*h)*(y[1][2]+(p-1)*y[1][3]+(6*pow(p,2)-18*p+11)*y[1][4]/12);
d3f=(1/h*h*h)*(y[1][3]+(12*p-18)*y[1][4]/12);
cout<<"\n ---------Newton Forward Interpolation Formula--------"<<endl;
cout<<"\n The required values are = "<<d1f<<" , "<<d2f<<" and "<<d3f;
getch(); }
JUNAID ABBASI (11ES16)
42
Result:
Enter total number of values n = 4
X
Y
0.6
0.6221
0.65 0.6155
0.7
0.6138
0.75 0.6170
***********Forward Difference Table ***********
------------------------------------------------------
0.60 0.62 -0.01 0.00 -0.00
0.65 0.62 -0.00 0.00
0.70 0.61 0.00
0.75 0.62
enter xn at which all 3 derivatives are needed? = 0.62
The value of p is = 0.4
---------Newton Forward Interpolation Formula--------
The required values are = -0.141801 , 0.0049 and -2.980229e-09
JUNAID ABBASI (11ES16)
43
Lab # 22
Program:
//Trapezoidal Rule when function is given
#include<constream.h>
#include<math.h>
#define f(x) (1/(1+x*x))
void main ( )
{ clrscr ( ) ;
float a,b,h,p,A,S=0;
int n;
cout<<"\nNumber of Sub-Intervals = "; cin>>n;
cout<<"\nValue of lower limit (a) = "; cin>>a;
cout<<"\nValue of higher limit (b) = "; cin>>b;
h=(b-a)/n;
for(p=(a+h);p<b;p=p+h)
{
S+=f(p);
}
A=(h/2)*( f(a)+f(b)+2*S);
cout<<"\n\n Result of required Definite Integral by Trapezoidal Rule is \n\n "<<endl;
cout<<A;
getch();
}
RESULT:Number of Sub-Intervals = 6
Value of lower limit (a) = 0
Value of higher limit (b) = 6
Result of required Definite Integral by Trapezoidal Rule is
1.410799
JUNAID ABBASI (11ES16)
44
lab # 23
Program:
//Trapezoidal Rule for Tabular Data
#include<constream.h>
#include<math.h>
void main ( )
{ clrscr ( ) ;
float x[20],f[20],h,sum=0.0,Area;
int n,i;
cout<<"\nEnter the total no. of data intervals : ";cin>>n;
cout<<"\nEnter the lower and upper limits of integration : ";
cin>>x[0]>>x[n];
h=(x[n]-x[0])/n;
for(i=0;i<=n;i++)
{
x[i]=x[0]+i*h;
cout<<"\nEnter the value of f(x) at "<<x[i]<<" : ";
cin>>f[i];
}
clrscr();
for(i=1;i<n;i++)
sum+=f[i];
Area=(h/2.0)*(f[0]+2*sum+f[n]) ;
cout<<"\n ****** Trapezoidal Rule for given Data Points *****";
cout<<"\n\n\tx\t\ty\n\n";
for(i=0;i<=n;i++)
cout<<"\n\t"<<x[i]<<"\t\t"<<f[i];
cout<<"\n\n\nUsing Trapezoidal Rule, the required result is = "<<Area;
getch();
}
Result:
****** Trapezoidal Rule for given Data Points *****
x
y
0
8
16
24
32
40
47
58
64
65
61
52
Using Trapezoidal Rule, the required result is = 2380
JUNAID ABBASI (11ES16)
45
Lab # 24
Program:
//Simpson's 1/3 Rule for given function (No. of Intervals should be even)
#include<constream.h>
#include<math.h>
#define f(x) (1/(1+x*x))
void main ( )
{ clrscr ( ) ;
float a, b, h, p, S1=0, S2=0, Area;
int n;
cout<<"No. of sub-intervals = ";
cin>>n;
cout<<"Value of lower limit (a) = "; cin>>a;
cout<<"Value of higher limit (b) = "; cin>>b;
h=(b-a)/n;
for(p=(a+2*h);p<b;p=p+2*h)
{
S1+=f(p); }
for(p=(a+h);p<b;p=p+2*h)
{
S2+=f(p); }
Area=(h/3)*
( f(a)+f(b)+2*S1+4*S2);
cout<<"\n Result of required Definite Integral by Simpson's 1/3 Rule is = " <<Area;
getch();
}
RESULT:No. of sub-intervals = 5
Value of lower limit (a) = 0
Value of higher limit (b) = 8
Result of required Definite Integral by Simpson's 1/3 Rule is = 1.349851
JUNAID ABBASI (11ES16)
46
Lab # 25
Program:
//Simpson's 1/3 Rule for tabular data (No. of Intervals should be even)
#include<constream.h>
#include<math.h>
void main ( )
{ clrscr ( ) ;
float x[20],f[20],h,sum1=0.0,sum2=0.0,Area;
int n,i;
cout<<"\nEnter the total no. of strips : ";
cin>>n;
cout<<"\nEnter the lower and upper limits of integration : ";
cin>>x[0]>>x[n];
h=(x[n]-x[0])/n;
for(i=0;i<=n;i++)
{
x[i]=x[0]+i*h;
cout<<"\nEnter the value of f(x) at "<<x[i]<<" : ";
cin>>f[i];
}
for(i=1;i<=n-1;i+=2)
sum1+=f[i];
for(i=2;i<=n-2;i+=2)
sum2+=f[i];
Area=(h/3.0)*(f[0]+4.0*sum1+2.0*sum2+f[n]);
cout<<"\n ****** Simpson's 1/3 Rule ******";
cout<<"\n\n\tx\t\ty\n\n";
cout<<"=====================================";
for(i=0;i<=n;i++)
cout<<"\n\t"<<x[i]<<"\t\t"<<f[i];
cout<<"\n\nUsing Simpson's 1/3rd rule, the required result is = "<<Area;
getch();
}
JUNAID ABBASI (11ES16)
47
Result:
Enter the total no. of strips : 5
Enter the lower and upper limits of integration : 0
4
Enter the value of f(x) at 0 : 1
Enter the value of f(x) at 0.8 : 1.5
Enter the value of f(x) at 1.6 : 2.5
Enter the value of f(x) at 2.4 : 2.8
Enter the value of f(x) at 3.2 : 3
Enter the value of f(x) at 4 : 3.5
****** Simpson's 1/3 Rule ******
x
y
=====================================
0
1
0.8
1.5
1.6
2.5
2.4
2.8
3.2
3
4
3.5
Using Simpson's 1/3rd rule, the required result is = 7.12
JUNAID ABBASI (11ES16)
48
Lab # 26
Program:
//Simpson 3/8 formula (No. of intervals must be multiple of 3)
#include<constream.h>
#include<math.h>
#define f(x) (2+log(x))/pow(x,2)
void main()
{ clrscr();
float a,b,h,Area,sum1=0.0,sum2=0.0;
int i,j,n;
cout<<"\n\n please enter a,b & no. of data points = \n\n";
cin>>a>>b>>n;
h=(b-a)/n;
j=3.0;
for(i=1;i<=n-1;i++)
{
if(i==j)
{
sum2+=f(a+h*i);
j=j+3;
}
else
sum1+=f(a+h*i);
}
Area=((3.0*h)/8.0)*(f(a)+3.0*sum1+2.0*sum2+f(b));
cout<<"\n\n Thus Simpson's 3/8 gave us = "<<Area;
getch();
}
Result:
please enter a,b & no. of data points =
125
Thus Simpson's 3/8 gave us = 1.134136
JUNAID ABBASI (11ES16)
49
Lab # 27
Program:
//Simpson's 3/8th Rule for Data Points
#include <iostream.h>
#include <conio.h>
#include <math.h>
#include <iomanip.h>
#include <stdio.h>
void main()
{
clrscr();
float x[20],f[20],h,sum1=0.0,sum2=0.0,Area;
int n,i,j;
cout<<"\nEnter the total no. of strips (multiple of 3) : ";
cin>>n;
cout<<"\nEnter the lower and upper limits of integration : ";
cin>>x[0]>>x[n];
h=(x[n]-x[0])/n;
for(i=0;i<=n;i++)
{ x[i]=x[0]+i*h;
cout<<"\nEnter the value of f(x) at "<<x[i]<<" : ";
cin>>f[i];
}
clrscr();
j=3.0;
for(i=1;i<=n-1;i++)
{
if(i==j)
{
sum2+=f[i];
j=j+3;
}
else
sum1+=f[i];
}
Area=((3.0*h)/8.0)*(f[0]+3.0*sum1+2.0*sum2+f[n]);
cout<<"\n **** Simpson's 3/8 Rule *****";
cout<<"\n\tx\t\ty\n";
cout<<"=====================================";
for(i=0;i<=n;i++)
cout<<"\n\t"<<x[i]<<setw(18)<<f[i];
cout<<"\n\n\n Result = "<<Area;
getch();
}
JUNAID ABBASI (11ES16)
50
RESULT:**** Simpson's 3/8 Rule *****
x
y
=====================================
0
44
10
56
20
66
30
68
40
78
50
88
Result = 3255
JUNAID ABBASI (11ES16)
51
Lab # 28
Program:
// Program to solve ordinary diff: equation by Taylor's Series Method
#include <conio.h>
#include <math.h>
#include <constream.h>
#define f(x,y)
(x+y)
#define d1f(x,y) (1+f(x,y))
#define d2f(x,y) (d1f(x,y))
#define d3f(x,y) (d2f(x,y))
void main()
{
clrscr();
float x0,y0,xn,yn,h;
cout<<"\n Please enter x0, y0, h, xn = \n "<<endl;
cin>>x0>>y0>>h>>xn;
while (x0<xn)
{
yn=y0+h*f(x0,y0)+(h*h/2.0)*d1f(x0,y0)+(h*h*h/6.0)*d2f(x0,y0)+(h*h*h*h/24)*d3f(x0,y0);
x0=x0+h;
cout<<"\n\n x = "<<x0<<"\t Y = "<<yn;
y0=yn;
}
getch();
}
RESULT:Please enter x0, y0, h, xn =
0 1 0.1 0.5
x = 0.1
Y = 1.110342
x = 0.2
Y = 1.242805
x = 0.3
Y = 1.399717
x = 0.4
Y = 1.583648
x = 0.5
Y = 1.797441
JUNAID ABBASI (11ES16)
52
Lab # 29
Program:
// Picard's Method of Successive Approximations for First Order ODE (IVP)
#include<stdio.h>
#include <math.h>
#include<conio.h>
// dy/dx = 1 + xy ; y(0)=1
#define Y1(x) (1 + (x) + pow(x,2)/2)
#define Y2(x) (1 + (x) + pow(x,2)/2 + pow(x,3)/3 + pow(x,4)/8)
#define Y3(x) (1 + (x) + pow(x,2)/2 + pow(x,3)/3 + pow(x,4)/8 + pow(x,5)/15 + pow(x,6)/48)
void main()
{
float y1[20],y2[20],y3[20],a,n,h,i;
int j;
clrscr();
printf("\n Enter the value of range: "); scanf("%f %f",&a,&n);
printf("\n Enter the h: "); scanf("%f",&h);
for(i=a,j=0;i<=n;i=i+h,j++)
{
y1[j]=Y1(i);
y2[j]=Y2(i);
y3[j]=Y3(i);
}
printf("\nX |");
for(i=a;i<=n;i=i+h)
{
printf(" %.3f",i); }
printf("\n--------------------------------------------------------------------------------");
printf("\n\nY1|");
for(i=a,j=0;i<=n;i=i+h,j++)
{
printf(" %.3f",y1[j]);
}
printf("\n\nY2|");
for(i=a,j=0;i<=n;i=i+h,j++)
{
printf(" %.3f",y2[j]);
}
printf("\n\nY3|");
for(i=a,j=0;i<=n;i=i+h,j++)
{
printf(" %.3f",y3[j]);
}
getch();
}
JUNAID ABBASI (11ES16)
53
RESULT:Enter the value of range: 0 6
Enter the h: 2
X | 0.000 2.000 4.000 6.000
--------------------------------------------------------------------------------
Y1| 1.000 5.000 13.000 25.000
Y2| 1.000 9.667 66.333 259.000
Y3| 1.000 13.133 219.933 1749.400
JUNAID ABBASI (11ES16)
54
Lab # 30
Program:
// Program for Euler's Method to solve First Order DE
#include <conio.h>
#include <iomanip.h>
# define f(x,y) (x+y)
void main()
{
clrscr();
float x0,y0,xn,yn,h;
cout<<"\n Enter the initial condition: x0 and y0 = "<<endl;
cin>>x0>>y0;
cout<<"\n Enter the value of x at which the result is required: xn = ";
cin>>xn;
cout<<"\n Enter the step size: h = ";
cin>>h;
while(x0<xn)
{
yn=y0+h*f(x0,y0);
x0+=h;
cout<<"\n X = "<<x0<<"\t Y = "<<yn;
y0=yn;
}
cout<<"\n\n The value of y at given x is = "<<yn;
getch();
}
RESULT:Enter the initial condition: x0 and y0 =
04
Enter the value of x at which the result is required: xn = 3
Enter the step size: h = .5
X = 0.5
X=1
X = 1.5
X=2
X = 2.5
X=3
Y=6
Y = 9.25
Y = 14.375
Y = 22.3125
Y = 34.46875
Y = 52.953125
The value of y at given x is = 52.953125
JUNAID ABBASI (11ES16)
55
Lab # 31
Program:
// Improved Euler's Method
#include<constream.h>
#include<math.h>
#define f(x,y) x+y
void main()
{
clrscr();
float xo,yo,yp,yc,xn,h;
cout<<"\nPLZ ENTER XO,YO,H,XN = ";
cin>>xo>>yo>>h>>xn;
while(xo<=xn)
{
yp=yo+h*f(xo,yo);
yc=yo+(h/2)*(f(xo,yo) +f(xo+h,yp));
xo=xo+h;yo=yc;
cout<<"\nX="<<xo<<"\tY="<<yc;
}
getch();
}
RESULT:PLZ ENTER XO,YO,H,XN = 0 1 .1 .5
X=0.1
X=0.2
X=0.3
X=0.4
X=0.5
X=0.6
Y=1.155
Y=1.34375
Y=1.571313
Y=1.843509
Y=2.167036
Y=2.549591
JUNAID ABBASI (11ES16)
56
Lab # 32
Program:
//Midpoint Euler's Method for First Order ODE (IVP)
#include<constream.h>
#include<math.h>
#define f(x,y) 1-2*x*y
void main()
{
clrscr();
float x0,y0,x,y,h;
int n,i;
cout<<"\n Enter the value of x0 and y0 = "; cin>>x0>>y0;
cout<<"\n Enter the value of x = "; cin>>x;
cout<<"\n Enter the value of h = "; cin>>h;
n=(x-x0)/h;
for(i=1;i<=n;i++)
{
y=y0+h*f(x0+h/2,y0+(h/2)*f(x0,y0));
x0=x0+h;
y0=y;
cout<<"\n\n y("<<x0<<")="<<y;
}
getch();
}
RESULT:Enter the value of x0 and y0 = 0 2
Enter the value of x = 1.5
Enter the value of h = .5
y(0.5)=3.25
y(1)=0.5625
y(1.5)=-1.671875
JUNAID ABBASI (11ES16)
57
lab # 33
Program:
//RK-Method of Order 3
#include<constream.h>
#include<math.h>
#define f(x,y) (-y)
void main(){
clrscr();
float x0,y0,x,y,h,k1,k2,k3,k;
int n,i;
cout<<"\n Enter the value of x0,y0 = ";
cin>>x0>>y0;
cout<<"\n Enter the value of x = ";
cin>>x;
cout<<"\nEnter the value of h = ";
cin>>h;
n=(x-x0)/h;
for(i=1;i<=n+1;i++)
{
k1=f(x0,y0);
k2=f(x0+h/2.0,y0+k1/2.0);
k3= f(x0+h,y0+2*k2-k1);
y=y0+(h/6.0)*(k1+4*k2+k3);
x0=x0+h;
y0=y;
cout<<"\n\n y("<<x0<<")="<<y;
}
getch();
}
RESULT:Enter the value of x0,y0 = 0 4
Enter the value of x = 1
Enter the value of h = 0.5
y(0.5)=0.666667
y(1)=0.111111
y(1.5)=0.018519
JUNAID ABBASI (11ES16)
58
Lab # 34
Program:
//Classical Runge-Kutta Method or RK4
//dy/dx = x+y ; y(0)=1; y(0.5)=? Take h=0.1
#include<constream.h>
#include<math.h>
#define f(x,y) (x+y)
void main()
{
clrscr();
float x0,y0,x,y,h,k1,k2,k3, k4, k;
int n,i;
cout<<"\n Enter the value of xo,yo = "; cin>>x0>>y0;
cout<<"\n Enter the value of x = "; cin>>x;
cout<<"\n Enter the value of h = "; cin>>h;
n=(x-x0)/h;
for(i=1;i<=n+1;i++)
{
k1=f(x0,y0);
k2=f(x0+0.5*h,y0+0.5*h*k1);
k3= f(x0+0.5*h,y0+0.5*h*k2); k4 =f(x0+h,y0+h*k3);
k=(k1+2*k2+2*k3+k4)/6;
y=y0+h*k;
x0+=h;
y0=y;
cout<<"\n\n y("<<x0<<")="<<y;
}
getch();
}
RESULT:Enter the value of xo,yo = 0 1
Enter the value of x = 0.5
Enter the value of h =
0.1
y(0.1)=1.110342
y(0.2)=1.242805
y(0.3)=1.399717
y(0.4)=1.583648
y(0.5)=1.797441
JUNAID ABBASI (11ES16)
59
Lab # 35
Program:
// Milne's Predictor Corrector method
// ODE y' = x^2-4*y ; y(1)=1.2134, y(1.5)=2.864, y(2)=3.0907, y(2.5)=5.0032
# include <iostream.h>
# include <conio.h>
# include <math.h>
# define f(x,y) (pow(x,2)-4*y)
void main ()
{ int i;
float h,c,x0,x1,x2,x3,x4,y0,y1,y2,y3,yp,yc,f0,f1,f2,f3,error;
clrscr();
cout<<"\n enter first four values of x = "<<endl;
cin>>x0>>x1>>x2>>x3;
cout<<"\n enter the corresponding values of y = "<<endl;
cin>>y0>>y1>>y2>>y3;
h=x1-x0;
cout<<endl;
cout<<"Milne's predictor Corrector Method"<<endl;
cout<<"Solution of ODE y'=x^3-3*y"<<endl;
cout<<"\tx\t"<<"y\t"<<"\terror\t"<<endl;
for (i=1;i<10;i++)
{ f1=f(x1,y1); f2=f(x2,y2); f3=f(x3,y3);
yp=y0+4*h*(2*f1-f2+2*f3)/3;
x4=x3+h;
c=y2+h*(f2+4*f3)/3;
line:
yc=c+h*f(x4,yp)/3;
error=fabs(yc-yp);
if (error<pow(10,-5))
cout<<"\t"<<x4<<"\t"<<yc<<"\t"<<error<<endl;
else
{yp=yc;
goto line;}
x1=x2;x2=x3;x3=x4;y0=y1;y1=y2;y2=y3;y3=yc;}
getch();
}
JUNAID ABBASI (11ES16)
60
RESULT:enter first four values of x =
1 1.5 2 2.5
enter the corresponding values of y =
1.2134 2.864 3.0907 5.0032
Milne's predictor Corrector Method
Solution of ODE y'=x^2-4*y
x
y
error
3
-3.586977
6.914139e-06
3.5 12.189807
9.536743e-06
4
-12.821083 9.536743e-06
4.5 32.601692
7.629395e-06
5
-42.526928 7.629395e-06
5.5 89.613426
7.629395e-06
JUNAID ABBASI (11ES16)
61
Lab # 36
Program:
// Adam's Predictor Corrector method
// ODE y' = 3x+y/2 ; y(0)=1, y(0.2)=1.1676, y(0.4)=1.4782, y(0.6)=1.9487
# include <iostream.h>
# include <conio.h>
# include <math.h>
# define f(x,y) (3*x+y/2)
void main ()
{ int i;
float h,c,x0,x1,x2,x3,x4,y0,y1,y2,y3,yp,yc,f0,f1,f2,f3,error;
clrscr();
cout<<"\n enter first four values of x = "<<endl;
cin>>x0>>x1>>x2>>x3;
cout<<"\n enter the corresponding values of y = "<<endl;
cin>>y0>>y1>>y2>>y3;
h=x1-x0;
cout<<endl;
cout<<"Adam's predictor Corrector Method"<<endl;
cout<<"Solution of ODE y'=3*x+y/2"<<endl;
cout<<"\tx\t"<<"y\t"<<"\terror\t"<<endl;
for (i=1;i<8;i++)
{
f1=f(x1,y1);
f2=f(x2,y2);
f3=f(x3,y3);
yp=y3+h*(55*f3-59*f2+37*f1-9*f0)/24;
x4=x3+h;
c=y3+h*(19*f3-5*f2+f1)/24;
line:
yc=c+9*h*f(x4,yp)/24;
error=fabs(yc-yp);
if (error<pow(10,-5))
cout<<"\t"<<x4<<"\t"<<yc<<"\t"<<error<<endl;
else
{yp=yc;
goto line;}
x0=x1;x1=x2;x2=x3;x3=x4;y0=y1;y1=y2;y2=y3;y3=yc;}
getch();
}
JUNAID ABBASI (11ES16)
62
RESULT:enter first four values of x =
0 0.2 0.4 0.6
enter the corresponding values of y =
1 1.1676 1.4782 1.9487
Adam's predictor Corrector Method
Solution of ODE y'=3*x+y/2
x
y
error
0.8 2.594329
1.907349e-06
1
3.434052
4.529953e-06
1.2 4.488297
7.152557e-06
1.4 5.779623
4.768372e-07
1.6 7.332965
4.768372e-07
1.8 9.17588 0
2
11.338822
9.536743e-07
JUNAID ABBASI (11ES16)
63
© Copyright 2026 Paperzz