
October 13th, 2008, 07:51 PM
#1
Secant Method problems
I'm trying to implement the secant method, but I'm having a problem. No matter the function or the initial guesses I use, I get the output "The solution is 0 and it was found in 2 iterations." I think my problem lies in the while loop, but my brain is mush and I can't pick it out.
Code:
#include <iostream>
#include <math.h>
using namespace std;
double x0, x1, x, tol=0.5e6;
int N = 2, Nmax = 100;
double f(double x) {return x*x  4.0;}
double secant(double &x, double x0, double x1)
{
while ( (fabs ((f(x1)f(x0))/f(x1)) > tol) && (N <= Nmax));
{
x = x1  (f(x1) * (x1  x0)) / (f(x1)  f(x0));
x1 = x;
x0 = x1;
N++;
}
return x;
}
int main () {
cout << "Secant Method: \n\n";
cout << "Give the first initial approximation to a root. \n";
cin >> x0;
cout << "Give the second initial approximation to a root. \n";
cin >> x1;
if (N > Nmax)
cout << "In " << Nmax << " iterations, no solution was found. \n";
else
cout << "The solution is " << x << " and it was found in "
<< N << " iterations. \n";
return 0;
}
Thank you!

October 13th, 2008, 08:24 PM
#2
Re: Secant Method problems
1) Well, for one, you never call the secant() function.
2) You want to remove the semicolon at the end of the line:
Code:
while ( (fabs ((f(x1)f(x0))/f(x1)) > tol) && (N <= Nmax)) ;

October 13th, 2008, 08:41 PM
#3
Re: Secant Method problems
I never would have caught that semicolon.
This is what I have now, and it seems to work. Thanks!
Code:
#include <iostream>
#include <math.h>
using namespace std;
double x0, x1, x, N = 2.0, tol=0.5e6;
int Nmax = 100;
double f(double x) {return x*x4.0;}
double secant(double &x, double x0, double x1)
{
while ( (fabs ((f(x1)f(x0))/f(x1)) > tol) && (N <= Nmax))
{
x = x1  ((f(x1) * (x1  x0)) / (f(x1)  f(x0)));
x0 = x1;
x1 = x;
N++;
}
return N;
}
int main () {
cout << "Secant Method: \n\n";
cout << "Give the first initial approximation to a root. \n";
cin >> x0;
cout << "Give the second initial approximation to a root. \n";
cin >> x1;
N = secant(x, x0, x1);
if (N > Nmax)
cout << "In " << Nmax << " iterations, no solution was found. \n";
else
cout << "The solution is " << x << " and it was found in "
<< N << " iterations. \n";
return 0;
}

October 13th, 2008, 09:30 PM
#4
Re: Secant Method problems
1) Every compiler I am familiar with should have issue3s a warning on the "Rogue" semicolon.
2) if a function is only going to be called from within the same compilation unit (typically same CPP), it should always be declared as "static" (making it file scope!)
You should have gotten one warning, and with proper codign, the compiler would have pointed out both of your mistakes.....
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
Posting Permissions
 You may not post new threads
 You may not post replies
 You may not post attachments
 You may not edit your posts

Forum Rules

Click Here to Expand Forum to Full Width
This is a CodeGuru survey question.
Featured
