Pages: [1]   Go Down
Author Topic: linear algebra formula  (Read 702 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 1
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I am not sure on the logic of computing algebra. My code and compile errors are below. I think my problem is in my syntax (of the operator ^), and would appreciate someone pointing me in the right direction. I have also tried " ^= "

invalid operands of types 'double' and 'double' to binary 'operator^'
sketch_nov29a.ino: In function 'int getHumidity()':
sketch_nov29a:457: error: invalid operands of types 'double' and 'double' to binary 'operator^'
sketch_nov29a:459: error: conflicting declaration 'float e'
sketch_nov29a:448: error: 'e' has a previous declaration as 'const float e'
sketch_nov29a:461: error: invalid operands of types 'double' and 'double' to binary 'operator^'
sketch_nov29a:463: error: conflicting declaration 'float e'
sketch_nov29a:448: error: 'e' has a previous declaration as 'const float e'

Code:
int getHumidity(){
  
 sensors.requestTemperatures();  

   float temp0 = sensors.getTempC(t0);
   float temp1 = sensors.getTempC(t1);

  const float p_sta = 754; //local atmospheric pressure constant in millibars
  const float e = 2.7182818;
  
  float t_w = temp0; //wet-bulb temperature in *C
  float t = temp1;  //air temperature in *C
  
  
  float e_w = 6.112*e^(17.67*t_w/t+243.5);   //vapour pressure in relation to wet-bulb temperature
  
  float e = e_w - p_sta*(t-t_w)*0.00066*(1+(0.0015*t_w)); //actual vapour pressure
  
  float e_s = 6.112*e^(17.67*t/t+243.5); //saturated vapour pressure
  
  
  int rh = (e/e_s)*100;
  
  Serial.print(rh);

}
« Last Edit: December 12, 2013, 03:39:22 am by lazaah » Logged

ivrea (to)
Offline Offline
Faraday Member
**
Karma: 87
Posts: 5017
miaaao ^-^
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Why you use operators without checking the correct syntax in a language reference?   smiley-lol
Languague C reference, see in this page what the real use of ^ operator:
http://arduino.cc/en/Reference/BitwiseAnd

Use this:
http://arduino.cc/en/Reference/Pow
« Last Edit: December 12, 2013, 03:59:26 am by nid69ita » Logged

my name is IGOR, not AIGOR

Manchester (England England)
Online Online
Brattain Member
*****
Karma: 634
Posts: 34572
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Computer syntax has absoloutly nothing to do with linear algebra which is a branch of mathematic.
Logged

ivrea (to)
Offline Offline
Faraday Member
**
Karma: 87
Posts: 5017
miaaao ^-^
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

And also warning about float (or double, same on Arduino):
http://arduino.cc/en/Reference/Float
"Floats have only 6-7 decimal digits of precision."
Logged

my name is IGOR, not AIGOR

Offline Offline
Newbie
*
Karma: 1
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Cheers for the advice everyone. I have updated my sketch to use pow() and have reduced my floats to 5 decimals. I am having problems with the arithmetic however. My guess is that using the pow() function is resulting in numbers too large to be floats? Serial prints the result as follows:

"Temp0 is : 21.75   Temp1 is : 21.75   e_s is : inf   e_w is : inf   e is : inf   R/H is : 0"

Code:
int printHumidity(){
 
 
 sensors.requestTemperatures();
 
 float e = 2.71828; // the number e, used to calculate e_s and e_w
 
 float temp0 = sensors.getTempC(t0);
 float temp1 = sensors.getTempC(t1);
   
  float t_w = temp0; //wet-bulb temperature in *C
  float t = temp1;  //air temperature in *C
  const float p_sta = 754; //local atmospheric pressure constant in millibars
 
  float e_s = 6.112 * pow(e, (17.67 * t / t + 243.5)); //saturated vapour pressure
  float e_w = 6.112 * pow(e, (17.67*t_w/t+243.5));   //vapour pressure in relation to wet-bulb temperature
   
  e = e_w - p_sta * (t - t_w) * 0.00066 *(1 + (0.0015 * t_w)); //actual vapour pressure
 
  int rh = (e/e_s)*100;
 
  Serial.print("Temp0 is : ");
  Serial.print(temp0);
  Serial.print ("\t");
 
  Serial.print("Temp1 is : ");
  Serial.print(temp1);
  Serial.print ("\t");
 
  Serial.print("e_s is : ");
  Serial.print(e_s);
  Serial.print ("\t");
 
  Serial.print("e_w is : ");
  Serial.print(e_w);
  Serial.print ("\t");
 
  Serial.print("e is :");
  Serial.print(e);
  Serial.print ("\t");
 
  Serial.print("R/H is :");
  Serial.print(rh);
  Serial.print("\n");

}
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 640
Posts: 50335
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
  float e_s = 6.112 * pow(e, (17.67 * t / t + 243.5)); //saturated vapour pressure
17.67 * t / t is 17.67. So, e_s = 6.112 * pow(2.71828, 261.17). I wouldn't be at all surprised to find that pow returned infinity for that value.

e_w is calculated just about as badly.

I'm guessing that you are missing parentheses.

Logged

Offline Offline
Edison Member
*
Karma: 49
Posts: 1670
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You can use exp() instead of pow. It should give a more accurate answer.

Pete
Logged

Offline Offline
Newbie
*
Karma: 1
Posts: 24
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Guess I will never know how accurate this is, but it appears to be right! exp() worked a charm. I did have to make adjustments to my parenthesis. Cheers everyone, I will have to do a write-up at some stage as this should be far more reliable that those DHT**'s !

"Temp0 is : 18.00   Temp1 is : 20.50   e_s is : 24.10   e_w is : 20.39   e is :19.11   R/H is :79"

Code:
int printHumidity(){
 
 sensors.requestTemperatures();
 
 float temp0 = sensors.getTempC(t0);
 float temp1 = sensors.getTempC(t1);
   
  float t_w = temp0; //wet-bulb temperature in *C
  float t = temp1;  //air temperature in *C
  const float p_sta = 754.0; //local atmospheric pressure constant in millibars
 
  float e_s = 6.112 * exp((17.67 * t) / (t + 243.5)); //saturated vapour pressure
  float e_w = 6.112 * exp((17.67 * t_w) / (t + 243.5));   //vapour pressure in relation to wet-bulb temperature
   
  e = e_w - p_sta * (t - t_w) * 0.00066 *(1 + (0.0015 * t_w)); //actual vapour pressure
 
  int rh = ((e/e_s)*100.0);
 
}

Logged

Pages: [1]   Go Up
Jump to: