필터 지우기
필터 지우기

For loop with two conditions

조회 수: 3 (최근 30일)
Yamana Uno
Yamana Uno 2023년 10월 19일
편집: Voss 2023년 10월 19일
I need to create a for loop that runs from n=-2 to 2. When n=0, I must use a different equation (D_n_0) to plot the result. When n=-2,-1,1,2 I will use another equation (D_n) to plot the result.
How do I set up a foot loop that allows for a different equation when n=0 but not for other values?
Thank you!
  댓글 수: 1
Dyuman Joshi
Dyuman Joshi 2023년 10월 19일
You can use if, elseif, else conditions to check the value and the plot accordingly.
Can you specify the equations that you need to plot?

댓글을 달려면 로그인하십시오.

채택된 답변

Voss
Voss 2023년 10월 19일
for n = -2:2
if n == 0
% use D_n_0
else
% use D_n
end
end
  댓글 수: 2
Yamana Uno
Yamana Uno 2023년 10월 19일
t = 0:0.8;
for n = -2:2
if n == 0
D_n_0 = 312.5.*((-0.08481.*1j.*exp(-0.08481.*1j.*n)/-7.8539*1j)-((-7.8539.*1j.*n)/(-61.6837.*(n^2)+400)).*(exp(-0.08481.*1j.*n)-1));
xt0 = (D_n_0).*(exp(7.8539.*1j.*n.*t));
subplot(2,1,2)
plot(t,xt0)
hold on
else
D_n = 312.5.*(((exp(-0.08481.*1j.*n)-1)/-7.8539.*1j.*n)-(((-7.8539.*1j.*n)/(-61.6837.*(n^2)+400)).*(exp(-0.08481.*1j.*n)-1)));
xt = (D_n).*(exp(7.8539.*1j.*n.*t));
plot(t,xt)
hold off
title('Exponential Fourier Series')
end
end
This is what I have so far. I'm not sure what is incorrect but I cannot get this to plot and the command window says imginary parts of complex X and Y ignored. Is that the issue?
Voss
Voss 2023년 10월 19일
편집: Voss 2023년 10월 19일
t is a scalar, so you're only ever plotting one point each time, which you won't see without a data marker. Check that the t I used below is something like you intended.
hold off in the else block makes subsequent plotted lines replace what was already plotted, so I changed it to hold on.
hold on in the if block is not necessary because that block is only executed one time (when n == 0), so only one line is being plotted, so I removed that (and I assumed that plot should be in another subplot).
"the command window says imginary parts of complex X and Y ignored"
For plotting complex numbers you may want to plot the magnitude and phase separately.
t = linspace(0,0.8,100);
for n = -2:2
if n == 0
D_n_0 = 312.5.*((-0.08481.*1j.*exp(-0.08481.*1j.*n)/-7.8539*1j)-((-7.8539.*1j.*n)/(-61.6837.*(n^2)+400)).*(exp(-0.08481.*1j.*n)-1));
xt0 = (D_n_0).*(exp(7.8539.*1j.*n.*t));
subplot(2,1,2)
plot(t,xt0)
else
D_n = 312.5.*(((exp(-0.08481.*1j.*n)-1)/-7.8539.*1j.*n)-(((-7.8539.*1j.*n)/(-61.6837.*(n^2)+400)).*(exp(-0.08481.*1j.*n)-1)));
xt = (D_n).*(exp(7.8539.*1j.*n.*t));
subplot(2,1,1)
plot(t,xt)
hold on
title('Exponential Fourier Series')
end
end
Warning: Imaginary parts of complex X and/or Y arguments ignored.
Warning: Imaginary parts of complex X and/or Y arguments ignored.
Warning: Imaginary parts of complex X and/or Y arguments ignored.
Warning: Imaginary parts of complex X and/or Y arguments ignored.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

태그

제품

Community Treasure Hunt

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

Start Hunting!

Translated by