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

4 次查看(过去 30 天)
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?

采纳的回答

the cyclist
the cyclist 2014-8-24
编辑:the cyclist 2014-8-24
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 个评论
Nabhdeep Bansal
Nabhdeep Bansal 2014-8-24
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 2014-8-24
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.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by