For loop not showing plot?

2 visualizaciones (últimos 30 días)
hag12899
hag12899 el 26 de Sept. de 2020
Respondida: David Hill el 26 de Sept. de 2020
I'm looking for some guidance:
This is intended to be a for loop to show the values of 1-50 for the inverse of mathematical e (e) with a plot showing the numbers 1-50 on the x-axis and the error on the y-axis. Using this code, I am getting the correct output of numbers in the command window but the plot that shows up lacks a line or any kind of data. Am I missing something in my code?
clear;
counter=1;
for n=1:50
error=(1-1./n).^n
end
% plot of error vs n
plot(n,error)
grid on;
title('error vs. n');
xlabel('n');
ylabel('error');

Respuestas (1)

David Hill
David Hill el 26 de Sept. de 2020
I answered your previous question also. This is the same answer. No loop is needed. Your override error on each loop to a single value.
n=1:50;
error=(1-1./n).^n;
plot(n,error);
grid on;
title('error vs. n');
xlabel('n');
ylabel('error');

Categorías

Más información sobre 2-D and 3-D Plots en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by