C ++:简单的四阶Runge-Kutta计算器

时间:2018-11-30 19:51:33

标签: c++ math differential-equations runge-kutta

我正在制作一个计算器来解决我们在课堂上所做的一些四阶Runge-Kutta方程,虽然我能够使计算器工作并运行,但是它给我的值并不正确,我无法弄清楚搞清楚为什么。

#include <iostream>                                                                                 
#include <cmath>                                                                                    
#include <iomanip>                                                                                  
#include <cctype>                                                                                   
#include <string>                                                                                   
#include <windows.h>                                                                                
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <sstream>                                                                                  
#include <vector>                                                                                   
#include <numeric>                                                                                  
#include <algorithm>                                                                                
using namespace std;                                                                                
double PI = acos(-1.0);                                                                             
long double Ks(int,int,long double,long double,long double,long double,long double);

int main ()                                                                                         
{
    char answer;
    do                                                                                              
    int Ans,PoN;
    long double X,Y,A,B,h,Fin,K;
    cout << "Please enter X and Y used for problem [X Y]: ";
    cin >> X >> Y; 
    cout << "What is the step size? ";
    cin >> h;
    cout << "What are we approximating to? Y(T) where T = ";
    cin >> Fin;
    cout << "Which equation form are you using (Please use number)?" << endl;
    cout << "1) Ax (+/-) By" << endl;
    cout << "2) Ax (+/-) By^2" << endl;
    cout << "3) Ay (+/-) By^2" << endl;
    cout << "Answer: ";
    cin >> Ans;
    cout << "You picked: ";
    switch(Ans)
    {
        case 1:
            cout << "Ax (+/-) By" << endl;
            cout << "Enter A: ";
            cin >> A;
            cout << "Is the sign in the middle positive(1) or negative(2)? ";
            cin >> PoN;
            cout << "Enter B: ";
            cin >> B;
            break;
        case 2:
            cout << "Ax (+/-) By^2" << endl;
            cout << "Enter A: ";
            cin >> A;
            cout << "Is the sign in the middle positive(1) or negative(2)? ";
            cin >> PoN;
            cout << "Enter B: ";
            cin >> B;
            break;
        case 3:
            cout << "Ay (+/-) By^2" << endl;
            cout << "Enter A: ";
            cin >> A;
            cout << "Is the sign in the middle positive(1) or negative(2)? ";
            cin >> PoN;
            cout << "Enter B: ";
            cin >> B;
            break;
    }
    //cout << fixed << setprecision(8);
    cout << " Y[#] |  New Y  | Exact" << endl;
    cout << "------|---------|------" << endl;
    for(long double i = X+h; i <= Fin; i += h)
    {   
        K = Ks(PoN,Ans,A,B,X,Y,h);
        Y = Y + (h/6)*K;
        cout << setw(2) << "Y[" << setw(3) << i << setw(1) << "]" << "|" << Y << endl;
    }



    cout << "Would you like to repeat this program?(Y/N) ";                                     
    cin >> answer;                                                                          
}
while (answer == 'y' || answer == 'Y');                                                         
if (answer == 'N' || answer == 'n')                                                             
{
cout << "Goodbye" << endl;                                                                      
system("start notepad.exe");                                                                    
}
system("pause");                                                                                    
return 0;
}
long double Ks(int PoN, int Ans, long double A,long double B,long double X,long double Y,long double h)
{
long double K1=0,K2=0,K3=0,K4=0,K=0;
switch(Ans)
{
    case 1:
    {   
        switch(PoN)
        {
            case 1: //Ax + By
                K1 = A*X+B*Y;
                K2 = A*(X+((1.0/2)*h)) + B*(Y+(1.0/2)*h*K1);
                K3 = A*(X+((1.0/2)*h)) + B*(Y+(1.0/2)*h*K2);
                K4 = A*(X+h) + B*(Y+h*K3);
                break;
            case 2: //Ax - By
                K1 = (A*X)-(B*Y);
                K2 = A*(X+((1.0/2)*h)) - B*(Y+(1.0/2)*h*K1);
                K3 = A*(X+((1.0/2)*h)) - B*(Y+(1.0/2)*h*K2);
                K4 = A*(X+h) - B*(Y+h*K3);
                //cout << K1 << " " << K2 << " " << K3 << " " << K4 << endl;
                break;
        }
    break;
    }
    case 2:
    {
        switch(PoN)
        {
            case 1: // Ax + By^2
                K1 = A*X+pow(B*Y,2);
                K2 = A*(X+((1.0/2)*h)) + pow(B*(Y+(1.0/2)*h*K1),2);
                K3 = A*(X+((1.0/2)*h)) + pow(B*(Y+(1.0/2)*h*K2),2);
                K4 = A*(X+h) + pow(B*(Y+h*K3),2);
                break;
            case 2: // Ax - By^2
                K1 = A*X - pow(B*Y,2);
                K2 = A*(X+((1.0/2)*h)) - pow(B*(Y+(1.0/2)*h*K1),2);
                K3 = A*(X+((1.0/2)*h)) - pow(B*(Y+(1.0/2)*h*K2),2);
                K4 = A*(X+h) - pow(B*(Y+h*K3),2);
                break;
        }
    break;
    }

    case 3:
    {   
        switch(PoN)
        {
            case 1: //Ay + By^2
                K1 = A*X+B*Y;
                K2 = A*(Y+((1.0/2)*h)) + B*(Y+(1.0/2)*h*K1);
                K3 = A*(Y+((1.0/2)*h)) + B*(Y+(1.0/2)*h*K2);
                K4 = A*(Y+h) + B*(Y+h*K3);
                break;
            case 2: //Ay - By^2
                K1 = A*X-B*Y;
                K2 = A*(Y+((1.0/2)*h)) - B*(Y+(1.0/2)*h*K1);
                K3 = A*(Y+((1.0/2)*h)) - B*(Y+(1.0/2)*h*K2);
                K4 = A*(Y+h) - B*(Y+h*K3);
                break;
        }
    break;
    }
}
K = (K1 + 2.0*K2 + 2.0*K3 + K4);
return K;
}

现在要测试它,我使用了这个问题:

其中h是步长,因为它每次迭代都会产生一个新的Y值,所以一个错误会导致在第一次迭代后滚雪球的答案。我以为小数点后可能没有计算足够的点,但是如您所见,我使用了长双精度没有任何作用。使用setprecision命令不起作用,它只是在末尾添加许多零以从技术上满足该命令。 enter image description here

在这张照片中,我有一个Runge-Kutta计算器,我在左侧找到了白色,而我自己的程序在左侧运行。如您所见,第一次迭代是正确的,但是在此之后,它们就会变得越来越错误。

1 个答案:

答案 0 :(得分:0)

随着该方法的每次迭代,您将更新Y的近似值,而不更新X。尝试更换

K = Ks(PoN,Ans,A,B,X,Y,h);

使用

K = Ks(PoN,Ans,A,B,i-h,Y,h);

(即,将X更改为i-h)。 您可能还想为变量考虑更有意义的名称,然后将函数分成较小的部分。两者都可以使这样的东西更容易发现。