Exercises
Write pseudocode or MATLAB to answer each question.
-
Here are the steps to complete this exercise:
Plot the function to find starting point
Obtain the successive approximation formula
Choose and apply the formula -
Write some pseudocode to compute the next approximation of a root of an expression or function based on Newton's Method.
To get you started, determine what terms and values you need, assign those values and compute the next approximation.
In the case of Newton's Method, you need an initial guess, the value of the expression at that guess and the value of the derivative at that guess.
One person's pseudocode may look like this:
- Assign an initial guess value to a variable named curr_x.
- Assign the value of the expression at curr_x to a variable named f_at_x.
- Assign the value of the derivative of the expression at x to a variable named fprime_at_x.
- Compute the value of the next approximation using Newton's method and assign this to next_x.
Notice how wordy the above pseudocode is. Most programmers do not require this much detail in their pseudocode and would instead write something shorter like:
- Initialize x (initial guess).
- Compute fx at x.
- Compute fprimex at x.
- Compute the next approximation using Newton's Method.
-
Modify the above pseudocode so that it continues approximating the root until two successive root approximations are within one-one thousandth of each other.
- Initialize error_threshold to 1/1000
- Initialize current_guess as initial guess.
- Initialize second_to_last_guess as current_guess + 2*error_threshold.
- while difference between current_guess and second_to_last_guess is greater than error_threshold:
- Compute fx at current_guess.
- Compute fprimex at current_guess.
- Compute next_approximation using Newton's Method.
- Assign current_guess to second_to_last_guess.
- Assign next_approximation to current_guess.
- Display next_approximation as this is the last approximation completed.
- Test your last root approximation value by computing the value of the function at that value and comparing this result to zero.
Adding a "while" loop is a good way to allow this approximation code to continue to execute while our last two approximations are "too far" apart. Caution: it is critial that you initialize the condition variables correctly before the loop, test them correctly at the start of the loop, and update them correctly within the loop to avoid infinite loops.
If the result of the final test value is not sufficiently close to zero, than one of several things may have occurred:
- You didn't implement the conditional expression for the while loop correctly.
- Newton's Method was not correctly implemented
- Your error_threshold was too large.
- The function is not continuous or does not cross the x-axis
- Your initial guess is poor. It must be on a part of the curve that does cross the x-axis without changing direction.