Sei sulla pagina 1di 32

INTERNATIONAL INSTITUTE OF TECHONOLOGY

AND MANAGEMENT

PRCTICAL FILE OF ANTC

AFFILATED TO
DEENBANDU CHOTU RAM UNIVERSITY OF SCIENCE AND
TECHONOLOGY

SUBMITTED BY: SUBMITTED TO:

NAME : ARPIT MS .NISHI


ROLL NO : 18015004902

SESSION 2017-21

1
INDEX
S.NO PROGRAMS DATE REMARKS
1 Solution of non linear
equation in single
variable using the :
(a) Bisection
method
(b) Newton rapson
method
(c) Secant method
2 Solution of linear
equation in single
variable using modified
euler method
3 Solution of system of
simultaneous algebraic
equation using gauss
elimination method.
4 Numerical solution of
an ordinary differential
equation using the
Euler’s method.
5 Solution of a system of
simultaneous algebraic
equation using the
gauss-seidel iterative
method.
6 Numerical solution of
an ordinary differential
equation using the
runga kutta method.

2
7 Numerical solution of
an ordinary differential
equation using the
predicator corrector
method .
8 Numerical solution of a
system of a system of
two ordinary differential
eq using numerical
integration.
9 Numerical solution of a
differential eq using
newton forward diff
method.
10 Numerical solution of a
differential eq using
newton divide diff
method.

Program to Implement BISECTION METHOD.

#include<stdio.h>
#include<conio.h>
#include<math.h>

#define F(x) pow((x),3)-4*(x)-9


int n;
float root=1;
void main()
{
clrscr();

3
printf("\n Solution by BISECTION METHOD ");
printf("\n Enter the no. of iterations ");
scanf("%d",&n);
bisect();
getch();
}

void bisect()
{
int count;
float x1,x2,x0;
float f1,f2,f0;
// Finding x2
for(x2=0; ;x2++)
{
f2=F(x2);
if(f2>0)
{
break;
}
}
// Finding x1
for(x1=x2-1; ;x1--)
{
f1=F(x1);
if(f1<0)
{
break;
}
}
for(count=1;count<=n;count++)
{
x0=(x1+x2)/2;
f0=F(x0);
if(f0==0)
{
root=x0;
}
if(f0*f1<0)
{
x2=x0;
f2=f0;
}
else
{
x1=x0;

4
f1=f0;
}

getch()

/**********PROGRAM FOR REGULA FALSI METHOD***********/

#include<stdio.h>
#include<conio.h>
#include<math.h>
float eqn(float);
float eqn1(float,float,float,float);
void main()
{
float x1=2,x2=3,x3,f1,f2,f3,z,t;
int i=1;
clrscr();

5
printf("********* REGULA FALSI METHOD********* \
\n------ equation is : X^4-34X-9\n");
do
{
printf("\n\niteration %d:\n", i);
f1=eqn(x1);
printf("value of f(x1)=%f",f1);
f2=eqn(x2);
printf("value of f(x2)=%f",f2);
x3=eqn1(x1,x2,f1,f2);
f3=eqn(x3);
printf("\nvalue of f(x3)=%f\n",f3);
if(f3>0)
x2=x3;
else
x1=x3;
printf("x1=%f\tx2=%f\n",x1,x2);
i++;
}while(fabs(f3)>.005);
printf("\nfinal ans=%f\n",x3);
getch();
}

/******function for given eqn********/


float eqn(float x)
{
float f;
f=x*x*x-4*x-9;
return(f);
}

/***** function for regula falsi*******/


float eqn1(float x1,float x2,float f1,float f2)
{
float z;
z=((x1*f2)-(x2*f1))/(f2-f1);
return z;

6
/*************PROGRAM FOR SECANT METHOD***********/

#include<stdio.h>
#include<conio.h>
#include<math.h>

7
float eqn(float);
float eqn1(float,float,float,float);
void main()
{
float x1=2,x2=3,x3,f1,f2,f3,z,t;
int i=1;
clrscr();
printf("********* SECANT METHOD********* \
\n------ equation is : X^4-34X-9\n");
do
{
printf("\n\niteration %d:\n", i);
f1=eqn(x1);
printf("value of f(x1)=%f",f1);
f2=eqn(x2);
printf("value of f(x2)=%f",f2);
x3=eqn1(x1,x2,f1,f2);
f3=eqn(x3);
printf("\nvalue of f(x3)=%f\n",f3);
x1=x2;
x2=x3;
printf("x1=%f\tx2=%f\n",x1,x2);
i++;
}while(fabs(f3)>.005);
printf("\nfinal ans=%f\n",x3);
getch();
}

/******function for given eqn********/


float eqn(float x)
{
float f;
f=x*x*x-4*x-9;
return(f);
}

/***** function for secant method*******/


float eqn1(float x1,float x2,float f1,float f2)
{
float z;
z=((x1*f2)-(x2*f1))/(f2-f1);
return z;
}

OUTPUT:-

8
/*************PROGRAM FOR ITERATION METHOD***********/

#include<stdio.h>
#include<conio.h>
#include<math.h>

9
float g(float x)
{
return (10/sqrt(1+x));
}
float f(float y) {
float z;
z=-5*1/pow((1+y),1.5);
return(z);
}
void main() {
float m=0.00,y2,y1,x1,x2,x3,error=0.00005;
int n=0;
clrscr();
printf("\n *******Solution By ITERATIVE Method**************");
printf("\n\n eqution is -> x^3+x^2-100");
printf("enter the initial interval of equation in y1 & y2");
printf("\n\n iteration root");
printf("\n------------------------------------");
scanf("%f%f",&y1,&y2);
m=(y1+y2)*.5;
x1=f(m);
printf("%f",x1);
if(fabs(x1)<1)
{ do {
n++;
x1=x2;
x2=g(x1);
x3=x1;
printf("\nthe value of x%d== %f",n, x3); }
while (fabs(x2-x1)>error);
printf("\n------------------------------------");
printf(“\n finally the root is %f”,x3);
} getch();
}

OUTPUT:-

10
11
/******PROGRAM FOR NEWTON RAPHSON METHOD********/

#include<stdio.h>
#include<conio.h>
#include<math.h>
float eqn(float);
float diff_eqn(float);
float nr(float, float, float);
void main()
{
float x1,x2,x3,f1,f2,r;
int i=1;
//clrscr();
printf("********* NEWTON RAMPSON METHOD********* \
\n------ equation is : cosx-xe^x\n");

printf("\nenter interval\n");
scanf("%f%f",&x1,&x2);
x3=(x1+x2)/2;
do
{
printf("\niteration %d\n",i);

printf("\nx3=%f\n",x3);
f1=eqn(x3);
printf("\nf1=%f\n",f1);
f2=diff_eqn(x3);
printf("\nf2=%f\n",f2);
r=nr(x3,f1,f2);
printf("\nvalue=%f\n",r);
x3=r;
i++;
}while(fabs(eqn(x3))>.005);
getch();
}
/*********function for given eqn**********/
float eqn(float y)
{
float z;
z=(cos(y)-(y*exp(y)));
return z;
}
/********function for differenciation*******/
float diff_eqn(float y)
{
float x;
x=-sin(y)-exp(y)-y*exp(y);

12
return(x);
}
/******function for newton rampson*******/
float nr(float x, float y, float z)
{
float w;
w=(x)-((y)/(z));
return w;
}

OUTPUT:-

13
/***PROGRAM FOR NEWTON FORWARD INTERPOLATION**/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
float h,x, diff[4][4],ax[4],ay[4],p,y1,y2,y3,y4,y;
//clrscr();
printf("enter no of elt\n");
scanf("%d",&n);

printf("\nenter value of x\n");


for(i=0;i<n;i++)
scanf("%f",&ax[i]);
h=ax[1]-ax[0];
printf("h=%f\n",h);

printf("\nenter value of y\n");


for(i=0;i<n;i++)
scanf("%f",&ay[i]);

printf("\nenter value of x tobe calculated\n");


scanf("%f",&x);

for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
diff[i][j]=0;
}

for(i=0;i<n-1;i++)
diff[i][1]=ay[i+1]-ay[i];

for(j=2;j<4;j++)
{
for(i=0;i<n-j;i++)
{
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
printf("*-------------**--DIFFERENCE TABLE---*------------*\n");

for(i=0;i<n;i++)
{
printf("%f\t%f",ax[i],ay[i]);

14
for(j=0;j<n;j++)
{
printf("%f\t",diff[i][j]);
}
printf("\n");
}
i=0;
do
{
i++;
}while(ax[i]<x);
i--;
p=(x-ax[i])/h;
printf("\n value of p=%f\n",p);
y1=p*diff[i][1];
y2=p*(p-1)*diff[i][2]/2;
y3=p*(p-1)*(p-2)*diff[i][3]/6;
y4=p*(p-1)*(p-2)*(p-3)*diff[i][4]/24;
y=ay[0]+y1+y2+y3+y4;
printf("FINAL RESULT=%f\n",y);
getch();
}

Output:-

15
/**PROGRAM FOR NEWTON BACKWARD INTERPOLATION*/

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
float h,x, diff[4][4],ax[4],ay[4],p,y1,y2,y3,y4,y;
//clrscr();
printf("enter no of elt\n");
scanf("%d",&n);

printf("\nenter value of x\n");


for(i=0;i<n;i++)
scanf("%f",&ax[i]);
h=ax[1]-ax[0];
printf("h=%f\n",h);

printf("\nenter value of y\n");


for(i=0;i<n;i++)
scanf("%f",&ay[i]);

printf("\nenter value of x to be calculated\n");


scanf("%f",&x);
p=x-ax[n-1]/h;
printf("p=%f\n",p);
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
diff[i][j]=0;
}

for(i=0;i<n-1;i++)
diff[i][1]=ay[i+1]-ay[i];

for(j=2;j<4;j++)
{
for(i=0;i<n-j;i++)
{
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
}
}
printf("*-------------**--DIFFERENCE TABLE---*------------*\n");

16
for(i=0;i<n;i++)
{
printf("%f\t%f",ax[i],ay[i]);

for(j=0;j<n;j++)
{
printf("%f\t",diff[i][j]);
}
printf("\n");
}

y1=p*diff[n-1][1];
y2=p*(p+1)*diff[n-1][2]/2;
y3=p*(p+1)*(p+2)*diff[n-1][3]/6;
y4=p*(p+1)*(p+2)*(p+3)*diff[n-1][4]/24;
y=ay[n-1]+y1+y2+y3+y4;
printf("FINAL RESULT=%f\n",y);
getch();
}

OUTPUT:-

17
/************* LAGRANGE’S INTERPOLATION FORMULA*************/

#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
float x[MAX],y[MAX],k=0,z,nr,dr;
int i,j,m;
//clrscr();
printf("\n enter the range ");
scanf("%d",&m);
printf("\n enter the x value ");
for(i=0;i<m;i++)
scanf("%f",&x[i]);
printf("\n enter the y value ");
for(i=0;i<m;i++)
scanf("%f",&y[i]);
printf("\n enter value OF Z to be calculated ");
scanf("%f",&z);

for(i=0;i<m;i++)
{ nr=1;dr=1;
for(j=0;j<m;j++)
{
if (j!=i)
{
nr=nr*(z-x[j]);
dr=dr*(x[i]-x[j]);
}
}
k=k+((nr/dr)*y[i]);
}
printf("\n final result=%f\n",k);
getch();
}

OUTPUT:-

18
19
/********** PROGARM FOR TRAPEZOIDAL METHOD*********/

#include<stdio.h>
#include<conio.h>
float fun(int );
void main()
{
int n,i,h,a,b;float sum=0;
//clrscr();
printf("\n******solution by TRAPEZOIDAL RULE method******");
printf("\n\n INTEGRATE the func 1/1+X^2 WITH limit 0 to 6 \n");
printf("\n enter the lower limit");
scanf("%d",&a);
printf("\n enter the upper limit");
scanf("%d",&b);
printf("\n enter the no of subintervals");
scanf("%d",&n);
h=(b-a)/n;
sum=fun(a)+fun(b);
for(i=1;i<n;i++)
sum=sum+2*fun(a+i);
sum=(sum)*h/2;
printf("\n result is%f\n",sum);
getch();
}
float fun(int x)
{
float p,q;
p=1+(x*x);
q=1/p;
return(q);
}

OUTPUT:-

20
/************PROGARM FOR SIMPSON 1/3***********/
#include<stdio.h>
#include<conio.h>
float fun(int);
void main()
{
int n,a,b,i;
float h, sum=0, result;
clrscr();

printf("enter range");
scanf("%d",&n);
printf("enter lower limit");
scanf("%d",&a);
printf("enter upper limit");
scanf("%d",&b);
h=(b-a)/n;
sum=fun(a)+fun(b);
for(i=0;i<n;i++)
{
if (i%2==0)
sum+=2*fun(a+i*h);
else
sum+=4*fun(a+i*h);
}
result=sum*h/3;
printf("%f", result);
getch();
}

