Esempio n. 1
0
 public FunctionDraw(FunctionOfOneVariableD func, double step = 0.001d)
 {
     m_func = func;
     m_step = step;
     //m_beginX = beginX;
     //m_endX = endX;
 }
    public static double GetMaxByThree(FunctionOfOneVariableD function, double left, double right)
    {
        do
        {
            double yLeft   = function(left);
            double yRight  = function(right);
            double mid     = (left + right) / 2;
            double yMid    = function(mid);
            double midMid  = (left + mid) / 2;
            double yMidMid = function(midMid);
            if (yMidMid < yMid)
            {
                left = midMid;
            }
            else
            {
                right = mid;
            }

            if (Math.Abs(left - right) < 0.001d)
            {
                return(left);
            }
        } while (true);
    }
    /// <summary>
    /// 反函数
    /// </summary>
    /// <param name="function"></param>
    /// <param name="endY"></param>
    /// <param name="beginX"></param>
    /// <param name="delta"></param>
    /// <returns></returns>
    public static double InvertFunc(FunctionOfOneVariableD function, double endY, double beginX, double delta = 0.0001d)
    {
        double t1 = beginX;
        double t2;

        // 牛顿切线法求解L(t1) = L(1.0) * percent;
        // Xn+1 = Xn - (L(xn) - L(1.0) * percent / L'(xn))
        do
        {
            t2 = t1 - (function(t1) - endY) / DerivativeD(function, t1);

            if (Math.Abs(t1 - t2) < 0.000001)
            {
                break;
            }

            t1 = t2;
        } while (true);

        return(t2);
    }
    /// <summary>
    /// 求导数
    /// </summary>
    /// <param name="x"></param>
    /// <returns></returns>

//     public static float Derivative(FunctionOfOneVariable function, float x, float delta = 0.0001f)
//     {
//         return (function(x + delta) - function(x)) / delta;
//     }

    public static double DerivativeD(FunctionOfOneVariableD function, double x, double delta = 0.0001d)
    {
        return((function(x + delta) - function(x)) / delta);
    }