Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
808 B
3
Indexable
Never
%SECANT METHOD
function [xa,sol,iter]=secantmetheps(f,xo,x1,tol,maxiter)
    if nargin<5
        maxiter=100;
    end
    if (f(x1)-f(xo))==0
        disp("Divided by epsilon to avoid division by zero")
        xa=x1-((x1-xo)*f(x1))/eps;
    else
        xa=x1-((x1-xo)*f(x1))/(f(x1)-f(xo));
    end
    iter=1;
    sol(1)=x1;
    while abs(f(x1))>tol && iter<maxiter
        xo=x1;
        x1=xa;
        if (f(x1)-f(xo))==0
            disp("Divided by epsilon to avoid division by zero")
            xa=x1-((x1-xo)*f(x1))/eps;
        else
            xa=x1-((x1-xo)*f(x1))/(f(x1)-f(xo));
        end
        sol=[sol x1];
        iter=iter+1;
    end
    if abs(f(xa))>tol
        warning("Method couldn`t find the root with enough accuracy in desired iterations")
    end
end