NUMERICAL METHODS LAB (PRACTICAL)
[Link] OF TRANSCENDENTAL AND LINEAR EQUATION
[Link] METHOD
[Link] a programme in C to find and print the smallest positive real root of the equation x-
tan(x)=0 correct upto 2 decimal places using bisection method and improve it correct upto
12 decimal places by fixed point iteration method.
Programme:
#include<stdio.h>
#include<conio.h>
#include<math.h>
double f(double x);
double fi(double x);
void main()
{
int i,j;
double a,b,c,x0,x1,t,s;
clrscr();
do
{
printf("Initial interval:\n");
scanf("%lf%lf",&a,&b);
c=f(a)*f(b);
}
while(c>=0.0);
printf("list of approximation using bisection method\n");
i=0;
do
{
x0=(a+b)/2;
i++;
printf("x[%d]=%6.2lf\n",i,x0);
t=fabs(a-b);
if(f(a)*f(x0)<0)
b=x0;
else
a=x0;
}
while(t>=0.005);
printf("the root correct upto 2 decimal places=%6.2lf\n",x0);
printf("list of approximation using FPI method\n");
j=0;
do
{
x1=fi(x0);
j++;
printf("x[%d]=%16.12lf\n",j,x1);
s=fabs(x1-x0);
x0=x1;
}
while(s>=0.0000000000005);
printf("the root correct upto 12 decimal places is=%16.12lf\n",x1);
getch();
}
double f(double x)
{
return(x-sin(x)/cos(x));
}
double fi(double x)
{
return((19*x-sin(x)/cos(x))/18);
}
INPUT SECTION:
Initial interval:
23
Initial interval:
45
Initial interval:
4.2 4.5
OUTPUT SECTION:
list of approximation using bisection method
x[1]= 4.35
x[2]= 4.42
x[3]= 4.46
x[4]= 4.48
x[5]= 4.49
x[6]= 4.50
x[7]= 4.49
the root correct upto 2 decimal places= 4.49
list of approximation using FPI method
x[1]= 4.493462069846
x[2]= 4.493403040014
x[3]= 4.493410238795
x[4]= 4.493409362866
x[5]= 4.493409469476
x[6]= 4.493409456501
x[7]= 4.493409458080
x[8]= 4.493409457888
x[9]= 4.493409457912
x[10]= 4.493409457909
x[11]= 4.493409457909
the root correct upto 12 decimal places is= 4.493409457909
[Link] a programme in C to find and print the smallest positive real root of the equation
x-tan(x)=0 correct upto 2 decimal places using bisection method and improve it correct upto
12 decimal places by Newton-Raphson method.
Programme:
#include<stdio.h>
#include<conio.h>
#include<math.h>
double f(double x);
double df(double x);
void main()
{
int i,j;
double a,b,x0,t,s,x1,c;
clrscr();
do
{
printf("initial interval\n");
scanf("%lf%lf",&a,&b);
c=f(a)*f(b);
}
while(c>=0.0);
printf("list of approximation using bisection method:\n");
i=0;
do
{
x0=(a+b)/2;
i++;
printf("x[%d]=%6.2lf\n",i,x0);
t=fabs(a-b);
if(f(a)*f(x0)<0)
b=x0;
else
a=x0;
}
while(t>0.005);
printf("the root is correct upto 2 decimal =%6.2lf\n",x0);
printf("list of approximation using N-R method:\n");
j=0;
do
{
x1=x0-f(x0)/df(x0);
j++;
printf("x[%d]=%16.12lf\n",j,x1);
s=fabs(x1-x0);
x0=x1;
}
while(s>0.0000000000005);
printf("the root is correct upto 12 decimal =%16.12lf\n",x1);
getch();
}
double f(double x)
{
return(x-sin(x)/cos(x));
}
double df(double x)
{
return(1-1/(cos(x)*cos(x)));
}
INPUT SECTION:
initial interval
23
initial interval
45
initial interval
4.2 4.5
OUTPUT SECTION:
list of approximation using bisection method:
x[1]= 4.35
x[2]= 4.42
x[3]= 4.46
x[4]= 4.48
x[5]= 4.49
x[6]= 4.50
x[7]= 4.49
the root is correct upto 2 decimal = 4.49
list of approximation using N-R method:
x[1]= 4.493410373979
x[2]= 4.493409457913
x[3]= 4.493409457909
x[4]= 4.493409457909
the root is correct upto 12 decimal = 4.493409457909
[Link] a programme in C to find and print the smallest positive real root of the equation x-
tan(x)=0 correct upto 12 decimal places using Bisection Method.
Programme:
#include<stdio.h>
#include<conio.h>
#include<math.h>
double f(double x);
void main()
{
int i;
double a,b,x0,t,c;
clrscr();
do
{
printf("initial interval\n");
scanf("%lf%lf",&a,&b);
c=f(a)*f(b);
}
while(c>=0.0);
i=0;
do
{
x0=(a+b)/2;
i++;
printf("x[%d]=%16.12lf\n",i,x0);
t=fabs(a-b);
if(f(a)*f(x0)<0)
b=x0;
else
a=x0;
}
while(t>0.0000000000005);
printf("the root is correct upto 12 decimal places is =%16.12lf\n",x0);
getch();
}
double f(double x)
{
return(x-sin(x)/cos(x));
}
INPUT SECTION:
initial interval
23
initial interval
45
initial interval
4.2 4.5
OUTPUT SECTION:
x[1]= 4.350000000000
x[2]= 4.425000000000
x[3]= 4.462500000000
x[4]= 4.481250000000
x[5]= 4.490625000000
x[6]= 4.495312500000
x[7]= 4.492968750000
x[8]= 4.494140625000
x[9]= 4.493554687500
x[10]= 4.493261718750
x[11]= 4.493408203125
x[12]= 4.493481445312
x[13]= 4.493444824219
x[14]= 4.493426513672
x[15]= 4.493417358398
x[16]= 4.493412780762
x[17]= 4.493410491943
x[18]= 4.493409347534
x[19]= 4.493409919739
x[20]= 4.493409633636
x[21]= 4.493409490585
x[22]= 4.493409419060
x[23]= 4.493409454823
x[24]= 4.493409472704
x[25]= 4.493409463763
x[26]= 4.493409459293
x[27]= 4.493409457058
x[28]= 4.493409458175
x[29]= 4.493409457617
x[30]= 4.493409457896
x[31]= 4.493409458036
x[32]= 4.493409457966
x[33]= 4.493409457931
x[34]= 4.493409457913
x[35]= 4.493409457905
x[36]= 4.493409457909
x[37]= 4.493409457911
x[38]= 4.493409457910
x[39]= 4.493409457910
x[40]= 4.493409457909
x[41]= 4.493409457909
the root is correct upto 12 decimal places is= 4.493409457909
[Link] RAPHSON METHOD (for simple roots)
[Link] a programme in C to find and print the smallest positive real root of the equation
x-tan(x)=0 correct upto 12 decimal places using Newton-Raphson method.
Programme:
#include<stdio.h>
#include<conio.h>
#include<math.h>
double f(double x);
double df(double x);
void main()
{
int i;
double x0,x1,t,a,b,c;
clrscr();
do
{
printf("initial interval\n");
scanf("%lf%lf",&a,&b);
c=f(a)*f(b);
}
while(c>=0.0);
i=0;
x0=(a+b)/2;
do
{
x1=x0-f(x0)/df(x0);
i++;
printf("x[%d]=%16.12lf\n",i,x1);
t=fabs(x1-x0);
x0=x1;
}
while(t>=0.00000000005);
printf("Hoe!the root correct upto 12 decimal places is=%16.12lf",x1);
getch();
}
double f(double x)
{
return(x-sin(x)/cos(x));
}
double df(double x)
{
return(1-1/(cos(x)*cos(x)));
}
INPUT SECTION:
initial interval
23
initial interval
45
initial interval
4.2 4.5
OUTPUT SECTION:
x[1]= 4.596143417627
x[2]= 4.542043051711
x[3]= 4.504422146538
x[4]= 4.493979588692
x[5]= 4.493410990568
x[6]= 4.493409457920
x[7]= 4.493409457909
Hoe!the root correct upto 12 decimal places is= 4.493409457909
[Link] METHOD
[Link] a program in c to compute a real root of the equation x2 – 4x -10 =0 using Secant
method correct upto six decimal places.
Program:
/*SECANT METHOD*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define ESP 0.0001
#define F(x) (x) * (x) - 4*(x) -10
void main()
{
float x1,x2,x3,f1,f2,t;
clrscr();
printf("\nEnter the value of x1: ");
scanf("%f",&x1);
printf("\nEnter the value of x2: ");
scanf("%f",&x2);
printf("\n____________________\n");
printf("\n x1\t x2\t x3\t ");
printf("\n____________________\n");
do
{
f1=F(x1);
f2=F(x2);
x3=x2-((f2*(x2-x1))/(f2-f1));
printf("\n%f %f %f %f %f",x1,x2,x3,f1,f2);
x1=x2;
x2=x3;
if (f2<0)
t=fabs(f2);
else
t=f2;
}while(t>ESP);
printf("\n_____________________\n");
printf("\n\[Link] =%f",x3);
getch();
}
OUTPUT
Enter the value of x1: 4
Enter the value of x2: 7
____________________
x1 x2 x3
____________________
4.000000 7.000000 5.428571 -10.000000 11.000000
7.000000 5.428571 5.694915 11.000000 -2.244899
5.428571 5.694915 5.743712 -2.244899 -0.347601
5.694915 5.743712 5.741644 -0.347601 0.015379
5.743712 5.741644 5.741657 0.015379 -0.000097
____________________
[Link] =5.741657
[Link] FALSI METHOD
[Link] a programme in C to find and print the smallest positive real root of the equation x-
tan(x)=0 correct upto 12 decimal places by the method of false position.
Programme:
#include<stdio.h>
#include<conio.h>
#include<math.h>
double f(double x);
void main()
{
int i;
double a,b,x0,t,c;
clrscr();
do
{
printf("initial interval\n");
scanf("%lf%lf",&a,&b);
c=f(a)*f(b);
}
while(c>=0.0);
i=0;
do
{
x0=a-(f(a)*(b-a))/(f(b)-f(a));
i++;
printf("x[%d]=%16.12lf\n",i,x0);
t=fabs(a-b);
if(f(a)*f(x0)<0)
b=x0;
else
a=x0;
}
while(t>0.0000000000005);
printf("the root is correct upto 12 decimal places is=%16.12lf\n",x0);
getch();
}
double f(double x)
{
return(x-sin(x)/cos(x));
}
INPUT SECTION:
initial interval
23
initial interval
45
initial interval
4.2 4.5
OUTPUT SECTION:
x[1]= 4.483903584745
x[2]= 4.493113893375
x[3]= 4.493400280137
x[4]= 4.493409172936
x[5]= 4.493409449061
x[6]= 4.493409457634
x[7]= 4.493409457901
x[8]= 4.493409457909
x[9]= 4.493409457909
x[10]= 4.493409457909
x[11]= 4.493409457909
the root is correct upto 12 decimal places is= 4.493409457909
[Link] OF SYSTEM OF LINEAR EQUATIONS
[Link] ELIMINATION METHOD
[Link] a programme in C to find and print an approximate solution of the system of linear
equations
4.21x1+22.67x2+3.85x3=30.24
2.31x1+31.49x2+1.52x3=40.60
3.49x1+4.85x2+28.97x3=42.81
Correct upto six decimal places , by Gauss Elimination method.
Programme:
#include<stdio.h>
#include<math.h>
void main()
{
int n,i,j,k;
double a[10][10],b[10][10],s,t[10],x[10],m[10][10];
printf("Enter the number of equations and number of unknowns:\n");
scanf("%d",&n);
printf("\n Enter the augmented matrix:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n+1;j++)
{
scanf("%lf",&a[i][j]);
b[i][j]=a[i][j];
}
for(i=1;i<=n-1;i++)
{
for(k=i+1;k<=n;k++)
{
m[k][i]=a[k][i]/a[i][i];
for(j=1;j<=n+1;j++)
a[k][j]=a[k][j]-m[k][i]*a[i][j];
}
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n+1;j++)
{
printf("%lf",a[i][j]);
}
printf("\n");
}
for(j=n;j>=1;j--)
{
s=0.0;
for(k=n;k>=j+1;k--)
{
s=s+a[j][k]*x[k];
}
x[j]=(a[j][n+1]-s)/a[j][j];
}
for(i=1;i<=n;i++)
{
s=0.0;
for(j=1;j<=n;j++)
{
s=s+b[i][j]*x[j];
t[i]=fabs(s-b[i][n+1]);
}
}
s=0.0;
for(i=1;i<=n;i++)
s=s+t[i];
if(s<0.005)
{
printf("Its Okey");
for(i=1;i<=n;i++)
printf("\n x(%d)=%lf",i,x[i]);
}
else
printf("Something Wrong");
}
INPUT SECTION:
Enter the number of equations and number of unknowns:
3
Enter the augmented matrix:
4.21 22.67 3.85 30.24
2.31 31.49 1.52 40.60
3.49 4.85 28.97 42.81
OUTPUT SECTION:
4.210000 22.670000 3.850000 30.240000
0.000000 19.051116 -0.592470 24.007506
0.000000 0.000000 25.344821 35.312066
Its Okey
x(1)=-1.110269
x(2)=1.303492
x(3)=1.393266
[Link] SEIDEL METHOD
[Link] a programme in C to find and print an approximate solution of the system of linear
equations
4.50x1+0.15x2+0.30x3=1.57
0.15x1-10.50x2+0.45x3=-3.86
0.45x1+0.30x2-15.00x3=14.28
Correct upto six decimal places , by Gauss Seidel method.
Programme:
#include<stdio.h>
#include<math.h>
void main()
{
int n,i,j;
double a[5][5],x[10],t[10],e,s,l[10];
printf("Enter the number of unknowns:\n");
scanf("%d",&n);
printf("Enter the diagonally dominant augmented matrix row-wise:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n+1;j++)
scanf("%lf",&a[i][j]);
printf("\n Enter the initial approximation:\n");
for(i=1;i<=n;i++)
scanf("%lf",&x[i]);
do
{
e=0.0;
for(i=1;i<=n;i++)
{
s=0.0;
for(j=1;j<=n;j++)
s=s+a[i][j]*x[j];
t[i]=(a[i][n+1]-s+a[i][i]*x[i])/a[i][i];
l[i]=fabs(t[i]-x[i]);
e=e+l[i];
x[i]=t[i];
}
}
while(e>=0.00005);
for(i=1;i<=n;i++)
printf("x[%d]=%lf\n",i,x[i]);
}
INPUT SECTION:
Enter the number of unknowns:
3
Enter the diagonally dominant augmented matrix row-wise:
4.50 0.15 0.30 1.57
0.15 -10.50 0.45 -3.86
0.45 0.30 -15.00 14.28
Enter the initial approximation:
000
OUTPUT SECTION:
x[1]=0.400000
x[2]=0.333333
x[3]=-0.933333
[Link] JACOBI METHOD
[Link] a programme in C to find and print an approximate solution of the system of linear
equations
4.50x1+0.15x2+0.30x3=1.57
0.15x1-10.50x2+0.45x3=-3.86
0.45x1+0.30x2-15.00x3=14.28
Correct upto six decimal places , by Gauss Jacobi method.
Programme:
#include<stdio.h>
#include<math.h>
void main()
{
int n,i,j,k;
double a[5][5],x[10],t[10],e,s,l[10];
printf("Enter the number of unknowns:\n");
scanf("%d",&n);
printf("Enter the diagonally dominant augmented matrix row-wise:\n");
for(i=1;i<=n;i++)
for(j=1;j<=n+1;j++)
scanf("%lf",&a[i][j]);
printf("\n Enter the initial approximation:\n");
for(i=1;i<=n;i++)
scanf("%lf",&x[i]);
do
{
e=0.0;
for(i=1;i<=n;i++)
{
s=0.0;
for(j=1;j<=n;j++)
s=s+a[i][j]*x[j];
t[i]=(a[i][n+1]-s+a[i][i]*x[i])/a[i][i];
l[i]=fabs(t[i]-x[i]);
e=e+l[i];
}
for(k=1;k<=n;k++)
x[k]=t[k];
}
while(e>=0.00005);
for(i=1;i<=n;i++)
printf("x[%d]=%lf\n",i,x[i]);
}
INPUT SECTION:
Enter the number of unknowns:3
Enter the diagonally dominant augmented matrix row-wise:
4.50 0.15 0.30 1.57
0.15 -10.50 0.45 -3.86
0.45 0.30 -15.00 14.28
Enter the initial approximation:
000
OUTPUT SECTION:
x[1]=0.400000
x[2]=0.333333
x[3]=-0.933333
3. INTERPOLATION
[Link] INTERPOLATION
#include<stdio.h>
#include<conio.h>
#include<math.h>
main()
{
float x[100],y[100],a,s=1,t=1,k=0;
int n,i,j,d=1;
printf("\n\n Enter the number of the terms of the table: ");
scanf("%d",&n);
printf("\n\n Enter the respective values of the variables x and y: \n");
for(i=0; i<n; i++)
{
scanf ("%f",&x[i]);
scanf("%f",&y[i]);
}
printf("\n\n The table you entered is as follows :\n\n");
for(i=0; i<n; i++)
{
printf("%0.3f\t%0.3f",x[i],y[i]);
printf("\n");
}
while(d==1)
{
printf(" \n\n\n Enter the value of the x to find the respective value of y\n\n\n");
scanf("%f",&a);
for(i=0; i<n; i++)
{
s=1;
t=1;
for(j=0; j<n; j++)
{
if(j!=i)
{
s=s*(a-x[j]);
t=t*(x[i]-x[j]);
}
}
k=k+((s/t)*y[i]);
}
printf("\n\n The respective value of the variable y is: %f",k);
printf("\n\n Do you want to continue?\n\n Press 1 to continue and any other key to exit");
scanf("%d",&d);
}
}
[Link] FORWARD INTERPOLATION
[Link] the following table, compute the value of f(51.3) , by Newton’s forward interpolating
formula.
51.0 1.665291195
52.0 1.682027650
53.0 1.698932309
54.0 1.716006862
55.0 1.733253018
56.0 1.750672500
57.0 1.768267051
Programme:
#include<stdio.h>
#include<math.h>
void main()
{
int i,n,j;
double x[10],t[10][10],h,u,tl,s,xl;
printf("Enter the number of intervals:\n");
scanf("%d",&n);
printf("Give xi's=\n");
for(i=1;i<=n+1;i++)
scanf("%lf",&x[i]);
printf("Give yi's=\n");
for(i=1;i<=n+1;i++)
scanf("%lf",&t[i][1]);
printf("Give the interpolating point=\n");
scanf("%lf",&xl);
h=x[2]-x[1];
u=(xl-x[1])/h;
for(j=2;j<=n;j++)
{
for(i=j;i<=n+1;i++)
t[i][j]=t[i][j-1]-t[i-1][j-1];
}
for(i=1;i<=n+1;i++)
{
for(j=1;j<=i;j++)
printf("%lf ",t[i][j]);
printf("\n");
}
s=t[1][1];
tl=1;
for(i=2;i<=n+1;i++)
{
tl=tl*(u-i+2)/(i-1);
s=s+tl*t[i][i];
}
printf("The required value of the function at xl =%lf",s);
}
INPUT SECTION:
Enter the number of intervals:
6
Give xi's=
51.0
52.0
53.0
54.0
55.0
56.0
57.0
Give yi's=
1.665291195
1.682027650
1.698932309
1.716006862
1.733253018
1.750672500
1.768267051
Give the interpolating point=
51.3
OUTPUT SECTION:
1.665291
1.682028 0.016736
1.698932 0.016905 0.000168
1.716007 0.017075 0.000170 0.000002
1.733253 0.017246 0.000172 0.000002 0.000000
1.750673 0.017419 0.000173 0.000002 0.000000 0.000000
1.768267 0.017595 0.000175 0.000002 0.000000 0.000000 0.000000
The required value of the function at xl =1.670295
[Link] BACKWARD INTERPOLATION
[Link] a programme in C to find a value of a function at a point given by user by
NEWTON’S BACKWARD INTERPOLATION FORMULA whose values at some equispaced
points are given.
Programme:
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int i,j,n;
double u1,h,x1,x[10],u[10][10],s,t;
printf("Enter the number of argument :\n");
scanf("%d",&n);
printf("\nEnter the arguments:\n");
for(i=1;i<=n;i++)
scanf("%lf",&x[i]);
printf("\nEnter the entries :\n");
for(i=1;i<=n;i++)
scanf("%lf",&u[i][1]);
printf("\nEnter the value x :\n");
scanf("%lf",&x1);
h=x[2]-x[1];
u1=(x1-x[n])/h;
for(j=2;j<=n;j++)
{
for(i=1;i<=n;i++)
u[i][j]=u[i][j-1]-u[i-1][j-1];
}
printf("\n\nDIFFERENCE TABLE\n\n");
for(i=1;i<=n;i++)
{
printf("%lf ",x[i]);
for(j=1;j<=i;j++)
printf("%lf ",u[i][j]);
printf("\n\n");
}
s=u[n][1];
t=1;
for(i=2;i<=n;i++)
{
t=t*(u1+i-2)/(i-1);
s=s+t*u[n][i];
}
printf("\nThe required value of the function at %lf is = %8.6lf",x1,s);
}
INPUT SECTION:
Enter the number of argument :
5
Enter the arguments:
.00
.10
.20
.30
.40
Enter the entries :
1.0000
1.2214
1.4918
1.8221
2.2255
Enter the value x :
.37
OUTPUT SECTION:
DIFFERENCE TABLE
0.000000 1.000000
0.100000 1.221400 0.221400
0.200000 1.491800 0.270400 0.049000
0.300000 1.822100 0.330300 0.059900 0.010900
0.400000 2.225500 0.403400 0.073100 0.013200 0.002300
The required value of the function at 0.37 is = 2.095927
[Link] INTEGRATION
[Link] RULE
[Link] integration using trapezoidal rule.
/* Trapezoidal Rule */
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return ( cos(x)) ;
}
void main()
{
float a,b,h,x,y,y0,yn,s,r;
int i,n;
float f(float x);
clrscr();
printf("\n\nEnter the lower limit : ");
scanf("%f",&a);
printf("\n\nEnter the upper limit : ");
scanf("%f",&b);
printf("Enter the number of intervals: ");
scanf("%d",&n);
h=(b-a)/n;
y0=f(a);
yn=f(b);
x=a+h;
s=0;
for(i=1;i<=n-1;i=i+1)
{
y=f(x);
s=s+y;
x=x+h;
}
r=(h/2)*(y0+yn+ 2*s);
printf("\n\n the result is : %f", r);
getch();
getch();
}
[Link]'S ONE THIRD RULE
[Link] a C program to Compute the integral0π21-0.162 sin2 φ dφ by Simpson’s one-third
rule correct upto six decimal places.
Program:
/*SIMPSONS one third */
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return (sqrt(1-0.162* sin(x)*sin(x))) ;
}
void main()
{
float a,b,h,x,y,y0,yn,xn,s0,se,r;
int i,n;
float f(float x);
clrscr();
printf("\n\nEnter the lower limit : ");
scanf("%f",&a);
printf("\n\nEnter the upper limit : ");
scanf("%f",&b);
printf("Enter the number of intervals: ");
scanf("%d",&n);
h=(b-a)/n;
y0=f(a);
yn=f(b);
x=a+h;
s0=0;
for(i=1;i<=n-1;i=i+2)
{
y=f(x);
s0=s0+y;
x=x+(2*h);
}
se=0;
x=a+(2*h);
for(i=2;i<=(n-2);i=i+2)
{
y=f(x);
se=se+y;
x=x+(2*h);
}
r=(h/3)*(y0+yn+(4*s0+(2*se)));
printf("\n\n the result is : %f", r);
getch();
getch();
}
OUTPUT:
Enter the lower limit : 0
Enter the upper limit : 1.570796
Enter the number of intervals: 12
the result is : 1.505103
[Link]'S RULE
Q Numerical integration using Weddle's rule.
/*PROGRAM FOR WEDDLE'S RULE*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float);
float f(float x)
{
float y;
y=1/(1+x*x);
return(y); }
void main() {
float a,b,h,s1=0,s2=0,s=0;
int i,n,m;
clrscr();
printf("Enter the value of upper limit= ");
scanf("%f",&b);
printf("Enter the value of lower limit= ");
scanf("%f",&a);
printf("Enter the value of n=");
scanf("%d",&n);
h=(b-a)/n;
printf("h= %f",h);
m=n/6;
s=0;
if(n%6==0) {
for(i=1;i<=m;i++)
{ s=s+((3*h/10)*(f(a)+f(a+2*h)+5*f(a+h)+6*f(a+3*h)+f(a+4*h)+5*f(a+5*h)+f(a+6*h)));
a=a+6*h; }
printf("\nResult is : %f",s);}
else {
printf(" Weddle’s rule is not applicable");}
getch();
}
OUTPUT:-
Enter the value of upper limit= 6
Enter the value of lower limit= 0
Enter the value of n=6
h= 1.000000
Result is : 1.373448
[Link] QUADRATURE
[Link] a programme in C to find and print the value of the integral
0.11.0(x)(x)dx
by the Gauss –Quadrature Method.
Programme:
#include<stdio.h>
#include<math.h>
double f(double x);
void main()
{
int i,n;
double a,b,t[10],w[10],s,x[10],p[10],G;
printf("Give lower and upper limit:\n");
scanf("%lf %lf",&a,&b);
printf("Enter number of intervals:\n");
scanf("%d",&n);
printf("Enter the values of ti's:\n");
for(i=1;i<=n;i++)
scanf("%lf",&t[i]);
printf("Enter the values of wi's:\n");
for(i=1;i<=n;i++)
scanf("%lf",&w[i]);
s=0.0;
for(i=1;i<=n;i++)
{
x[i]=((b-a)*t[i]+(b+a))/2;
p[i]=w[i]*f(x[i]);
s=s+p[i];
}
G=(b-a)*s/2;
printf("The value of the integral by Gauss Quadrature form=%16.12lf",G);
}
double f(double x)
{
return(pow(x,x));
}
INPUT SECTION:
Give lower and upper limit:
.1 1
Enter number of intervals:
4
Enter the values of ti's:
0.33998104 -0.33998104 0.86113631 -0.86113631
Enter the values of wi's:
0.65214515 0.65214515 0.34785485 0.34785485
OUTPUT SECTION:
The value of the integral by Gauss Quadrature form= 0.696293095403
[Link] OF ORDINARY DIFFERENTIAL EQUATIONS
[Link]'S METHOD
[Link] a program in C to compute the differential equation
dydx=xy, y=1 when x=0 for x=1 by taking h=0.2 using EULER’S Method.
Program:
/* EULER'S METHOD*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main ()
{ int i;
float x,y,h,n,xf;
float f(float,float);
clrscr();
printf("\nEnter initial x :");
scanf("%f",&x);
printf("\nEnter initial f(x) :");
scanf("%f",&y);
printf("\nEnter h :");
scanf("%f",&h);
printf("\nEnter value for which you haveto find :");
scanf("%f",&xf);
n=(xf-x)/h;
for(i=1;i<=(int)n;i++)
{
y=y+(h*f(x,y));
x=x+h;
}
printf("\n\n\nResult is : %f",y);
getch();
}
float f(float x,float y)
{
return (x*y);
}
OUTPUT
Enter initial x :0
Enter initial f(x) :1
Enter h :0.2
Enter value for which you haveto find :1
Result is : 1.459261
[Link] EULER METHOD
Q Write a programme in C to find and print the solution of the ODE
dydx=x+y at x=0.1 with the initial condition y(0)=1 by Modified Euler Method,taking step
length h=0.05.
Programme:
#include<stdio.h>
#include<math.h>
double f(double x,double y);
void main()
{
int i,n;
double x0,y0,x1,y1,h,yn1,x,t;
printf("Enter the step length=");
scanf("%lf",&h);
printf("Enter the value of x0=");
scanf("%lf",&x0);
printf("Enter the value of y0=");
scanf("%lf",&y0);
printf("Enter the value at which the final value is computed=");
scanf("%lf",&x);
n=(x-x0)/h;
for(i=1;i<=n;i++)
{
y1=y0+h*f(x0,y0);
x1=x0+h;
do
{
yn1=y0+h*(f(x0,y0)+f(x1,y1))/2;
t=fabs(yn1-y1);
y1=yn1;
}
while(t>=0.0000005);
y0=yn1;
x0=x1;
}
printf("\n y[%lf]=%lf\n",x0,y0);
}
double f(double x,double y)
{
return(x+y);
}
INPUT SECTION:
Enter the step length=0.05
Enter the value of x0=0
Enter the value of y0=1
Enter the value at which the final value is computed=0.1
OUTPUT SECTION:
y[0.100000]=1.110388
[Link] KUTTA METHOD (4th order only)
[Link] a programme in C to find y at x=0.6, by Runge-Kutta method,from the differential
equation dy/dx=
(0.5-x+y2)/(1+y+x2) with y(0)=0,taking h=0.1.
Programme:
#include<stdio.h>
#include<math.h>
double f(double x,double y);
void main()
{
int i,n;
double x0,y0,a,h,x1,y1,k1,k2,k3,k4;
printf("Enter the value of h=\n");
scanf("%lf",&h);
printf("\nEnter the value of x0=\n");
scanf("%lf",&x0);
printf("\nEnter the value of y0=\n");
scanf("%lf",&y0);
printf("\nEnter the final value at which the value is computed=\n");
scanf("%lf",&a);
n=(a-x0)/h;
for(i=1;i<=n+1;i++)
{
k1=h*f(x0,y0);
k2=h*f(x0+h/2,y0+k1/2);
k3=h*f(x0+h/2,y0+k2/2);
k4=h*f(x0+h,y0+k3);
y1=y0+(k1+2*k2+2*k3+k4)/6;
x1=x0+h;
printf("x1=%lf\n y1=%lf\n",x1,y1);
x0=x1;
y0=y1;
}
}
double f(double x,double y)
{
return((0.5-x+y*y)/(1+y+x*x));
}
INPUT SECTION:
Enter the value of h=
0.1
Enter the value of x0=
0
Enter the value of y0=
0
Enter the final value at which the value is computed=
0.6
OUTPUT SECTION:
x1=0.100000
y1=0.043966
x1=0.200000
y1=0.076643
x1=0.300000
y1=0.099082
x1=0.400000
y1=0.112251
x1=0.500000
y1=0.117099
x1=0.600000
y1=0.114575