Pregunta

So I have this code that is meant to do this equation:

(1 / 15) * arccos(-tan(L) * tan(23.44 * sin(360 * (D + 284) / 365))) 

and for testing purposes alert it! But for some reason it returns "NaN". I've probably done something really stupid :P

var now = new Date();
var start = new Date(now.getFullYear(), 0, 0);
var diff = now - start;
var oneDay = 1000 * 60 * 60 * 24;
var d = Math.floor(diff / oneDay);


var lat = position.coords.latitude;
var long = position.coords.longitude;
var tanlat = Math.atan(lat); 
var tantwentythree = Math.tan(23.44);
var dayplus = d + 284;
var sinday = Math.sin(360 * dayplus);
var arccos = Math.acos(tanlat);
var start = 1 / 15;
var equation = start * arccos * tantwentythree * sinday / 365;
alert(equation);


var result = rad2deg / 15 * Math.acos(-Math.tan(lat*deg2rad) * Math.tan(23.44*deg2rad * Math.sin(360*deg2rad * (day + 284) / 365)));
alert(result);
var resultstr = string(result);
document.getElementById('sunrise').innerHTML = resultstr;

I have a with the id "sunrise", when the function is run, the var resultstr should be added to the (i think?) But i doesn't!

¿Fue útil?

Solución

maybe you wanted

tanlat= Math.tan(lat)

not Math.atan(lat) which is the inverse...

But then you have another problem: all the arguments to the trigonometric functions should be radians, not degrees. You will have to multiply degrees by Math.PI / 180

But then you have even more problems. The tan(23.44) is not a quantity you need, you need the tan of a product, to start with.

What I would do is simply take your formula as it is, adding only the degrees to radians conversion:

var deg2rad = Math.PI / 180;
var rad2deg = 180 / Math.PI;
var result = rad2deg / 15 * Math.acos(-Math.tan(lat*deg2rad) *
             Math.tan(23.44*deg2rad * Math.sin(360*deg2rad * (d + 284) / 365)))
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top