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

AI Thread Summary
The C++ program for finding roots using the secant method successfully identifies two roots of the function 2x^3 - 6x^2 + 3x + 1 but fails to find the third root. The user suspects that the issue lies in the choice of intervals, as the secant method is sensitive to these selections. Suggestions include using smaller intervals around the origin, such as [-1,0], [0,1], and [1,3], since all three roots are close to this area. The code is still in a rough state, with plans for further refinement after achieving functionality. Adjusting the interval strategy may help in locating the elusive third root.
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{
cout<<" Enter an intial guess ";
cin >> xg;
cout<<" Enter another intial guess ";
cin >> xg2;
secant(xg,xg2,good,rt,ct);
cout<< setiosflags(ios::fixed)<<setprecision(5);
if (good)
{
cout<<"Staring with an intial interval of" <<xg<<" and "<<xg2<< endl;
cout<<"The root of the function is" <<rt<< endl;
cout<<"It took " <<ct<<" tries to find it"<< endl;


}
cout<<" 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].
 

Similar threads

Replies
3
Views
1K
Replies
6
Views
3K
Replies
8
Views
8K
Replies
13
Views
2K
Replies
23
Views
3K
Replies
5
Views
2K
Replies
3
Views
2K
Back
Top