フィルターのクリア

Why am I not able to plot the error vs iterations?

30 ビュー (過去 30 日間)
Ipshita
Ipshita 2020 年 11 月 21 日
コメント済み: Leo Torres 2020 年 12 月 3 日
I am new to MATLAB. Can someone please tell what should I do to get a plot of the error (here, abs (z-Z)) vs the number of iterations? Another question is, earlier I used different variables and for x-axis plot of i (using plot(i,abs(z-Z)), I used ilim([0 20]), which showed "undefined function ilim for input argument double". Does this mean I can plot only when variables are named as x and y?
z=1;
for i=1:100
f=(1-z)*exp(-2*z)-3*exp(-z)+2;
fprime=exp(-2*z)*(2*z-3)+3*exp(-z);
Z=z;
z=Z-f/fprime;
if abs (Z-z)<10^(-6)
break
end
x=i;
y=abs (Z-z);
plot(x,y)
xlim([0 20])
ylim([0 1])
end
  1 件のコメント
Serhiy Shtovba
Serhiy Shtovba 2020 年 11 月 21 日
  1. Type "hold on" before plot.
  2. But your code is not perfect. It is not a good idea to use plot inside for. It will be better to collect error on each iteration in vector, for example, y(i)=abs (Z-z), and finnaly, after end_of_for make your graph: plot(1:100, y, 'ro--'

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2020 年 11 月 21 日
You plotted inside the loop but did not put "hold on" so each time you plotted it blew away the prior data point. It's probably better to just keep track of the error and plot everything all at once once the loop exists:
z = 1;
for k = 1 : 100
f = (1-z)*exp(-2*z)-3*exp(-z)+2;
fprime = exp(-2*z)*(2*z-3)+3*exp(-z);
Z = z;
z = Z - f / fprime;
theError(k) = abs(Z - z);
if abs (Z-z)<10^(-6)
break
end
end
iterationNumber = 1 : length(theError);
plot(iterationNumber, theError, 'b.-', 'LineWidth', 2, 'MarkerSize', 30)
grid on;
xlim([1, max(iterationNumber)])
title('Error vs. Iteration Number', 'FontSize', 20);
xlabel('Iteration Number', 'FontSize', 20);
ylabel('abs(Error)', 'FontSize', 20);
Or else you'd have to say "hold on" inside the loop but then you'd only get markers and no lines connecting the markers.
z = 1;
for k = 1 : 100
f = (1-z)*exp(-2*z)-3*exp(-z)+2;
fprime = exp(-2*z)*(2*z-3)+3*exp(-z);
Z = z;
z = Z - f / fprime;
theError = abs(Z - z);
plot(k, theError, 'b.-', 'LineWidth', 2, 'MarkerSize', 30)
hold on;
if abs (Z-z)<10^(-6)
break
end
end
grid on;
title('Error vs. Iteration Number', 'FontSize', 20);
xlabel('Iteration Number', 'FontSize', 20);
ylabel('abs(Error)', 'FontSize', 20);
  3 件のコメント
Ipshita
Ipshita 2020 年 11 月 22 日
Thankyou very much for explaining so nicely and for fast response. I understood now :)
Leo Torres
Leo Torres 2020 年 12 月 3 日
Thanks for this clear answer. I was having a 'brain fart' plotting the relative error norm within a for loop and this helped greatly. Cheers! Saludos!

サインインしてコメントする。

その他の回答 (1 件)

Setsuna Yuuki.
Setsuna Yuuki. 2020 年 11 月 21 日
You can store the data of (Z-z) in a vector:
for ...
zz(i) = (Z-z)
if abs(zz)<10^(-6)
break
end
end...
And leave the graph out of the loop
x=1:100;
y=zz;
plot(x,y);
xlim([0 20])
ylim([0 1])
  1 件のコメント
Ipshita
Ipshita 2020 年 11 月 22 日
Thanks for answering :)

サインインしてコメントする。

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by