你應該是要寫下面的演算法,我用Newton's做的
你自行再改成你要的...
%% fixed_point.m
function [x,ea] = fixed_point(g, x0, es, N)
x = zeros(N, 1);
ea = zeros(N, 1);
i = 1;
x(i) = x0;
i = i + 1;
x(i) = g(x(i-1));
ea(i) = abs((x(i)-x(i-1))/x(i))*100;
while ea(i) > es
i = i + 1;
gprime = (g(x(i-1)+1e-6) - g(x(i-1)))/1e-6;
x(i) = x(i-1)- g(x(i-1))/gprime;
ea(i) = abs((x(i)-x(i-1))/x(i));
if i >= N
break;
end
end
x = x(1:i);
ea = ea(1:i);
end
%% Run.m
g= @(x) cos(x)-x^3;
x0=5;
es=1e-10;
N=100;
[x,ea] = fixed_point(g, x0, es, N);