/*
This program first defines the differential equation as a function f.
Then, it implements the Second order Runga-Kutta method as a function
second_order_runge_kutta. The main function of the program takes the initial
conditions, the end time, the step size, and the pointer to the array y as
input. It then calls the second_order_runge_kutta function to solve the
differential equation and prints the solution.
*/
#include <iostream>
using namespace std;
// Function to define the differential equation
double f(double t, double y) {
return 2 * y;
}
// Second order Runga-Kutta method
void second_order_runge_kutta(double t0, double y0, double t_end, double h, double *y) {
for (double t = t0; t <= t_end; t += h) {
// Calculate k1 and k2
double k1 = h * f(t, y[0]);
double k2 = h * f(t + h, y[0] + k1);
// Update y
y[0] += k1 + k2;
}
}
int main() {
// Initial conditions
double t0 = 0;
double y0 = 1;
double t_end = 1;
double h = 0.1;
// Allocate memory for y
double *y = new double[1];
// Solve the differential equation
second_order_runge_kutta(t0, y0, t_end, h, y);
// Print the solution
cout << y[0] << endl;
// Free memory
delete[] y;
return 0;
}