C++ program for secant method

In summary, the conversation is about the difficulties the speaker is having in finding all three roots of a given function using the secant method. They have tried different initial guesses and intervals, but have not been successful in finding the third root. The code and output are not neat at the moment, but the speaker is grateful for anyone who can help them find their mistake.
  • #1
2slowtogofast
135
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
  • #2
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].
 
  • #3


Dear scientist,

Thank you for sharing your C++ program for the secant method and your experience using it to find the roots of the function 2x^3 - 6x^2 + 3x + 1. It seems that your program is working for some initial intervals but not for others. I agree with your assessment that the sensitivity of the secant method to the initial interval may be causing the problem.

One suggestion I have is to try using a different initial interval, perhaps one that is closer to the root you are trying to find. You could also try increasing the number of iterations in your while loop to see if that helps find the third root.

Another potential issue could be with the calculation of the function value at a given point. It may be helpful to use a debugger to check the values of your variables and make sure they are being calculated correctly.

Overall, I commend your efforts in writing this program and I encourage you to continue troubleshooting and experimenting with different inputs to find the root. Good luck!
 

1. What is the secant method in C++?

The secant method is a numerical method used for finding the root of a mathematical function. It is an iterative process that uses a series of approximations to eventually converge on the root of the function. In C++, the secant method is implemented using a loop structure and mathematical calculations.

2. How does the secant method differ from other root-finding methods in C++?

The secant method differs from other root-finding methods, such as the bisection method or Newton's method, in that it does not require the evaluation of the function's derivative. Instead, it uses two initial guesses and a formula to calculate a new approximation for the root in each iteration.

3. What are the advantages of using the secant method in a C++ program?

One advantage of using the secant method in a C++ program is that it can be more efficient than other methods for finding roots, especially for functions that are not well-behaved or have multiple roots. It also does not require knowledge of the function's derivative, making it a more versatile option for a variety of functions.

4. How do you implement the secant method in a C++ program?

To implement the secant method in a C++ program, you first need to define the function you want to find the root of. Then, you need to initialize two initial guesses for the root and set up a loop that will iterate until a desired level of accuracy is reached. Within the loop, you will use the secant formula to calculate a new approximation for the root and update the initial guesses until the desired accuracy is achieved.

5. Can the secant method fail in a C++ program?

Yes, the secant method can fail in a C++ program if the initial guesses are not chosen carefully or if the function does not meet certain criteria, such as being continuous and having a root within the interval of the initial guesses. In these cases, the method may not converge to the correct root or may not converge at all.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
13
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
8K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
Back
Top