Professional Documents
Culture Documents
Example
Live Demo
#include <iostream>
using namespace std;
#define EP 0.01
// An example function whose solution is determined using
// Bisection Method. The function is x^3 - x^2 + 2
double solution(double x) {
return x*x*x - x*x + 2;
}
// Prints root of solution(x) with error in EPSILON
void bisection(double a, double b) {
if (solution(a) * solution(b) >= 0) {
cout << "You have not assumed right a and b\n";
return;
}
double c = a;
while ((b-a) >= EP) {
// Find middle point
c = (a+b)/2;
// Check if middle point is root
if (solution(c) == 0.0)
break;
// Decide the side to repeat the steps
else if (solution(c)*solution(a) < 0)
b = c;
else
a = c;
}
cout << "The value of root is : " << c;
}
// main function
int main() {
double a =-500, b = 100;
bisection(a, b);
return 0;
}
Output
The value of root is : -0.991821
/*
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;
f1 = f;
}
else
{
x0 = x;
f0 = f;
}
step = step + 1;
}while(fabs(f)>e);
return 0;
}
*********************
False Position Method
*********************
Iteration-1: x = 0.314665 and f(x) = 0.519871
Iteration-2: x = 0.446728 and f(x) = 0.203545
Iteration-3: x = 0.494015 and f(x) = 0.070802
Iteration-4: x = 0.509946 and f(x) = 0.023608
Iteration-5: x = 0.515201 and f(x) = 0.007760
Iteration-6: x = 0.516922 and f(x) = 0.002539
Iteration-7: x = 0.517485 and f(x) = 0.000829
Iteration-8: x = 0.517668 and f(x) = 0.000271
Iteration-9: x = 0.517728 and f(x) = 0.000088
Iteration-10: x = 0.517748 and f(x) = 0.000029
Iteration-11: x = 0.517754 and f(x) = 0.000009
Program
int main()
{
int step=1, N;
float x0, x1, e;
step = step + 1;
if(step>N)
{
cout<<"Not Convergent.";
exit(0);
}
x0 = x1;
Output
**************************
Fixed Point Iteration Method
**************************
Iteration-1: x1 = 0.513434 and f(x1) = 0.330761
Iteration-2: x1 = 0.623688 and f(x1) = -0.059333
Iteration-3: x1 = 0.603910 and f(x1) = 0.011391
Iteration-4: x1 = 0.607707 and f(x1) = -0.002162
Iteration-5: x1 = 0.606986 and f(x1) = 0.000411
Iteration-6: x1 = 0.607124 and f(x1) = -0.000078
Iteration-7: x1 = 0.607098 and f(x1) = 0.000015
Iteration-8: x1 = 0.607102 and f(x1) = -0.000003
Iteration-9: x1 = 0.607102 and f(x1) = 0.000001
Root is 0.607102
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);
*********************
Newton Raphson Method
*********************
Iteration-1: x1 = 0.614547 and f(x1) = 0.026607
Iteration-2: x1 = 0.607108 and f(x1) = 0.000023
Iteration-3: x1 = 0.607102 and f(x1) = -0.000000
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
int main()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, 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;
}