[英]How to connect points together using plot
我编写了一些代码,这些代码将使用牛顿方法搜索功能的最小值。 我在绘制结果时遇到了一些问题。 结果,我得到了未连接点的图,但是我想获得具有相同颜色的连接点的图。 有人可以告诉我代码有什么问题吗?
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
图片相关: 情节
按照Daniel的评论,您可以按照以下方式进行操作:
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');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.