Problem with plotting a semilog function

7 views (last 30 days)
am
am on 27 Mar 2019
Commented: am on 27 Mar 2019
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--')

Accepted Answer

Stephan
Stephan on 27 Mar 2019
Edited: Stephan on 27 Mar 2019
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 Comments
Stephan
Stephan on 27 Mar 2019
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 on 27 Mar 2019
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Linear and Nonlinear Regression in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!