### I need help In Runge-Kutta 4

I made a program that seems to work and runs, but I'm not sure if the output of my Runge-Kutta 4 is correct because it's not close to the outputs of the real answers. I'm kind of a beginner in C++ by the way:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103`` `````` #include #include //function 1 (u1_0 uses this function as u1_1 uses the other function and t1 is time) double f1(double t1, double u1_0, double u1_1) { double dudt1 = (3 * u1_0) + (2 * u1_1) - (2 * (t1 * t1) + 1) * exp(2 * t1); return dudt1; } //function 2 (u2_0 uses the other function as u2_1 uses this function and t2 is time) double f2(double t2, double u2_0, double u2_1) { double dudt2 = (4 * u2_0) + u2_1 - ((t2 * t2) + (2 * t2) - 4) * exp(2 * t2); return dudt2; } int main () { using namespace std; //variables double h, t0, u1_0, u2_0; //loop variable and number of steps int i, nos; nos = 10; h = 0.1; t0 = 0.0; u1_0 = 1.0; u2_0 = 1.0; //array variables double t[nos + 1]; double u[2][nos + 1]; double u_n[2][1]; t[0] = t0; u[0][0] = u1_0; //u[0][0] = 1.0 u[1][0] = u2_0; //u[1][0] = 1.0 double u_npl[2][1]; //Runge-Kutta variables for both functions above double k1[2], k2[2], k3[2], k4[2]; cout << "This is the RK4 Method: " << endl << endl; for(i = 0; i <= nos; i++) { t[i + 1] = t[i] + h; u_n[0][0] = u[0][i]; u_n[1][0] = u[1][i]; k1[0] = h * f1(t[i], u_n[0][0], u_n[1][0]); k1[1] = h * f2(t[i], u_n[0][0], u_n[1][0]); //not sure if this is how you would get k2 with three variables k2[0] = h * f1(t[i] + 0.5 * h, u_n[0][0] + 0.5 * k1[0], u_n[1][0] + 0.5 * k1[0]); k2[1] = h * f2(t[i] + 0.5 * h, u_n[0][0] + 0.5 * k1[1], u_n[1][0] + 0.5 * k1[1]); //not sure if this is how you would get k3 with three variables k3[0] = h * f1(t[i] + 0.5 * h, u_n[0][0] + 0.5 * k2[0], u_n[1][0] + 0.5 * k2[0]); k3[1] = h * f2(t[i] + 0.5 * h, u_n[0][0] + 0.5 * k2[1], u_n[1][0] + 0.5 * k2[1]); k4[0] = h * f1(t[i] + h, u_n[0][0] + k3[0], u_n[1][0] + k3[0]); k4[1] = h * f2(t[i] + h, u_n[0][0] + k3[1], u_n[1][0] + k3[1]); u_npl[0][0] = u_n[0][0] + ((k1[0] + 2 * k2[0] + 2 * k3[0] + k4[0]) / 6); u_npl[1][0] = u_n[1][0] + ((k1[1] + 2 * k2[1] + 2 * k3[1] + k4[1]) / 6); u[0][i + 1] = u_npl[0][0]; u[1][i + 1] = u_npl[1][0]; cout << "value of u[0][" << i << "] is: " << u[0][i] << endl; cout << "value of u[1][" << i << "] is: " << u[1][i] << endl << endl; } cout << "This is the actual result: " << endl << endl; u[0][0] = u1_0; u[1][0] = u2_0; t[0] = t0; for (i = 0; i <= nos; i++) { t[i + 1] = t[i] + h; u_npl[0][0] = (0.333333 * exp( 5 * t[i])) - (0.333333 * exp(-t[i])) + exp(2 * t[i]); u_npl[1][0] = (0.333333 * exp(5 * t[i])) + (0.666667 * exp(-t[i])) + ((t[i] * t[i]) * exp( 2 * t[i])); u[0][i + 1] = u_npl[0][0]; u[1][i + 1] = u_npl[1][0]; cout << "value of u[0][" << i << "] is: " << u[0][i] << endl; cout << "value of u[1][" << i << "] is: " << u[1][i] << endl << endl; } return 0; } ``````

This function is suppose to take two functions that use the output of each other. I got a Python version of this code:
 ``12345678910111213141516171819202122232425262728293031323334353637`` `````` def f(t,u): dudt = zeros([2,1]) dudt[0][0] = 3*u[0][0] + 2*u[1][0] - (2*t**2 + 1)*exp(2*t) dudt[1][0] = 4*u[0][0] + u[1][0] + (t**2 + 2*t - 4)*exp(2*t) return dudt h = 0.1 n_steps = 10 t0 = 0.0 u1_0 = 1.0 u2_0 = 1.0 t = zeros(n_steps+1) u = zeros([2,n_steps+1]) t[0] = t0 u[0][0] = u1_0 u[1][0] = u2_0 for n in range(0,n_steps): t[n+1] = t[n] + h # RK4 u_n = zeros([2,1]) u_n[0][0] = u[0][n] u_n[1][0] = u[1][n] k1 = h*f(t[n],u_n) k2 = h*f(t[n]+0.5*h,u_n+0.5*k1) k3 = h*f(t[n]+0.5*h,u_n+0.5*k2) k4 = h*f(t[n]+h,u_n+k3) u_np1 = u_n + (k1 + 2*k2 + 2*k3 + k4)/6 u[0][n+1] = u_np1[0][0] u[1][n+1] = u_np1[1][0] ``````

Since I don't know anything about Python, I tried to code it into C++. Can anyone tell me if the output for my Runge-Kutta 4 is correct? Or if anyone could point me in the correct direction on how to code this Python code to C++ and give the right output?

Thank you.
Last edited on
I think this program is working the way it should.
Topic archived. No new replies allowed.