Jump to content

PHP Trig


Nikki-M

Recommended Posts

Hello everybody.

 

I've very new to PHP and a little rusty with math, so please forgive me if this is a silly question.

 

I've been playing around with averaging wind direction, for example I have 10 wind readings in degrees: 355, 350, 0, 15, 10, 5, 0, 5,350,355. So without doubt I need to employ trigonometry to calculate the average correctly.

 

Anyway, my formula words perfectly in a speadsheet or a calculator but PHP is giving a strange problem. When I try to calculate the cos(270)  i don't get 0 instead -1.83690953. the cos(271) is ok.

 

Please see this code example:

 

<?php

// VALUE IN DEGREES
$value = 270;

//CONVERT TO RADIANS

$value_rads = $value * PI()/180;

//PRINT OUTPUT

print $value . "</BR>";
print $value_rads . "</BR>";
print "SIN: " . sin($value_rads) . "</BR>";
print "COS: " . cos($value_rads) . "</BR>";

?>

 

The SIN gives the correct answer but the COS is way off. As I already said, this code works perfectly when $value = 271. The same problem exists if I use the deg2rag function.

 

I suspect that my logic is flawed as its very late here and I've had a few too many glasses of a rather cheeky red wine ;-) so I would appreciate it if somebody would be able to point me in the right direction.

 

Many thanks for reading

Nikki

 

 

Link to comment
https://forums.phpfreaks.com/topic/133854-php-trig/
Share on other sites

 

why not showing 0 now intrested using celi

<?php

// VALUE IN DEGREES
$value = 270;

//CONVERT TO RADIANS

$value_rads = $value * PI()/180;

//PRINT OUTPUT

print $value . "</BR>";
print $value_rads . "</BR>";
print "SIN: " . sin($value_rads) . "</BR>";
print "COS: " . cos(ceil($value_rads)) . "</BR>";

?>

Link to comment
https://forums.phpfreaks.com/topic/133854-php-trig/#findComment-696686
Share on other sites

Dude, average!

 

$Sum = 355+350+0+15+10+5+0+5+350+355;
$Sum = $Sum/10;
echo $Sum;

 

 

Hi there

 

No, normal averaging will not work with degrees (wind direction)

 

If you have the following: 270, 270, 260, 250, 240, 270, 270, 245, 245 then as you said the average is easy as the wind would be blowing from 232 degrees. However consider the following:

 

0, 355, 10, 30, 345, 0, 10, 1, 10, 350. Using a normal average this would mean that the wind was blowing from 111 degrees which is totally wrong. Calculated trigonometrical  the value is 3 degrees (NORTH).

Link to comment
https://forums.phpfreaks.com/topic/133854-php-trig/#findComment-696697
Share on other sites

You're not getting -1.83690953, you're getting -1.8369095307336E-16 (or something else near it) which is a HUGE difference and is practically equal to 0.

 

You know, you're right - told you that the wine was a bit cheeky :) Is there any reason why PHP does not show this as zero? (probably a simple math reason - the wines hit hard)

 

Regards

Nikki

 

Link to comment
https://forums.phpfreaks.com/topic/133854-php-trig/#findComment-696699
Share on other sites

May I ask why you need to do sin and cos functions at all? I'm taking calculus right now and I would just average. Add the numbers, divide by the number of numbers added.

 

Maybe I too am missing something. :)

 

Hi There

 

Yes, you are. Normal averaging wont work for wind direction. Its all fine until the wind blows from the North and fluctuates between say 10 and 350 degrees which then screws up the average.

 

Regards

Nikki

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/133854-php-trig/#findComment-696707
Share on other sites

dont get yours, where you get the 10 figures from............

 

wind format......

 

Knots:

m/s: 

Beaufort: 

km/h: 

mph:

 

this websites tell you everthink and any country but where you get you figures from.

http://www.windfinder.com/wind/windspeed.htm

 

Hiya

 

I'm talking about wind direction (in degrees) not velocity!

 

Regards

 

Nikki

 

Link to comment
https://forums.phpfreaks.com/topic/133854-php-trig/#findComment-696720
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.