float fun(int x)
{
float val;
val=1/(1+(x*x));
return(val);
}

OUTPUT:-

21
22
/************ PROGARM FOR SIMPSON 3/8*************/

#include<stdio.h>
#include<conio.h>
float fun(int);
void main()
{
int n,a,b,i;
float h, sum=0, result;
//clrscr();

printf("enter range");
scanf("%d",&n);
printf("enter lower limit");
scanf("%d",&a);
printf("enter upper limit");
scanf("%d",&b);
h=(b-a)/n;
sum=fun(a)+fun(b);
for(i=0;i<n;i++)
{
if (i%2==0)
sum+=2*fun(a+i*h);
else
sum+=3*fun(a+i*h);
}
result=sum*3/8*h;
printf("%f", result);
getch();
}

float fun(int x)
{
float val;
val=1/(1+(x*x));
return(val);
}

OUTPUT:-

23
24
/************ PROGARM FOR WEDDLE’S RULE*************/

#include<conio.h>
#include<math.h>
#define MAX 15
#define F(x) 1/(1+pow(x,2))
void main()
{
int i=0,j,n;
float x[MAX],y[MAX],h,xl,xu,yo=0,ye=0,yf=0,yy;
printf("\n *************** solutin by WEDDLES RULE method************");
printf("\n equation is F(x) = 1/(1+x^2)\n");
printf("Enter the number of intervals: ");
scanf("%d",&n);
printf("enter lower limit");
scanf("%f",&xl);
printf("Enter the upper limit: ");
scanf("%f",&xu);
h=(xu-xl)/n;
printf("\n x y ");

do
{
x[i]=xl;
y[i]=F(x[i]);
printf("\n%.2f = %.4f",x[i],y[i]);
xl=xl+h;
i++;
}while(xl<=xu);
i--;
for(j=1;j<i;j++)
{
if(j%2!=0 && j%3==0)
yo=yo+y[j];
else
if(j%2==0) {
ye=ye+y[j];
if(j%6==0 && j!=i)
ye=ye+y[j];
}
else
yf=yf+y[j];
}
yy=3*h*(y[0]+y[i]+6*yo+5*yf+ye)/10;

25
printf("\n-------------------------------- ") ;
printf("\n\nfinal output y = %f",yy);
getch();
}

OUTPUT:-

26
/******** PROGARM FOR CURVE FITTING FOR y=a+bx********/

