Runge Kutta Method In C

The code snippet provided below prompts the user to input the initial values for the initial condition, denoted as x0 and y0, in order to solve first-order ordinary differential equations employing the RK4 technique. Following this, the user is required to define the step size 'h' and the ultimate x value at which the y value needs to be computed.

The function f(x,y) is specified to compute the slope each time it is invoked within this C program implementing the Runge Kutta technique.

f(x,y) = x-y/(x+y)

A computational technique known as the Runge-Kutta method is applied to estimate the solution at specific points during the resolution of ordinary differential equations (ODEs). This method is particularly beneficial in cases where obtaining analytical solutions is difficult or unattainable. It involves calculating intermediary values to incrementally adjust the solution, resulting in a sequence of approximations that approximate the actual solution.

1. Problem setup

Consider a differential equation of the first order in the form:

dy/dx = f(x, y)

Where f(x, y) is a function that defines the rate at which y changes with respect to x

2. Initial Conditions

At a specific starting point x0, an initial value of y0 is provided.

3. Step Size

Decide on the increment value you prefer to utilize. This value governs the gap between consecutive steps at which you will approximate the function y.

4. Iterative Process

Start from the initial position (x0, y0) and apply the provided equations to calculate the subsequent coordinates (x, y) iteratively:

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 + 2k2 + 2k3 + k4) / 6

x = x + h

  • In these calculations, the slope at the start of the interval is represented by k1 .
  • The slope at the halfway point of the interval, denoted by k2 , is determined by k1 .
  • Using k2, k3 is a midpoint estimated slope that is similar to k2 .
  • The slope at the end of the interval, denoted by k4 , is estimated using k3 .
  • After that, the updated value of x is calculated as a weighted average of these slopes, and the new value of y is determined.
  • 5. Repeat

Continue the process until you reach the designated final point, xn.

Example:

Let's consider an illustration to grasp the application of the Runge-Kutta technique in C programming.

Example

#include<stdio.h>

#include<math.h>

float f(float x,float y);

int main()

{

    float x0,y0,m1,m2,m3,m4,m,y,x,h,xn;

    printf("Enter the values of x0, y0, xn, h:");

    scanf("%f %f %f %f",&x0,&y0,&xn,&h);

    x=x0;

    y=y0;

    printf("\n\n\tX\t\t\t\tY\n");

    while(x<xn)

    {

        m1=f(x0,y0);

        m2=f((x0+h/2.0),(y0+m1*h/2.0));

        m3=f((x0+h/2.0),(y0+m2*h/2.0));

        m4=f((x0+h),(y0+m3*h));

        m=((m1+2*m2+2*m3+m4)/6);

        y=y+m*h;

        x=x+h;

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

    }

}

float f(float x,float y)

{

    float m;

    m=(x-y)/(x+y);

    return m;

}

Output:

Output

Enter the values of x0, y0, xn, h:0 2 2 0.5

    X		    Y

0.500000	1.621356

1.000000	1.242713

1.500000	0.864069

2.000000	0.485426

Explanation:

  • In this example, the code begins by declaring a function called f that computes the derivative of y concerning x and the relevant header files.
  • The user is prompted for the initial values (x0, y0) , the goal final x value (xn) , and the step size (h) in the main function.
  • Using the initial values, the x and y variables are initialized.
  • The Runge-Kutta technique is applied inside the while loop . The derivative function f is used at various places to calculate four intermediate slopes (m1, m2, m3, and m4) .
  • As a weighted average of the intermediate slopes, the final slope, m , is derived.
  • With the help of the final slope and step size, the y value is updated.
  • By the step size , the value of x is increased.
  • Printouts show the current x and y values .
  • The loop continues until x reaches the desired location, xn .
  • Based on the supplied equation (x - y) / (x + y) , the derivative is calculated using the f function.

The previous C program for the Runge Kutta 4 algorithm and the RK4 technique itself provide enhanced precision compared to the time-consuming Taylor series method. The level of accuracy attained corresponds to the term hr, where r varies depending on the specific method utilized and signifies the order of that particular technique.

Only specific function values at selected points are necessary for the Runge-Kutta formulas. Furthermore, the Runge-Kutta method of first order is analogous to Euler's method.

Input Required

This code uses input(). Please provide values below: