I've been following this guide, porting it to javascript: http://www.saao.ac.za/public-info/sun-moon-stars/sun-index/how-to-calculate-altaz/
Everything was going swimmingly up until 9.(right ascension) and 10.(declination). I can't recreate the answers they give for these.
(9) find alpha the right ascension of the sun: (a) for cape town:
lambda = 326.186
epsilon = 23.4396
alpha = arctan (tan(lambda) x cos(epsilon)) // in same quadrant as lambda
// THEIR RESULT
alpha = 328.428
// MY RESULT
var DEGREES = function (val) {
return val / (Math.PI / 180);
};
var alpha = Math.atan(Math.tan(lambda) * Math.sin(epsilon));
alpha = 0.495;
alpha = DEGREES(0.495) = 28.39;
I also tried:
var alpha = Math.atan2(Math.tan(lambda) * Math.sin(epsilon), lambda);
alpha = DEGREES(result) = 1.321;
Not even close!
And 10(a), the declination
delta = arcsin (sin(lambda) x sin(epsilon))
// THEIR RESULT
(a) delta = -12.789
// MY RESULT
var result = Math.asin(Math.sin(eclipticLong) * Math.sin(obliq));
result = DEGREES(result);
result = -10.966;
As you can see I'm clutching at straws as I don't really have a clue about this. Any help would be much appreciated.
Well, the biggest issue I see is here:
alpha = arctan (tan(lambda) x cos(epsilon)) // in same quadrant as lambda
...
var alpha = Math.atan(Math.tan(lambda) * Math.sin(epsilon));
You went from using cosine to sine in the second expression.
Now, this on the face of it doesn't lead to the same result, so lets dig in a bit deeper. For clarity, I'm going to use these functions and constants:
var lambda = 326.186;
var epsilon = 23.4396;
function rad (v) { return v * Math.PI / 180 }
function deg (v) { return v * 180 / Math.PI }
Javascript math functions take a radial coordinate, so lets give this a try:
var result = deg(Math.atan(Math.tan(rad(lambda)) * Math.cos(rad(epsilon))));
console.log(result); // -.31.5717
Thanks to the magic of how degrees work, this is the same answer as 360 + -31.5717 = 328.428.