//
// Modified False Position Method, for details
// see p 129 in Numerical Methods by Chapra.
// f – function pointer to the function to find a root
// xl – lower x bound
// xu – upper x bound
// es – relative stopping error
// et – stopping error (abs(f(xr)) < et)
// imax - maximum iterations
// verbose - whether to print information to cout
//
// This is a templated Function so that a member function pointer
// may be passed. This is very useful when the function has
// several arguments, but only one is being varied. Then the
// caller may define a member function, using class members to
// find the other parameters.
template
double ModFalsePos(double (F::*f)(double), F & obj,
double xl, double xu, double es, double et, int imax,
bool verbose)
{
// the current iteration
int iter = 0;
// lower and upper iteration counts,
// used to detect when one of the bounds
// is ‘stuck’.
int il = 0;
int iu = 0;
// the current root
double xr = 0;
// the current error estimate
double ea = 0;
// the lower function value
double fl = (obj.*f)(xl);
// the upper function value
double fu = (obj.*f)(xu);
//
// Modified False Position Method, for details
// see p 129 in Numerical Methods by Chapra.
// f – function pointer to the function to find a root
// xl – lower x bound
// xu – upper x bound
// es – relative stopping error
// et – stopping error (abs(f(xr)) < et)
// imax - maximum iterations
// verbose - whether to print information to cout
//
// This is a templated Function so that a member function pointer
// may be passed. This is very useful when the function has
// several arguments, but only one is being varied. Then the
// caller may define a member function, using class members to
// find the other parameters.
template
double ModFalsePos(double (F::*f)(double), F & obj,
double xl, double xu, double es, double et, int imax,
bool verbose)
{
// the current iteration
int iter = 0;
// lower and upper iteration counts,
// used to detect when one of the bounds
// is ‘stuck’.
int il = 0;
int iu = 0;
// the current root
double xr = 0;
// the current error estimate
double ea = 0;
// the lower function value
double fl = (obj.*f)(xl);
// the upper function value
double fu = (obj.*f)(xu);
//
// Modified False Position Method, for details
// see p 129 in Numerical Methods by Chapra.
// f – function pointer to the function to find a root
// xl – lower x bound
// xu – upper x bound
// es – relative stopping error
// et – stopping error (abs(f(xr)) < et)
// imax - maximum iterations
// verbose - whether to print information to cout
//
// This is a templated Function so that a member function pointer
// may be passed. This is very useful when the function has
// several arguments, but only one is being varied. Then the
// caller may define a member function, using class members to
// find the other parameters.
template
double ModFalsePos(double (F::*f)(double), F & obj,
double xl, double xu, double es, double et, int imax,
bool verbose)
{
// the current iteration
int iter = 0;
// lower and upper iteration counts,
// used to detect when one of the bounds
// is ‘stuck’.
int il = 0;
int iu = 0;
// the current root
double xr = 0;
// the current error estimate
double ea = 0;
// the lower function value
double fl = (obj.*f)(xl);
// the upper function value
double fu = (obj.*f)(xu);
eAnswers Team
#include
#include
#ifndef __MODIFIEDFALSEPOSITION_H
#define __MODIFIEDFALSEPOSITION_H
//
// Modified False Position Method, for details
// see p 129 in Numerical Methods by Chapra.
// f – function pointer to the function to find a root
// xl – lower x bound
// xu – upper x bound
// es – relative stopping error
// et – stopping error (abs(f(xr)) < et)
// imax - maximum iterations
// verbose - whether to print information to cout
//
// This is a templated Function so that a member function pointer
// may be passed. This is very useful when the function has
// several arguments, but only one is being varied. Then the
// caller may define a member function, using class members to
// find the other parameters.
template
double ModFalsePos(double (F::*f)(double), F & obj,
double xl, double xu, double es, double et, int imax,
bool verbose)
{
// the current iteration
int iter = 0;
// lower and upper iteration counts,
// used to detect when one of the bounds
// is ‘stuck’.
int il = 0;
int iu = 0;
// the current root
double xr = 0;
// the current error estimate
double ea = 0;
// the lower function value
double fl = (obj.*f)(xl);
// the upper function value
double fu = (obj.*f)(xu);
if (verbose)
{
printf(“xl = %12.4e, xu = %12.4en”, xl, xu);
printf(“%6s%12s%12s%12s%12s%12sn”,
“Iter.”, “x_l”, “x_u”, “x_r”, “f(x_r)”, “ea”);
}
while(true)
{
// new root estimate and function value
double xrold = xr;
xr = xu – fu * (xl – xu) / (fl – fu);
// if out of bounds – false position
// has failed, use bisection
if ( xr > xu || xr < xl )
{
xr = (xl+xu)/2;
}
double fr = (obj.*f)(xr);
// iteration counter increment
iter += 1;
// error estimate (relative)
if (xr != 0 )
ea = std::abs((xr-xrold)/xr)*100;
// test signs
double test = fl*fr;
if (test = 2)
fl = fl / 2;
}
else if (test > 0)
{
xl = xr;
fl = (obj.*f)(xl);
il = 0;
iu += 1;
if (iu >= 2)
fu = fu /2;
}
else
{
ea = 0;
}
if(verbose)
{
printf(“%6i%12.4e%12.4e%12.4e%12.4e%12.4en”,
iter, xl, xu, xr, fr, ea);
}
if (ea < es || std::abs(fr) = imax)
break;
}
return xr;
}
#endif
eAnswers Team
#include
#include
#ifndef __MODIFIEDFALSEPOSITION_H
#define __MODIFIEDFALSEPOSITION_H
//
// Modified False Position Method, for details
// see p 129 in Numerical Methods by Chapra.
// f – function pointer to the function to find a root
// xl – lower x bound
// xu – upper x bound
// es – relative stopping error
// et – stopping error (abs(f(xr)) < et)
// imax - maximum iterations
// verbose - whether to print information to cout
//
// This is a templated Function so that a member function pointer
// may be passed. This is very useful when the function has
// several arguments, but only one is being varied. Then the
// caller may define a member function, using class members to
// find the other parameters.
template
double ModFalsePos(double (F::*f)(double), F & obj,
double xl, double xu, double es, double et, int imax,
bool verbose)
{
// the current iteration
int iter = 0;
// lower and upper iteration counts,
// used to detect when one of the bounds
// is ‘stuck’.
int il = 0;
int iu = 0;
// the current root
double xr = 0;
// the current error estimate
double ea = 0;
// the lower function value
double fl = (obj.*f)(xl);
// the upper function value
double fu = (obj.*f)(xu);
if (verbose)
{
printf(“xl = %12.4e, xu = %12.4en”, xl, xu);
printf(“%6s%12s%12s%12s%12s%12sn”,
“Iter.”, “x_l”, “x_u”, “x_r”, “f(x_r)”, “ea”);
}
while(true)
{
// new root estimate and function value
double xrold = xr;
xr = xu – fu * (xl – xu) / (fl – fu);
// if out of bounds – false position
// has failed, use bisection
if ( xr > xu || xr < xl )
{
xr = (xl+xu)/2;
}
double fr = (obj.*f)(xr);
// iteration counter increment
iter += 1;
// error estimate (relative)
if (xr != 0 )
ea = std::abs((xr-xrold)/xr)*100;
// test signs
double test = fl*fr;
if (test = 2)
fl = fl / 2;
}
else if (test > 0)
{
xl = xr;
fl = (obj.*f)(xl);
il = 0;
iu += 1;
if (iu >= 2)
fu = fu /2;
}
else
{
ea = 0;
}
if(verbose)
{
printf(“%6i%12.4e%12.4e%12.4e%12.4e%12.4en”,
iter, xl, xu, xr, fr, ea);
}
if (ea < es || std::abs(fr) = imax)
break;
}
return xr;
}
#endif
eAnswers Team
#include
#include
#ifndef __MODIFIEDFALSEPOSITION_H
#define __MODIFIEDFALSEPOSITION_H
//
// Modified False Position Method, for details
// see p 129 in Numerical Methods by Chapra.
// f – function pointer to the function to find a root
// xl – lower x bound
// xu – upper x bound
// es – relative stopping error
// et – stopping error (abs(f(xr)) < et)
// imax - maximum iterations
// verbose - whether to print information to cout
//
// This is a templated Function so that a member function pointer
// may be passed. This is very useful when the function has
// several arguments, but only one is being varied. Then the
// caller may define a member function, using class members to
// find the other parameters.
template
double ModFalsePos(double (F::*f)(double), F & obj,
double xl, double xu, double es, double et, int imax,
bool verbose)
{
// the current iteration
int iter = 0;
// lower and upper iteration counts,
// used to detect when one of the bounds
// is ‘stuck’.
int il = 0;
int iu = 0;
// the current root
double xr = 0;
// the current error estimate
double ea = 0;
// the lower function value
double fl = (obj.*f)(xl);
// the upper function value
double fu = (obj.*f)(xu);
if (verbose)
{
printf(“xl = %12.4e, xu = %12.4en”, xl, xu);
printf(“%6s%12s%12s%12s%12s%12sn”,
“Iter.”, “x_l”, “x_u”, “x_r”, “f(x_r)”, “ea”);
}
while(true)
{
// new root estimate and function value
double xrold = xr;
xr = xu – fu * (xl – xu) / (fl – fu);
// if out of bounds – false position
// has failed, use bisection
if ( xr > xu || xr < xl )
{
xr = (xl+xu)/2;
}
double fr = (obj.*f)(xr);
// iteration counter increment
iter += 1;
// error estimate (relative)
if (xr != 0 )
ea = std::abs((xr-xrold)/xr)*100;
// test signs
double test = fl*fr;
if (test = 2)
fl = fl / 2;
}
else if (test > 0)
{
xl = xr;
fl = (obj.*f)(xl);
il = 0;
iu += 1;
if (iu >= 2)
fu = fu /2;
}
else
{
ea = 0;
}
if(verbose)
{
printf(“%6i%12.4e%12.4e%12.4e%12.4e%12.4en”,
iter, xl, xu, xr, fr, ea);
}
if (ea < es || std::abs(fr) = imax)
break;
}
return xr;
}
#endif