0

how to find the min and max for quadratic equation using c# ??

f(x,y) = x^2 + y^2 + 25 * (sin(x)^2 + sin(y)^2) ,where (x,y) from (-2Pi, 2Pi) ??

in the manual solving I got min is = 0 , max = 8Pi^2 = 78.957 .

I tried to write the code based on liner quadratic code but something goes totally wrong this code give the min = -4.?? and the max = 96 could you help to know where is my mistake please ??

I uploaded the code to dropbox if anyone can have look : https://www.dropbox.com/s/p7y6krk2gk29i9e/Program.cs

    double[] X, Y, Result; // Range array and result array.

    private void BtnRun_Click(object sender, EventArgs e)
    {
        //Set any Range for the function
        X = setRange(-2 * Math.PI, 2 * Math.PI, 10000);
        Y = setRange(-2 * Math.PI, 2 * Math.PI, 10000);

        Result = getOutput_twoVariablesFunction(X, Y);

        int MaxIndex = getMaxIndex(Result);
        int MinIndex = getMinIndex(Result);

        TxtMin.Text = Result[MinIndex].ToString();
        TxtMax.Text = Result[MaxIndex].ToString();
    }

    private double twoVariablesFunction(double x,double y)
    {
        double f;
        //Set any two variables function
        f = Math.Pow(x, 2) + Math.Pow(y, 2) + 25 * (Math.Pow(Math.Sin(x), 2) + Math.Pow(Math.Sin(y), 2));
        return f;
    }

    private double[] setRange(double Start, double End, int Sample)
    {
        double Step = (End - Start) / Sample;
        double CurrentVaue = Start;
        double[] Array = new double[Sample];
        for (int Index = 0; Index < Sample; Index++)
        {
            Array[Index] = CurrentVaue;
            CurrentVaue += Step;
        }
        return Array;
    }

    private double[] getOutput_twoVariablesFunction(double[] X, double[] Y)
    {
        int Step = X.Length;
        double[] Array = new double[Step];
        for (int Index = 0; Index < X.Length ; Index++)
        {
            Array[Index] = twoVariablesFunction(X[Index], Y[Index]);
        }
        return Array;
    }

    private int getMaxIndex(double[] ValuesArray)
    {
        double M = ValuesArray.Max();
        int Index = ValuesArray.ToList().IndexOf(M);
        return Index;
    }

    private int getMinIndex(double[] ValuesArray)
    {
        double M = ValuesArray.Min();
        int Index = ValuesArray.ToList().IndexOf(M);
        return Index;
    }
  • Are you asking why your code doesn't work or, how to write code to solve arbritrary quadratic equations? – Jodrell Apr 24 '14 at 09:40
  • What kind of quadratic equation has `sin` and `cos` in it? – Rawling Apr 24 '14 at 09:43
  • 1
    In fact, is the equation in the question actually quadratic? http://en.wikipedia.org/wiki/Quadratic_equation – Jodrell Apr 24 '14 at 09:44
  • small thing that springs into view, the range in the formula is form -2pi to 2pi, but in the code the range is set from -pi^2 to pi^2. Not saying that is the cause, but it is a discrepancy. – Me.Name Apr 24 '14 at 09:47
  • and also that instead of sin(x)^2, the code uses sin(x^2) – Me.Name Apr 24 '14 at 09:48
  • it's benchmark function I need to use it to benchmark my result – user3568101 Apr 24 '14 at 10:41
  • I modified the code, the code I think it's work but I think I have problem with the equation formula in C# or the boundary for the Array – user3568101 Apr 24 '14 at 10:54

1 Answers1

2

Do you want to compute (sin(x))^2 or sin(x^2)? In your f(x,y) formula it looks like (sin(x))^2, but in your method twoVariablesFunction like sin(x^2).

gdir
  • 922
  • 2
  • 17
  • 25
  • the power is for the sin not for the x degree I tried both it's still not working .. any other way to write the function in C# – user3568101 Apr 24 '14 at 10:39
  • @Me.Name .. great .. I modified the code, the code I think it's work. but I think I have problem with the equation formula in C# or the boundary for the Array. I tried all the possible Sin(x^2) and Sin(x)^2 still give me wrong result ... – user3568101 Apr 24 '14 at 11:33