Problem with plotting a semilog function

6 次查看(过去 30 天)
Hi,
I got help to solve syntax issues here (https://se.mathworks.com/matlabcentral/answers/452726-jacobain-with-3-functions) and go on.
I run that function once to get the result, saved it in variable x:ref and now I am trying to plot how the error decreases: I saved the number of iterations in an error vector and the norm in another vector and plot them against each other. It does not work iunfortunately!
f=@(x) [3.*x(1) - cos(x(2)*x(3)) - 3/2;
4.*x(1)^2 - 625.*x(2)^2 + 2.*x(3) - 1;
20.*x(3)^3 + exp(-1.*(x(1)*x(2))) + 9]
J=@(x) [3, x(3)*sin(x(2)*x(3)), x(2)*sin(x(2)*x(3));
8*x(1) , -1250*x(2), 2;
-x(2)*exp(-x(1)*x(2)), -x(1)*exp(-x(1)*x(2)), 60*x(3)^2]
TOL=1e-10;
h=inf;
x=[1;1;1];
error = []
iterations = []
iter = 0
x_ref= [0.8333, 0.0175, -0.7933]
TOL=1e-15;
h=inf;
x=[1;1;1];
while (norm(h)>TOL)
h=J(x)\f(x);
x=x-h;
norm(h);
end
h
x_ref=x;
h=inf;
x=[1;1;1];
iter = 0;
error = []
iterations = []
while (norm(h)>TOL)
h=J(x)\f(x);
x=x-h;
iter = iter + 1
iterations = iter
error = norm(x-x_ref)
end
%I tried to change the limits of the plot!
xlim([1 13])
ylim([1 10^17])
semilogy(iterations, error, 'r--')

采纳的回答

Stephan
Stephan 2019-3-27
编辑:Stephan 2019-3-27
f=@(x) [3.*x(1) - cos(x(2)*x(3)) - 3/2;
4.*x(1)^2 - 625.*x(2)^2 + 2.*x(3) - 1;
20.*x(3)^3 + exp(-1.*(x(1)*x(2))) + 9]
J=@(x) [3, x(3)*sin(x(2)*x(3)), x(2)*sin(x(2)*x(3));
8*x(1) , -1250*x(2), 2;
-x(2)*exp(-x(1)*x(2)), -x(1)*exp(-x(1)*x(2)), 60*x(3)^2]
TOL=1e-10;
h=inf;
x=[1;1;1];
error = []
iterations = []
iter = 0
x_ref= [0.8333, 0.0175, -0.7933]
TOL=1e-15;
h=inf;
x=[1;1;1];
while (norm(h)>TOL)
h=J(x)\f(x);
x=x-h;
norm(h);
end
h
x_ref=x;
h=inf;
x=[1;1;1];
iter = 0;
error = []
iterations = []
while (norm(h)>TOL)
h=J(x)\f(x);
x=x-h;
iter = iter + 1
iterations(iter) = iter
error(iter) = norm(x-x_ref)
end
%I tried to change the limits of the plot!
semilogy(iterations, error, 'r--')
xlim([1 13])
ylim([10e-17 1])
resulting_plot.PNG
  3 个评论
Stephan
Stephan 2019-3-27
The main problem was:
iterations(iter) = iter
error(iter) = norm(x-x_ref)
You did not saved the actual value of every iteration, but overwrited the values in every iteration.
am
am 2019-3-27
I thought the vector understood that automatically, and grew like a dynamic list? And I did not ned indexing? I nerver used indexing before in my plots.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

标签

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by