I'm Implementing a Fixed Point method for root finding. Below is the code:
clear;
clc;
f = inline('y^4 - 2', 'y');
phii = inline('y - (y^4 -2)/(4*(y^3))', 'y');
x0 = 4;
toler = 10^-10;
nmax = 1000;
a = fixpoint(x0,toler,nmax,f,phii)
function[alpha,res,iters] = fixpoint(x_0,tol,max_iters,fun,phi)
error = tol +1;
iters = 0;
xvect = x_0;
x = x_0;
res = fun(x);
xdif = [];
while iters < max_iters & error > tol
iters = iters +1;
x_n = phi(x);
error = abs(x_n - x);
xdif = [xdif; error];
x = x_n;
xvect = [xvect;x];
res = [res;fun(x)];
end
alpha = xvect(end);
residual = res(end);
return
end
The method works as it should. The only issue I'm having is that I want it to return all three of: alpha, res (a vector in which the last entry is the residual of importance, but all values are needed), and iterations.
Currently it only returns alpha.
What is the best way to go about this?
[alpha,res,iters] = fixpoint(x0,toler,nmax,f,phii)
Also, no need for return
here.