Пример #1
0
        /// <summary>
        /// Compute expressions for y[t + h] in terms of y[t], the resulting expressions will be implicit for implicit methods.
        /// </summary>
        /// <param name="dy_dt">Equations to solve.</param>
        /// <param name="y">Functions to solve for.</param>
        /// <param name="t">Independent variable.</param>
        /// <param name="h">Step size.</param>
        /// <param name="method">Integration method to use for differential equations.</param>
        /// <returns>Expressions for y[t + h].</returns>
        public static IEnumerable <Arrow> NDIntegrate(this IEnumerable <Arrow> dy_dt, Expression t, Expression h, IntegrationMethod method)
        {
            switch (method)
            {
            // y[t + h] = y[t] + h*f[t, y[t]]
            case IntegrationMethod.Euler:
                return(dy_dt.Select(i => Arrow.New(
                                        DOf(i.Left).Evaluate(t, t + h),
                                        DOf(i.Left) + h * i.Right)));

            // y[t + h] = y[t] + h*f[t + h, y[t + h]]
            case IntegrationMethod.BackwardEuler:
                return(dy_dt.Select(i => Arrow.New(
                                        DOf(i.Left).Evaluate(t, t + h),
                                        DOf(i.Left) + h * i.Right.Evaluate(t, t + h))));

            // y[t + h] = y[t] + (h/2)*(f[t, y[t]] + f[t + h, y[t + h]])
            case IntegrationMethod.Trapezoid:
                return(dy_dt.Select(i => Arrow.New(
                                        DOf(i.Left).Evaluate(t, t + h),
                                        DOf(i.Left) + (h / 2) * (i.Right + i.Right.Evaluate(t, t + h)))));

            default:
                throw new NotImplementedException(method.ToString());
            }
        }
Пример #2
0
        /// <summary>
        /// Compute expressions for y[t] in terms of y[t - h], the resulting expressions will be implicit for implicit methods.
        /// </summary>
        /// <param name="dy_dt">Equations to solve.</param>
        /// <param name="y">Functions to solve for.</param>
        /// <param name="t">Independent variable.</param>
        /// <param name="h">Step size.</param>
        /// <param name="method">Integration method to use for differential equations.</param>
        /// <returns>Expressions for y[t].</returns>
        public static IEnumerable <Arrow> NDIntegrate(this IEnumerable <Arrow> dy_dt, Expression t, Expression h, IntegrationMethod method)
        {
            switch (method)
            {
            // y[t] = y[t - h] + h*f[t - h, y[t - h]]
            case IntegrationMethod.Euler:
                return(dy_dt.Select(i => Arrow.New(
                                        DOf(i.Left),
                                        DOf(i.Left).Substitute(t, t - h) + h * i.Right.Substitute(t, t - h))));

            // y[t] = y[t - h] + h*f[t, y[t]]
            case IntegrationMethod.BackwardEuler:
                return(dy_dt.Select(i => Arrow.New(
                                        DOf(i.Left),
                                        DOf(i.Left).Substitute(t, t - h) + h * i.Right)));

            // y[t] = y[t - h] + (h/2)*(f[t - h, y[t - h]] + f[t, y[t]])
            case IntegrationMethod.Trapezoid:
                return(dy_dt.Select(i => Arrow.New(
                                        DOf(i.Left),
                                        DOf(i.Left).Substitute(t, t - h) + (h / 2) * (i.Right.Substitute(t, t - h) + i.Right))));

            // y[t] = (4/3)*y[t - h] - (1/3)y[t - 2h] + (2h/3)*f[t, y[t]]
            case IntegrationMethod.BackwardDifferenceFormula2:
                return(dy_dt.Select(i => Arrow.New(
                                        DOf(i.Left),
                                        4 * DOf(i.Left).Substitute(t, t - h) / 3 -
                                        DOf(i.Left).Substitute(t, t - 2 * h) / 3 +
                                        2 * h * i.Right / 3)));

            // y[t] = (2/11)*y[t - 3h] - (9/11)*y[t - 2h] + (18/11)*y[t - h] + (6h/11)*f[t, y[t]]
            case IntegrationMethod.BackwardDifferenceFormula3:
                return(dy_dt.Select(i => Arrow.New(
                                        DOf(i.Left),
                                        2 * DOf(i.Left).Substitute(t, t - 3 * h) / 11 -
                                        9 * DOf(i.Left).Substitute(t, t - 2 * h) / 11 +
                                        18 * DOf(i.Left).Substitute(t, t - h) / 11 +
                                        6 * h * i.Right / 11)));

            default:
                throw new NotImplementedException(method.ToString());
            }
        }
Пример #3
0
        /// <summary>
        /// Compute expressions for y[t + h] in terms of y[t], the resulting expressions will be implicit for implicit methods.
        /// </summary>
        /// <param name="dy_dt">Equations to solve.</param>
        /// <param name="y">Functions to solve for.</param>
        /// <param name="t">Independent variable.</param>
        /// <param name="h">Step size.</param>
        /// <param name="method">Integration method to use for differential equations.</param>
        /// <returns>Expressions for y[t + h].</returns>
        public static IEnumerable<Arrow> NDIntegrate(this IEnumerable<Arrow> dy_dt, Expression t, Expression h, IntegrationMethod method)
        {
            switch (method)
            {
                // y[t + h] = y[t] + h*f[t, y[t]]
                case IntegrationMethod.Euler:
                    return dy_dt.Select(i => Arrow.New(
                        DOf(i.Left).Evaluate(t, t + h),
                        DOf(i.Left) + h * i.Right));

                // y[t + h] = y[t] + h*f[t + h, y[t + h]]
                case IntegrationMethod.BackwardEuler:
                    return dy_dt.Select(i => Arrow.New(
                        DOf(i.Left).Evaluate(t, t + h),
                        DOf(i.Left) + h * i.Right.Evaluate(t, t + h)));

                // y[t + h] = y[t] + (h/2)*(f[t, y[t]] + f[t + h, y[t + h]])
                case IntegrationMethod.Trapezoid:
                    return dy_dt.Select(i => Arrow.New(
                        DOf(i.Left).Evaluate(t, t + h),
                        DOf(i.Left) + (h / 2) * (i.Right + i.Right.Evaluate(t, t + h))));

                default:
                    throw new NotImplementedException(method.ToString());
            }
        }