0

I am new in MATLAB and working on secant method of numerical computing course, But I am not getting result as I solved in my paper sheet. I am providing my code, Kindly tell me what I am missing in this code.

function x = mysecant(f,x0,x1,n)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
y0 = f(x0);
y1 = f(x1);

for i = 1:n
    x = x1 - (x1 - x0)*y1/(y1-y0); %secant formula.
    y = f(x);
    x0 = x1;
    y0=y1;
    x1=x;
end

end

1 Answers1

2

Basically we have to update the values of x1 and y1 after each iteration in the secant method. You are only updating x1 but not y1, add y1 = y. This will solve your problem.

function x = mysecant(f,x0,x1,n)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here
y0 = f(x0);
y1 = f(x1);

for i = 1:n
    x = x1 - (x1 - x0)*y1/(y1-y0); %secant formula.
    y = f(x);
    x0 = x1;
    y0=y1;
    x1=x;
    y1=y;
end

end