Why Does My C++ Secant Method Program Only Find Two Roots?

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
1 reply · 28K views
2slowtogofast
Messages
134
Reaction score
1
so i worte this and it works somtimes. the function I am trying to find the roots of is

2x^3 - 6x^2 + 3x + 1

the program i wrote will find two of the roots but i can't get the third. I know the secant method it sensitive to the interval that you pick so i was thinking that was the problem. But i have tried all different intervals. here's the code and output. If someone could look this over and point me in the direction of where my mistake is i would appreciate it.

**CODE**

#include <iostream>
#include <cmath>
#include <iomanip>
using namespace std;
double f(double);
void secant(double, double,bool&,double& , int&);

void main()
{

char ch;
double xg,xg2,rt ;
bool good;
int ct,answer;
do{
count<<" Enter an intial guess ";
cin >> xg;
count<<" Enter another intial guess ";
cin >> xg2;
secant(xg,xg2,good,rt,ct);
count<< setiosflags(ios::fixed)<<setprecision(5);
if (good)
{
count<<"Staring with an intial interval of" <<xg<<" and "<<xg2<< endl;
count<<"The root of the function is" <<rt<< endl;
count<<"It took " <<ct<<" tries to find it"<< endl;


}
count<<" Enter 1 to find another root";
cin>>answer;
}
while(answer <= 1);
cin>>ch;
}

void secant (double x0,double x7,bool&valid,double&root,int&count)
{
double xl;
double const maxerror = 0.000001;
int const maxcount = 100;

xl = ((x7*f(x0)) - (x0*f(x7))) / (f(x0) - f(x7));
count = 1;
valid = false;
while (fabs (x0-xl) > maxerror && count <= maxcount && valid == false)
{
x0 = xl;

if (f(x0)-f(x7)==0)

x0+=0.1;

xl = ((x7*f(x0)) - (x0*f(x7))) / (f(x0) - f(x7));
count ++;


if (f(xl)==0)
{
valid = true;
root = xl;
}
}
if (fabs(x0-xl)<=maxerror)
{
root=xl;
valid=true;
}


}

double f(double x)
{
return 2*pow(x,3)-6*pow(x,2)+3*x+1;

}



***OUTPUT***

Enter an intial guess -9
Enter another intial guess 4
Staring with an intial interval of-9.00000 and 4.00000
The root of the function is2.22475
It took 46 tries to find it
Enter 1 to find another root1
Enter an intial guess -2
Enter another intial guess 7
Staring with an intial interval of-2.00000 and 7.00000
The root of the function is-0.22475
It took 97 tries to find it
Enter 1 to find another root1
Enter an intial guess 0
Enter another intial guess 100
Enter 1 to find another root1
Enter an intial guess -1
Enter another intial guess 100
Enter 1 to find another root1
Enter an intial guess -100
Enter another intial guess 0
Staring with an intial interval of-100.00000 and 0.00000
The root of the function is-0.22474
It took 15 tries to find it
Enter 1 to find another root

the code and output are pretty messy right now I am just trying to get it to work and then I am going to neaten things up. If anyone takes the time to read through all this i thank you.
 
Physics news on Phys.org
I didn't read through your code, but the function you picked has all 3 of its roots very close to the origin. Trying smaller intervals like [-1,0], [0,1], and [1,3].