English 中文(简体)
C# # C: 寻找功能迷你符圈
原标题:C#: Loop to find minima of function

我目前有这个功能:

    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;
    }

在斜坡下降的曲线上, 这将找到最小值... 不过, < a href=" "http://www.wolflamalpha.com/ input/?? i=Plot% 5B% 280.167% 20B% 20x% 5E5% 2bx% 5E4% 20_ 20_ 3958% 2908.% 225.708% 5E3-%2888.854% 20x%5E2% 29%2b% 202. 202.b% 283. 86. 87.% 20x% 29-70%29,% 7Bx,0. 6% 7D%5D" rel=“ nofollown norerer” < a>

< 坚固> 如何找到三个迷你, 并将其输出为数组 < / 坚固 > 或单个变量? 此曲线只是个示例 - 它可以是反向的 - 不论如何, 我需要找到多个变量 。 所以一旦发现第一分钟, 它需要知道如何跨过透视点并找到下一个变量... : /

a href="http://en.wikipedia.org/wiki/Lagrange_multiplier" rel="不跟随 nofollow noreferrer" 这里可以找到 Lagrange 函数 。 ** 出于所有实际目的, Lagrange 函数会给我f(x), 当我输入 x... 视觉... 时, 它意味着由 wolfram alpha 提供的曲线 。

a href="https://math.stackschange.com/ questions/148762/method-to-find-cent-roid-of-curve" > 这个难题的数学方面可在这里找到 。 **

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?

最佳回答

假设你只是试图“粗力” 计算出它到一定的精密度, 你需要你的算法 基本上找到任何值, 如果邻居和邻居的值 都高于循环的当前值。

为了简化这一点,让我们只说您有一系列数字, 您想要找到三个本地迷你模型的索引。 这里可以使用简单的算法 :

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;
}

因此,基本上,您通过您为一组输入(xs) (未显示) 计算出来的函数结果(ys) 。您从此函数中得到的回报是最小指数。因此,在此示例中,您可以返回8、14和17。

问题回答

一段时间以来,我开始了一个数字方法类,所以请容我一容。简言之,有 的几种方法 来搜索函数的根,并取决于你的职能是什么(持续吗?不同的?),你需要选择合适的方法。

对于您的问题,我可能首先尝试使用 < a href=" "http://en.wikipedia. org/wiki/ Newton% 27s_ method" rel=“ nofollow” > Newtonss 方法 来寻找您函数的二级 Lagrange 多边语言的根。 我还没有测试过这个库, 但有一个 < a href=" http:// numberical.codefleple.com/" rel="nofolpolce" > C# 基于数字方法包 < /a>, 用于执行打开源的 Newton 方法的codelPlex 。 如果您想通过您可以使用的代码挖掘的话 。

大部分根查方法都有表兄弟们在CS搜索这个大主题中。如果想要快速和肮脏的方法,或者拥有非常大的搜索空间,请考虑一些类似“http://en.wikipedia.org/wiki/Simulate_annealing” rel = “nofollow” > 模拟Annaaling 的东西。发现你所有的迷你膜是没有保证的,但却是快速和容易编码的。





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签