C# e下降函数。 //大小越小的值产生的线性函数越多。 //幅值越大,生成的阶跃函数越多。 //零导致错误。 //例如+1.0、+6.0、+20.0和-1.0、-6.0、-20.0 双边界=+6.0; 对于(Int32步骤=0;步骤

C# e下降函数。 //大小越小的值产生的线性函数越多。 //幅值越大,生成的阶跃函数越多。 //零导致错误。 //例如+1.0、+6.0、+20.0和-1.0、-6.0、-20.0 双边界=+6.0; 对于(Int32步骤=0;步骤,c#,math,.net-3.5,virtual-earth,C#,Math,.net 3.5,Virtual Earth,更好的解决方案可能是使用类似的函数 Double minal=0.0; 双maxAlt=500000.0; Int32 numberSteps=1000; 双边界=+6.0; 对于(Int32步骤=0;步骤

更好的解决方案可能是使用类似的函数

Double minal=0.0;
双maxAlt=500000.0;
Int32 numberSteps=1000;
双边界=+6.0;
对于(Int32步骤=0;步骤
因为当前高度是显式计算的,所以您不必依赖于引入各种精度相关误差的迭代计算

有关如何调整函数形状,请参见示例代码


下面是一个显示函数的示例控制台应用程序。您可以对参数进行一些调整,以了解行为

using System;

namespace LogisticFunction
{
    class Program
    {
        static void Main(string[] args)
        {
            Double minAlt = 5.0;
            Double maxAlt = 95.0;

            Int32 numberSteps = 60;

            // Keep maxAlt and numberSteps small if you don't want a giant console window.
            Console.SetWindowSize((Int32)maxAlt + 12, numberSteps + 1);

            // Positive values produce ascending functions.
            // Negative values produce descending functions.
            // Values with smaller magnitude produce more linear functions.
            // Values with larger magnitude produce more step like functions.
            // Zero causes an error.
            // Try for example +1.0, +6.0, +20.0 and -1.0, -6.0, -20.0
            Double boundary = +6.0;

            for (Int32 step = 0; step < numberSteps; step++)
            {
                Double t = -boundary + 2.0 * boundary * step / (numberSteps - 1);
                Double correction = 1.0 / (1.0 + Math.Exp(Math.Abs(boundary)));
                Double value = 1.0 / (1.0 + Math.Exp(-t));
                Double correctedValue = (value - correction) / (1.0 - 2.0 * correction);
                Double curAlt = correctedValue * (maxAlt - minAlt) + minAlt;

                Console.WriteLine(String.Format("{0, 10:N4} {1}", curAlt, new String('#', (Int32)Math.Round(curAlt))));
            }

            Console.ReadLine();
        }
    }
}
使用系统;
命名空间逻辑函数
{
班级计划
{
静态void Main(字符串[]参数)
{
双尖塔=5.0;
双maxAlt=95.0;
Int32 numberSteps=60;
//如果你不想看到一个巨大的控制台窗口,就把maxAlt和numberSteps保持在小范围内。
Console.setWindowsSize((Int32)maxAlt+12,numberSteps+1);
//正值产生升序函数。
//负值产生递减函数。
//大小越小的值产生的线性函数越多。
//幅值越大,生成的阶跃函数越多。
//零导致错误。
//例如+1.0、+6.0、+20.0和-1.0、-6.0、-20.0
双边界=+6.0;
对于(Int32步骤=0;步骤
既然您使用curAlt-=curAlt/150进行降级,为什么不使用curAlt+=curAlt*150进行升级?

既然您使用curAlt-=curAlt/150进行降级,为什么不使用curAlt+=curAlt*150进行升级?

最大值由用户指定,对于这种情况,假设这不完全相同。使用一个测试用例,下降时我得到1338步,上升时我得到1557步,但我猜这很接近,这可能与你前面提到的额外的减法有关。或者可能出现舍入错误,因为你在下降时有很多台阶高度很小,而在上升时有很多台阶高度很大。一个小问题,这会在较低的高度造成较大的跳跃,这正是我不想要的。我希望在较低的高度跳得更小,在较高的高度跳得更大最大值由用户指定,在这种情况下,假设这不完全相同。使用一个测试用例,下降时我得到1338步,上升时我得到1557步,但我猜这很接近,这可能与你前面提到的额外的减法有关。或者可能出现舍入错误,因为你在下降时有很多台阶高度很小,而在上升时有很多台阶高度很大。一个小问题,这会在较低的高度造成较大的跳跃,这正是我不想要的。我希望在较低的高度有较小的跳跃,在较高的高度有较大的跳跃,这可能就是我想要的!让我来测试一下,我想我可能是用错误的方式实现的。我在第一次迭代中获得了巨大的飞跃,但之后的每个人都是如此。下面是代码行:double curAlt=1/(1+Math.Exp(-t))*(properties.endingAltitude)-properties.startingAltitude;另外,比如说,我只想要曲线的前半部分或后半部分,我该怎么做?例如,使用我刚刚发布的内容,第一次迭代的增长是11861英尺,第二次是149英尺。这仍然会给第一个数字带来无法满足的高增长。第一次迭代:12510,第二次:150,第三次152,等等,这可能就是我要找的!让我来测试一下,我想我可能是用错误的方式实现的。我在第一次迭代中获得了巨大的飞跃,但之后的每个人都是如此。下面是代码行:double curAlt=1/(1+Math.Exp(-t))*(properties.endingAltitude)-properties.startingAltitude;另外,比如说,我只想要曲线的前半部分或后半部分,我该怎么做?例如,使用我刚刚发布的内容,第一次迭代的增长是11861英尺,第二次迭代的增长是149英尺。这仍然会给第一个数字带来无法满足的高增长
curAlt += (maxAlt - curAlt) / 150
epsilon = 0.1; // A small value that fits your needs

curAlt = startAlt;

while (curAlt > endAlt + epsilon)
{
   curAlt -= (curAlt - endAlt) / 150;
}
private int lastTime;
if(lastTime == 0)
{
    lastTime = Environment.TickCount;
    return;
}

int curTime = Environment.TickCount; // store this baby.
int timeDiff = lastTime - curTime;

if(timeDiff == 0)
   return;

curAlt += (maxAlt - curAlt) * timeDiff / (150000); // TickCount reports
                                                   // time in Ticks
                                                   // (1000 ticks per second)

lastTime = curTime;
Double minAlt = 0.0;
Double maxAlt = 500000.0;

Int32 numberSteps = 1000;

Double boundary = +6.0;

for (Int32 step = 0; step < numberSteps; step++)
{
   Double t = -boundary + 2.0 * boundary * step / (numberSteps - 1);
   Double correction = 1.0 / (1.0 + Math.Exp(Math.Abs(boundary)));
   Double value = 1.0 / (1.0 + Math.Exp(-t));
   Double correctedValue = (value - correction) / (1.0 - 2.0 * correction);
   Double curAlt = correctedValue * (maxAlt - minAlt) + minAlt;
}
using System;

namespace LogisticFunction
{
    class Program
    {
        static void Main(string[] args)
        {
            Double minAlt = 5.0;
            Double maxAlt = 95.0;

            Int32 numberSteps = 60;

            // Keep maxAlt and numberSteps small if you don't want a giant console window.
            Console.SetWindowSize((Int32)maxAlt + 12, numberSteps + 1);

            // Positive values produce ascending functions.
            // Negative values produce descending functions.
            // Values with smaller magnitude produce more linear functions.
            // Values with larger magnitude produce more step like functions.
            // Zero causes an error.
            // Try for example +1.0, +6.0, +20.0 and -1.0, -6.0, -20.0
            Double boundary = +6.0;

            for (Int32 step = 0; step < numberSteps; step++)
            {
                Double t = -boundary + 2.0 * boundary * step / (numberSteps - 1);
                Double correction = 1.0 / (1.0 + Math.Exp(Math.Abs(boundary)));
                Double value = 1.0 / (1.0 + Math.Exp(-t));
                Double correctedValue = (value - correction) / (1.0 - 2.0 * correction);
                Double curAlt = correctedValue * (maxAlt - minAlt) + minAlt;

                Console.WriteLine(String.Format("{0, 10:N4} {1}", curAlt, new String('#', (Int32)Math.Round(curAlt))));
            }

            Console.ReadLine();
        }
    }
}