You are on page 1of 17

INDEX

Serial Program Page no. Date


no.
Write a program to find the roots of an
1. equation by Bisection Method. 1

2. Write a program to find the roots of an 2


equation by Regula Falsi Method
3.
Write a program to implement Newton 3
Raphson Method

4. Write a program to implement Newton 4


Forward Difference Method

[Type here]
Output

[Type here]
1. Write a program to find the roots of an equation by Bisection Method.

#include<iostream.h>
#include<conio.h>
#include<math.h>
float f(float x)_
{
float fx1;
fx1=pow(x,3)-(2*x)-5;
return(fx1);
}
void main()
{
float x1,x2,x3;
int count=0;
int iter;
clrscr();
cout<<"Enter x1=";
cin>>x1;
cout<<"Enter x2=";
cin>>x2;
cout<<"Enter number of iterations=";
cin>>iter;
do
{
if(count==iter)
{
break;
x3=(x1+x2)/2;
cout<<"x1="<<x1<<"/x2="<<x2<<"/x3="<<x3<<"/"<<"f(x1)="<<f(x1)<<"/f(x2)="<<f(x2)<<"/f(x3)="<<f(x3)<
<endl<<endl;
if(f(x1)*f(x3)<0)
{
x2=x3;
}
else
{
x1=x3;
}
count++;
}
while(abs(x1-x2)<0.0000001 //f(x3)==0);
getch();
}

[Type here]
Output

[Type here]
2. Write a program to find the roots of an equation by Regula Falsi Method.

#include<iostream.h>
#include<conio.h>
#include<math.h>
float f(float x)_
{
float fx1;
fx1=pow(x,3)-(2*x)-5;
return(fx1);
}
void main()
{
float x1,x2,x3;
int count=0;
int iter;
clrscr();
cout<<"Enter x1=";
cin>>x1;
cout<<"Enter x2=";
cin>>x2;
cout<<"Enter number of iterations=";
cin>>iter;
do
{
if(count==iter)
{
break;
x3=(x1+x2)/2;
cout<<"x1="<<x1<<"/x2="<<x2<<"/x3="<<x3<<"/"<<"f(x1)="<<f(x1)<<"/f(x2)="<<f(x2)<<"/f(x3)="<<f(x3)<
<endl<<endl;
if(f(x1)*f(x3)<0)
{
x2=x3;
}
else
{
x1=x3;
}
count++;
}
while(abs(x1-x2)<0.0000001 //f(x3)==0);
getch();
}

[Type here]
OUTPUT

[Type here]
#include<iostream.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(pow(x,4)-x-10);
}
float g(float x)
{
return(4*(pow(x,3))-1);
}
void main()
{
float x0,fx,gx,x1;
int count=0;
int iter,maxmiter;
clrscr();
cout<<"enter x0=";
cin>>x0;
cout<<"enter number of iterations=";
cin>>iter >>maxmiter;
for(iter=1; iter<=maxmiter;iter++)
{
fx=f(x0);
gx=g(x0);
x1=x0-(f(x0)/g(x0));
{
cout<<"|x0="<<x0<<endl<<endl;
}
x0=x1;
}
getch();
}

[Type here]
#include<iostream.h>
#include<conio.h>
#include<math.h>
float f(float x)
{
return(pow(x,4)-x-10);
}
float g(float x)
{
return(4*(pow(x,3))-1);
}
void main()
{
float x0,fx,gx,x1;
int count=0;
int iter,maxmiter;
clrscr();
cout<<"enter x0=";
cin>>x0;
cout<<"enter number of iterations=";
cin>>iter >>maxmiter;
for(iter=1; iter<=maxmiter;iter++)
{
fx=f(x0);
gx=g(x0);
x1=x0-(f(x0)/g(x0));
{
cout<<"|x0="<<x0<<endl<<endl;
}
x0=x1;
}
getch();
}

#include<iostream.h>

[Type here]
#include<conio.h>
#include<math.h>
float f(float x)
{
return(pow(x,4)-x-10);
}
float g(float x)
{
return(4*(pow(x,3))-1);
}
void main()
{
float x0,fx,gx,x1;
int count=0;
int iter,maxmiter;
clrscr();
cout<<"enter x0=";
cin>>x0;
cout<<"enter number of iterations=";
cin>>iter >>maxmiter;
for(iter=1; iter<=maxmiter;iter++)
{
fx=f(x0);
gx=g(x0);
x1=x0-(f(x0)/g(x0));
{
cout<<"|x0="<<x0<<endl<<endl;
}
x0=x1;
}
getch();
}

[Type here]
OUTPUT

[Type here]
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int i,j,n,k;
float ax[10],ay[10],diff[20][20],x,y,p,h,y1,y2,y3,y4;
clrscr();
cout<<"enter the value of n=";
cin>>n;
cout<<"enter the value of x"<<endl;
for(i=0;i<n;i++)
cin>>ax[i];
cout<<"enter value of y"<<endl;
for(i=0;i<n;i++)
cin>>ay[i];
cout<<"enter value of x for which the value of y is wanted:"<<endl;
cin>>x;
h=ax[1]-ax[0];
for(i=0;i<n-1;i++)
diff[i][1]=ay[i+1]-ay[i];
for(j=2;j<=5;j++)
for(i=0;i<=n-j;i++)
diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
p=(x-ax[i])/h;
y1=p*diff[i+1][1];
y2=((p*(p-1))/2)*diff[i+2][2];
y3=((p*(p-1)*(p-2))/6)*diff[i+3][3];
y4=((p*(p-1)*(p-2)*(p-3))/24)*diff[i+4][4];
y=ay[i]+y1+y2+y3+y4;
cout<<"corresponding y="<<y;
getch
}

[Type here]
OUTPUT

[Type here]
#include.iostream.h>
#include.conio.h>
#include.math.h>
void main()
{
int n,i;
float x[20],y[20],h,s,s1=0.0,integral;
clrscr()
cout<<"enter no of values=";
cin>>n;
cout<<"enter values of xi and yi=";
for(i=1;i<=n;i++)
cin>>x[i]>>y[i];
h=x[2]-x[1];
s=y[1]+y[n];
for(i=2;i<=n-1;i++)
s1+=y[i];
integral=(h/2)*(s+2*s1);
cout<<"integral value="<<integral;
getch();
}

[Type here]
[Type here]
#include<iostream.h>

#include<conio.h>
#include<math.h>
void main()
{
int n,i;
floatx[20],y[20],h,s,s1=0.0,s2=0.0,integral;

clrscr();
cout<<"enter no of values=";
cin>>n;
cout<<"enter values of xi and yi=";
for(i=1;i<=n;i++)
cin>>x[i]>>y[i];
h=x[2]-x[1];
s=y[1]+y[n];
for(i=2;i<=n-1;i+=2)
s1+=y[i];
for(i=3;i<=n-2;i+=2)
s2+=y[i];
integral=(h/3)*(s+4*s1+2*s2);
cout<<"integral value="<<integral;
getch();
}

[Type here]
[Type here]
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
int n,i;
float x[20],y[20],h,s,s1=0.0,s2=0.0,integral;
clrscr();
cout<<"enter no of values=";
cin>>n;
cout<<"enter values of xi and yi=";
for(i=1;i<=n;i++)
cin>>x[i]>>y[i];
h=x[2]-x[1];
s=y[1]+y[n];
for(i=2;i<=n-1;i+=3)
s1+=y[i]+y[i+1];
for(i=4;i<=n-3;i+=3)
s2+=y[i];
integral=(3*(h/8))*(s+3*s1+2*s2);
cout<<"integral value="<<integral;
getch();
}

[Type here]

You might also like