so i worte this and it works somtimes. the function im trying to find the roots of is(adsbygoogle = window.adsbygoogle || []).push({});

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

the program i wrote will find two of the roots but i cant 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. heres 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 im just trying to get it to work and then im going to neaten things up. If anyone takes the time to read through all this i thank you.

**Physics Forums | Science Articles, Homework Help, Discussion**

Dismiss Notice

Join Physics Forums Today!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

# Homework Help: C++ program for secant method

**Physics Forums | Science Articles, Homework Help, Discussion**