## The Runge-Kutta and Hamming Methods for Numerical Solution of Ordinary Differential Equations with an Example. C Code and Octave Script

### 2. Introduction

##### Unfortunately this method does not always provide a stable solution and computation errors can be increased during calculations at each new step and lead to an incorrect result. For more information see [1]. The method provides low accuracy (10 a). The following are stable methods for solving differential equations from Runge-Kutta, Hamming, Adams and model.

Figure 2-1: Euler Formula Explanation

### 3.2 Example of the Runge-Kutta Method

##### Figure 3-1 shows the solution “y” for (17), (18) by the Runge-Kutta method:

Figure 3-1: Solution (17), (18) of the Runge-Kutta Method with h=Ts=0.01s

### 4.2 Example of the Hamming Method

##### Figure 4-1 shows the solution “y” for (17), (18) by the Hamming method, and Figure 4-2 shows the error estimate at each step of the calculation:

Figure 4-1: Solution (17), (18) of the Hamming Method with h=Ts=0.01

Figure 4-2: Step Error of the Solution (17), (18) of the Hamming Method with h = Ts = 0.01s

### 5. Conclusions

##### In Figure 5-1, solutions (17), (18) are constructed together using both methods: Runge-Kutta and Hamming. The graphs completely match and prove that the step h = 0.01 is chosen correctly for both methods.

Figure 5-1: The Solution (17), (18) of the Runge-Kutta and Hamming Methods together with h = Ts = 0.01s

• ##### In the article “Control of Non Linear Object Using the Vostrikov’s Localization Method. C Code and Octave Script” the differential equation (17), (18) was solved using a model based on integrators. See the Figure 5-2

Figure 5-2: The Model Solution of the (17), (18)

##### Figure 5-3 shows the results of solving the differential equation (17), (18) by Runge-Kutta and the model methods at h = Ts = 0.01 s. It is obvious that the solution by the model method has low accuracy. This means that the sample value is not suitable for the model. An acceptable result for the model can be obtained at h = Ts < 0.001s. For more information, see the article “Control of a nonlinear object using the Vostrikov localization method. C code and octave script”

Figure 5-3: The Solution (17), (18) of the Runge-Kutta and Model Methods together with h = Ts = 0.01s

### 6. Octave GNU file NumericalDifferentialEquation.m

##### The .m script generates the (17), (18) solution with the Runge-Kutta, Hamming and Model methods, step signal and plots. The following functions are defined in this file:

% Numerical Solution Of the Differential Equation with the Runge Kutta Method
% y”(t) = a_coef_0*y^2(t) + a_coef_1*y'(t) + b_coef*u(t)
% Define the new function z(t)
% y'(t) = z(t)
% z'(t) = a_coef_0*y^2(t) + a_coef_1*z(t) + b_coef*u(t)
% Input parameters:
% u_input – input signal
% a_coef_1 – object parameter
% a_coef_0 – object parameter
% b_coef – object parameter
% y_start – begin value of the y output
% y_deriv1_start – begin value y’ output
% Ts – sample time
% Output parameter:
% y_output – output
function [y_output] = RungeKuttaOfDifferentialEquation(u_input, a_coef_1, a_coef_0, b_coef, y_start, y_deriv1_start, Ts)

% Numerical Solution Of the Differential Equation with the Hamming Method
% y”(t) = a_coef_0*y^2(t) + a_coef_1*y'(t) + b_coef*u(t)
% Define the new function z(t)
% y'(t) = z(t)
% z'(t) = a_coef_0*y^2(t) + a_coef_1*z(t) + b_coef*u(t)
% Input parameters:
% u_input – input signal
% a_coef_1 – object parameter
% a_coef_0 – object parameter
% b_coef – object parameter
% y_start – begin value of the y output
% y_deriv1_start – begin value y’ output
% y2 – y_output(2) for step 2
% y2_deriv1 – y’ for step 2
% Ts – sample time
% Output parameter:
% y_output – output
function [y_output y_error] = HammingOfDifferentialEquation(u_input, a_coef_1, a_coef_0, b_coef, y_start, y_deriv1_start, y2, y2_deriv1, Ts)

% Numerical Model Solution Of the Differential Equation:
% y”(t) = a_coef_0*y^2(t) + a_coef_1*y'(t) + b_coef*u(t)
% Input parameters:
% u_input – input signal
% a_coef_1 – object parameter
% a_coef_0 – object parameter
% b_coef – object parameter
% y_start – begin value of the y output
% y_deriv1_start – begin value y’ output
% Ts – sample time
% Output parameter:
% y_output – output
function [y_output] = ModelSolutionOfDifferentialEquation(u_input, a_coef_1, a_coef_0, b_coef, y_start, y_deriv1_start, Ts)

% Float Table to File
% Support C-Code
% Input parameters:
% ParameterVector – data array
% FileNameString – output file name
function FloatParamVector2file(ParameterVector, FileNameString)

### 7. C Language NumericalDifferentialEquation.c/h

##### Functions:

/*
Differential Equation Initialization
Initialization of the working arrays
Input: void
Return: void
*/
void DifferentialEquationInit(float y_start, float y_deriv_start, float y1, float y1_deriv);

/*
Runge-Kutta Method
Input
float u_control – control value
RungeKutta_data_s* history_ptr – previous history
Return
float – y_RungeKutta output value
*/
float RungeKuttaOfDifferentialEquation(float u_control, RungeKutta_data_s* history_ptr);

/*
Hamming Method
Input
float u_control – control value
Hamming_data_s* history_ptr – previous history
float* y_error_ptr – current step error
Return
float – y_Hamming output value
*/
float HammingOfDifferentialEquation(float u_control, Hamming_data_s* history_ptr, float* y_error_ptr);

/*
Bilinear Integrator for Model Solution
Laplace: Integrator(s) = 1/s
From bilinear transform => Integrator(z) = (Ts/2)(1 + z^-1)/(1 – z^-1)
y(n)=y(n-1)+(Ts/2)(x(n)+x(n-1))
Input
float input – input
model_integrator_data_s* history_ptr – integrator history
Return
float – output
*/
float bilinear_integrator(float input, model_integrator_data_s* history_ptr);

/*
Model Solution
Input
float u_control – control value
Return
float – y_model output value
*/
float ModelSolutionOfDifferentialEquation(float u_control, model_integrator_data_s* history1_ptr, model_integrator_data_s* history2_ptr);

### Appendix 2. Hamming Errors (33), (36)

Figure A2-1: The Example of the Function y(t): y_n-2, y_n-1, y_n