why is my error plot not showing?

조회 수: 7(최근 30일)
Luis Garcia
Luis Garcia 2018년 2월 21일
편집: John BG 2018년 3월 2일
x = linspace(-10,10,100);
h = logspace(-1,-16,100);
error = (300);
hold on;
for i = 1:100
error(i) = abs(1-(exp(h(i)-exp(h(i))))/(h(i)));


John BG
John BG 2018년 3월 2일
편집: John BG 2018년 3월 2일
Hi Luis Garcia
there's no need for a for loop
x = linspace(-10,10,100);
h = logspace(-1,-16,100);
plot(x,err);grid on
and since you wish a display of the error, perhaps you would like to consider a table:
T1 =
100×2 table
Var1 Var2
__________________ ____________________
-10 2.65982076505758
-9.7979797979798 4.20132574336608
-9.5959595959596 6.38225249098713
-9.39393939393939 9.47080574029657
7.57575757575757 55914403982699.7
7.77777777777778 79257222980929
7.97979797979798 112345065800796
8.18181818181818 159246228104940
8.38383838383838 225727413882301
8.58585858585858 319962777042449
8.78787878787879 453538969555982
8.98989898989899 642879771226050
9.19191919191919 911265465581224
9.39393939393939 1.29169525302885e+15
9.5959595959596 1.8309446475436e+15
9.7979797979798 2.59531673164217e+15
10 3.67879441171442e+15
If you find this answer useful would you please be so kind to consider marking my answer as Accepted Answer?
To any other reader, if you find this answer useful please consider clicking on the thumbs-up vote link
thanks in advance
John BG
Modified Answer: Applied Jan Simon's advice not to use any variable named 'error', thanks

Jan 2018년 3월 2일
편집: Jan 2018년 3월 2일
Do not use "error" as a name of a variable, because this shadows an important built-in function with the same name. Afterwards trying to call error() can have unexpected results.
What is the purpose of
error = (300);
? Do you mean:
error = zeros(1, 100);
as a pre-allocation?
x = linspace(-10, 10, 100);
h = logspace(-1, -16, 100);
err = zeros(1, 100);
hold on;
for i = 1:100
err(i) = abs(1 - (exp(h(i) - exp(h(i)))) / (h(i)));
plot(x(i), err(i), 'o');
% ^^^ ^^^ modified
If you plot only single points, they are not visible. Use a marker like the 'o' to make them visible.
The other answers explained how to plot outside the loop. I wanted to add an explanation of why you do not see the plot with your method.

Star Strider
Star Strider 2018년 2월 21일
The plot call needs to go after the loop:
hold on;
for i = 1:100
error(i) = abs(1-(exp(h(i)-exp(h(i))))/(h(i)));
hold off


Find more on Creating and Concatenating Matrices in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by