## CS368 Homework 7
| ||

## AnnouncementsCheck here periodically.
| ||

## ProblemsYou can get started with this homework by downloading this MATLAB script:
## Problem 1: (12 points) The Secant Method
In MATLAB, roots of functions are determined numerically (i.e., rather than symbolically)
using numeric approximation algorithms. The Successive Numeric Approximation module describes
Newton's Method for finding roots
and you saw an implementation of the Bisection Method and Newton's Method in the
Finding Roots example from the
Successive Numeric Approximation handout.
In this problem, you will implement a different numeric approximation algorithm for finding roots, the f(x) = x^{3} + 3x^{2} - 5x - 0.4 e^{x}
This function is defined for you already (as a function handle named -
(2 pts) Add code to your `homework7.m`script to**plot**. Use the*f*(*x*) from*x*= -6 to*x*= 8`grid ON`command and use the`axis`command to scale the plot so the*y*-axis ranges from -40 to 40.
A function which implements Newton's Method for finding roots has been implemented for you. Download the file Newton's Method uses the first derivative of the function in its computation. In order for the -
(1 pt) Add code to your `homework7.m`script to**define a function handle**. The derivative`fctn_prime`representing the derivative of*f*(*x*)*f*'(*x*) of the function*f*(*x*) is*f*'(*x*) = 3*x*^{2}+ 6*x*- 5 - 0.4 e^{x}
The Secant Method takes two initial guesses ( x), which must be distinct. It is very similar to Newton's method except that instead of using the derivative to find the slope of the tangent line at a point on the function, it estimates the slope of the tangent line by using the slope of the line between two nearby points on the function (i.e., the secant line)._{1}The formula for determining the next guess (based on the two previous guesses) is: Starting with the initial guesses, x, a new guess, _{1}x_{2}, is then obtained by finding where the line between the function values at the initial guesses intersects the x-axis; x and _{1}x are then used to compute a new guess, _{2}x_{3},
and the process repeats until the desired precision is reached (or two guesses in succession are identical).- (7 pts)
**Define a MATLAB function**which takes 3 arguments:`secant``func`- a function handle for the function whose root is being determined`x0`- one initial guess,*x*_{0}`x1`- the second initial guess,*x*_{1}
and returns a vector containing 2 values: `root`- the value for the root obtained by implementing the secant method`iters`- the number of iterations it took for the secant method to find the root (where the`func(root)`is within ε of 0 or two successive guesses are identical) or 50 (the maximum number of iterations the method will perform)
The first line of the `secant`function should be:`function [root, iters] = secant(func, x0, x1)`The `secant`function should implement the following algorithm:-
if
*x*_{0}and*x*_{1}are the same value, quit with the error message "`x0 and x1 must be distinct`" (hint: use the`error`function)
- initially,
*curr*is*x*_{1}and*prev*is*x*_{0} while |*func*(*curr*)| > ε and # iterations < 50 and*curr*is not the same as*prev* compute a new*guess*using formula above update*curr*and*prev*
Use ε = 10 ^{-15}and don't forget to keep track of the number of iterations. Tip: look at the`newton`function and use it as a guide for the overall structure of step 2.
We now wish to compare the results we get using -
(2 pts) **Add the code below to your**This code compares the results of using`homework7.m`script.`fzero`,`newton`, and`secant`using initial guesses ranging from -6 to 8. Note that the code should work as given.disp( 'method : root (# iterations)') for guess = -6 : 8 % the next line is used to visually separate the tests disp('--------------------------------------------------') disp(['Initial guess: ' num2str(guess)]) % calculate root and display results using each method % note: num2str(value, N) displays value to a maximum of N digits root_f = fzero(fctn, guess); disp(['fzero : ', num2str(root_f, 20)]) [root_n, iters_n] = newton(fctn, fctn_prime, guess); disp(['Newton''s : ', num2str(root_n, 20), ' (' num2str(iters_n), ')']) offset = 1; [root_s, iters_s] = secant(fctn, guess - offset, guess + offset); disp(['Secant : ', num2str(root_s, 20), ' (' num2str(iters_s), ')'... ' offset: ' num2str(offset)]) end Notice that in the code above, we obtain the two initial guesses needed by the Secant Method by adding and subtracting an offset to `guess`. We wish to also look at how the size of this offset affects the results we get from the Secant Method.**Add code to the provided code above to compute and display the results when the Secant Method is called using offsets of 0.1, 0.01, and 0.001**(in addition to the offset of 1).
## Problem 2: (8 points) Area between two curvesThis problem deals with the two curves: and These curves are displayed in the next figure:
For this problem, you will need to make appropriate use
of MATLAB's built-in -
**Determine the**Make sure your script displays the coordinates of the intersection points.*x*- and*y*-coordinates of the two intersection points. -
**Determine the area of the region between the two curves.**Make sure your script displays this value. Note that you will need to use your intersection points from part a.
| ||

## Handing in
- homework7.m
- secant.m
In order for your work to be considered to have been turned in on-time, you must upload your files to your Learn@UW Dropbox | ||

Last Updated: 4/26/2016 © 2016 Beck Hasti, hasti@cs.wisc.edu |