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

Click For Summary
SUMMARY

The discussion focuses on a C++ implementation of the secant method to find the roots of the polynomial function 2x³ - 6x² + 3x + 1. The user reports that the program successfully identifies two roots but fails to find the third. It is concluded that the sensitivity of the secant method to the initial interval selection is a critical factor, and suggestions are made to try smaller intervals such as [-1,0], [0,1], and [1,3] to capture all roots effectively.

PREREQUISITES
  • Understanding of the secant method for root-finding
  • Familiarity with C++ programming, specifically with functions and loops
  • Knowledge of polynomial functions and their properties
  • Experience with numerical methods and error analysis
NEXT STEPS
  • Implement smaller initial intervals in the secant method to capture all roots
  • Explore the use of derivative-based methods like Newton's method for root-finding
  • Learn about error handling and optimization techniques in C++ for numerical algorithms
  • Study the behavior of polynomial functions and their roots using graphing tools
USEFUL FOR

C++ developers, mathematicians, and students interested in numerical methods for solving polynomial equations, particularly those working with root-finding algorithms.

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].
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
9K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 23 ·
Replies
23
Views
3K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 14 ·
Replies
14
Views
5K