plotting 2 variables as they change with respect to one another

2 次查看(过去 30 天)
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 CenterFile Exchange 中查找有关 Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by