In: Computer Science
Write a C++ program for all the methods (Bisection, Newton-Raphson, Secant, False-Position, and Modified Secant) for locating roots. Make sure that you have clever checks in your program to be warned and stop if you have a divergent solution or stop if the solution is very slowly convergent after a maximum number of iterations.
PLEASE GIVE IT A THUMBS UP, I SERIOUSLY NEED ONE, IF YOU NEED ANY MODIFICATION THEN LET ME KNOW, I WILL DO IT FOR YOU
1. Bisection Method
#include<bits/stdc++.h>
using namespace std;
#define EPSILON 0.01
double func(double x)
{
return 2 * x * x * x - 11.7 * x * x + 2 + 17.7 * x - 5;
}
// Prints root of func(x) with error of EPSILON
void bisection(double a, double b)
{
if (func(a) * func(b) >= 0)
{
cout << "You have not assumed right a and b\n";
return;
}
double c = a;
while ((b - a) >= EPSILON)
{
c = (a + b) / 2;
if (func(c) == 0.0)
break;
else if (func(c)*func(a) < 0)
b = c;
else
a = c;
}
cout << "The value of root is : " << c;
}
int main()
{
double a = 0 , b = 4;
bisection(a, b);
return 0;
}
2. Newton Raphson Method
#include<bits/stdc++.h>
using namespace std;
#define EPSILON 0.01
double func(double x)
{
return 2 * x * x * x - 11.7 * x * x + 17.7 * x - 5;
}
// Prints root of func(x) with error of EPSILON
double derivFunc(double x) {
6 * x * x - 23.14 * x + 17.7;
}
void newtonRaphson(double x)
{
double h = func(x) / derivFunc(x);
while (abs(h) >= EPSILON)
{
h = func(x) / derivFunc(x);
// x(i+1) = x(i) - f(x) / f'(x)
x = x - h;
}
cout << "The value of the root is : " << x;
}
int main()
{
double x0 = -20; // Initial values assumed
newtonRaphson(x0);
return 0;
}
3.Secant Method
#include<bits/stdc++.h>
using namespace std;
const int E = 0.01;
double f(double x)
{
return 2 * x * x * x - 11.7 * x * x + 17.7 * x - 5;
}
void secant(float x1, float x2, float E)
{
float n = 0, xm, x0, c;
if (f(x1) * f(x2) < 0) {
do {
x0 = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
c = f(x1) * f(x0);
x1 = x2;
x2 = x0;
n++;
if (c == 0)
break;
xm = (x1 * f(x2) - x2 * f(x1)) / (f(x2) - f(x1));
} while (fabs(xm - x0) >= E && n <= 100);
cout << "Root of the given equation=" << x0 << endl;
cout << "No. of iterations = " << n << endl;
} else
cout << "Can not find a root in the given inteval";
}
// Driver code
int main()
{
// initializing the values
float x1 = 0, x2 = 1, E = 0.0001;
secant(x1, x2, E);
return 0;
}
4. False-Position Method
#include<bits/stdc++.h>
using namespace std;
const int E = 0.01;
const int MAX_ITER = 100;
double func(double x)
{
return 2 * x * x * x - 11.7 * x * x + 17.7 * x - 5;
}
void regulaFalsi(double a, double b)
{
if (func(a) * func(b) >= 0)
{
cout << "You have not assumed right a and b\n";
return;
}
double c = a; // Initialize result
for (int i = 0; i < MAX_ITER; i++)
{
// Find the point that touches x axis
c = (a * func(b) - b * func(a)) / (func(b) - func(a));
// Check if the above found point is root
if (func(c) == 0)
break;
// Decide the side to repeat the steps
else if (func(c)*func(a) < 0)
b = c;
else
a = c;
}
cout << "The value of root is : " << c;
}
// Driver program to test above function
int main()
{
// Initial values assumed
double a = 0, b = 4;
regulaFalsi(a, b);
return 0;
}