Runge- Kutta Method

Hello,

I am supposed to implement the Runge Kutta Method of order 4 for a scalar first-order ordinary differential equation. In the end, I should be able to compare it with the actual value of the function. My problem is that given the (first) derivative of the equation, I cannot find the true function (i.e. the integrated one), if the derivative depends on both y and t.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include<iostream>
#include<iomanip>
#include<cmath>

using namespace std;

double df(double t, double y)
{
    double a= (5*(t*t)-y)/exp(t+y);
    return a;
}

int main()
{
    double t0=0;
    double y0=1;
    double y;
    double z; // z should be the actual (integrated) function that should be defined below to then compare it with the approximation.
    double F1, F2, F3, F4;
    double h;
    
    
    while(t0<=10)
    {
        h=0.1;
        F1= h*df(t0,y0);
        F2=h*df(t0+ h/2, y0+ F1/2);
        F3=h*df(t0+ h/2, y0+ F2/2);
        F4=h*df(t0+h, y0+ F3);
        y = y0 + (F1+ 2*F2+ 2*F3+ F4)/6;
        y0=y;
        t0=t0+h;
        cout << "Values of t are: " << t0 << endl;
        cout << "The approximate values are:" << setprecision(10) << y << endl;

    }

    
    return 0;
}
Last edited on
Topic archived. No new replies allowed.