'What is the proper way of calculating this flow calculation in Objective C?
I'm attempting to make a iPhone flow rate calculator that uses these 3 formulas.
Pressure ratio b
b = p2 + 0.1 / p1 + 0.1
The C-value and B-Value are given numbers depending on the valve. See page 6 on this pdf http://content2.smcetech.com/pdf/SY.New.pdf
Q Sonic flow
Q = 600 * C (p1 + 0.1) sqrt(293/(273 + t))
This is my working code.
float pressure1 = ([pressureMPa.text floatValue])+ 0.1;
float c = 600 * ([cValue.text floatValue]);
float temperature = ([tempC.text floatValue]);
float root = sqrtf(293/(273+temperature));
float sonicFlow = c * pressure1 * root;
Q
= flow rate l/minC
= dm^3/s * barp1
= Supply pressure: MPat
= temperature deg C
Q subsonic flow
Q = 600 * C (p1 + 0.1)* sqrt(1-(p2+0.1/p1 +0.1 - b / 1-b)^2) * sqrt(293/(273 + t))
Q
= flow rate l/min C
-value = dm^3/s * bar p1
Supply pressure: MPa t
temperature deg C b
-Value
What is the best way to write the subsonic flow code?
I have the beginning and end figured out it's the sqrt(1-(p2+0.1/p1 0.1)-b / 1-b)^2
I'm having issues with.
Solution 1:[1]
It's a little hard to see what the problem you're having is. Is it the syntax needed to do square roots and powers?
Square root: sqrtf(float)
Power: powf(float, float)
- second float is the power you want to raise the first float to.
Slightly easier for power-of-two (perhaps easier to read) is just to calculate the intermediate value first, then multiply it by itself:
valueA * valueA
Also, remember to be careful with regards to operator precedence in C. Multiplication, division and modulus are evaluated before addition and subtraction. Make sure you wrap things in brackets appropriately, or better still - as suggested by @Warkst under your post - do the calculation in multiple steps. So for the bit you're having trouble with:
// sqrt((1-((p2+0.1)/(p1+0.1))-b) / (1-b))^2
float part1 = (p2 + 0.1) / (p1 + 0.1);
float part2 = (1 - part1 - b) / (1 - b);
float part3 = sqrtf(part2);
float result = part3 * part3;
part1/2/3
are perhaps not ideal variable names, but you get the idea.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 |