#include<stdio.h>
#include<conio.h>
void main()
{
int x[50],y[50],xy[50],pow_x[50],n,i;
float p,q,a,b,sum_x=0.0,sum_y=0.0,sum_xy=0.0,sum_pow_x=0.0,sq_sum_x=0.0;
printf("\nenter no of values u want to enter\n");
scanf("%d",&n);
printf("enter values of x\n");
for(i=0;i<n;i++)
{
scanf("%d",&x[i]);
sum_x=sum_x+x[i];
pow_x[i]=x[i]*x[i];
sum_pow_x=sum_pow_x+pow_x[i];
}
sq_sum_x=sum_x*sum_x;
printf("enter values of y\n");
for(i=0;i<n;i++)
{
scanf("%d",&y[i]);
sum_y=sum_y+y[i];
}
for(i=0;i<n;i++)
{
xy[i]=x[i]*y[i];
sum_xy=sum_xy+xy[i];
}
b=((n*sum_xy)-(sum_x*sum_y))/((n*sum_pow_x)-(sq_sum_x));
a=fabs((sum_y-b*sum_x)/n);
printf("value of a=%f & b=%f\n",a,b);
}

27
OUTPUT:-

28
/*****CURVR FITTING FOR EXPONANCIAL FUNCTION******/

#include<stdio.h>
#include<conio.h>
#include<math.h>
#define max 15
void main() {
float x[max],y[max],sumx=0,sumy=0,sumxx=0,sumxy=0;
double q,p,a,b ;
int n,i;
clrscr();
printf("\n\n*******solution by CURVE FITTING IN EXPONANTIAL function******");
printf("\nenter the limit");
scanf("%d",&n);
printf("\n enter the value of x");
for(i=0;i<n;i++)
scanf("%f",&x[i]);
printf("\n enter the value of x");
for(i=0;i<n;i++)
{scanf("%f",&y[i]);
y[i]=log10(y[i]);
}
//y=a+bx
//Ey=an+bEx
//Exy=aEx+bEx*x
for(i=0;i<n;i++) {
sumy=sumy+y[i];
sumx=sumx+x[i];
sumxy=sumxy+x[i]*y[i];
sumxx=sumxx+x[i]*x[i];
}
printf("x=%f y=%f xx=%f xy=%f",sumx,sumy,sumxx,sumxy);
b=((sumx*sumy)-(n*sumxy))/((sumx*sumx)-(n*sumxx));
a=(sumy-b*sumx )/n;
p=pow(10,a);
q=b/log10(exp(1));
printf("\nA=%f\nB=%f",p,q);
printf("\n ----------------------------------------");
printf("\n the required equation y=%fe^%fx",p,q);
getch();
}

29
OUTPUT:-

30
/************PROGRAM FOR LINEAR REGRATION***********/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void main()
{
float x[10],y[10],xy[10],x2=0,y2=0,byx,bxy;
float sumx=0,sumy=0,sumxy=0,sumx2=0,sumy2=0;
int i,n;
clrscr();
printf("\n ************solution by REGRESSION method*********** ");
printf("\n enter the limit");
scanf("%d",&n);
printf("enter the value of x");
for(i=1;i<=n;i++)
{
scanf("%f",&x[i]);
sumx=sumx+x[i];
}
printf("\n enter the value of Y");
for(i=1;i<=n;i++)
{
scanf("%f",&y[i]);
sumy=sumy+y[i];
}
for(i=1;i<=n;i++)
{
xy[i]=x[i]*y[i];
sumxy=sumxy+xy[i];
}
for(i=1;i<=n;i++)
{
x2=x[i]*x[i];
sumx2=sumx2+x2;
}
for(i=1;i<=n;i++)
{
y2=y[i]*y[i];
sumy2=sumy2+y2;
}
printf("\n Ex=%f",sumx);
printf(" Ey=%f",sumy);
printf(" Exy=%f",sumxy);
printf(" Ex2=%f",sumx2);

31
printf(" Ey2=%f",sumy2);
printf("\n ----------------------------------");
byx=(n*sumxy-sumx*sumy)/(n*sumx2-(sumx*sumx));
printf("\n byx= %f",byx);
bxy=(n*sumxy-sumx*sumy)/(n*sumy2-(sumy*sumy));
printf("\n bxy= %f",bxy);
getch();
}

OUTPUT:-

32

Potrebbero piacerti anche