Pressure-Density Relationship with Van der Waals Equation in MATLAB

Click For Summary

Discussion Overview

The discussion revolves around the challenges of using the Van der Waals equation to calculate the density of a gas as a function of pressure, specifically within the context of a MATLAB programming assignment. Participants explore numerical methods and coding techniques to solve the equation, which lacks a closed-form solution for specific volume.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant expresses difficulty in using MATLAB's solve function within a loop to compute density for varying pressures, indicating a need for numerical methods due to the absence of a closed-form solution.
  • Another participant comments on the historical context of Van der Waals' work, suggesting that numerical methods were not available to him, which some find unhelpful in the context of modern computational needs.
  • A different participant reports success using a Newton-Raphson iteration method instead of the solve function, but still seeks advice on how to parameterize the symbolic solver in MATLAB.
  • One participant challenges the notion that a cubic formula approach is practical, arguing that coding a lengthy algorithm is inefficient compared to using built-in functions like solve.
  • A later reply introduces a user-defined function approach to utilize the solve function effectively within a loop, sharing code snippets as a potential solution for others facing similar issues.

Areas of Agreement / Disagreement

Participants express differing views on the appropriateness of various methods for solving the problem, with some advocating for traditional mathematical approaches while others emphasize the necessity of computational solutions. There is no consensus on the best method to use.

Contextual Notes

Participants note the complexity of the Van der Waals equation and the challenges of selecting the correct root in cubic equations, as well as the limitations of MATLAB's symbolic solver in handling parameters directly.

Who May Find This Useful

This discussion may be useful for students and practitioners in engineering and physics who are working with the Van der Waals equation and seeking computational methods for solving related problems in MATLAB.

enigma
Staff Emeritus
Science Advisor
Gold Member
Messages
1,739
Reaction score
20
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
Van der Waals did NOT have access to computers and numerical methods; that's hint #1.
 
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:
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.
 
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 algorithm 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:
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
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 39 ·
2
Replies
39
Views
4K
  • · Replies 6 ·
Replies
6
Views
3K
Replies
6
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
Replies
9
Views
2K
  • · Replies 23 ·
Replies
23
Views
12K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 6 ·
Replies
6
Views
4K