plotting 2 variables as they change with respect to one another
2 views (last 30 days)
Show older comments
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')
0 Comments
Answers (2)
Star Strider
on 15 Apr 2021
Edited: Star Strider
on 15 Apr 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.
0 Comments
Ahmed Redissi
on 15 Apr 2021
Edited: Ahmed Redissi
on 15 Apr 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')
0 Comments
See Also
Categories
Find more on Graphics Objects in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!