Object Oriented
Programming
C++
Assignments
INDEX
Sr. No. Title Date Sign Grade
1 Bisection Method
2 Regula-Falsi Method
3 Secant Method
4 Newton-Raphson Method
5 Gauss-Jordan Method
6 Gauss-Elimination Method
7 Method of Least Squares
8 Euler’s Method
9 Runge-Kutta Method
BISECTION METHOD
Program:
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
float f(float x)
{
return (x*sin(x)-1);
}
void bisect(float *x,float a,float b,int *itr)
{
*x=(a+b)/2;
++(*itr);
cout<<"Iteration No."<<setw(3)<<*itr<<"x="<<setw(7)
<<setprecision(5)<<*x<<endl;
}
int main()
{
int itr=0,maxitr;
float x,a,b,aerr,x1;
cout<<"Enter the value of a=t";
cin>>a;
cout<<"Enter the value of b=t";
cin>>b;
cout<<"Enter the Value of allowed error=t";
cin>>aerr;
cout<<"Enter the value of maximum iterations=t";
cin>>maxitr;
bisect(&x,a,b,&itr);
do
{
if(f(a)*f(x)<0)
b=x;
else
a=x;
bisect(&x1,a,b,&itr);
if(fabs(x1-x)<aerr)
{
cout<<"After "<<itr<<" itrations, Our Desired root is"
<<"="<<setw(6)<<setprecision(4)<<x1<<endl;
return 0;
}
x=x1;
}
while(itr<maxitr);
cout<<"Solution does not converge"<<"iteration not sufficient"
<<endl;
return 1;
}
Output:
REGULA-FALSI METHOD
Program:
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
float f(float x)
{
return x*x*x-x-5;
}
void regula(float *x,float x0,float x1,float fx0,float fx1, int *itr)
{
*x=x0-((x1-x0)/(fx1-fx0))*fx0;
++(*itr);
cout<<"Iteration no."<<setw(3)<<*itr<<"x="<<setw(7)
<<setprecision(5)<<*x<<endl;
}
int main()
{
int itr=0,maxitr;
float x0,x1,x2,x3,aerr;
cout<<"Enter the value of x0=t";
cin>>x0;
cout<<"Enter the value of x1=t";
cin>>x1;
cout<<"Enter the value of allowed error=t";
cin>>aerr;
cout<<"Enter the value of maximum iteration=t";
cin>>maxitr;
regula(&x2,x0,x1,f(x0),f(x1),&itr);
do
{
if(f(x0)*f(x2)<0)
x1=x2;
else
x0=x2;
regula(&x3,x0,x1,f(x0),f(x1),&itr);
if(fabs(x3-x2)<aerr)
{
cout<<"After "<<itr<<" iterations,"<<"tOur desired root is="
<<setw(6)<<setprecision(4)<<x3<<endl;
return 0;
}
x2=x3;
}
while(itr<maxitr);
cout<<"Solution does not converge,"
<<"iteration not sufficient"<<endl;
return 1;
}
Output:
SECANT METHOD
Program:
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
float f(float x)
{
return x*x*x-exp(x);
}
void secant(float *x,float x0,float x1,float fx0,float fx1, int *itr)
{
*x=x1-((x1-x0)/(fx1-fx0))*fx1;
++(*itr);
cout<<"Iteration no."<<setw(3)<<*itr<<"x="<<setw(7)
<<setprecision(5)<<*x<<endl;
}
int main()
{
int itr=0,maxitr;
float x0,x1,x2,x3,aerr;
cout<<"Enter the value of x0=t";
cin>>x0;
cout<<"Enter the value of x1=t";
cin>>x1;
cout<<"Enter the value of allowed errors=t";
cin>>aerr;
cout<<"Enter the value of maximum iterations=t";
cin>>maxitr;
secant(&x2,x0,x1,f(x0),f(x1),&itr);
do
{
x0=x1;
x1=x2;
secant(&x3,x0,x1,f(x0),f(x1),&itr);
if(fabs(x3-x2)<aerr)
{
cout<<"After "<<itr<<" iterations, Our desired root is="
<<setw(6)<<setprecision(4)<<x3<<endl;
return 0;
}
x2=x3;
}
while(itr<maxitr);
cout<<"Solution does not converge,"
<<"iterations not sufficient"<<endl;
return 1;
}
Output:
NEWTON RAPHSON METHOD
Program:
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
float f(float x)
{
return cos(x)-x*x;
}
float df(float x)
{
return -sin(x)-2*x;
}
int main()
{
int itr,maxitr;
float h,x0,x1,aerr;
cout<<"Enter the value of x0=t";
cin>>x0;
cout<<"Enter the value of allowed error=t";
cin>>aerr;
cout<<"Enter the value of maximum iterations=t";
cin>>maxitr;
for(itr=1;itr<=maxitr;itr++)
{
h=f(x0)/df(x0);
x1=x0-h;
cout<<"Iteration no."<<setw(3)<<itr<<"x="<<setw(9)
<<setprecision(6)<<x1<<endl;
if(fabs(h)<aerr)
{
cout<<"After no. "<<setw(3)<<itr
<<" iterations,tOur desired root is="
<<setw(8)<<setprecision(6)<<x1;
return 0;
}
x0=x1;
}
cout<<"Iterations not sufficient,"
<<"Solution does not converge"<<endl;
return 1;
}
Output:
GAUSS-JORDAN METHOD
Program:
#include<iostream.h>
#include<iomanip.h>
#define N 3
int main()
{
float a[N][N+1],t;
int i,j,k;
cout<<"Enter the matrix elements row wise:-n";
for(i=0;i<N;i++)
{
for(j=0;j<N+1;j++)
{
cin>>a[i][j];
}
}
for(j=0;j<N;j++)
{
for(i=0;i<N;i++)
{
if(i!=j)
{
t=a[i][j]/a[j][j];
for(k=0;k<N+1;k++)
{
a[i][k]-=a[j][k]*t;
}
}
}
}
cout<<"The diagonal matrix is:- n";
for(i=0;i<N;i++)
{
for(j=0;j<N+1;j++)
{
cout<<setw(15)<<setprecision(4)<<a[i][j];
}
cout<<"n";
}
cout<<"n";
cout<<"The solution is:-n";
for(i=0;i<N;i++)
{
cout<<"x["<<i+1<<"]="<<setw(7)<<setprecision(3)
<<a[i][N]/a[i][i]<<endl;
}
return 0;
}
Output:
GAUSS ELIMINATION METHOD
Program:
#include<iostream.h>
#include<iomanip.h>
#include<math.h>
#define N 3
int main()
{
float a[N][N+1],x[N],t,s;
int i,j,k;
cout<<"Enter the matrix elements row wise:-n";
for(i=0;i<N;i++)
{
for(j=0;j<N+1;j++)
{
cin>>a[i][j];
}
}
for(j=0;j<N-1;j++)
{
for(i=j+1;i<N;i++)
{
t=a[i][j]/a[j][j];
for(k=0;k<N+1;k++)
{
a[i][k]-=a[j][k]*t;
}
}
}
cout<<"The upper triangular matrix is:- n";
for(i=0;i<N;i++)
{
for(j=0;j<N+1;j++)
{
cout<<setw(15)<<setprecision(4)<<a[i][j];
}
cout<<"n";
}
for(i=N-1;i>=0;i--)
{
s=0;
{
for(j=i+1;j<N;j++)
{
s+=a[i][j]*x[j];
}
x[i]=(a[i][N]-s)/a[i][i];
}
}
cout<<"n";
cout<<"The solution is:-n";
for(i=0;i<N;i++)
{
cout<<"x["<<i+1<<"]="<<setw(7)<<setprecision(3)<<x[i]<<endl;
}
return 0;
}
Output:
METHOD OF LEAST SQUARES
Program:
#include<iostream.h>
#include<iomanip.h>
int main()
{
float mat[3][4]={{0,0,0,0},{0,0,0,0},{0,0,0,0}};
float t,a,b,c,x,y,xsq;
int i,j,k,n;
cout<<"Enter the no. of pairs of observed values:";
cin>>n;
mat[0][0]=n;
for(i=0;i<n;i++)
{
cout<<"Pair no.:"<<i+1<<"n";
cin>>x>>y;
xsq=x*x;
mat[0][1]+=x;
mat[0][2]+=xsq;
mat[1][2]+=x*xsq;
mat[2][2]+=xsq*xsq;
mat[0][3]+=y;
mat[1][3]+=x*y;
mat[2][3]+=xsq*y;
}
mat[1][1]=mat[0][2];
mat[2][1]=mat[1][2];
mat[1][0]=mat[0][1];
mat[2][0]=mat[1][1];
cout<<"The Matrix is:-n";
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
cout<<setw(10)<<setprecision(4)<<mat[i][j];
}
cout<<"n";
}
for(j=0;j<3;j++)
{
for(i=0;i<3;i++)
{
if(i!=j)
{
t=mat[i][j]/mat[j][j];
for(k=0;k<4;k++)
{
mat[i][k]-=mat[j][k]*t;
}
}
}
}
a=mat[0][3]/mat[0][0];
b=mat[1][3]/mat[1][1];
c=mat[2][3]/mat[2][2];
cout<<setprecision(4)
<<"a="<<setw(5)<<a<<"n"
<<"b="<<setw(5)<<b<<"n"
<<"c="<<setw(5)<<c<<"n";
return 0;
}
Output:
EULER’S METHOD
Program:
#include<iostream.h>
#include<iomanip.h>
float df(float x,float y)
{
return (y-x)/(y+x);
}
int main()
{
float x0,y0,h,x,x1,y1;
cout<<"Enter the values of x0,y0,h,x:n";
cin>>x0>>y0>>h>>x;
x1=x0;
y1=y0;
while(1)
{
if(x1>x)
{
return 0;
}
y1+=h*df(x1,y1);
x1+=h;
cout<<"when x="<<setw(3)<<setprecision(2)<<x1<<"t"
<<"y="<<setw(3)<<setprecision(4)<<y1<<"n";
}
}
Output:
RUNGE-KUTTA METHOD
Program:
#include<iostream.h>
#include<iomanip.h>
float f(float x,float y)
{
return x+y*y;
}
int main()
{
float x0,y0,h,xn,x,y,k1,k2,k3,k4,k;
cout<<"Enter the values of x0,y0,h,xn:n";
cin>>x0>>y0>>h>>xn;
x=x0;
y=y0;
while(1)
{
if(x==xn)
{
break;
}
k1=h*f(x,y);
k2=h*f(x+h/2,y+k1/2);
k3=h*f(x+h/2,y+k2/2);
k4=h*f(x+h,y+k3);
k=(k1+(k2+k3)*2+k4)/6;
x+=h;
y+=k;
cout<<"When x="<<setprecision(4)<<setw(4)<<x<<setw(8)
<<"y="<<setprecision(4)<<setw(4)<<y<<endl;
}
return 0;
}
Output:

Numerical Methods with Computer Programming