1

I'm trying to calculate the Declination Angle of the sun using a given algorithm.

I've ported Java code found here on so to C#, and it gives me the correct result : approx -22,89

My problem is I want to implement this algorithm, but I get the wrong result (approx -11.65).

I've created a WinForm Application for testing this:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private int DayNumber
    {
        get
        {
            return DateTime.Now.DayOfYear;
        }
    }

    private double CalculateDeclinationAngleJAVA()
    {
        return 23.45 * Math.Sin((DegreeToRadian(360.0 / 365.25)) * (DayNumber - 81));
    }

    private double CalculateDeclinationAngle()
    {
        var tmp = (2 * Math.PI) * ((284 + DayNumber) / 36.25);
        return 23.45 * (Math.PI / 180.0) * Math.Sin(tmp);
    }

    private double DegreeToRadian(double angle)
    {
        return Math.PI * angle / 180.0;
    }

    private double RadianToDegree(double angle)
    {
        return angle * (180.0 / Math.PI);
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.Text = "C#: " + RadianToDegree( CalculateDeclinationAngle()).ToString();
        textBox2.Text = "JAVA: " + CalculateDeclinationAngleJAVA().ToString();
    }
}
Community
  • 1
  • 1

1 Answers1

1

I would re-write your CalculateDeclinationAngle() as

private double CalculateDeclinationAngle()
{
   var tmp = (2 * Math.PI) * ((284 + DayNumber) / 365.25);
   return 23.45 * (Math.PI / 180.0) * Math.Sin(tmp);
}

It's just a trivial mistake, it's not 36.25 but 365.25

See my Expiriment here : http://rextester.com/QSLE14863

Let'sRefactor
  • 3,303
  • 4
  • 27
  • 43