cpp_newbie
03-15-2004, 05:47 PM
Hi everyone,
I'm having difficulty implementing the N-R method in the N.R. for C++ book and wonder if anyone can help. I think I am not defining my funcd correctly. Can someone please let me know what I have to do to get the program to compile correctly?
Thanks in advance,
cpp_newbie
Here's the problem code (mostly from Pg369&370 of N.R. in C++):
#include <iostream>
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::rtnewt(void funcd(const DP, DP &, DP &), const DP x1, const DP x2, const DP xacc)
{
const int JMAX=20;
int j;
DP df, dx, f, rtn;
rtn=0.5*(x1+x2);
for (j=0; j<JMAX; j++){
funcd(rtn, f, df);
dx=f/df;
rtn -= dx;
if ((x1-rtn)*(rtn-x2) < 0.0)
nrerror("Jumped out of brackets in rtnewt");
if (fabs(dx) < xacc) return rtn;
}
nrerror("Maximum number of iterations exceeded in rtnewt");
return 0.0;
}
void funcd(const DP x, DP &f, DP &df)
{
f = pow(x, 0.5);
df = 0.5*pow(x, -0.5);
}
int main()
{
const DP x = 0;
DP f;
DP df;
DP x1 = 1;
DP x2 = 2;
DP xacc = 0.001;
NR::rtnewt(funcd(x, f, df), x1 , x2, xacc);
system("pause");
return 0;
}
I'm having difficulty implementing the N-R method in the N.R. for C++ book and wonder if anyone can help. I think I am not defining my funcd correctly. Can someone please let me know what I have to do to get the program to compile correctly?
Thanks in advance,
cpp_newbie
Here's the problem code (mostly from Pg369&370 of N.R. in C++):
#include <iostream>
#include <cmath>
#include "nr.h"
using namespace std;
DP NR::rtnewt(void funcd(const DP, DP &, DP &), const DP x1, const DP x2, const DP xacc)
{
const int JMAX=20;
int j;
DP df, dx, f, rtn;
rtn=0.5*(x1+x2);
for (j=0; j<JMAX; j++){
funcd(rtn, f, df);
dx=f/df;
rtn -= dx;
if ((x1-rtn)*(rtn-x2) < 0.0)
nrerror("Jumped out of brackets in rtnewt");
if (fabs(dx) < xacc) return rtn;
}
nrerror("Maximum number of iterations exceeded in rtnewt");
return 0.0;
}
void funcd(const DP x, DP &f, DP &df)
{
f = pow(x, 0.5);
df = 0.5*pow(x, -0.5);
}
int main()
{
const DP x = 0;
DP f;
DP df;
DP x1 = 1;
DP x2 = 2;
DP xacc = 0.001;
NR::rtnewt(funcd(x, f, df), x1 , x2, xacc);
system("pause");
return 0;
}