I need to write a fixed point iteration algorithm and it's mostly coded but I'm running into this error:
Undefined function or variable 'g'.
I want to define g as g(x) = 1/2*(1+5/x). However, I'm a matlab n00b and unsure how to do this. Any help would be much appreciated, thanks.
function [y,k] = fixedpoint(g,p0,tol,max1)
for k=1:max1
p = g(p0);
err = abs(p-p0);
abserr = abs(sqrt(5)-p);
ratioerr = abserr/(abs(sqrt(5)-p0));
if (err<tol)
break
end
p0 = p;
end
if (k==max1)
disp('The algorithm did not converge')
end
y = p;
You can define g as a anonymous function
g = @(x)1/2*(1+5/x)
Then call your function fixedpoint in usual way
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With