Im supposed to write a code in matlab that uses the secant m

Im supposed to write a code in matlab that uses the secant method to estimate roots, using function[xn,n,fxn,ea]=secant(f,xm1,x0,maxit,es) xm1=x-1 and x0. A question i have is how i am going to make it recognize after the first iteration that x0 is the new first point of the bound and xn=x0-(f(x0)(x-1-x0)/(f(x1)-f(x0)) the 2nd point of the bound using these inputs and outputs. Im new to matlab and just cannot come up with a solution. Any help would be appreciated
Im supposed to write a code in matlab that uses the secant method to estimate roots, using function[xn,n,fxn,ea]=secant(f,xm1,x0,maxit,es) xm1=x-1 and x0. A question i have is how i am going to make it recognize after the first iteration that x0 is the new first point of the bound and xn=x0-(f(x0)(x-1-x0)/(f(x1)-f(x0)) the 2nd point of the bound using these inputs and outputs. Im new to matlab and just cannot come up with a solution. Any help would be appreciated
Im supposed to write a code in matlab that uses the secant method to estimate roots, using function[xn,n,fxn,ea]=secant(f,xm1,x0,maxit,es) xm1=x-1 and x0. A question i have is how i am going to make it recognize after the first iteration that x0 is the new first point of the bound and xn=x0-(f(x0)(x-1-x0)/(f(x1)-f(x0)) the 2nd point of the bound using these inputs and outputs. Im new to matlab and just cannot come up with a solution. Any help would be appreciated

Solution

function x = mysecant (f ,x0 ,x1 , maxit,es)
clc
clear all
% Solve f (x ) = 0 by secant method
% starting with x0 and x1 .
% Inputs : f -- function , input as an inline function
% x0 -- starting guess
% x1 -- second starting guess
% n -- the number of steps
% Output : x -- the approximate solution
y0 = f( x0 );
y1 = f( x1 );
n = 1;
while 1
%for i = 1: maxit % Do n times
x = x1 - (x1 - x0 )* y1 /( y1 - y0 ); % secant formula
y = f (x ); % y value at the new approximate solution
% Move numbers to get ready for the next step
x0 = x1 ;
y0 = y1 ;
x1 = x;
y1 = y;
error = abs(x1-x0);
if error<es
break;
end
n = n+1;
if n>maxit
break
end
end

 Im supposed to write a code in matlab that uses the secant method to estimate roots, using function[xn,n,fxn,ea]=secant(f,xm1,x0,maxit,es) xm1=x-1 and x0. A qu

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site