Effacer les filtres
Effacer les filtres

For loop with two conditions

3 vues (au cours des 30 derniers jours)
Yamana Uno
Yamana Uno le 19 Oct 2023
Modifié(e) : Voss le 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 commentaire
Dyuman Joshi
Dyuman Joshi le 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?

Connectez-vous pour commenter.

Réponse acceptée

Voss
Voss le 19 Oct 2023
for n = -2:2
if n == 0
% use D_n_0
else
% use D_n
end
end
  2 commentaires
Yamana Uno
Yamana Uno le 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 le 19 Oct 2023
Modifié(e) : Voss le 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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Tags

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by