Problem with plotting a semilog function

3 ビュー (過去 30 日間)
am
am 2019 年 3 月 27 日
コメント済み: am 2019 年 3 月 27 日
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 Center および File ExchangeRead, Write, and Modify Image についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by