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

# Program for RK-4 first order IVP

Numerical Method · Chapter 6: Solution of ordinary differential equations

- Asked in: 2073 Bhadra, 6 marks
- Video: [Fourth-order Runge-Kutta method (Python)](https://www.youtube.com/watch?v=jrMzo5REU9g) — Dr Jon Shiach
- 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-first-order
- Whole chapter: https://www.bctengineer.com/md/semester_4/numerical_method/chapter-6

## Question

Write a program in any high level language (C/C++/FORTRAN) to solve a first order initial value problem using classical RK-4 method.

## Short answer

The C program reads x0, y0, xn, h, computes steps n=(xn-x0)/h, and loops applying RK4: k1=h f(x,y), k2=h f(x+h/2,y+k1/2), k3=h f(x+h/2,y+k2/2), k4=h f(x+h,y+k3), then y=y+(k1+2k2+2k3+k4)/6, x=x+h, printing each step.

## Answer

Classical RK-4 for dy/dx = f(x, y), y(x₀) = y₀, step h:

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

#### Algorithm
1. Read x₀, y₀, the end point xₙ and the step h.
2. Compute the number of steps n = (xₙ − x₀)/h.
3. Repeat n times: find k₁, k₂, k₃, k₄, update y and x, print them.

#### C program
```c
#include <stdio.h>
#include <math.h>

/* dy/dx = f(x, y): change this function for another problem */
double f(double x, double y)
{
    return x + y;
}

int main(void)
{
    double x0, y0, xn, h, x, y, k1, k2, k3, k4;
    int i, n;

    printf("Enter x0, y0, xn and step size h: ");
    scanf("%lf %lf %lf %lf", &x0, &y0, &xn, &h);

    n = (int)((xn - x0) / h + 0.5);      /* number of steps */
    x = x0;
    y = y0;

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

    for (i = 0; i < n; i++)
    {
        k1 = h * f(x, y);
        k2 = h * f(x + h / 2, y + k1 / 2);
        k3 = h * f(x + h / 2, y + k2 / 2);
        k4 = h * f(x + h, y + k3);

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

#### Sample run
For f = x + y, input `0 0 0.4 0.1` (y(0) = 0, up to x = 0.4, h = 0.1):

```text
  x         y
  0.0000      0.0000
  0.1000      0.0052
  0.2000      0.0214
  0.3000      0.0499
  0.4000      0.0918
```
The exact value y = eˣ − x − 1 gives y(0.4) = 0.0918. To solve another problem, change only the body of `f`.

## Check yourself

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

1. k2 is?
   A. h f(x+h, y+k1)
   B. h f(x+h/2, y+k1/2)
   C. h f(x, y+k1/2)
   D. h f(x+h/2, y)
   Answer: B. Mid-point slope. (Key point: k2)

2. y update?
   A. y+(k1+k2+k3+k4)/4
   B. y+k4
   C. y + (k1+2k2+2k3+k4)/6
   D. y+(k1+k4)/2
   Answer: C. Weights 1,2,2,1. (Key point: Update)

3. Number of steps?
   A. xn/h+1
   B. h/(xn-x0)
   C. (xn-x0)*h
   D. n = (xn-x0)/h
   Answer: D. Rounded. (Key point: Steps)

4. To change problem?
   A. Change only f
   B. Rewrite main
   C. Change k's
   D. Change n
   Answer: A. Isolated in f. (Key point: Reuse)

## Related in Numerical Method

- [Program for RK-4 second order IVP](https://www.bctengineer.com/question.md?key=semester_4%2Fnumerical_method%3Ark4-program-second-order) — Numerical Method, chapter 6

## Related in other subjects

- [High level languages; C versus FORTRAN](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Ahll-types-c-vs-fortran) — Computer Programming, chapter 1
- [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×
- [High level languages and natural language](https://www.bctengineer.com/question.md?key=semester_1%2Fcomputer_programming%3Ahll-similar-natural-language) — Computer Programming, chapter 1
