Clear Filters
Clear Filters

For loop with two conditions

3 views (last 30 days)
Yamana Uno
Yamana Uno on 19 Oct 2023
Edited: Voss on 19 Oct 2023
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 Comment
Dyuman Joshi
Dyuman Joshi on 19 Oct 2023
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?

Sign in to comment.

Accepted Answer

Voss
Voss on 19 Oct 2023
for n = -2:2
if n == 0
% use D_n_0
else
% use D_n
end
end
  2 Comments
Yamana Uno
Yamana Uno on 19 Oct 2023
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 on 19 Oct 2023
Edited: Voss on 19 Oct 2023
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.

Sign in to comment.

More Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products

Community Treasure Hunt

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

Start Hunting!