我编写了一些代码,使用newton方法搜索最少的函数。我在绘制结果时遇到了一些问题。结果我得到了没有连接点的情节,但我想获得具有相同颜色的连接点的图。有人能告诉我我的代码有什么问题吗?
x = [[-1;-1],[-0.8;-0.8],[-0.6;-0.6],[-0.4;-0.4],[-0.2;-0.2],[0;0],[0.2;0.2],[0.4;0.4],[0.6;0.6],[0.8;0.8],[1;1]];
eps = [0.1, 0.01, 0.001, 0.0001];
f = @(x) x(1).^2 - 2.1*x(1).^4 + (x(1).^6)/3 + x(1)*x(2) - 4*x(2).^2 + 4*x(2).^4;
for i=1:length(x)
% axis([0 0.001 0 100])
colorVec = hsv(12);
hold on;
for e=1:length(eps)
%fprintf('Starting point: %2.1f, %2.1f\n', x(:,i));
%fprintf('Tollerance: %1.3f\n', eps(e));
[r, iters] = NewtonMethod(f, x(:,i), eps(e), 100);
plot(eps(e), iters, '-s', 'Color', colorVec(i,:))
end
hold off;
xlabel('Tollerance')
ylabel('Number of iterations')
title('Title')
end
图片相关:plot
答案 0 :(得分:0)
根据丹尼尔的评论,你可以这样做:
x = [[-1;-1],[-0.8;-0.8],[-0.6;-0.6],[-0.4;-0.4],[-0.2;-0.2],[0;0],[0.2;0.2],[0.4;0.4], [0.6;0.6],[0.8;0.8],[1;1]];
eps = [0.1, 0.01, 0.001, 0.0001];
f = @(x) x(1).^2 - 2.1*x(1).^4 + (x(1).^6)/3 + x(1)*x(2) - 4*x(2).^2 + 4*x(2).^4;
iters = zeros(length(x),length(eps));
for i=1:length(x)
colorVec = hsv(12);
hold on;
for e=1:length(eps)
%fprintf('Starting point: %2.1f, %2.1f\n', x(:,i));
%fprintf('Tolerance: %1.3f\n', eps(e));
[r, iters(i,e)] = NewtonMethod(f, x(:,i), eps(e), 100);
end
hold off;
xlabel('Tolerance')
ylabel('Number of iterations')
title('Title')
end
% choose one of these:
plot(iters);
plot(iters');