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

I need to write a mathlab code that graphs a certain function by finding its roo

ID: 3771112 • Letter: I

Question

I need to write a mathlab code that graphs a certain function by finding its roots using the secant root finding method. However, everytime I do that I get this error: Index exceeds matrix dimensions. How do I fix it

Secant Method:
function root=secantRoot1 (func,a,b,tol)
%Basic Secant method for finding the root
% func is name of a string refering to a function
% a and b are points near the area of the root
%tol is the maximum error
%counterMaX is the maximum number of iterations
for i=1:1:200
fa=feval(func,a);
fb=feval(func,b);
xi=b-fb*(b-a)/(fb-fa);
fxi=feval (func, xi);
if (abs(fxi)<tol)
root =xi;
return
end
a=b;
b=xi;
root=xi;
end
end

Main:
data=[];
for p=0:0.01:1
depth=secantRoot1(@(x)(-x^3 + 3*x^2-4*p),0,1.99,0.01)
data=[data;p:depth];
end
plot (data(:,1), data(:,2));
title = ('Depth Sphere Sinks to')
xlabel= ('Density')
ylabel= ('Depth')

Explanation / Answer

Root finding is the process of finding solutions of a function f(x) = 0. As we learned in high school algebra, this is relatively easy with polynomials. A line’s root can be found just by setting f(x) = 0 and solving with simple algebra. The quadratic formula gives us the way to solve for the roots of any parabola, and even allows us to find complex roots if they exist. Furthermore, there is also a cubic formula for solving for any cubic and a quartic formula for solving for any quartic. For polynomials greater than degree four and for any other continuous function where the roots cannot be solved for by algebraic means, we must use other more general methods to find the roots of the function. Well known important root finding methods will be discussed along with their pros and cons, the functions they work with best, and what are good ways to go about finding roots of certain functions

The secant method is slightly different than the bisection method. It takes two input values over which there is a sign change, but instead of bisecting the difference between them after each iteration, it takes the points on the function at those initial guesses and constructs a line (secant line) connecting the two. Where this line crosses the x-axis is the next guess, and will replace the initial value with the same sign, and repeat the process. The method can be thought of in the following way: let a and b be the initial guesses, and let c be the next guess determined by the method: c = a f(a) ab/ f(a)f(b) .

Different root-finding algorithms are compared by the speed at which the approximate solution converges (i.e., gets closer) to the true solution. An iterative method xn+1=g(xn) is defined as having pth order convergence if for a sequence xn where limnxn= exists then

limn|xn+1||xn|p=L0.(3)

Newton's method has (generally) second-order convergence, so in Eq. (3) we would have p=2, but it converges so quickly that it can be difficult to see the convergence (there are not enough terms in the sequence). The secant method has a order of convergence between 1 and 2. To discover it we need to modify the code so that it remembers all the approximations.

The following code, is Newton's method but it remembers all the iterations in the list x. We use x(1) for x1 and similarly x(n) for xn:

The semicolon (;) at the end of line 4 tells MATLAB not to display the value of x after the assignment (also in line 1. Without the lonely x on line 9 the code would calculate x, but not show us anything.