Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

MATLAB: In this problem, you will implement a different numeric approximation al

ID: 3707134 • Letter: M

Question

MATLAB: In this problem, you will implement a different numeric approximation algorithm for finding roots, the Secant Method. For the purposes of this problem, the function whose roots you will be finding is

The Secant Method takes two initial guesses (x0 and x1), 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).

The formula for determining the next guess (based on the two previous guesses) is:

Starting with the initial guesses, x0 and x1, a new guess, x2, is then obtained by finding where the line between the function values at the initial guesses intersects the x-axis; x1 and x2 are then used to compute a new guess, x3, and the process repeats until the desired precision is reached (or two guesses in succession are identical).

a. Define a MATLAB function secant which takes 3 arguments:

func - a function handle for the function whose root is being determined

x0 - one initial guess, x0

x1 - the second initial guess, x1

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 100 (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 must implement the following algorithm:

1. if x0 and x1are the same value, quit with the error message "x0 and x1 must be distinct" (hint: use the error function)

2. initially, curr is x1 and prev is x0
while |func(curr)| > ? and # iterations < 100 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.

f (x) = x3 + 4x2-62-0.3ez

Explanation / Answer

function [root,iters]=secant(func,x0,x1) if(x0==x1) error('x0 and x1 must be distinct') end prev=x0; curr=x1; iters=1;%iteration count while (abs(func(curr))>10^-15)&&iters