C#:循环查找功能极小 [英] C#: Loop to find minima of function

查看:192
本文介绍了C#:循环查找功能极小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在有这个功能:

    public double Max(double[] x, double[] y)
    {
        //Get min and max of x array as integer
        int xMin = Convert.ToInt32(x.Min());
        int xMax = Convert.ToInt32(x.Max());


        // Generate a list of x values for input to Lagrange

        double i = 2;
        double xOld = Lagrange(xMin,x,y);
        double xNew = xMax;
        do
        {
            xOld = xNew;
            xNew = Lagrange(i,x,y);
            i = i + 0.01;
        } while (xOld > xNew);

        return i;
    }

这会发现斜率下降曲线的最低值,但是... ,的给这条曲线,我需要找三极小。

This will find the minimum value on a curve with decreasing slope...however, given this curve, I need to find three minima.

如何才能找到三个极小并输出作为数组或个体变量?这条曲线仅仅是一个例子 - 它可以被倒置 - 不管,我需要找到多个变量。因此,一旦第一分钟发现它需要知道如何克服拐点,并寻找下一个...:/

How can I find the three minima and output them as an array or individual variables? This curve is just an example--it could be inverted--regardless, I need to find multiple variables. So once the first min is found it needs to know how to get over the point of inflection and find the next... :/

拉格朗日函数可以在这里找到 的*对于所有的实际目的,拉格朗日函数会给我˚F (X),当我输入X ...视觉上,它是指由Wolfram Alpha的供给曲线

The Lagrange function can be found here.* For all practical purposes, the Lagrange function will give me f(x) when I input x...visually, it means the curve supplied by wolfram alpha.

The数学端可以在这里找到 的*

可能的解决方法?
生成输入数组,例如x [1,1.1,1.2,1.3,1.4 ...],得到一个数组从后面拉格朗日函数。然后找到这个函数的三个最低值?然后得到相应的价值的钥匙?我会怎么做呢?

Possible solution? Generate an array of input, say x[1,1.1,1.2,1.3,1.4...], get an array back from the Lagrange function. Then find the three lowest values of this function? Then get the keys corresponding to the values? How would I do this?

推荐答案

假设你只是试图蛮力计算这prcision一定水平,你需要你的算法基本上找到其中两个邻居比你的循环的当前值的任何值。

Assuming you're just trying to "brute force" calculate this to a certain level of prcision, you need your algorithm to basically find any value where both neighbors are greater than the current value of your loop.

要简化此,我们只能说你有一个数组数字,你想找到三个局部极小的指数。这里有一个简单的算法来做到这一点:

To simplify this, let's just say you have an array of numbers, and you want to find the indices of the three local minima. Here's a simple algorithm to do it:

public void Test()
{
    var ys = new[] { 1, 2, 3, 4, 5, 4, 3, 2, 1, 2, 3, 4, 5, 4, 3, 4, 5, 4 };
    var indices = GetMinIndices(ys);
}

public List<int> GetMinIndices(int[] ys)
{
    var minIndices = new List<int>();
    for (var index = 1; index < ys.Length; index++)
    {
        var currentY = ys[index];
        var previousY = ys[index - 1];
        if (index < ys.Length - 1)
        {
            var neytY = ys[index + 1];
            if (previousY > currentY && neytY > currentY) // neighbors are greater
                minIndices.Add(index); // add the index to the list
        }
        else // we're at the last index
        {
            if (previousY > currentY) // previous is greater
                minIndices.Add(index);
        }
    }
    return minIndices;
}



所以,基本上,你您的函数的结果(YS)数组中传递您的输入(XS)的阵列计算的(未示出)。你得到的这个函数后面是最低指数。因此,在本例中,将取回8,14,和17

So, basically, you pass in your array of function results (ys) that you calculated for an array of inputs (xs) (not shown). What you get back from this function is the minimum indices. So, in this example, you get back 8, 14, and 17.

这篇关于C#:循环查找功能极小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