Effacer les filtres
Effacer les filtres

plotting 2 variables as they change with respect to one another

4 vues (au cours des 30 derniers jours)
Joshua Anderson
Joshua Anderson le 15 Avr 2021
Modifié(e) : Ahmed Redissi le 15 Avr 2021
I'm trying to plot both A and M and they change simultaneously. If M changes, there is an associated A value within the given ranges. I'm having getting multiple M values to show even though multiple A values are being displayed. Any help would be greatly appreciated.
gamma = 1.4;
%M=1.53
for M= (0.1:0.01:2.19);
for A= (1:0.01:1)
A = 0.5*sqrt((1/M^2)*(2/(gamma+1)*(1+((gamma-1)/2)*M^2))^((gamma+1)/(gamma-1)))
end
end
plot(A,M)
ylabel('Area')
xlabel('Mach number')

Réponses (2)

Star Strider
Star Strider le 15 Avr 2021
Modifié(e) : Star Strider le 15 Avr 2021
The solution is to subscript ‘A’.
Try this:
gamma = 1.4;
%M=1.53
Mv = (0.1:0.01:2.19);
for k1 = 1:numel(Mv)
M = Mv(k1);
A(k1) = 0.5*sqrt((1/M^2)*(2/(gamma+1)*(1+((gamma-1)/2)*M^2))^((gamma+1)/(gamma-1)));
end
figure
plot(Mv,A)
ylabel('Area')
xlabel('Mach number')
grid
However in MATLAB the explicit loop is not necessary, using element-wise operations:
M = 0.1:0.01:2.19;
A = 0.5*sqrt((1./M.^2).*(2/(gamma+1).*(1+((gamma-1)/2).*M.^2)).^((gamma+1)/(gamma-1)));
figure
plot(M,A)
ylabel('Area')
xlabel('Mach number')
grid
This produces the same result as the loop.
See Array vs. Matrix Operations for details.

Ahmed Redissi
Ahmed Redissi le 15 Avr 2021
Modifié(e) : Ahmed Redissi le 15 Avr 2021
It's not clear why you are iterating over the values of A when you are not using them in the loop. Generaly, you shouldn't iterate over a variable and then change its content inside the loop unless you are incrementing or decrementing.
Does this perhaps solve your issue?
gamma = 1.4;
M = 0.1:0.01:2.19;
A = 0.5*sqrt((1./M.^2).*(2./(gamma+1).*(1+((gamma-1)/2)*M.^2)).^((gamma+1)./(gamma-1)));
plot(M,A)
ylabel('Area')
xlabel('Mach number')

Catégories

En savoir plus sur Gamma Functions dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by