【问题标题】:Find the min and max for quadratic equation找到二次方程的最小值和最大值
【发布时间】:2014-06-09 12:20:18
【问题描述】:

如何使用 c# 求二次方程的最小值和最大值??

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

在手动求解中我得到 min is = 0 , max = 8Pi^2 = 78.957 。

我尝试根据线性二次代码编写代码,但完全出错了 这段代码给出了 min = -4.?? max = 96 你能帮忙知道我的错误在哪里吗??

如果有人可以看,我将代码上传到 Dropbox: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;
    }

【问题讨论】:

  • 您是在问为什么您的代码不起作用,或者,如何编写代码来求解任意二次方程?
  • 什么二次方程里面有sincos
  • 事实上,问题中的方程实际上是二次的吗? en.wikipedia.org/wiki/Quadratic_equation
  • 眼前出现的小东西,公式中的范围是-2pi到2pi,但在代码中范围是从-pi^2到pi^2。不是说那是原因,但这是一个差异。
  • 并且代码使用 sin(x^2) 代替 sin(x)^2

标签: c# max min equation quadratic


【解决方案1】:

你想计算 (sin(x))^2 还是 sin(x^2)?在您的 f(x,y) 公式中,它看起来像 (sin(x))^2,但在您的方法 twoVariablesFunction 中像 sin(x^2)。

【讨论】:

  • 力量是针对罪孽的,不是针对 x 度的,我尝试了这两种方法仍然无法正常工作.. 用 C# 编写函数的任何其他方式
  • @Me.Name .. 太好了 .. 我修改了代码,我认为代码可以工作。但我认为我对 C# 中的方程公式或数组的边界有疑问。我尝试了所有可能的 Sin(x^2) 和 Sin(x)^2 仍然给我错误的结果...
猜你喜欢
  • 2022-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-21
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多