Luis Garcia
Luis Garcia

Reputation: 61

why is my error not showing in matlab?

Im trying to plot an error graph but when I run it, it does not show anything.

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']);

Upvotes: 0

Views: 55

Answers (1)

Tommaso Belluzzo
Tommaso Belluzzo

Reputation: 23675

Instead of performing the computations and the plotting operations within a for loop, you can vectorize the whole process to achieve a correct result together with a better performance. Just remember to convert your scalar operators to element-wise operators in order to avoid size consistency errors (for instance, ./ must be used instead of /).

Here is the code:

x = linspace(-10,10,100);
h = logspace(-1,-16,100);
error = abs(1 - (exp(h - exp(h)) ./ h));
plot(x,error);

And here is the result:

Result

Upvotes: 1

Related Questions