Attached below is the C++ code I wrote to find four roots of a polynomial equation using Newton's Method. using the template given down below (photo) please convert the code that used the Newtons Method into the Secant Method following the template of the picture provied.
#include
#include
using namespace std;
int main()
{
double x, d, fx, fpx;
double xtol = 0.000001, ftol = 0.000001;
int nmax = 100;
char answer;
cout << "This program finds roots of f(x)=-x^4+20x^2-4x-50 using Newton method" << endl;
cout << " Do you want to find first ROOT? Y or N ";
cin >> answer;
while (answer == 'Y')
{
cout << "Enter inital x values :";
cin >> x;
cout << setw(6) << "n" << setw(10) << "x" << setw(12) << "f(x)" << endl;
for (int n = 1;n <= nmax;n++)
{
fx = -pow(x, 4) + 20 * pow(x, 2) - 4 * x - 50;
fpx = -4 * pow(x, 3) + 40 * x - 4;
d = fx / fpx;
x = x - d;
cout << setw(6) << n << setw(10) << setprecision(4)<< fixed<< x << setw(12) << setprecision(5)<
if (abs(d) <= xtol || abs(fx) <= ftol)
{
cout << "Root is :" << x << endl;
break;
}
}
cout << "No roots in this interval" << endl;
cout << " Do you want to find next ROOT? Y or N ";
cin >> answer;
}
return 0;
}
Need to code follow this C++ template