You are on page 1of 11

Project on various numerical methods

Submitted to

Made by
Atul goel
04216101409
/*A program to show various numerical method.
Assumption the program can only solve cubical
And lower order equation.*/

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

void function(void);
float value(float);
void bisection(void);
void newton(void);
void trapezoidal(void);
void simpson(void);
void simpsons(void);
void jacobi(void);
void function1(void);
float value1(float,float);
void runge(void);
void secant(void);
void gauss(void);
float value2(float);
float a,b,c,d;
void main()
{
clrscr();
int num;
char ch;
do{
clrscr();
cout<<"Welcome to the main menu";
cout<<"\n 1.bisection method";
cout<<"\n 2.newton raphson";
cout<<"\n 3.trapezoidal method";
cout<<"\n 4.simpson's 1/3rd rule";
cout<<"\n 5.simpson's 3/8th rule";
cout<<"\n 6.gauss jacobi method";
cout<<"\n 7.runge method";
cout<<"\n 8.newton iteration or secant method";
cout<<"\n 9.gauss elimination\n\n\n";
cin>>num;

switch(num)
{
case 1: clrscr();
cout<<"\n bisection method" ;
function();
bisection();
break;
case 2: clrscr();
cout<<"\n newton raphson" ;
function();
newton();
break;
case 3: clrscr();
cout<<"\n trapezoidal method";
function();
trapezoidal();
break;
case 4: clrscr();
cout<<"\n simpsons's 1/3rd rule";
function();
simpson();
break;
case 5: clrscr();
cout<<"\n simpsons's 3/8th rule";
function();
simpsons();
break;
case 6: clrscr();
cout<<"\n gauss jacobi method";
jacobi();
break;
case 7: clrscr();
cout<<"\n runge method";
runge();
break;
case 8: clrscr();
cout<<"\n secant method";
secant();
break;
case 9: clrscr();
cout<<"\n gauss elimination";
gauss();
break;
default: cout<<"/n wrong choice" ;
}
cout<<"\n\n\n do u want to continue Y/N??";
cin>>ch;
}while(ch=='y');

void function(void)
{
cout<<"\n enter the value of the cofficient of x^3,x^2,x andconst\n\n";
cin>>a>>b>>c>>d;
cout<<"\nthe function entered by you is\n";
cout<<a<<"x^3+"<<b<<"x^2+"<<c<<"x+"<<d<<"\n\n\n";
}

float value(float i)
{
float x;
x=(a*pow(i,3)+b*pow(i,2)+c*i+d);
return x;
}

void bisection(void)
{
int C=0;
float A,B,root,x1,x2;
for(int i=0;i<10;i++)
{
float A,B;
A=value(i);
B=value(++i);
if((A>0&&B<0)||(A<0&&B>0))
{
C=i-1;
}

}
x1=C;
x2=C+1;
for(int i=0;i<20;i++)
{

A=value(x1);
B=value(x2);
root=(x1+x2)/2;
if((A>0&&value(root)<0)||(A<0&&value(root)>0))
{
B=value(root);
x2=root;
}
else
{
A=value(root);
x1=root;
}
}
cout<<"\n the root of the given equation is"<<root;

void newton(void)
{
int c=0;
float A,B,x1,x2;
double root;
for(int i=0;i<10;i++)
{
float A,B;
A=value(i);
B=value(++i);
if((A>0&&B<0)||(A<0&&B>0))
{
c=i-1;
}
}
x1=c;
x2=c+1;
for(int i=0;i<20;i++)
{

A=value(x1);
B=value(x2);
root= (((x1*B)-(x2*A))/(B-A));
if((A>0&&value(root)<0)||(A<0&&value(root)>0))
{
B=value(root);
x2=root;
}
else
{
A=value(root);
x1=root;
}

cout<<"\n the root of the given equation is"<<root;

void trapezoidal(void)
{
int ll,ul,j ;
float a[11],b,sum=0,h;
cout<<"\n enter the lower ,upper and the value of h";
cin>>ll>>ul>>h;
for(int i=0;i<11;i++)
{
j=(ll+(i*h));
a[i]=value(j);
}

float d=a[0]+a[10];

for(int j=1;j<=9;j++)
{
sum+=a[j];
}
float c=2*sum;
b=(h/2)*(d+c);
cout<<"\nthe value of the integration is "<<b;

void simpson(void)
{

int ll,ul,j ;
float a[11],b,sum1=0,sum2=0,h;
cout<<"\n enter the lower ,upper and the value of h";
cin>>ll>>ul>>h;
for(int i=0;i<11;i++)
{
j=(ll+(i*h));
a[i]=value(j);
}

float d=a[0]+a[10];

for(int j=1;j<=9;j++)
{
if(j%2==0)
{sum1 +=a[j];}

else
{ sum2 +=a[j];}
}
float x=4*sum2;
float y=2*sum1;
float z=h/3;
b=z*(d+x+y);

cout<<"\n"<<b;

void simpsons(void)
{

int ll,ul,j ;
float a[11],b,sum1=0,sum2=0,h;
cout<<"\n enter the lower ,upper and the value of h";
cin>>ll>>ul>>h;
for(int i=0;i<11;i++)
{
j=(ll+(i*h));
a[i]=value(j);
}

float d=a[0]+a[10];

for(int j=1;j<=9;j++)
{
if(j%3==0)
{sum1 +=a[j];}

else
{ sum2 +=a[j];}
}
float x=3*sum2;
float y=2*sum1;
float z=(3*h)/8;
b=z*(d+x+y);

cout<<"\n"<<b;

void jacobi(void)
{

cout<<"\n enter the equation in increasig order";


float a1,a2,a3,b1,b2,b3,c1,c2,c3,d1,d2,d3;
float x=0,y=0,z=0,k1,k2,k3;
cout<<"\n enter the coff of the eqtn1";
cin>>a1>>b1>>c1>>d1;
cout<<"\n enter the coff of the eqtn2";
cin>>a2>>b2>>c2>>d2;
cout<<"\n enter the coff of the eqtn3";
cin>>a3>>b3>>c3>>d3;
for(int i=0;i<10;i++)
{
k1=(d1-(b1*y)-(c1*z))/a1;
k2=(d2-(a2*x)-(c2*z))/b2;
k3=(d3-(a3*x)-(b3*y))/c3;
x=k1;y=k2;z=k3;
}

cout<<"\n coff of x\t"<<k1<<"\n coff of y\t"<<k2<<"\n coff of z\t"<<k3;


}

void function1(void)
{
cout<<"\nenter the coff of x,y,xy,const";
cin>>a>>b>>c>>d;
cout<<"\n the equation entered is";
cout<<a<<"x+"<<b<<"+"<<c<<"xy+"<<d;
}

float value1(float i,float j)


{
float sum=0;
sum= ((a*i)+(b*j)+(c*(i*j))+d);
return sum;
}

void runge(void)
{
float x0,y0,h,k1,k2,k3,k4,k,y1;
float x,y,z,p,i,j,q;
cout<<"\n enter the differential equation ";
function1();
cout<<"\n enter the initial value of x,y and h";
cin>>x0>>y0>>h;

x=value1(x0,y0);
k1=h*x;
y=value1((x0+h),(y0+k1));
k2=h*y;
z=value1((x0+h),(y0+k2));
k3=h*z;
i=(h/2);
j=(k1/2);
p=value1((x0+i),(y0+j));
k4=h*p;
q=(k1+(4*k4)+k3);
cout<<"\n"<<q;
k=(0.16666*q);
cout<<"\n"<<k;
y1=y0+k;
cout<<"\n the value is given as"<<y1;

float value2(float j)
{
float X,Y,Z;
Y=value(j);
X=(((3*a)*pow(j,2))+((2*b)*j)+c) ;
Z=(Y/X);
return Z;
}

void secant(void)
{
function();
int c=0;
float A,B,root,x1,x2;
for(int i=0;i<10;i++)
{
float A,B;
A=value(i);
B=value(++i);
if((A>0&&B<0)||(A<0&&B>0))
{
c=i-1;
}

}
cout<<"\n"<<c;
x1=c;
x2=c+1;
cout<<"\n root lie between"<<x1<<"and"<<x2;
root=(x1+x2)/2;
for(int j=0;j<10;j++)
{
float p,q;
p=value2(root);
q=root-p;
root=q;
}

cout<<"\n the root of the eqution is given by"<<root;


}

void gauss(void)
{
float a[3][4];
cout<<"\n enter the x,y,z and const of the first equation";
cin>>a[0][0]>>a[0][1]>>a[0][2]>>a[0][3];
cout<<"\n enter the x,y,z and const of the second equation";
cin>>a[1][0]>>a[1][1]>>a[1][2]>>a[1][3];
cout<<"\n enter the x,y,z and const of the third equation";
cin>>a[2][0]>>a[2][1]>>a[2][2]>>a[2][3];

float coff;
coff=(a[1][0]/a[0][0]);
for(int j=0;j<4;j++)
{
a[1][j]=(a[1][j]-(coff*a[0][j]));
}

coff=(a[2][0]/a[0][0]);
for(int j=0;j<4;j++)
{
a[2][j]=(a[2][j]-(coff*a[0][j]));
}

coff=(a[2][1]/a[1][1]);
for(int j=0;j<4;j++)
{
a[2][j]=(a[2][j]-(coff*a[1][j]));
}

float x,y,z;
z=a[2][3]/a[2][2];
y=(a[1][3]-(z*a[1][2]))/a[1][1];
x=(a[0][3]-(z*a[0][2])-(y*a[0][1]))/a[0][0];

cout<<"\nthe value ofx\t"<<x<<"\nthe value of y\t"<<y;


cout<<"\nthe value of z\t"<<z;

You might also like