Professional Documents
Culture Documents
Nacp Programmes
Nacp Programmes
2012
TABLE OF CONTENTS
TABLE OF CONTENTS .............................................................................................................................................. 1
PROGRAM TO CALCULATE SUM AND AVERAGE OF 100 NATURAL NUMBERS ........................................................ 2
PROGRAM TO FIND PRODUCT OF 1ST 'N' NATURAL NUMBERS .............................................................................. 3
FIBBONACCI SERIES ................................................................................................................................................ 4
GENERATING PRIME NO. BETWEEN 1 AND 100 ...................................................................................................... 5
GENERATE PRIME NOS. BETWEEN 1 AND 100 EXCEPT THOSE DIVISIBLE BY 5 ........................................................ 6
CALCULATION OF SINE SERIES ................................................................................................................................ 8
CALCULATION OF COSINE SERIES ........................................................................................................................... 9
PROGRAM TO FIND OUT AVERAGE MARKS OF STUDENTS ................................................................................... 10
STANDARD DEVIATION AND VARIATION .............................................................................................................. 12
ADDTION OF MATRIX ............................................................................................................................................ 13
MULTIPLICATION OF MATRIX ............................................................................................................................... 15
BISECTION METHOD ............................................................................................................................................. 17
NEWTON RAPHSON .............................................................................................................................................. 19
REGULA-FALSI METHOD ....................................................................................................................................... 21
NEWTON GREGORY FORWARD INTERPOLATION ................................................................................................. 23
NEWTON GREGORY BACKWARD INTERPOLATION ............................................................................................... 25
LAGRANGE METHOD OF INTERPOLATION ............................................................................................................ 27
NEWTON DIVIDED DIFFERENCE METHOD ............................................................................................................ 29
TRAPEZOIDAL RULE .............................................................................................................................................. 32
SIMPSON 1/3 RULE ............................................................................................................................................... 34
SIMPSON 3/8 RULE ............................................................................................................................................... 36
WEEDEL'S RULE .................................................................................................................................................... 38
GAUSS ELIMINATION METHOD ............................................................................................................................ 40
GAUSS JORDAN METHOD ..................................................................................................................................... 42
GAUSS SIEDAL METHOD ....................................................................................................................................... 44
CURVE FITTING - STRAIGHT LINE........................................................................................................................... 46
RUNGA-KUTA METHOD ........................................................................................................................................ 48
FUNCTION SUB PROGRAM ................................................................................................................................... 49
PROGRAM TO CALCULATE VALUE OF GIVEN FUNCTIONEXP(X*X*X*SIN(2*X)FROM 0.0 TO 1.2 USING SIMPSONS 1/3 AND
3/8 RULES RESPECTIVELY ...................................................................................................................................... 51
PARABOLIC FIT BY LEAST SQUARES ....................................................................................................................... 53
CROUTS METHOD ................................................................................................................................................ 58
MILNE'S PREDICTOR CORRECTOR ......................................................................................................................... 64
NUMERICAL DOUBLE INTEGRATION 67
ROMBERG INTEGRAL ..70
FIBBONACCI SERIES
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int i;
double x1,x2,x;
x1=1;
x2=1;
printf("%12.0f\t%12.0f",x1,x2);
for(i=3;i<=25;i++)
{
x=x1+x2;
x1=x2;
x2=x;
printf("%12.0f",x);
}
getch();
}
OUTPUT:
1 1 2 3 5 8 13 21
flag=0;
break;
}
}
if (flag==1)
{
printf ("\t %d",temp);
}
}
OUTPUT:
2 3 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
roll=i;
}
}
printf("\n The topper of the class is student %d with average
=%f",roll,max);
getch();
}
OUTPUT:
Enter the number of students =3
Enter the number of subjects =5
Enter marks for 1 student
Subject 1=69
Subject 2=58
Subject 3=45
Subject 4=10
Subject 5=35
Enter marks for 2 student
Subject 1=47
Subject 2=25
Subject 3=16
Subject 4=97
Subject 5=46
Enter marks for 3 student
Subject 1=30
Subject 2=90
Subject 3=76
Subject 4=58
Subject 5=47
The average of the 1 student = 42.400002
The average of the 2 student = 45.200001
The average of the 3 student = 59.200001
The topper of the class is student 3 with average = 59.200001
ADDTION OF MATRIX
#include<stdio.h>
#include<conio.h>
void main()
{
int mat1[10][10],mat2[10][10],mat3[10][10];
int i,j,m,n;
clrscr();
printf ("\n The number of rows are=");
scanf ("%d",&m);
printf ("\n The number of columns are=");
scanf ("%d",&n);
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf ("\n The matrix 1 is [%d][%d]=",i,j);
scanf ("%d",& mat1[i][j]);
}
}
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf ("\n The matrix 2is [%d][%d]=",i,j);
scanf ("%d",& mat2[i][j]);
}
}
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
mat3[i][j]= mat1[i][j]+mat2[i][j];
}
}
for (i=0;i<m;i++)
{
for (j=0;j<n;j++)
{
printf ("\n The resultant matrix is %d\n",mat3[i][j]);
}
printf ("\n");
}
}
OUTPUT:
The number of rows are=2
The number of columns are=2
The matrix 1 is [0][0]= 1
The matrix 1 is [0][1]= 2
The matrix 1 is [1][0]= 3
The matrix 1 is [1][1]= 4
The matrix 2 is [0][0]= 5
The matrix 2 is [0][1]= 6
The matrix 2 is [1][0]= 7
The matrix 2 is [1][1]= 8
The resultant matrix is
6 8
10 12
MULTIPLICATION OF MATRIX
#include<stdio.h>
#include<conio.h>
void main()
{int m,n,p,q,i,j,k;
int a[10][10],b[10][10],c[10][10];
clrscr();
printf("\nenter no. of row and col of matrix a:");
scanf("%d%d",&m,&n);
printf("\nenter no. of row and col of matrix b:");
scanf("%d%d",&p,&q);
if(n!=p)
{ printf("\nmatrix can't be multiplied\n");
goto end;
}
printf("\nenter matrix a\n");
for(i=0;i<m;i++)
{ for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
}
printf("\nenter matrix b\n");
for(i=0;i<p;++i)
{ for(j=0;j<q;j++)
scanf("%d",&b[i][j]);
}
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{ c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
printf("the product ofmatrix is:");
for(i=0;i<m;i++)
{printf("\n");
for(j=0;j<q;j++)
printf("%3d",c[i][j]);
}
end:
getch();
}
output:
enter no. of row and col of matrix a:3 3
enter no. of row and col of matrix b:3 2
enter matrix a
0 1 2
1 2 3
2 3 4
enter matrix b
1 -2
-1 0
2 -1
the product ofmatrix is:
3 -2
5 -5
7 -8
BISECTION METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{ float fun(float m);
float x1,x2,x3,p,q,r;
int i=0;
clrscr();
l10: printf("\nequation:x*(exp(x)-1) ");
printf("\nenter the app value of x1,x2:");
scanf("%f %f",&x1,&x2);
if(fun(x1)*fun(x2)>0)
{ printf("\n wrong values entered...enter again:\n");
goto l10;}
else
printf("\n the root lies b/w %f & %f",x1,x2);
printf("\n n x1 x2 x3 f(x1) f(x2)
f(x3)");
l15: x3=(x1+x2)/2;
p=fun(x1);
q=fun(x2);
r=fun(x3);
i=i++;
printf("\n%d %f %f %f %f %f %f",i,x1,x2,x3,p,q,r);
if((p*r)>0)
x1=x3;
else
x2=x3;
if((fabs((x2-x1)/x2))<=0.001)
{printf("\n root of the equ is %f",x3);
getch();
exit(0);}
NEWTON RAPHSON
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{float f(float a);
float df(float a);
int i=1;
float x0,x1,p,q;
float error =0.0001,delta =0.001;
clrscr();
printf("\n\nThe equation is X^3+1.2X^2-5X-7.2");
printf("\nenter the initial value of x0:");
scanf("%f",&x0);
printf("\n i x0 x1 f(x0) df(x0)\n ");
if (fabs (df(x0))<delta)
{printf("slope is very small and= %f",df(x0));}
else a10: p=f(x0);q=df(x0);
x1=x0-(p/q);
i++;
printf("\n %d\t%f\t%f\t%f\t%f\n",i,x0,x1,p,q);
if(fabs((x1-x0)/x1)<=error)
{printf("\nThe root of equation X^3+1.2X^2-5X-7.2 is
%f",x0);
getch();
exit(0);}
else
{x0=x1;
goto a10;}
}
/* Function sub program */
float f(float a)
{float g;
g = pow(a,3)+((1.2)*(pow(a,2)))-5*a-7.2;
return(g);}
float df(float a)
{float g1;
g1 = (3*pow(a,2))+(2.4*a)-5;
return(g1);
}
/*
output:
The equation is X^3+1.2X^2-5X-7.2
enter the initial value of x0:2
i x0 x1 f(x0) df(x0)
2 2.000000 2.372881 -4.400000 11.800000
3 2.372881 2.313010 1.052938 17.586615
4 2.313010 2.311227 0.029603 16.601265
5 2.311227 2.311225 0.000026 16.572248
The root of equation X^3+1.2X^2-5X-7.2 is 2.311227
REGULA-FALSI METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
float f(float x)
{return(3*x-cos(x)-1);
}
void main()
{ float f(float x);
double x1,x2,m;
int c=0;
clrscr();
printf("\n enter the first approximation :");
scanf("%f",&x1);
printf("\n enter the second approximation :");
scanf("%f",&x2);
if(f(x1)*f(x2)<0.0)
{
m=(x1*f(x2)-x2*f(x1))/(f(x2)-f(x1));
while(fabs(f(m))>=0.0001)
{
c++;
if(f(x1)*f(m)<0.0)
x2=m;
else
x1=m;
m=((x1*f(x2))-(x2*f(x1)))/(f(x2)-f(x1));
printf("\n\n the %d,ilteration is %f ",c,m);
}
printf("\n\n the answer is repeated at %d ilteration
is %f",c,m);
}
else
printf("enter valid initial approximation :");
getch();
}
OUTPUT:
Enter the first approximation : 0
Enter the second approximation : 1
The 1,ilteration is 0.605959
The 2,ilteration is 0.607057
The 3,ilteration is 0.607100
The answer is repeated at 3 ilteration is 0.607100
i--;
p=(x-mx[i])/h;
y1=p*diff[i-1][1];
y2=p*(p+1)*diff[i-1][2]/2;
y3=(p+1)*p*(p-1)*diff[i-2][3]/6;
y4=(p+2)*(p+1)*p*(p-1)*diff[i-3][4]/24;
y=my[i]+y1+y2+y3+y4;
printf("\nwhen x=%6.4f,y=%6.8f",x,y);
getch();
}
OUTPUT:
enter no. of terms:5
enter values x y:
3 13
5 23
11 899
27 17315
34 35606
enter value of x at which y is to be calculated:7
when x=7.0000,y=899.00000000
programme no.24
x0=mx[i];
y0=my[i];
p=(x-x0)/h;
sum=y0;
for(k=1;k<=4;k++)
{
fun=(fun*(p-(k-1)))/k;
sum=sum+fun*diff[i][k];
}
printf("\nwhen x=%6.4f,y=%6.8f",x,sum);
getch();
}
OUTPUT:
enter no. of terms:5
enter values x y:
20 41
40 103
60 168
80 218
100 235
enter value of x at which y is to be calculated:70
when x=70.0000,y=196.00000000
}
printf("\nwhen x=%5.2f then y=%5.2f",x,y);
getch();
}
output:
Enter No. of Points:6
Enter the given set of values:
x y
4 18
5 100
7 294
10 900
11 1210
13 2028
Enter the value of x at which f(x)is required:8
when x= 8.00 then y=445.62
{
for(z=0;z<m-1;i++,z++)
{
diff[i]= (diff[z+1+A]-diff[z+A])/(ax[z+j+2]-ax[z]);
}
A+=m;
m--;
}
}
//printing difference table
cout<<"\n difference table is as follows:\n";
for(z=0;z<i;z++)
cout<<"\n"<<diff[z];
// now calculating value of y for x
ay[n]=ay[0];
m=n;
A=0;
for (z=0;z<n-1;z++)
{
temp*=(ax[n]-ax[z]);
ay[n]+=temp*diff[z+A];
A+=m-2;
m--;
}
cout<<"\n\n The value of y for x = "<<ax[n]<<" is
:"<<ay[n];
getch();
}
OUTPUT:
Enter the number of points=5
Enter (x1 ,y1 ) 5 150
Enter (x2 ,y2 ) 7 392
Enter (x3 ,y3 ) 11 1452
Enter (x4 ,y4 ) 13 2366
Enter (x5 ,y5 ) 17 5202
Enter the value of x=9
The difference table is as follows:
121
265
457
709
24
32
42
1
1
0
The value of y for x = 9 is: 810
TRAPEZOIDAL RULE
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
float fun(float);
float h , k1=0.0 ;
float x[20] , y[20];
int n , i;
clrscr();
printf("\nEnter number of parts : ");
scanf("%d" , &n);
printf("\nEnter lower and upper limits : ");
scanf("%f %f" , &x[0] , &x[n]);
y[0] = fun(x[0]);
h = (x[n] - x[0])/n ;
printf("\nx y");
printf("\n %8.5f %8.5f " , x[0] ,y[0]);
for(i=1 ; i < n ; i++)
{ x[i] = x[0] + i * h ;
y[i] = fun(x[i]);
printf("\n %8.5f %8.5f " , x[i] , y[i]);
k1 = k1 + 2 * y[i];
}
y[n] = fun(x[n]);
printf("\n %8.5f %8.5f " , x[n] , y[n]);
y[0] = (h / 2.0 ) * (y[0] + y[n] + k1 );
WEEDEL'S RULE
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{ float fun(float);
float h , k1=0.0 , k2=0.0 , k3=0.0 , k4=0.0;
float x[20] , y[20];
int n , i;
clrscr();
printf("\nEnter number of parts : ");
scanf("%d" , &n);
printf("\nEnter lower and upper limits : ");
scanf("%f %f" , &x[0] , &x[n]);
y[0] = fun(x[0]);
h = (x[n] - x[0]) / n;
printf("\nx y");
printf("\n%8.5f %8.5f" , x[0] ,y[0]);
for(i=1 ; i < n ; i++)
{ x[i] = x[0] + i * h ;
y[i] = fun(x[i]);
printf("\n %8.5f %8.5f " , x[i] , y[i]);
if(i % 6 == 0)
k1 = k1 + 2 * y[i];
else if(i % 3 == 0)
k1 = k1 + 6 * y[i];
else if(i % 2 == 0)
k1 = k1 + y[i];
else
k4 = k4 + 5 * y[i];
}
y[n] = fun(x[n]);
printf("\n %8.5f %8.5f " , x[n] , y[n]);
y[0] = ((3 * h)/10) * (y[0] + y[n] + k1 + k2 + k3 +
k4);
printf("\nresult =%f \n" , y[0]);
getch();
}
float fun(float x)
{ float g;
g = sin(x) - log(x) + exp(x);
return g;
}
OUTPUT :Enter number of parts : 6
Enter lower and upper limits : 0.2 1.4
x y
0.20000 3.02951
0.40000 2.79753
0.60000 2.89759
0.80000 3.16604
1.00000 3.55975
1.20000 4.06983
1.40000 4.70418
result = 4.051446
printf("\n");
}
//now printing the result
printf("\nSolution is :-\n");
for(i = 0 ; i < n ; i++)
printf("\nx[%d]=%7.4f" , i+1
,matrix[i][n]/matrix[i][i]);
getch();
}
OUTPUT :Enter the elements of the augment matrix row wise :3 1 -1 3
2 -8 1 -5
1 -2 9 8
The digonal matrix is :-3.000000 0.000000 0.000000 3.000000
0.000000 -8.666667 0.000000 -8.666667
0.000000 0.000000 8.884615 8.884615
Solution is :x[1]= 1.0000
x[2]= 1.0000
x[3]= 1.0000
if (err>maxerr)
maxerr=err;
x[i]=t;
}
printf ("%d",itr);
for (i=1;i<=n;i++)
printf ("%7.4f",x[i]);
if (maxerr<aerr)
{
for (i=1;i<=n;i++)
printf ("x[%d]=%7.4f",i,x[i]);
exit(0);
} }
}
OUTPUT:
Enter the number of unknowns = 3
Enter the augmented matrix row wise
20 1 -2 17
3 20 -1 -18
2 -3 20 25
Enter allowed error, max iteration=0.001 ,4
Iteration x[1]= 0.8500 -1.0275
x[2] = 1.01091 1.0025
x[3] = -0.9998 0.99981
1.0000 - 1.0000 1.0000
x[1]= 1.0000 x[2]=-1.0000 x[3]= 1.0000
sum4+=x2[i];
}
a=(sum2*sum4-sum3*sum1)/(ob*sum4-sum1*sum1);
b=(sum2-ob*a)/sum1;
printf("\n\n Equation of the STRAIGHT LINE of the form y=a+b*x
is );
printf("\n\n\t\t\t y=[%f] + [%f]x.a,b);
}
OUTPUT:
Enter the no. of observations : 5
Enter the values of x :
Enter the value of x1 : 1
Enter the value of x2 : 2
Enter the value of x3 : 3
Enter the value of x4 : 4
Enter the value of x5 : 5
Enter the values of y :
Enter the value of y1: 14
Enter the value of y2: 27
Enter the value of y3: 40
Enter the value of y4: 55
Enter the value of y5: 68
Equation of the STRAIGHT LINE of the form y=a+b*x is :
y=0 + 13.6*x
Runga-kuta Method
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float f(float x , float y);
float x0 = 0.1 , y0 = 1 , xn = 2, h =0.2 , k1 , k2 ,
k3 , k4 ;
int i , n ;
clrscr();
printf("\ndy/dx = (x^2 +y^2)/10 ");
printf("\ngiven y0=1 && x0= 0.1 && h=0.2 (in the range
x0 < x <
2.0)\n");
n = (xn - x0) / h;
for(i = 0 ; i <= 10 ; 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);
y0 = y0 + (1 / 6.0) * (k1 + 2*k2 + 2*k3 + k4);
printf("\nThe solution of differential equation is
when x =
%f y = %f \n" , x0 , y0);
x0 = x0 + h;
}
getch();
}
Function sub program
float f(float x , float y)
{
float g;
g = (x*x + y*y) / 10 ;
return g ;
}
OUTPUTdy/dx = (x^2 +y^2)/10
given y0=1 && x0= 0.1 && h=0.2 (in the range x0 < x
<2.0)
else
k2=k2+y[i];
}
k3=h*(y[0]+y[n]+2*k1+4*k2)/3;
printf("Value of integral using Simpsons 1/3 rule");
printf("%f",k3);
/*Simpsons 3/8 rule is as follows*/
for(j=1;j<n;j++)
{
if(j%3==0)
k4=k4+y[j];
else
k5=k5+y[j];
}
k6=((3.0*h)*(y[0]+y[n]+2*k4+3*k5))/8;
printf("\nValue of integral using Simpsons 3/8 rule is
%f",k6);
getch();
}
OUTPUTEnter value of n:
6
Value of integral using Simpsons 1/3 rule is 1.898555
Value of integral using Simpsons 3/8 rule is 1.894638
arr[0][3]+=y;
arr[1][3]+=x*y;
arr[2][3]+=xx*y;
}
arr[1][0]=arr[0][1];
arr[1][1]=arr[0][2];
arr[2][0]=arr[1][1];
arr[2][1]=arr[1][2];
}
void displayaugmentedmatrix()
{
int i,j;
printf("The augmented matrix is \n");
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("%9.4f",arr[i][j]);
}
printf("\n");
}
}
void applygaussjordon()
{
int i,j,k;
float t;
for(j=0;j<3;j++)
for(i=0;i<3;i++)
if(i!=j)
{
t=arr[i][j]/arr[j][j];
for(k=0;k<4;k++)
arr[i][k]-=arr[j][k]*t;
}
a=arr[0][3]/arr[0][0];
b=arr[1][3]/arr[1][1];
c=arr[2][3]/arr[2][2];
}
void displaybestfit()
{
printf("y=%f",a);
if(b>=0)
printf("+");
printf("%f x",b);
if(c>0)
printf("+");
printf("%f x^2",c);
}
void main()
{
clrscr();
printf("Enter the number of pairs of observed
values\n");
scanf("%d",&n);
initialize();
arr[0][0]=n;
readdata();
displayaugmentedmatrix();
applygaussjordon();
printf("The best fit curve is\n");
displaybestfit();
}
Enter the number of pairs of observed values
5
Pair num 1
0
1
Pair num 2
1
1.8
Pair num 3
2
1.3
Pair num 4
3
2.5
Pair num 5
4
6.3
The augmented matrix is
5.0000
10.0000
10.0000
30.0000
12.9000
30.0000 100.0000
37.1000
CROUTS METHOD
# include<stdio.h>
# include<conio.h>
# define n 4
typedef float matrix[n][n];
matrix l,u,a;
float b[n],x[n],y[n];
void urow(int i)
{
float s;
int j,k;
for(j=i;j<n;j++)
{
s=0;
for(k=0;k<n-1;k++)
s+=u[k][j]*l[i][k];
u[i][j]=a[i][j]-s;
}
}
void lcol(int j)
{
float s;
int i,k;
for(i=j+1;i<n;i++)
{
s=0;
for(k=0;k<=j;k++)
s+=u[k][j]*l[i][k];
l[i][j]=(a[i][j]-s)/u[j][j];
}
}
void printmat(matrix m)
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
printf("%8.4f",m[i][j]);
printf("\n");
}
}
void readmat()
{
int i,j;
printf("Enter the elements of augmented matrix\n");
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
scanf("%f",&a[i][j]);
scanf("%f",&b[i]);
}
}
void solveLY_B()
{
int i,j;
float s;
for(i=0;i<n;i++)
{
s=0;
for(j=0;j<i;j++)
s+=l[i][j]*y[j];
y[i]=b[i]-s;
}
}
void solveUX_Y()
{
int i,j;
float s;
for(i=n-1;i>=0;i--)
{
s=0;
for(j=i+1;j<n;j++)
s+=u[i][j]*x[j];
x[i]=(y[i]-s)/u[i][i];
}
}
void main()
{
int i,j,m;
clrscr();
readmat();
for(i=0;i<n;i++)
l[i][i]=1.0;
for(m=0;m<n;m++)
{
urow(m);
if(m<n-1)
lcol(m);
}
printf("\n\t\tU\n");
printmat(u);
printf("\n\t\tL\n");
printmat(l);
solveLY_B();
solveUX_Y();
printf("The solution is:\n");
for(i=0;i<n;i++)
printf("x[%d]=%6.4f\n",i+1,x[i]);
getch();
}
OUTPUTEnter the elements of augmented matrix
5 1 1 1 4
1 7 1 4 6
1 1 6 1 -5
1 1 1 4 -6
U
5.0000
1.0000
1.0000
1.0000
0.0000
6.8000
0.8000
3.8000
0.0000
0.0000
5.7059
0.3529
0.0000
0.0000
0.0000
3.3093
L
1.0000
0.0000
0.0000
0.0000
0.2000
1.0000
0.0000
0.0000
0.2000
0.1176
1.0000
0.0000
0.2000
0.1176
0.1237
1.0000
Predicated
Corrected");
puts("
f");
while(1)
{
if(x[3]==xr) return;
x[4]=x[3]+h;
y[4]=y[0]+(4*h/3)*(2*(f(1)+f(3))-f(2));
printf("%6.2f %8.4f %8.4f\n",x[4],y[4],f(4));
correct();
while(1)
{
yc=y[4];
correct();
if(fabs(yc-y[4])<=aerr)break;
}
for(i=0;i<=3;i++)
{
x[i]=x[i+1];
y[i]=y[i+1];
}
}
getch();
}
OUTPUT
Enter the values of x0,xr,h,allowed error
0 1 .2 .0001
Enter the values of y[i],i=0,3
0 .02 .0795 .1762
x
0.80
1.00
Predicated
y
0.3049
0.7070
0.4554
Corrected
y
0.3046
0.7072
0.3046
0.7072
0.4556
0.7925
0.4556
0.7925
0.7926
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,x,y;
float h,k,a,b,c,d,xy[20][20],ax[20],ans;
clrscr();
printf("Integral a to b, integral c to d
(function) dxdy");
printf("\nEnter a,b,c,d,h,k where h,k are number
of parts for a,b & c,d resp\n");
fflush(stdin);
scanf("%f %f %f %f %f %f",&a,&b,&c,&d,&h,&k);
x=((b-a)/h)+0.5;
y=((d-c)/k)+0.5;
printf("\nx=%d,y=%d\n",x,y);
for(i=0;i<=x;i++)
{
for(j=0;j<=y;j++)
{
xy[i][j]=func(a+i*h,c+j*k);
printf("%0.4f
",xy[i][j]);
}
printf("\n");
}
for(i=0;i<=x;i++)
{
ax[i]=0;
for(j=0;j<=y;j++)
{
if(j==0 || j==y)
ax[i]+=xy[i][j];
Simpson of 1st row goes into A0 and then 2nd row into
A1 and so on upto An
else if(j%2==1)
ax[i]+=4*xy[i][j];
else
ax[i]+=2*xy[i][j];
}
ax[i]=(h/3)*ax[i];
printf("\nAx(%d)=%f",i,ax[i]);
}
ans=0;
for(i=0;i<=x;i++)
Now Simpson of vertical column of A[i],i.e., A1, A2, A3,..., An goes
into ans
{
if(i==0 || i==x)
ans+=ax[i];
else if(i%2==1)
ans+=4*ax[i];
else
ans+=2*ax[i];
}
ans=(k/3)*ans;
ROMBERG INTEGRATION
// Romberg
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,k,n,p;
float h,y[50],a,b,ih,ir[50];
clrscr();
for(i=0;i<=n;i++)
{
ih=0;
p=pow(2,i);
h=(b-a)/p;
for(j=0;j<=p;j++)
{
y[j]=a+j*h;
if(j!=0)
y[j]=y[j]/sin(y[j]);
else
y[0]=1;
if(j>0 && j<p)
ih=ih+2*(y[j]);
}
ih=ih+y[0]+y[p];
ih=ih*h/2;
ir[i]=ih;
printf("\n%f",ir[i]);
}
k=2;
while(n>=1)
{
p=pow(2,k);
for(i=0;i<n;i++)
ir[i]=(p*ir[i+1]-ir[i])/(p-1);
n--;
k=k+2;
}