Pressure-Density Relationship with Van der Waals Equation in MATLAB

In summary, Van der Waals wasn't able to solve the equation for specific volume in a closed form, so he used a numerical method. He tried using a <for> loop to use Matlab's solve function, but he can't get the blasted thing to accept the variable <p> to operate in the loop. He finally solved the equation by creating a user defined function with the solve function in it and using a for loop to fill in the <p> input.
  • #1
enigma
Staff Emeritus
Science Advisor
Gold Member
1,757
17
I'm having some problems here...

Part of an assignment is to find how density of a gas changes as a function of pressure (1 - 700 atmospheres) using Van der Waals. Unless I've really missed the boat, there is no closed form solution for specific volume in that equation,

(p+a/v^2)(v-b)=RT

so a numerical method will need to be used.
I tried using a <for> loop to use Matlab's solve function, but I can't get the blasted thing to accept the variable <p> to operate in the loop.

Anybody have any ideas? I REALLY don't want to have to calculate the value for density 700 times for each of 2 gases... no... really, I don't.

Code:
% Nitrogen
% density variable = rho_N
% a=179.65
% b=.001398
% m=28
% R=8314
% T=298.15
for p=[1:700] % atmospheres
    FLAG=false; % Catch multiple real answers
    % (p*Pascals/atm + a/v^2)*(v-b) = R/m*t 
    v=solve('(p*1.01*10^5+179.65/v^2)*(v-.001398)=8314/28*298.15');
    for i=[1:length(v)] % find real component
        if isreal(v(i))
            if FLAG==true; %Catch multiple solutions
                error('multiple real solutions for N2 VdW')
            end
            rho_N(p)=1/v(i);
            FLAG==true;
        else % No reals error
            p
            error('no real solutions for N2 VdW')
        end
    end
end
 
Physics news on Phys.org
  • #2
Van der Waals did NOT have access to computers and numerical methods; that's hint #1.
 
  • #3
Wow... that was helpful, Bystander...
Van der Waals also wasn't asked to churn out 1400 data points by hand for a rocket propulsion systems homework assignment...

Anyway, I couldn't find a way to get the solve function to accept a parameter, so I dumped it and replaced it with a Newton-Raphson iteration.

Graphs came out fine.

EDIT: If anyone knows how to get the Matlab symbolic solver to accept a parameter, I'm still interrested.
 
Last edited:
  • #4
Originally posted by enigma
Wow... that was helpful, Bystander...
Van der Waals also wasn't asked to churn out 1400 data points by hand for a rocket propulsion systems homework assignment...

Needed something less subtle? Yes, you missed the boat --- VDW is a cubic EOS --- something engineers love for just the property you've missed. It does require that the user pick one of three roots, but even engineers are capable of that much.

1400 pts.? VdW did more than that a day, by hand --- 'course, it wasn't "rocket science" in his day. Quit yer belly-aching.
 
  • #5
Is this what you're referring to: 'Cubic Formula'

You've got to be joking. That's 25 steps. This sort of problem kinda does need to be done on a computer... hence me asking a question about Matlab code. Do you honestly think that coding a 25 step algoritm is an effective way to solve something when (assuming I found an answer to my question) it could be solved by typing solve('function'); ?!? At _very_ least 25 lines vs. 5 lines for 'solve' or 12 lines for Newton-Raphson including variable definition and error catches. And you're insulting engineers' problem solving skills? Please...

I don't feel I am bellyaching. I am irritated that I asked for help with a question about computer code and got a deliberately vague, tongue-in-cheek insult about my math skills.
 
Last edited:
  • #6
Originally posted by enigma
I'm having some problems here...
Code:
% Nitrogen
% density variable = rho_N
% a=179.65
% b=.001398
% m=28
% R=8314
% T=298.15
for p=[1:700] % atmospheres
    FLAG=false; % Catch multiple real answers
    % (p*Pascals/atm + a/v^2)*(v-b) = R/m*t 
    v=solve('(p*1.01*10^5+179.65/v^2)*(v-.001398)=8314/28*298.15');
end

I think if you print p inside your loop (decrease the stop value before you try it) you will find that it is a vector in MATLAB and solve does not like p(1:700). I'm pretty sure you wanted something more like
for i=[1:700]
v=solve('(p(i)*1.01 ... etc);

with the vector p filled out before the for loop. Sorry I don't have time to check this out before I reply.

mmwave.
 
  • #7
I know this post is really old. But i was having a similar problem. I first went to google, and when that didn't work. I actually had to think! lol. Either way I figured it out. You just need to create a user defined function with the solve function in it. From there you create a for loop with inputs for the function and tada! Since I can never make sense when people explain their code, see below to see it actually done. (i kinda just copied and pasted out of my code without my comments (cause it just looks messy in here with it so i just sort of added another set of comments to try to keep this post short-er) Hope this helps!

% My user defined function: See link below for more info on this, if needed
% http://www.mathworks.com/help/techdoc/ref/function.html
function a2 = ind_factor(lambda)
syms a l
S=solve('(((1-4*a)^2*(1-a))/(1-3*a))=l^2','a');
l=lambda;
% It's kinda long to explain why i did the next step, just know I needed the real part of the
% second row every time.
a2=real(eval(S(2,:)));

% How I used it in my code
lambda=0.5:0.1:10;
for i=1:length(lambda)
a2(i)=ind_factor(lambda(i));
end
 

1. What is the Van der Waals Equation and how does it relate to pressure and density?

The Van der Waals Equation is a mathematical model that describes the behavior of real gases, taking into account the intermolecular forces and the finite size of gas particles. It relates pressure and density through the equation: P = (nRT)/(V-nb) - a(n/V)^2, where P is pressure, n is the number of moles, R is the gas constant, T is temperature, V is volume, and a and b are constants that depend on the type of gas.

2. How is the Van der Waals Equation implemented in MATLAB?

In MATLAB, the Van der Waals Equation can be implemented by defining the variables (P, n, R, T, V, a, and b) and then using the equation to calculate the pressure for a given set of values. Alternatively, you can use the vdw function in the thermo package to solve the equation automatically.

3. Can the Van der Waals Equation be used for all types of gases?

No, the Van der Waals Equation is most accurate for gases that have small molecules and weak intermolecular forces. It is not suitable for highly polar or non-ideal gases. In those cases, more complex equations, such as the Peng-Robinson Equation, may be used.

4. What is the significance of the constants a and b in the Van der Waals Equation?

The constant a takes into account the attractive forces between molecules, while b represents the volume occupied by one mole of the gas particles. These constants allow the Van der Waals Equation to better approximate the behavior of real gases, which cannot be fully described by the ideal gas law.

5. Can the Van der Waals Equation be used to predict the behavior of gases at extreme pressures and temperatures?

No, the Van der Waals Equation is only accurate for gases at moderate temperatures and pressures. At extreme conditions, the intermolecular forces and molecular size become more significant and the equation breaks down. In these cases, more advanced equations or experimental data must be used.

Similar threads

  • Introductory Physics Homework Help
Replies
2
Views
801
  • Classical Physics
2
Replies
39
Views
2K
  • Introductory Physics Homework Help
Replies
6
Views
2K
  • Introductory Physics Homework Help
Replies
6
Views
1K
  • Classical Physics
Replies
1
Views
835
  • Introductory Physics Homework Help
Replies
9
Views
3K
  • Calculus and Beyond Homework Help
Replies
9
Views
1K
  • Advanced Physics Homework Help
Replies
23
Views
9K
  • Advanced Physics Homework Help
Replies
4
Views
3K
  • Advanced Physics Homework Help
Replies
6
Views
3K
Back
Top