简体   繁体   English

Runge Kutta C#中的错误返回值

[英]Wrong return value in Runge Kutta C#

Could someone please tell me, why It always returns the same "y" value? 有人可以告诉我,为什么它总是返回相同的“ y”值? I've searched a lot on the Internet and I still don't know why it is not working. 我在互联网上进行了很多搜索,但我仍然不知道为什么它不起作用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Inżynierkuuuu
{
class Lecimy
{
    double t, y, krok, cel;

    public Lecimy(double t, double y, double krok, double cel)
    {
        this.t = t;
        this.y = y;
        this.krok = krok;
        this.cel = cel;
    }

    public delegate double funkcja(double t, double y);

    public double RK(double t, double y, double krok, funkcja yp)
    {
        double k1 = krok * yp(t, y);
        double k2 = krok * yp(t + krok * 0.5, y + k1 * 0.5);
        double k3 = krok * yp(t + krok * 0.5, y + k2 * 0.5);
        double k4 = krok * yp(t + krok, y + k3);

        double reszta = 1 / 6 * (k1 + 2 * k2 + 2 * k3 + k4);

        this.y = y + reszta;

        return y;
    }

    public void Run()
    {
        while(t < cel)
        {
            y = RK(t, y, krok, FN1);
            t = t + krok;
            Console.WriteLine("t: {0}, y: {1}", t, y);
        }
    }

    public double FN1(double t, double y)
    {
        return y;
    }

}
}

Second class: The "y" value here is 1, when I change It egto 5, it will always return 5 in the Output window. 第二类:这里的“ y”值为1,当我将其更改为5时,它将始终在“输出”窗口中返回5。

static void Main(string[] args)
    {
        Lecimy e = new Lecimy(0.0, 1.0, 0.0001, 1.0); 
        e.Run();
        //Application.EnableVisualStyles();
        //Application.SetCompatibleTextRenderingDefault(false);
        //Application.Run(new Form1());
    }

I have no idea what you want to do, but I'm sure the following line is not what yo want: 我不知道您想做什么,但是我确定以下行不是您想要的:

double reszta = 1 / 6 * (k1 + 2 * k2 + 2 * k3 + k4);

reszta will always be zero. reszta将始终为零。 1 / 6 is 0 , as both 1 and 6 are integers. 1 / 60 ,因为1和6均为整数。 Use 1.0 / 6 or something similar. 使用1.0 / 6或类似的值。

There are two obvious issues with your code even though I don't understand what you are trying to do. 即使我不理解您要执行的操作,您的代码也有两个明显的问题。

  1. 1/6 will always be zero, you should be doing 1.0/6 . 1/6将始终为零,您应该执行1.0/6 The reason being that in the former, the /(int, int) overload is chosen. 原因是在前者中,选择了/(int, int)重载。
  2. You are conflating the argument y with the class field y . 您正在将参数y与类字段y混淆。 Do not name a local variable / argument / field with the same name, its confusing and very error prone; 命名一个局部变量/参数/使用相同的名称,它的混乱,很容易出错的领域; in method RK , are you sure you shouldn't be returning this.y ? 在方法RK ,您确定不应该返回this.y吗? Are you sure this.y = y + reszta is correct and it shouldn't be this.y += reszta . 您确定this.y = y + reszta是正确的,而不应该是this.y += reszta See how confusing this is? 看到这是多么令人困惑?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM