필터 지우기
필터 지우기

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

조회 수: 4 (최근 30일)
Nabhdeep Bansal
Nabhdeep Bansal 2014년 8월 24일
댓글: the cyclist 2014년 8월 24일
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에서 Logical에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by