c# - 如何通过 Runge-Kutta 4 传递硬编码微分方程

标签 c# numerical-analysis

我正在尝试针对示例问题实现 Runge-Kutta dy/dt = y - t^2 + 1 和 dy/dt = t * y + t^3 在 C# 中,我似乎无法获得我期望的输出。我将我的程序分成几个类来尝试单独查看工作。我认为我的主要错误来自尝试使用委托(delegate)将方法作为变量传递给 Runge-Kutta 过程。

方程类:

namespace RK4
{
    public class Eqn
    {
        double t;
        double y;
        double dt;
        double b;
        public Eqn(double t, double y, double dt, double b)
        {
            this.t = t;
            this.y = y;
            this.dt = dt;
            this.b = b;
        }
        public void Run1()
        {
            double temp;
            int step = 1;
            RK4 n = new RK4();
            while (t < b)
                    {
                        temp = n.Runge(t, y, dt, FN1);
                        y = temp;
                        Console.WriteLine("At step number {0}, t: {1}, y: {2}", step, t, y);
                        t = t + dt;
                        step++;
                    }
        }
        public void Run2()
        {
            int step = 1;
            RK4 m = new RK4();
            while (t < b)
            {
                y = m.Runge(t, y, dt, FN2);
                Console.WriteLine("At step number {0}, t: {1}, y: {2}", step, t, y);
                t = t + dt;
                step++;
            }
        }
        public static double FN1(double t, double y)
        {
            double x = y - Math.Pow(t, 2) + 1;
            return x;
        }
        public static double FN2(double t, double y)
        {
            double x = t * y + Math.Pow(t, 3);
            return x;
        }
    }
}

然后是龙格-库塔 4 级:

    namespace RK4
    {
        class RK4
        {
            public delegate double Calc(double t, double y);
            public double Runge(double t, double y, double dt, Calc yp)
            {
                double k1 = dt * yp(t, y);
                double k2 = dt * yp(t + 0.5 * dt, y + k1 * 0.5 * dt);
                double k3 = dt * yp(t + 0.5 * dt, y + k2 * 0.5 * dt);
                double k4 = dt * yp(t + dt, y + k3 * dt);
                return (y + (1 / 6) * (k1 + 2 * k2 + 2 * k3 + k4));
            }
        }
    }

And my Program Class:

namespace RK4
{
    class Program
    {
            static void Main(string[] args)
        {
            RunProgram();
        }
        public static void RunProgram()
        {
            Console.WriteLine("*******************************************************************************");
            Console.WriteLine("************************** Fourth Order Runge-Kutta ***************************");
            Console.WriteLine("*******************************************************************************");
            Console.WriteLine("\nWould you like to implement the fourth-order Runge-Kutta on:");
            string Fn1 = "y' = y - t^2 + 1";
            string Fn2 = "y' = t * y + t^3";
            Console.WriteLine("1) {0}", Fn1);
            Console.WriteLine("2) {0}", Fn2);
            Console.WriteLine("Please enter 1 or 2");
            switch (Int32.Parse(Console.ReadLine()))
            {
                case 1:
                    Console.WriteLine("\nPlease enter beginning of the interval (a):");
                    double a = Double.Parse(Console.ReadLine());
                    Console.WriteLine("Please enter end of the interval (b):");
                    double b = Double.Parse(Console.ReadLine());
                    Console.WriteLine("Please enter the step size (h) to be used:");
                    double h = Double.Parse(Console.ReadLine());
                    Console.WriteLine("Please enter the inital conditions to satisfy y({0}) = d",a);
                    Console.WriteLine("d = ");
                    double d = Double.Parse(Console.ReadLine());
                    Console.Clear();
                    Console.WriteLine("Using the interval [{0},{1}] and step size of {2} and the inital condition of y({3}) = {4}:", a, b, h, a, d);
                    Console.WriteLine("With equation: {0}", Fn1);
                    Eqn One = new Eqn(a, d, h, b);
                    One.Run1();
                    Console.WriteLine("Press enter to exit.");
                    Console.ReadLine();
                    Environment.Exit(1);
                    break;
                case 2:
                    Console.WriteLine("\nPlease enter beginning of the interval (a):");
                    a = Double.Parse(Console.ReadLine());
                    Console.WriteLine("Please enter end of the interval (b):");
                    b = Double.Parse(Console.ReadLine());
                    Console.WriteLine("Please enter the step size (h) to be used:");
                    h = Double.Parse(Console.ReadLine());
                    Console.WriteLine("Please enter the inital conditions to satisfy y({0}) = d",a);
                    Console.WriteLine("d = ");
                    d = Double.Parse(Console.ReadLine());
                    Console.Clear();
                    Console.WriteLine("Using the interval [{0},{1}] and step size of {2} and the inital condition of y({3}) = {4}:", a, b, h, a, d);
                    Console.WriteLine("With equation: {0}", Fn1);
                    Eqn Two = new Eqn(a, d, h, b);
                    Two.Run2();
                    Console.WriteLine("Press enter to exit.");
                    Console.ReadLine();
                    Environment.Exit(1);
                    break;
                default:
                    Console.WriteLine("Improper input, please press enter to exit.");
                    Console.ReadLine();
                    Environment.Exit(1);
                    break;
            }
        }
    }
}

这无论如何都不是优雅的编程,但我没有工作知识知道我在这一点上做错了什么。根据我所阅读的内容,我认为 RK4 类中的委托(delegate)能够通过我的硬编码 diff eq。

最佳答案

你在 RK4 实现中犯了一个经典错误:有两个变体来定位与 dt 的乘法以供选择,你正在使用两者。

要么是

k2 = dt*f(t+0.5*dt, y+0.5*k1)

k2 = f(t+0.5*dt, y+0.5*dt*k1)

类似地在算法的其他行中。

关于c# - 如何通过 Runge-Kutta 4 传递硬编码微分方程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34159953/

相关文章:

c# - 如何在 Startup.cs 中使用通用类型注册接口(interface)

matlab - 生成有限差分的矩阵

java - BigDecimal 乘以零

c++ - 特征多项式的 Souriau 方法

c# - 如何加密密码并将其添加到我的sql数据库?

c# - 输出存储过程结果

c# - PdfSharp:旋转文档中的页面

c# - 让 Resharper 在类/命名空间标识符和花括号之间放置一个空格

precision - 避免精度损失的最佳算法?

matlab - 为什么 Matlab 的 inv 缓慢且不准确?