Reputation: 61
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
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:
Upvotes: 1