SlideShare a Scribd company logo
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:

More Related Content

PDF
Computer Oriented Numerical Methods Practical File
PPTX
Recursion in c++
PDF
Numerical Computing
PPTX
Procedural programming
PDF
Operator Overloading in C++
PPT
Asymptotic notation
PPTX
Stream classes in C++
PPT
Chapter 2 - Beginning the Problem-Solving Process
Computer Oriented Numerical Methods Practical File
Recursion in c++
Numerical Computing
Procedural programming
Operator Overloading in C++
Asymptotic notation
Stream classes in C++
Chapter 2 - Beginning the Problem-Solving Process

What's hot (20)

PPT
Chapter3 Search
PDF
Elliptic Curve Cryptography
PPT
introduction to Numerical Analysis
PDF
Rules of inference
PPT
Numerical method
PPT
3 Centrality
PDF
סיכום הקורס בבינה מלאכותית
PPTX
Open addressiing &amp;rehashing,extendiblevhashing
PDF
numerical differentiation&integration
PPTX
GCD of n Numbers
PPT
Chap4
PDF
Notes
PPT
Introduction to Functions of Several Variables
PDF
Lecture 5 6_7 - divide and conquer and method of solving recurrences
PPTX
Error Finding in Numerical method
PPTX
Indefinite Integral
PPT
Minimum spanning tree
PPTX
Error analysis
PPTX
The gamma function
Chapter3 Search
Elliptic Curve Cryptography
introduction to Numerical Analysis
Rules of inference
Numerical method
3 Centrality
סיכום הקורס בבינה מלאכותית
Open addressiing &amp;rehashing,extendiblevhashing
numerical differentiation&integration
GCD of n Numbers
Chap4
Notes
Introduction to Functions of Several Variables
Lecture 5 6_7 - divide and conquer and method of solving recurrences
Error Finding in Numerical method
Indefinite Integral
Minimum spanning tree
Error analysis
The gamma function
Ad

Similar to Numerical Methods with Computer Programming (20)

PDF
C++ TUTORIAL 6
PDF
C++ TUTORIAL 9
PDF
C++ TUTORIAL 7
DOCX
Matlab lab manual
PPTX
Finding root of equation (numarical method)
PDF
C++ TUTORIAL 10
DOCX
Trabajo Scilab
DOCX
MolinaLeydi_FinalProject
PPTX
First part of my NYU Tandon course "Numerical and Simulation Techniques in Fi...
DOCX
scientific computing
PDF
C language numanal
PDF
NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...
PPTX
Ee 3122 numerical methods and statistics sessional credit
PDF
PDF
Numerical Study of Some Iterative Methods for Solving Nonlinear Equations
PDF
Numerical methods course project report
PDF
Numerical Analysis
PPTX
Root Finding Methods in Numerical Analysis
DOC
C++ TUTORIAL 6
C++ TUTORIAL 9
C++ TUTORIAL 7
Matlab lab manual
Finding root of equation (numarical method)
C++ TUTORIAL 10
Trabajo Scilab
MolinaLeydi_FinalProject
First part of my NYU Tandon course "Numerical and Simulation Techniques in Fi...
scientific computing
C language numanal
NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false poin...
Ee 3122 numerical methods and statistics sessional credit
Numerical Study of Some Iterative Methods for Solving Nonlinear Equations
Numerical methods course project report
Numerical Analysis
Root Finding Methods in Numerical Analysis
Ad

More from Utsav Patel (14)

PPTX
Geometric Dimensioning and Tolerancing
PPTX
Energy Production from Slow Moving Water
PPTX
Geothermal Energy Power Plant
PPTX
The Himalayas Mountain Range - Breathtaking Beauty
PPTX
Gearless Power Transmission - Minor Project
PDF
Electricity Production from Slow Moving Water - Poster Presentation
PDF
Static and Fatigue Analysis of Pressure Vessel as per ASME Codes
PDF
ONGC Summer Training Report
PDF
Gearbox Design Data with Drawings
PDF
Heat and Mass Transfer Practical Manual (C Coded)
PDF
Design of Multi-plate clutch Report
PDF
Geothermal Energy Power Plant Report
PDF
Gearless Power Transmission - Project Report
PDF
Ammann Apollo India Pvt Ltd - Industrial Training Report
Geometric Dimensioning and Tolerancing
Energy Production from Slow Moving Water
Geothermal Energy Power Plant
The Himalayas Mountain Range - Breathtaking Beauty
Gearless Power Transmission - Minor Project
Electricity Production from Slow Moving Water - Poster Presentation
Static and Fatigue Analysis of Pressure Vessel as per ASME Codes
ONGC Summer Training Report
Gearbox Design Data with Drawings
Heat and Mass Transfer Practical Manual (C Coded)
Design of Multi-plate clutch Report
Geothermal Energy Power Plant Report
Gearless Power Transmission - Project Report
Ammann Apollo India Pvt Ltd - Industrial Training Report

Recently uploaded (20)

PPTX
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
PPTX
Sustainable Sites - Green Building Construction
PPTX
bas. eng. economics group 4 presentation 1.pptx
PDF
Operating System & Kernel Study Guide-1 - converted.pdf
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Welding lecture in detail for understanding
PPTX
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PPTX
Lesson 3_Tessellation.pptx finite Mathematics
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
UNIT 4 Total Quality Management .pptx
PDF
Structs to JSON How Go Powers REST APIs.pdf
PDF
Arduino robotics embedded978-1-4302-3184-4.pdf
PDF
PPT on Performance Review to get promotions
PPTX
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
PDF
Digital Logic Computer Design lecture notes
PPTX
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
PPTX
CH1 Production IntroductoryConcepts.pptx
DOCX
573137875-Attendance-Management-System-original
KTU 2019 -S7-MCN 401 MODULE 2-VINAY.pptx
Sustainable Sites - Green Building Construction
bas. eng. economics group 4 presentation 1.pptx
Operating System & Kernel Study Guide-1 - converted.pdf
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Welding lecture in detail for understanding
MCN 401 KTU-2019-PPE KITS-MODULE 2.pptx
Lecture Notes Electrical Wiring System Components
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
Lesson 3_Tessellation.pptx finite Mathematics
Foundation to blockchain - A guide to Blockchain Tech
UNIT 4 Total Quality Management .pptx
Structs to JSON How Go Powers REST APIs.pdf
Arduino robotics embedded978-1-4302-3184-4.pdf
PPT on Performance Review to get promotions
FINAL REVIEW FOR COPD DIANOSIS FOR PULMONARY DISEASE.pptx
Digital Logic Computer Design lecture notes
Engineering Ethics, Safety and Environment [Autosaved] (1).pptx
CH1 Production IntroductoryConcepts.pptx
573137875-Attendance-Management-System-original

Numerical Methods with Computer Programming