How to plot sine graph by using for loop?
Afficher commentaires plus anciens
I tried to use a for loop to plot a sine graph. The values are generated but then when I try to graph them, I get either an error or no graph. Here's my code:
for i = 0:0.1:2*pi
m=sin(i);
n=i;
plot(n,m,'-r');
hold on
end
2 commentaires
Mohammad Alhashash
le 5 Juin 2019
do you want to animate the sine graph or just plot it
Xing Xing
le 5 Juin 2019
Réponse acceptée
Plus de réponses (1)
Mohammad Alhashash
le 5 Juin 2019
maybe that is what you need.
t= 0:0.01:2*pi;
m =sin(t);
t_plot = NaN(1,length(t));
m_plot = NaN(1,length(m));
u = 1;
for i = 1:length(t)
cla
t_plot(i) = t(u);
m_plot(i) = m(u);
plot(t_plot,m_plot)
drawnow
u = u + 1;
end
1 commentaire
Xing Xing
le 5 Juin 2019
Catégories
En savoir plus sur MATLAB dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!