Error using arrayfun
Undefined function 'f' for input arguments of type 'double'.
Error in golden (line 3)
plot(t,arrayfun(@f,t));
function golden(a, b)
t=[a:0.01:b];
plot(t,arrayfun(@f,t));
hold on
tol=10^(-12);
r=(3-sqrt(5))/2;
c=a+r*(b-a);
d=b-r*(b-a);
fa=f(a);
fb=f(b);
fc=f(c);
fd=f(d);
while (b-a>tol)
if (fc<fd) b=d; fb=fd; d=c; fd=fc; c=a+r*(b-a); fc=f(c); plot(c,fc,'r*'); else a=c; fa=fc; c=d; fc=fd; d=b-r*(b-a); fd=f(d); plot(d,fd,'r*'); end
end
fprintf('Minimum found at x=%.12f \n', (a+b)/2, f(a+b)/2);
hold off
end
Best Answer