#### enigma

Staff Emeritus

Science Advisor

Gold Member

- 1,738

- 9

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
```