Finding volume (van der waal eos) c++

  • Context: C/C++ 
  • Thread starter Thread starter Lord Dark
  • Start date Start date
  • Tags Tags
    C++ Volume
Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
5 replies · 7K views
Lord Dark
Messages
120
Reaction score
0
Hi guys ,,

I made a program to find the volume/mol using van der waals EOS

/* following equation is van der waals eos which gives you the volume per mol */

#include <iostream>
#include <math.h>
#include <iomanip>

using namespace std;

int main ()
{
double p, // pressure in KPa
v, // volume in meter cubed
r, // gas constant or universal constant
t, // tempreture in kelvin
a,b, // van der waals constants
tcr,pcr ;// the critical points of the pressure & tempreture

r = 8.318 ;

count << " Enter the value of pressure in KPa = " ;
cin >> p ; count << endl ;
count << " Enter the value of the tempreture in Kelvin = " ;
cin >> t ; count << endl ;
count << " Enter the value of the pressure critical point in Kpa = ";
cin >> pcr ; count << endl ;
count << " Enter the value of the temrpeture critical point in K = ";
cin >> tcr ; count << endl ;

a = (27*pow(r,2)*pow(tcr,2))/(64*pcr);
b = (r*tcr)/(8*pcr);

p = ((r*t)/(v-b)) - (a/pow(v,2)) ;

count << " The value of the Volume in m3/mol = " << v ;

return 0;
}

I know that my mistake is in
p = ((r*t)/(v-b)) - (a/pow(v,2))
the problem is that I can't let v be alone ,, so can anyone help me to find a way to slove for v ??
 
Physics news on Phys.org
You can't set p to a value that depends on v, which is uninitialized, so what will happen is that the value that was input for p will be overwritten by a garbage value.

You need to do some algebra first. You have
[tex]p~=~\frac{r~t}{v - b} - \frac{a}{v^2}[/tex]

which I assume is the correct equation.

If you multiply both sides of this equation by v2(v - b), you get this equation:
[tex]p~v^2(v - b)~=~r~t~v^2 - a(v - b)[/tex]

Simplifying and moving terms around results in the following equations:

[tex]p~v^3 - p~b~v^2 - r~t~v^2 + a~v + a~b = 0[/tex]

[tex]v^3 - (b + \frac{r~t}{p})v^2 + \frac{a}{p}v + \frac{a~b}{p} = 0[/tex]

Unfortunately, this is a third-degree equation in v. There are ways to solve cubics, but they are fairly involved and aren't too well known.
 
so I can't use c++ for Cubic equations right ??

can you recommend a program that will work with cubic equations ??
 
iirc, MATLAB or mathematica can do that job.
 
Lord Dark said:
so I can't use c++ for Cubic equations right ??

can you recommend a program that will work with cubic equations ??
Nor can you use C++, C, C#, Fortran, Basic, and most other programming languages to solve quadratic or even linear equation. They aren't programs; they are general purpose programming languages that you can use to write programs.
 
Ok ,, I guess I'll try mathematica .

Thanks very much guys :)