Effacer les filtres
Effacer les filtres

how to plot different graphs for each iteration of a for loop?

4 vues (au cours des 30 derniers jours)
Nabhdeep Bansal
Nabhdeep Bansal le 24 Août 2014
Commenté : the cyclist le 24 Août 2014
t=linspace(0,12,1000);
T=2;
Y=g(t)';
f=0;
N=50;
hold on;
h=zeros(N,1);
clr=lines(N);
for n=1:25:N;
line(t,Y,'linewidth',5)
grid on;hold on;
A=(1/T)*quadgk(@g,0,T);
P=@(t)g(t).*(cos(n*pi*t));
Q=@(t)g(t).*(sin(n*pi*t));
Ax=(2/T)*quadgk(P,0,T);
Bx=(2/T)*quadgk(Q,0,T);
f=f+(Ax*cos(pi*n*t)+Bx*sin(pi*n*t));
final=A+f;
h(n)=plot(t,final,'linewidth',2,'Color',clr(n,:));
end
hold off;
legend(h, num2str((1:N)','harmonic-%d')).
I wish to plot different graphs for every iteration done. Is it poosible?

Réponse acceptée

the cyclist
the cyclist le 24 Août 2014
Modifié(e) : the cyclist le 24 Août 2014
Put the "figure" command at the start of the for loop.
doc figure
for details.
Also, in the line
A=(1/T)*quadgk(@g,0,T);
you don't want the "@". It should be just
A=(1/T)*quadgk(g,0,T);
  2 commentaires
Nabhdeep Bansal
Nabhdeep Bansal le 24 Août 2014
thanks Sir. Though I have already applied what you said but still I wish to know what is the difference between g & @g here.
the cyclist
the cyclist le 24 Août 2014
It's easy to get confused on this. What quadgk() requires as its first argument is a function handle. So, for example, if you wanted to integrate the sine function, you would use
@sin
because that is the handle to the sine function. However, you have defined g yourself, and g is itself a function handle.
See this documentation page for details.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Logical dans Help Center et File Exchange

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by