Professional Documents
Culture Documents
Nacp Programmes Dont Delete
Nacp Programmes Dont Delete
2012
SUBMITTED BY:-
RISHI RAUSHAN
10-CSS-54
COMP. ENGG.
}
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);}
else goto l15;
}
float fun(float m)
{float g;
g=(m*(exp(m))-1);
return(g); }
/*output:
equation:x*(exp(x)-1)
enter the app value of x1,x2:0.5 1
the root lies b/w 0.500000 & 1.000000
n x1 x2 x3 f(x1) f(x2) f(x3)
1 0.500000 1.000000 0.750000 -0.175639 1.718282 0.587750
2 0.500000 0.750000 0.625000 -0.175639 0.587750 0.167654
3 0.500000 0.625000 0.562500 -0.175639 0.167654 -0.012782
4 0.562500 0.625000 0.593750 -0.012782 0.167654 0.075142
5 0.562500 0.593750 0.578125 -0.012782 0.075142 0.030619
6 0.562500 0.578125 0.570312 -0.012782 0.030619 0.008780
7 0.562500 0.570312 0.566406 -0.012782 0.008780 -0.002035
8 0.566406 0.570312 0.568359 -0.002035 0.008780 0.003364
9 0.566406 0.568359 0.567383 -0.002035 0.003364 0.000662
10 0.566406 0.567383 0.566895 -0.002035 0.000662 -0.000687
result = 1.827570
SIMPSON 1/3 RULE
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{ float fun(float);
float h , k1=0.0 , k2=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 % 2 == 0)
k1 = k1 + 2 * y[i];
else
k2 = k2 + 4 * y[i];
}
y[n] = fun(x[n]);
printf("\n %8.5f %8.5f " , x[n] , y[n]);
y[0] = (h / 3.0 ) * (y[0] + y[n] + k1 + k2 );
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.052133
SIMPSON 3/8 RULE
#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{ float fun(float);
float h , k1=0.0 , k2=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 % 3 == 0)
k1 = k1 + 2 * y[i];
else
k2 = k2 + 3 * y[i];
}
y[n] = fun(x[n]);
printf("\n %8.5f %8.5f " , x[n] , y[n]);
y[0] = ((3 *h) / 8.0 ) * (y[0] + y[n] + k1 + k2 );
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 part 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.052991
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
GAUSS ELIMINATION METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define n 3
void main()
{ float temp , s , matrix[n][n+1] , x[n];
int i , j , k;
clrscr();
printf("\nEnter the elements of the augment matrix row
wise
:\n");
for(i = 0 ; i < n ; i++)
for(j=0 ; j <= n ; j++)
scanf("%f" , &matrix[i][j]);
printf("\nmatrix:-");
for(i=0 ; i<n ; i++)
{ for(j=0 ;j<=n ;j++)
printf("%f\t",matrix[i][j]);
printf("\n");
}
/*now calculating the upper triangular matrix */
for(j=0 ; j < n-1 ; j++)
for(i=j+1 ; i < n ; i++)
{ temp = matrix[i][j] / matrix[j][j];
for(k = 0 ; k <= n ; k++)
matrix[i][k] -= matrix[j][k] * temp;
}
//now performing back substitution
for(i = n -1 ; i >= 0 ; i--)
{ s = 0;
for(j = i + 1 ; j < n ; j++)
s += matrix[i][j] * x[j];
x[i] = (matrix[i][n] - s) / matrix[i][i];
}
//now printing the result
printf("\nSolution is :-\n");
for(i = 0 ; i < n ; i++)
printf("\nx[%d]=%7.4f" , i+1 ,x[i]);
getch();
}
OUTPUT :-
Enter the elements of the augment matrix row wise :
3 1 -1 3
2 -8 1 -5
1 -2 9 8
matrix:-
3.000000 1.000000 -1.000000 3.000000
2.000000 -8.000000 1.000000 -5.000000
1.000000 -2.000000 9.000000 8.000000
Solution is :-
x[1]= 1.0000
x[2]= 1.0000
x[3]= 1.0000
GAUSS JORDAN METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define n 3
void main()
{ float temp , matrix[n][n+1];
int i , j , k;
clrscr();
printf("\nEnter the elements of the augment matrix row
wise :-
\n");
for(i = 0 ; i < n ; i++)
for(j=0 ; j <= n ; j++)
scanf("%f" , &matrix[i][j]);
/*now calculating the digonal matrix */
for(j=0 ; j < n ; j++)
for(i=0 ; i < n ; i++)
if(j != i)
{ temp = matrix[i][j] / matrix[j][j];
for(k = 0 ; k <= n ; k++)
matrix[i][k] -= matrix[j][k] * temp;
}
/*now printing the matrix */
printf("\nThe digonal matrix is :--\n");
for(i = 0 ; i < n ; i++)
{ for(j=0 ; j <= n ; j++)
printf("\t%f" , matrix[i][j]);
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
GAUSS SIEDAL METHOD
#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
void main()
{
float a[10][10],x[10],aerr, maxerr, t, s, err;
int i,j,itr,maxitr,n;
printf ("\n Enter the number of unknowns=");
scanf ("%d",&n);
for(i=1;i<=n;i++)
x[i]=0.0;
printf ("\n Enter the augmented matrix row wise=");
for (i=1;i<=n;i++)
for (j=1;j<=n+1;j++)
scanf ("%f",&a[i][j]);
printf ("\n Enter allowed error, max iteration=");
scanf ("%f %d",&aerr,&maxitr);
printf ("\n Iteration");
for (i=1;i<=n;i++)
printf ("\n x[%d]",i);
for (itr=1;itr<=maxitr;i++)
{
maxerr=0;
for (i=1;i<=n;i++)
{
s=0;
for (j=1;j<=n;j++)
if (j!=i)
s = s+a[i][j]*x[j];
t = (a[i][n+1]-s)/a[i][i];
err=fabs(x[i]-t);
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
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 ;
}
OUTPUT-
dy/dx = (x^2 +y^2)/10
given y0=1 && x0= 0.1 && h=0.2 (in the range x0 < x
<2.0)
#include<conio.h>
#include<math.h>
void main()
float x[15],y[15],h,k1,k2,k3,k4,k5,k6;
int i,j,n;
clrscr();
scanf("%d",&n);
k1=k2=0.0,x[0]=0.0,x[n]=1.2,k4=0.0,k5=0.0;
h=(x[n]-x[0])/n;
for(i=1;i<n;i++)
x[i]=x[0]+i*h;
for(i=0;i<=n;i++)
y[i]=exp(pow(x[i],3)*sin(2*x[i]));
for(i=1;i<n;i++)
if(i%2==0)
k1=k1+y[i];
else
k2=k2+y[i];
k3=h*(y[0]+y[n]+2*k1+4*k2)/3;
printf("%f",k3);
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;
getch();
OUTPUT-
Enter value of n:
#include<conio.h>
float arr[3][4],x,y,xx,a,b,c;
int n;
void initialize()
int i,j;
for(i=0;i<3;i++)
for(j=0;j<4;j++)
arr[i][j]=0;
void readdata()
int i;
for(i=0;i<n;i++)
scanf("%f %f",&x,&y);
xx=x*x;
arr[0][1]+=x;
arr[0][2]+=xx;
arr[1][2]+=x*xx;
arr[2][2]+=xx*xx;
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;
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();
scanf("%d",&n);
initialize();
arr[0][0]=n;
readdata();
displayaugmentedmatrix();
applygaussjordon();
displaybestfit();
Pair num 1
Pair num 2
1.8
Pair num 3
2
1.3
Pair num 4
2.5
Pair num 5
6.3
# include<conio.h>
# define n 4
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;
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();
for(i=0;i<n;i++)
printf("x[%d]=%6.4f\n",i+1,x[i]);
getch();
OUTPUT-
5 1 1 1 4
1 7 1 4 6
1 1 6 1 -5
1 1 1 4 -6
x[1]=1.0000
x[2]=2.0000
x[3]=-1.0000
x[4]=-2.0000
MILNE'S PREDICTOR CORRECTOR
# include<stdio.h>
# include<conio.h>
# include<math.h>
float x[5],y[5],h;
float f(int i)
return x[i]-y[i]*y[i];
void correct()
y[4]=y[2]+(h/3)*(f(2)+4*f(3)+f(4));
main()
float xr,aerr,yc;
int i;
clrscr();
scanf("%f %f %f %f",&x[0],&xr,&h,&aerr);
for(i=0;i<=3;i++) scanf("%f",&y[i]);
for(i=1;i<=3;i++) x[i]=x[0]+i*h;
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));
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
0 1 .2 .0001
x Predicated Corrected
y f y f
0.3046 0.7072
0.3046 0.7072
0.4556 0.7925
0.4556 0.7925
NUMERICAL DOUBLE INTEGRATION
#include<stdio.h>
#include<conio.h>
#include<math.h>
//return(1/sqrt(pow(x,2)+pow(y,2)));
return(sin(x*y)/(1+x*y));
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");
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];
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++)
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;
getch();
}
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();
scanf("%f\t%f\t%d",&a,&b,&n);
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;
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;
}
printf("\nThe value of integral is %f",ir[0]);
getch();