why is my error plot not showing?

7 views (last 30 days)
Luis Garcia
Luis Garcia on 21 Feb 2018
Edited: John BG on 2 Mar 2018
x = linspace(-10,10,100);
h = logspace(-1,-16,100);
error = (300);
figure(1);
hold on;
for i = 1:100
error(i) = abs(1-(exp(h(i)-exp(h(i))))/(h(i)));
plot(x,error(i));
end
disp([error']);

Answers (3)

John BG
John BG on 2 Mar 2018
Edited: John BG on 2 Mar 2018
Hi Luis Garcia
there's no need for a for loop
x = linspace(-10,10,100);
h = logspace(-1,-16,100);
err=abs(ones(1,numel(h))-(exp(h-exp(h)))./h)
plot(x,err);grid on
.
and since you wish a display of the error, perhaps you would like to consider a table:
.
T1=table(x',err')
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
.
Luis
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
Jan on 2 Mar 2018
Edited: Jan on 2 Mar 2018
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);
figure(1);
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
end
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 on 21 Feb 2018
The plot call needs to go after the loop:
figure(1);
hold on;
for i = 1:100
error(i) = abs(1-(exp(h(i)-exp(h(i))))/(h(i)));
end
plot(x,error)
hold off

Categories

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

Tags

Community Treasure Hunt

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

Start Hunting!