Professional Documents
Culture Documents
CM Experiment 3
CM Experiment 3
Objective: To write a C++ program to find the root of the equation: x*x*x – 2*x - 5
using Secant’s Method.
Algorithm:
1. Start
2. Get values of x0, x1 and ε
3. Compute f(x0) and f(x1)
4. Compute x2 = [x0*f(x1) – x1*f(x0)] / [f(x1) – f(x0)]
5. Test for accuracy of x2
If [ (x2 – x1) / x2 ] > ε, *Here [ ] is used as modulus sign*
then assign x0 = x1 and x1 = x2
go to step 4.
Else,
go to step 6.
6. Display the required root as x2.
7. Stop.
Practical Code:
#include<iostream>
#include<iomanip>
#include<math.h>
#include<stdlib.h>
int main ()
{
float x0, x1, x2, f0, f1, f2, e;
int step = 1, N;
cout<< setprecision(6)<< fixed;
do
{
f0 = f(x0);
f1 = f(x1);
if(f0 == f1)
{
cout<<"Mathematical Error.";
exit(0);
}
step = step + 1;
if(step > N)
{
cout<<"Not Convergent.";
exit(0);
}
}
while(fabs(f2)>e);
return 0;
}
Result / Output:
Applications:
1. This method is used to find the real root of mathematical transcendental
equations which involve logarithms, exponentials, trigonometric and
inverse trigonometric functions and algebraic equations.