C Plus Plus Code PDF
C Plus Plus Code PDF
C Plus Plus Code PDF
#include<iostream>
#include<iomanip>
#include<math.h>
/*
Defining equation to be solved.
Change this equation to solve another problem.
*/
int main()
{
/* Declaring required variables */
float x0, x1, x, f0, f1, f, e;
int step = 1;
/* Inputs */
up:
cout<<"Enter first guess: ";
cin>>x0;
cout<<"Enter second guess: ";
cin>>x1;
cout<<"Enter tolerable error: ";
cin>>e;
if( f0 * f < 0)
{
x1 = x;
}
else
{
x0 = x;
}
step = step + 1;
}while(fabs(f)>e);
return 0;
}
/*
C++ code for Newton Raphson (NR) Method
for different functions that cannot be solved analytically
you can use it for any function based on your interest
3. C++ Program for Newton Raphson (NR) Method
Program: Finding real roots of nonlinear
equation using Newton Raphson Method
Author: CodeSansar
Date: November 18, 2018
*/
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
int main()
{
float x0, x1, f0, f1, g0, e;
int step = 1, N;
/* Inputs */
cout<<"Enter initial guess: ";
cin>>x0;
cout<<"Enter tolerable error: ";
cin>>e;
cout<<"Enter maximum iteration: ";
cin>>N;
x1 = x0 - f0/g0;
step = step+1;
if(step > N)
{
cout<<"Not Convergent.";
exit(0);
}
f1 = f(x1);
}while(fabs(f1)>e);
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;
/* Inputs */
cout<<"Enter first guess: ";
cin>>x0;
cout<<"Enter second guess: ";
cin>>x1;
cout<<"Enter tolerable error: ";
cin>>e;
cout<<"Enter maximum iteration: ";
cin>>N;
x0 = x1;
f0 = f1;
x1 = x2;
f1 = f2;
step = step + 1;
if(step > N)
{
cout<<"Not Convergent.";
exit(0);
}
}while(fabs(f2)>e);
return 0;
}