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

13 views (last 30 days)
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?

Accepted Answer

the cyclist
the cyclist on 24 Aug 2014
Edited: the cyclist on 24 Aug 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 Comments
Nabhdeep Bansal
Nabhdeep Bansal on 24 Aug 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 on 24 Aug 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.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

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

Start Hunting!