---
title: "Program for RK-4 second order IVP"
subject: "Numerical Method"
chapter: 6
key: "semester_4/numerical_method:rk4-program-second-order"
source: https://www.bctengineer.com/q/semester_4/numerical_method/rk4-program-second-order
site: BCT Engineering
---

# Program for RK-4 second order IVP

Numerical Method · Chapter 6: Solution of ordinary differential equations

- Asked in: 2073 Magh, 6 marks
- Video: [RK4 2nd order ODE | Numerical Methods | LetThereBeMath |](https://www.youtube.com/watch?v=TjZgQa2kec0) — Let there be math
- Answer checked by Claude Opus 5.5 (high effort) on 2026-10-14
- Page: https://www.bctengineer.com/q/semester_4/numerical_method/rk4-program-second-order
- Whole chapter: https://www.bctengineer.com/md/semester_4/numerical_method/chapter-6

## Question

Write a program in C/C++/FORTRAN to solve a second order ordinary differential equation (initial value problem) using the Runge-Kutta fourth order method.

## Short answer

The code reduces y''= g(x,y,y') to first-order system: dy/dx=z, dz/dx=g(x,y,z). RK4 is applied simultaneously: compute k1..k4 for y and l1..l4 for z using half-step evaluations, then update y and z. It prints x, y, y' each step.

## Answer

A second-order IVP y'' = g(x, y, y') with y(x₀) = y₀, y'(x₀) = z₀ is reduced to two first-order equations by putting z = y':

```text
dy/dx = z            = f₁(x, y, z)
dz/dx = g(x, y, z)   = f₂(x, y, z)
```
RK-4 is then applied to both equations together:

```text
k₁ = h·f₁(x, y, z)                     l₁ = h·f₂(x, y, z)
k₂ = h·f₁(x+h/2, y+k₁/2, z+l₁/2)       l₂ = h·f₂(x+h/2, y+k₁/2, z+l₁/2)
k₃ = h·f₁(x+h/2, y+k₂/2, z+l₂/2)       l₃ = h·f₂(x+h/2, y+k₂/2, z+l₂/2)
k₄ = h·f₁(x+h, y+k₃, z+l₃)             l₄ = h·f₂(x+h, y+k₃, z+l₃)
y ← y + (k₁+2k₂+2k₃+k₄)/6              z ← z + (l₁+2l₂+2l₃+l₄)/6
```

#### C program (example: y'' = y − x·y')
```c
#include <stdio.h>
#include <math.h>

/* y'' = g(x, y, z) with z = y'.  Example: y'' = y - x*y' */
double f1(double x, double y, double z)      /* dy/dx = z */
{
    return z;
}
double f2(double x, double y, double z)      /* dz/dx = y'' */
{
    return y - x * z;
}

int main(void)
{
    double x0, y0, z0, xn, h, x, y, z;
    double k1, k2, k3, k4, l1, l2, l3, l4;
    int i, n;

    printf("Enter x0, y(x0), y'(x0), xn and h: ");
    scanf("%lf %lf %lf %lf %lf", &x0, &y0, &z0, &xn, &h);

    n = (int)((xn - x0) / h + 0.5);
    x = x0; y = y0; z = z0;

    printf("\n  x         y          y'\n");
    printf("%8.4f  %10.4f  %10.4f\n", x, y, z);

    for (i = 0; i < n; i++)
    {
        k1 = h * f1(x, y, z);
        l1 = h * f2(x, y, z);
        k2 = h * f1(x + h / 2, y + k1 / 2, z + l1 / 2);
        l2 = h * f2(x + h / 2, y + k1 / 2, z + l1 / 2);
        k3 = h * f1(x + h / 2, y + k2 / 2, z + l2 / 2);
        l3 = h * f2(x + h / 2, y + k2 / 2, z + l2 / 2);
        k4 = h * f1(x + h, y + k3, z + l3);
        l4 = h * f2(x + h, y + k3, z + l3);

        y = y + (k1 + 2 * k2 + 2 * k3 + k4) / 6;
        z = z + (l1 + 2 * l2 + 2 * l3 + l4) / 6;
        x = x + h;
        printf("%8.4f  %10.4f  %10.4f\n", x, y, z);
    }
    return 0;
}
```

#### Sample run
Input `0 1 0 0.2 0.1` (y(0) = 1, y'(0) = 0, up to x = 0.2, h = 0.1):

```text
  x         y          y'
  0.0000      1.0000      0.0000
  0.1000      1.0050      0.0998
  0.2000      1.0199      0.1987
```
For any other equation change only the `f2` function (the right side y'' = …).

## Check yourself

The answer's key points as multiple-choice questions, with the correct option and why.

1. How is a second-order IVP y'' = g(x, y, y') converted for RK-4?
   A. Apply RK-4 directly to y'' treating it as dy/dx
   B. Integrate y'' twice analytically before coding
   C. Put z = y', giving dy/dx = z and dz/dx = g(x, y, z), and apply RK-4 to both together
   D. Replace y'' by a forward difference and apply Euler's method
   Answer: C. Setting z = y' turns the equation into two first-order equations, which RK-4 can solve simultaneously. (Key point: Reduction to a system)

2. In the program, how are y and z updated at the end of each step?
   A. y += (k1+k2+k3+k4)/4 and z += (l1+l2+l3+l4)/4
   B. y += k4 and z += l4
   C. y += (l1+2l2+2l3+l4)/6 and z += (k1+2k2+2k3+k4)/6
   D. y += (k1+2k2+2k3+k4)/6 and z += (l1+2l2+2l3+l4)/6
   Answer: D. Each variable uses its own weighted average (1,2,2,1)/6 of its slopes: k for y and l for z. (Key point: Updating y and z)

3. To solve a different second-order equation with the same program, what must be changed?
   A. Only the function f2 that gives the right side y'' = ...
   B. Only the function f1
   C. Both f1 and the RK-4 loop formulas
   D. The step size formula n = (xn-x0)/h
   Answer: A. f1 = z is always dy/dx; only f2 (the expression for y'') depends on the particular equation. (Key point: Changing the equation)

4. In the RK-4 stage k2/l2, what arguments are passed to f1 and f2?
   A. x + h/2, y + k2/2, z + l2/2
   B. x + h/2, y + k1/2, z + l1/2
   C. x + h, y + k1, z + l1
   D. x, y + k1/2, z
   Answer: B. The second stage evaluates at the midpoint using half of the first-stage increments of both y and z. (Key point: Intermediate arguments)

## Related in Numerical Method

- [Program for RK-4 first order IVP](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Ark4-program-first-order) — Numerical Method, chapter 6
- [RK-4: y'' - 3y' + y = sin x](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Ark4-second-order-ivp-y12) — Numerical Method, chapter 6
- [RK-2: simultaneous equations](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Ark2-simultaneous) — Numerical Method, chapter 6
- [RK-2: y' = sin x + cos y](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Ark2-sinx-cosy) — Numerical Method, chapter 6
- [RK-2: y'' = y - x y'](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Ark2-y-double-prime-y-xy) — Numerical Method, chapter 6, asked 2×
- [RK-2: y' = xy + y²](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Ark2-xy-y2) — Numerical Method, chapter 6, asked 2×

## Related in other subjects

- [Initial value problem y''−4y'+3y](https://www.bctengineer.com/question.md?key=semester_2%2Fmathematics%3Ainitial-value-problem-y-4y) — Mathematics, chapter 4, asked 3×
- [Initial value problem y''+2y'+5y=0](https://www.bctengineer.com/question.md?key=semester_2%2Fmathematics%3Ainitial-value-problem-y-2y) — Mathematics, chapter 4, asked 3×
- [Solve a partial differential equation](https://www.bctengineer.com/question.md?key=semester_3%2Fmathematics_iii%3Asolve-a-partial-differential-equation) — Mathematics III, chapter 4
- [Solve a partial differential equation](https://www.bctengineer.com/question.md?key=semester_3%2Fmathematics_iii%3Asolve-a-partial-differential-equation-two) — Mathematics III, chapter 4
