I have a code which is definitely correct, but the loglog graph wont work properly.

33 ビュー (過去 30 日間)
Rayhaan Bhikha
Rayhaan Bhikha 2016 年 3 月 8 日
移動済み: DGM 2024 年 2 月 11 日
clear all
x=linspace(-1,1,40);
t=linspace(0,1,20);
n=[5,10,20];
hold on
for i=1:3
U=(exp(-(pi.^2)*t(n(1,i)))+sin(t(n(1,i))))*sin(pi*x);
v=CN(n(1,i));
En(i)=max(abs(U-v));
end
loglog(n,En,'--')
hold on
loglog(n,n)
this is the graph I get
this is the graph i should get
  2 件のコメント
Star Strider
Star Strider 2016 年 3 月 8 日
What’s ‘CN’?
John D'Errico
John D'Errico 2024 年 2 月 10 日
編集済み: John D'Errico 2024 年 2 月 10 日
It is definitely perfectly, absolutely correct, BUT, it does not work. Why does that seem to put the lie to your claim of correctness?
The point being, a simple definition of correctness of code, is that it works as designed, and it does what it is supposed to do.

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

採用された回答

Steven Lord
Steven Lord 2016 年 3 月 8 日
You told MATLAB to " hold on ". That means MATLAB is allowed to change a few properties of the axes (from that documentation page: "MATLAB® adjusts axes limits, tick marks, and tick labels to display the full range of data.") but keeps most properties (including XScale and YScale, the properties that control whether the axes are in linear or log scale) unchanged.
You can explicitly tell MATLAB to change the scale at the end of your code and it will do it, but HOLD prevents it from changing the scale automatically.
set(gca, 'XScale', 'log', 'YScale', 'log');
If you had a handle to the axes in a variable, like ax, you could instead using the dot notation to change the scales.
ax.XScale = 'log';
ax.YScale = 'log';
  4 件のコメント
Hank Lin
Hank Lin 2020 年 5 月 24 日
20200524
Thank you very much. :)
Jürgen
Jürgen 2024 年 2 月 10 日
移動済み: DGM 2024 年 2 月 11 日
I can understand the arguments of Steven Lords answer.
However the arguemtns are kind of artificial and the behaviour of two loglog in succession guarded wiht hold on is simply unexpected and irritating. It should be regarded as an error and be corrected in the next release.
I stumpled several times on that obstacle, look up the topic, apply the workaround, forget it again and stumble again into the same trap after a while.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeData Exploration についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by