C/C++ Finding volume (van der waal eos) c++

  • Thread starter Thread starter Lord Dark
  • Start date Start date
  • Tags Tags
    C++ Volume
AI Thread Summary
The discussion revolves around a program created to calculate the volume per mole using the van der Waals equation of state (EOS). The user encounters an issue with the equation for pressure, where the variable for volume (v) cannot be isolated due to its dependence on pressure (p), leading to incorrect results. Participants suggest that the user needs to rearrange the equation into a cubic form, which results in a third-degree polynomial in v. They point out that while C++ can be used to solve such equations, it may not be the most straightforward approach. Recommendations for alternative software include MATLAB and Mathematica, which are better suited for solving cubic equations. The user expresses intent to try Mathematica for this purpose.
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 ;

cout << " Enter the value of pressure in KPa = " ;
cin >> p ; cout << endl ;
cout << " Enter the value of the tempreture in Kelvin = " ;
cin >> t ; cout << endl ;
cout << " Enter the value of the pressure critical point in Kpa = ";
cin >> pcr ; cout << endl ;
cout << " Enter the value of the temrpeture critical point in K = ";
cin >> tcr ; cout << 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)) ;

cout << " 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 ??
 
Technology 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
p~=~\frac{r~t}{v - b} - \frac{a}{v^2}

which I assume is the correct equation.

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

Simplifying and moving terms around results in the following equations:

p~v^3 - p~b~v^2 - r~t~v^2 + a~v + a~b = 0

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

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 :)
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
5
Views
3K
Replies
17
Views
2K
Replies
2
Views
3K
Replies
13
Views
7K
Back
Top