Hi
I am computing errors on a function and to ensure properly indexed data values have remapped my loop:
for n2=1:11
n=(n2-1)/10;
I am trying to to loop between 0 and 1 using 0.1 steps:
n= 0:0.1:1
Everything in the code works fine (I think!) At the end of my loop I have captured the data and attempt to plot.
I have used the simple command:
plot(error,'g')
This plots the error with the values n2 on the x-axis but I need them to show the values from n. The error does not need to change as this has been generated by the appropriate value (n) and not the index value (n2).
Does anyone know of a way to ensure the plot displays the correct value on the x-axis?
Best wishes
Joe

2 件のコメント

Walter Roberson
Walter Roberson 2016 年 11 月 13 日
Please do not use error as the name of a variable, as it is a crucial MATLAB function for error control.
Joe Bannen
Joe Bannen 2016 年 11 月 13 日
Walter
The code uses multiple errors (none of them coded as error!) I shouldn't have included this in the forum, my apologies.
Joe

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

 採用された回答

Star Strider
Star Strider 2016 年 11 月 13 日

1 投票

If ‘error’ is the same length as ‘n’, try this:
plot(n, error, 'g')

9 件のコメント

Joe Bannen
Joe Bannen 2016 年 11 月 13 日
Hi
Thanks this works a treat!
plot([0:0.1:1],error2,'g')
One strange thing appears to be happening -> 0 is not displayed on the x-axis. It seems the code is not evaluating the case n=0 so my n2 to n loop is not working!
Where do you think I am going wrong?
Best wishes
Joe
Star Strider
Star Strider 2016 年 11 月 13 日
My pleasure!
You didn’t post your code, so I don’t know the reason ‘n=0’ isn’t being evaluated. It could be that you are doing a calculation that results in ‘0/0’, that would result in NaN, and NaN values are never plotted.
You can define the range of your axes so that they display other than the default 'XLim' (the x-limit) range.
For example:
set(gca, 'XLim',[min(n) max(n)])
Put this after your plot call, because the plot call defines the axes, and gca used the axes handle it created to set the axis limits here.
Joe Bannen
Joe Bannen 2016 年 11 月 13 日
編集済み: Joe Bannen 2016 年 11 月 13 日
Hi
Thanks for this. Using the set command I can see that the display is (on the x-axis) from 2 to 11!
My 0 is generated from (1-1)/10 and 0.1 generated from (2-1)/10. The second one (0.1) is being plotted as n2=2, but my n1=1 is failing to plot.
Should I be using a different approach to generate 0:0.1:1?
Cheers
Joe
Star Strider
Star Strider 2016 年 11 月 13 日
My pleasure.
You can use the:
set(gca, 'XLim',[0 1])
to define the x-limits to whatever you want them to be. See the documentaton for axis and Axes Properties for details.
I would just use:
n = 0 : 0.1 : 1;
to create your ‘n’ vector. MATLAB use a sophisticated algorithm to create colon-operator vectors to minimise the accumulating errors that would result from building it sequentially from one end of the vector.
There are actually at least two reliable ways to create your vector:
nc = 0 : 0.1 : 1; % ‘colon’ Operator
nl = linspace(0, 1, 11); % ‘linspace’ Function
n_dif = nc - nl; % Difference
Joe Bannen
Joe Bannen 2016 年 11 月 13 日
Intriguing!
How would I loop through the n_dif and maintain the output as as a vector that can be plotted for each loop?
Cheers
Joe
Star Strider
Star Strider 2016 年 11 月 13 日
I have no idea what question you’re asking. I would use ‘nl’ for your vector. You’ve not posted your code, so I can’t comment further.
The purpose of calculating ‘n_dif’ was to demonstrate the difference between the two methods of calculating the vector. It has no utility beyond that.
Star Strider
Star Strider 2016 年 11 月 13 日
I don’t have the Financial Toolbox (I’ve no need for it), so I can’t run your code.
Looking at your code, the problem may be in the way you calculate ‘n’. I would define it as:
nv = linspace(0, 1, 11);
for n2=1:11
n=nv(n2);
tic
shape=n;
:
< REST OF YOUR LOOP >
:
end
That refers it to elements of ‘nv’. I have no idea what your problem is, since I can’t run your code, but if the value of ‘n’ is the problem, this may be the solution.
Also, remember to plot your dependent variables as functions of ‘n’:
figure
plot(nv, time,'r')
title('Shape Parameter v. CPU time')
xlabel('Shape Parameter')
ylabel('CPU time')
figure
plot(nv, maxerror,'g')
title('Shape Parameter v. Maximum Error')
xlabel('Shape Parameter')
ylabel('Maximum Error')
figure
plot(nv, rms_error,'m')
title('Shape Parameter v. Root Mean Square Error')
xlabel('Shape Parameter')
ylabel('RMS Error')
That should work.
Joe Bannen
Joe Bannen 2016 年 11 月 13 日
Hi
Thanks for that. A brilliant approach.
A quick point --> How could this be extended to look at 0.1 increments between 0 and 10 (or any other number)?
Cheers
Joe
Star Strider
Star Strider 2016 年 11 月 13 日
My pleasure. Thank you.
For creating vectors, there are two primary approaches depending on the result you want. I already illustrated them, so I’ll discuss them in a bit of detail.
  • If you want to define a vector with a fixed step (the length of the vector will vary to extend to the final value), use the colon operator;
  • If you want to define a vector with a fixed length (the constant step increment will vary to satisfy the length requirement), use the linspace function.
So for a fixed step of 0.1, use the colon operator:
start_value = 0;
final_value = 10:
step_size = 0.1
nv = start_value : step_size : final_value;

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

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeProgramming についてさらに検索

製品

タグ

質問済み:

2016 年 11 月 13 日

コメント済み:

2016 年 11 月 13 日

Community Treasure Hunt

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

Start Hunting!

Translated by