A root-finding algorithm is a numerical method, or algorithm, for finding a value x such that f(x) = 0, for a given function f. Such an x is called a root of the function f.
An example of a root finding method is the Newton-Raphson method. Essentially, to find the root, one repeatedly evaluates (where k is the kth iteration):
•
xk+1 = xk - f(xk)/f'(xk);
•
Where
o
f'(x) is the first derivative of f with respect to x at the value x
o
x0 = some_initial_x
Until f(xk) is 0.
Your task is to write a function in C++ specifically for the Newton-Raphson algorithm, keeping in mind that we may want to find a root of the following functions (where x is in radians):
Function
Derivative
sin(x)
cos(x)
cos(x)
-sin(x)
sin(x) + 1
cos(x)
sin(x) + 2
cos(x)
Write or describe tests that test the boundaries of this algorithm.
cout << "\n root x = " << x << ", test of f(x) = " << func_1(x);
54
else cout << "\n failed to find root ";
55
system("pause");
56
return 0;
57
}
Yes, I searched on the internet too and following is one sample that I found:
// Newton-Raphson method of finding roots //
// Passing references to functions f(x) and f'(x) as function parameters //
// also demonstrates use of a function template //
//----------------------------------------------------------------------------//
// Function template: Newton-Raphson method find a root of the equation f(x) //
// see en.wikipedia.org/wiki/...n's_method //
// Parameters in: &x reference to first approximation of root //
// (&f)(x) reference to function f(x) //
// (fdiv)(x) reference to function f'(x) //
// max_loop maxiumn number of itterations //
// accuracy required accuracy //
// out: &x return root found //
// function result: > 0 (true) if root found, 0 (false) if max_loop exceeded //
template <class T1>
int newton(T1 &x, T1 (&f)(T1), T1 (&fdiv)(T1),
int max_loop, const double accuracy)
{
T1 term;
do
{
// calculate next term f(x) / f'(x) then subtract from current root
term = (f)(x) / (fdiv)(x);
x = x - term; // new root
}
// check if term is within required accuracy or loop limit is exceeded
while ((abs(term / x) > accuracy) && (--max_loop));
return max_loop;
}
//----------------------------------------------------------------------------//
// test functions
//----------------------------------------------------------------------------//
// Main program to test above function
int main()
{
cout << "\nFind root of f(x) = cosh(x) + cos(x) - 3 = 0";
double x = 1.0; // initial 'guess' at root
if (newton(x, func_1, fdiv_1, 100, 1.0e-)
cout << "\n root x = " << x << ", test of f(x) = " << func_1(x);
else cout << "\n failed to find root ";
cout << "\n\nFind root of f(x) = x * x - 25 = 0";
x = 1.0; // initial 'guess' at root
if ( newton(x, func_2, fdiv_2, 100, 1.0e-)
cout << "\n root x = " << x << ", test of f(x) = " << func_2(x);
else cout << "\n failed to find root ";
cout << "\n\nFind root of f(x) = x^2 - 10x + 34 = 0";
complex<double> xc = complex<double>(1.0, 1.0); // initial 'guess' at root
if ( newton(xc, func_3, fdiv_3, 100, 1.0e-)
cout << "\n root x = " << xc << ", test of f(x) = " << func_3(xc);
else cout << "\n failed to find root ";
cout << "\n\nFind root of f(x) = x^2 - 10x + 34 = 0";
xc = complex<double>(1.0, -1.0); // initial 'guess' at root
if ( newton(xc, func_3, fdiv_3, 100, 1.0e-)
cout << "\n root x = " << xc << ", test of f(x) = " << func_3(xc);
else cout << "\n failed to find root ";
cout << "\n\nFind root of f(x) = 2x^3 - 4x^2 - 22x + 24 = 0";
x = 5.0; // initial 'guess' at root
if ( newton(x, func_4, fdiv_4, 100, 1.0e-)
cout << "\n root x = " << x << ", test of f(x) = " << func_4(x);
else cout << "\n failed to find root ";
cin.get();
return 0;
}
I can see your sample code and my sample code are very similar except the format of functions and corresponding derivatives. But how do they related to my original task:
Find a root of the following functions (where x is in radians):