This document contains summaries of 9 numerical methods programs written in C++ including: Bisection Method, Regula-Falsi Method, Secant Method, Newton-Raphson Method, Gauss-Jordan Method, Gauss Elimination Method, Method of Least Squares, Euler's Method, and Runge-Kutta Method. For each method, the document provides the program code, sample output, and a brief 1-2 sentence description.
2. 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
3. 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;
4. 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;
}
6. 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;
7. 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;
}
9. 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;
10. 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;
}
12. 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++)