Question
Write a function that attempts to find the zero of a function using Newton's Method. The method requires four items a function the function's derivative
Write a function that attempts to find the zero of a function using Newton's Method. The method requires four items
a function
the function's derivative
an initial value that is close to a zero
number of iterations to perform
The method uses the following relationship
So, x0, which is the initial value passed to the function, is used to determine x1, x1 to determine x2, and so on until xn-1 is used to determine xn where n is the number of iterations passed to the function.
Your solution should be a file that contains four functions (you should rewrite the comments to be more meaningful):
// The function to find the zero of : f(x). // double f(double x); // The derivative of the function : f'(x). // double fPrime(double x); // The function that applies Newton's Method and returns the estimate of // the zero. // // g : the function to find the zero of // dg : the derivative of g // initialValue : the initial values used to start the method // iterations : the number of times to try to get a zero // // The function will return before the number of iterations is achieved // if a zero is found. // double netwon(double (*g)(double), double (*dg)(double), double initialValue, int iterations);
The method requires certain things be true of the function and the derivative. Your function should assume that the user is passing only functions and initial values for which the method makes sense. For example, the user should never used cos(x) with an initial value of 0.
Functions as Parameters
To be able to complete this assignment you will need to be able to pass a function to a function. In C++, functions are objects and have addresses in memory. This means you can reference them like a variable and so, in turn, pass them as parameters. To really understand how it works requires insight we won't gain until we discuss pointers but using it is pretty simple. Mostly, it comes down to what will appear to be goofy syntax.
The function that you will need to pass as a parameter is the function that defines the curve. In your calculus class you more often than not just called it "f". The function is of one variable that maps a real number to a real number. The C++ version, then, would have a prototype that looks like
double f(double x);
Suppose we want to create a function, display(), that evaluates f(x) at multiples of 2 and display the result.display()'s definition would be
void display(double (*f)(double)) { for (int i=0, iSo, the formal parameter for the function is
double (*f)(double)and then notice in the implementation of the function, f is just used as normal.
To use display(), we just need to pass it a function that takes a single double and returns a double. So, we could do the following:
void quadratic(double (*f)(double)) { return x*x; } int main() { display(quadratic); }or we could use a function from cmath like sin():
int main() { display(sin); }In both cases, observe that we are only using the function name when passing the parameter. It's not being called (i.e., sin(3.2)) but rather the identifier is being used like a variable and passed into display().
For this assignment, your functions that estimate area will be like display but with other formal parameters defined.
n+1. f (an) n+1. f (an)
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started