plotting 2 variables as they change with respect to one another

1 回表示 (過去 30 日間)
Joshua Anderson
Joshua Anderson 2021 年 4 月 15 日
編集済み: Ahmed Redissi 2021 年 4 月 15 日
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')

回答 (2 件)

Star Strider
Star Strider 2021 年 4 月 15 日
編集済み: Star Strider 2021 年 4 月 15 日
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 2021 年 4 月 15 日
編集済み: Ahmed Redissi 2021 年 4 月 15 日
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')

カテゴリ

Help Center および File ExchangeGraphics Objects についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by