0

Code

clear;clc
T=800;
Pc=48.45;
Tc=375;
w=0.153;
R=82.06;
a=((0.45724)*(R^2)*(Tc^2))/Pc;
b=((0.07780)*R*Tc)/Pc;
B=(0.37464+(1.54226*w)-(0.26992*(w^2)));
Tr=T/Tc;
s=(1+(B*(1-sqrt(Tr))))^2;
for Vm=90:5:1000
    P=((R*T)/(Vm-b))-((a*s)/((Vm)^2+(2*b*Vm)-b^2));
end
plot(Vm, P)

Problem

Every time I run this code, it comes out with a completely empty plot with just numbers on both axes as the image shown below. I've checked my code a few times, but I still can't find the problem, especially since the code runs with no errors. The result I am supposed to be getting on this plot is the behavior of P as the value of Vm increases.

The result of the code

Additional information about the source of the question

Here's the original question if you're interested (Exercise 1).

The original question (Exercise 1)

1 Answer 1

1

Try displaying your variables. You'll see Vm is not an array, rather it's a single-valued scalar. When you loop over Vm it takes one value at a time; it doesn't build an array.

MATLAB can do calculations on multiple values at once, so if you define Vm to be an array and drop the loop I'm guessing it'll work...

Try something like this (replace the for-loop with these lines):

Vm = 90:5:1000
P=((R*T)./(Vm-b))-((a*s)./((Vm).^2+(2*b.*Vm)-b^2));

P will then be an array. Notice we use .* rather than * when multiplying by the array Vm since we want to do element-wise multiplication, rather than matrix multiplication. Similarly we use ./ rather than / and .^ rather than ^.

EDIT: If you need to use a for-loop then you could define both P and Vm as arrays, and then work on each element separately within a loop:

Vm = 90:5:1000;
P = NaN(size(Vm));
for i=1:numel(Vm)
    P(i)=((R*T)./(Vm(i)-b))-((a*s)./((Vm(i)).^2+(2*b.*Vm(i))-b^2));
end

Since the above is working on scalar values, it doesn't matter if you use .* or *...

Sign up to request clarification or add additional context in comments.

5 Comments

I tried that, but I am still getting the same exact empty plot.
Hmmm, okay, Just logged on to my MATLAB machine and tried it out and it works fine for me, I get a nice plot that looks a bit like an exponential decay? Try again? Just to be clear, you replace the for-loop with those two lines. You don't do the for loop.
Oh I am sorry, I reread your answer and noticed you said to eliminate the for loop. It works without the for-loop without a problem, however, on the original question, it's required for me to use a for-loop for this code. Do you think there's any way to go around this problem by using a for-loop?
Seems a bit stupid to insist on a for-loop (tell your teacher that for extra credit)... But you could do: Vm = 90:5:1000; P = NaN(size(Vm)); for i=1:numel(Vm); P(i)=((R*T)./(Vm(i)-b))-((a*s)./((Vm(i)).^2+(2*b.*Vm(i))-b^2)); end; plot(Vm, P)
@Justin You should edit that into your answer. And yes, it is stupid, but a lot of teachers think forcing students to use a for-loop in unnecessary circumstances is good for learning the language, which is wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.