#include <iostream>
using namespace std;
class solveForRoot
{
public:
solveForRoot();
void askQuadEqn();
void askPoints();
double f(double x);
void findX2();
void findError();
void solve();
void display();
private:
double x0,x1,x2,Error,a,b,c;
};
solveForRoot::solveForRoot()
{
Error = 99;
}
void solveForRoot::askQuadEqn()
{
cout.setf(ios::fixed,ios::floatfield);
cout.precision(4);
cout << "Enter a,b,c of ax^2+bx+c = 0: " ;
cin >> a >> b >> c;
}
void solveForRoot::askPoints()
{
cout << "Enter two points: " ;
cin >> x0 >> x1;
}
double solveForRoot::f(double x)
{
return a*x*x+b*x+c; // solves all equation of the form a*x*x+b*x+c;
}
void solveForRoot::findX2()
{
x2 = x1 - (f(x1)*(x1-x0))/(f(x1)-f(x0));
}
void solveForRoot::findError()
{
Error = (x2-x1)/x2;
if(Error < 0)
Error = -Error;
}
void solveForRoot::solve()
{
cout << "x0\t" << "x1\t" << "x2\t" << "f(x0)\t" << "f(x1)\t" << "f(x2)\t" <<
"Error\t" << endl;
while(Error >= 0.00009)
{
findX2();
findError();
display();
x0 = x1;
x1 = x2;
}
cout << endl << endl;
cout << "The value of root is : " << x2 << endl;
}
void solveForRoot::display()
{
cout << x0 << "\t"<< x1 << "\t" << x2 << "\t" << f(x0) << "\t" << f(x1)
<< "\t" << f(x2)<< "\t" << Error << endl;
}
int main()
{
solveForRoot no1;
no1.askQuadEqn();
no1.askPoints();
no1.solve();
return 0;
}
C and C++ programs, games, softwares. Easy programming guide. Learn to code and enjoy coding in CoderNepal
Home » C++ Program code for Secant Method
C++ Program code for Secant Method
This is the solution for Secant Method using C++ Object Oriented Aproach.
Subscribe to:
Comments (Atom)
No comments:
Post a Comment