m0_70367231 2022-05-31 14:56 采纳率: 100%
浏览 55
已结题

C#龙格库塔解微分方程

请问大家,C#解龙格库塔该怎么写,刚学一点C#基本语句,只会定义k的四行,不知道完整的该怎么写

img

  • 写回答

2条回答 默认 最新

  • NnWinter冬 2022-05-31 16:41
    关注

    img

    float dydx(float x, float y)
    {
        return ((x - y) / 2);
    }
    float rungeKutta(float x0, float y0, float x, float h)
    {
        // Count number of iterations using step size or
        // step height h
        int n = (int)((x - x0) / h);
    
        float k1, k2, k3, k4, k5;
    
        // Iterate for number of iterations
        float y = y0;
        for (int i = 1; i <= n; i++)
        {
            // Apply Runge Kutta Formulas to find
            // next value of y
            k1 = h * dydx(x0, y);
            k2 = h * dydx(x0 + 0.5f * h, y + 0.5f * k1);
            k3 = h * dydx(x0 + 0.5f * h, y + 0.5f * k2);
            k4 = h * dydx(x0 + h, y + k3);
    
            // Update next value of y
            y = y + (1.0f / 6.0f) * (k1 + 2 * k2 + 2 * k3 + k4); ;
    
            // Update next value of x
            x0 = x0 + h;
        }
    
        return y;
    }
    
    //调用:
    rungeKutta(1, 2, 3, 4);
    
    /*函数引用自:
    https://www.geeksforgeeks.org/runge-kutta-4th-order-method-solve-differential-equation/
    */
    

    题主可以试一下。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 6月15日
  • 已采纳回答 6月7日
  • 创建了问题 5月31日

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行