How to resolve the algorithm Runge-Kutta method step by step in the C programming language

Published on 7 June 2024 03:52 AM
#C

How to resolve the algorithm Runge-Kutta method step by step in the C programming language

Table of Contents

Problem Statement

Given the example Differential equation: With initial condition: This equation has an exact solution:

Demonstrate the commonly used explicit   fourth-order Runge–Kutta method   to solve the above differential equation.

Starting with a given

y

n

{\displaystyle y_{n}}

and

t

n

{\displaystyle t_{n}}

calculate: then:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Runge-Kutta method step by step in the C programming language

This source code is a C program that solves a differential equation using the fourth-order Runge-Kutta method.

The program first includes the necessary header files.

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

The program then defines a function called rk4 that implements the fourth-order Runge-Kutta method. This function takes a derivative function, a step size, a starting value for the independent variable, and a starting value for the dependent variable as arguments. It returns the value of the dependent variable at the next step.

double rk4(double(*f)(double, double), double dx, double x, double y)
{
   double	k1 = dx * f(x, y),
   	k2 = dx * f(x + dx / 2, y + k1 / 2),
   	k3 = dx * f(x + dx / 2, y + k2 / 2),
   	k4 = dx * f(x + dx, y + k3);
   return y + (k1 + 2 * k2 + 2 * k3 + k4) / 6;
}

The program then defines a function called rate that computes the derivative of the dependent variable. This function takes the independent variable and the dependent variable as arguments. It returns the value of the derivative.

double rate(double x, double y)
{
   return x * sqrt(y);
}

The program then defines the main function. This function is the entry point of the program.

int main(void)

The main function first declares a pointer to an array of doubles, a double variable called x, and a double variable called y. It then declares a double variable called x0 and a double variable called x1. It then declares a double variable called dx and an integer variable called i. It then declares an integer variable called n.

The main function then allocates memory for the array of doubles.

y = (double *)malloc(sizeof(double) * n);

The main function then sets the value of y[0] to 1. It then enters a loop that iterates over the values of i from 1 to n-1. In each iteration of the loop, the main function calls the rk4 function to compute the value of y[i].


for (y[0] = 1, i = 1; i < n; i++)
   	y[i] = rk4(rate, dx, x0 + dx * (i - 1), y[i-1]);

The main function then prints the values of x, y, and the relative error.


printf("x\ty\trel. err.\n------------\n");
   for (i = 0; i < n; i += 10) {
   	x = x0 + dx * i;
   	y2 = pow(x * x / 4 + 1, 2);
   	printf("%g\t%g\t%g\n", x, y[i], y[i]/y2 - 1);
   }

The main function then returns 0.


   return 0;
}

Source code in the c programming language

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

double rk4(double(*f)(double, double), double dx, double x, double y)
{
	double	k1 = dx * f(x, y),
		k2 = dx * f(x + dx / 2, y + k1 / 2),
		k3 = dx * f(x + dx / 2, y + k2 / 2),
		k4 = dx * f(x + dx, y + k3);
	return y + (k1 + 2 * k2 + 2 * k3 + k4) / 6;
}

double rate(double x, double y)
{
	return x * sqrt(y);
}

int main(void)
{
	double *y, x, y2;
	double x0 = 0, x1 = 10, dx = .1;
	int i, n = 1 + (x1 - x0)/dx;
	y = (double *)malloc(sizeof(double) * n);

	for (y[0] = 1, i = 1; i < n; i++)
		y[i] = rk4(rate, dx, x0 + dx * (i - 1), y[i-1]);

	printf("x\ty\trel. err.\n------------\n");
	for (i = 0; i < n; i += 10) {
		x = x0 + dx * i;
		y2 = pow(x * x / 4 + 1, 2);
		printf("%g\t%g\t%g\n", x, y[i], y[i]/y2 - 1);
	}

	return 0;
}


  

You may also check:How to resolve the algorithm Ludic numbers step by step in the Racket programming language
You may also check:How to resolve the algorithm Null object step by step in the OCaml programming language
You may also check:How to resolve the algorithm Color wheel step by step in the Fōrmulæ programming language
You may also check:How to resolve the algorithm Element-wise operations step by step in the Julia programming language
You may also check:How to resolve the algorithm Draw a sphere step by step in the Evaldraw programming language