Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Rational Root-Finding Method, the approximation in this case is not linear but rational f(x)~ a+b/(x+c) (hypothesis: f'(x_k)*f''(x_k)\neq 0 )
function [flag,xk1] = RationalCode(x0,f,df,d2f,TOL,MaxIt)
%[flag,xk] = RationalCode(x0,f,df,d2f,TOL,MaxIt)
%
% This code approximate a root of f using a rational fuction approx.
% Input:
% x0 := initial value.
% f := function whose roots will be approximated.
% df := derivative of f.
% d2f := second derivative of f.
% TOL := Stoping criteria: relative error<TOL.
% MaxIt:= Maximun number of iterations.
%
% Output:
% flag:= 0 if df(xk)~0 or d2f(xk)~0.
% xk1:= root approximation.
% Last modified: Feb. 11, 2018.
it = 0; % Number of iterations
eps1 = 10^(-14); %
xk1 = x0; % First candidate to root
xk = x0;
AbsEr= inf; % Absolute error
while((AbsEr>TOL*abs(xk)) && MaxIt>it)
xk=xk1;
if(abs(df(xk))>eps1 && abs(df(xk))>eps1)
xk1 = xk+2*(df(xk)/d2f(xk))*...
(1-(df(xk)^2/(df(xk)^2-0.5*f(xk)*d2f(xk))));
AbsEr = abs(xk1-xk);
it = it+1;
else
flag = 0; %
xk1 = NaN;
break
end
flag=1;
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment