Greetings all,

I have written the following code to calculate the solution to a system of ODEs, called the Matsuoka equations, by using the Runge-Kutta 4th order method. I am trying to implement this code on an Arduino microcontroller. The output of the equations, IC - IC, should oscillate but instead it "blows up (down)" and goes to negative infinity. This is written for Arduino so some things may need to be changed to get it to work in a pure c or c++ environment.

First I set up 5 arrays: 1 dummy array and 4 arrays to hold the values of the K1,K2,K3, and K4 coefficients. The Runge Kutta algorithm is set up as a function. The program calls the function and passes the dummy K array by reference to the function. The K array is altered due to the function calculations and then those values are stored in the K1 array to be used later to calculate the next step of the ODE. This process is repeated 3 times for the K2,K3, and K4 arrays. Finally the next step of the solution to the ODE is solved using K1,K2,K3, and K4.

My problem is made curiouser by the fact that I have gotten the code to work by foregoing all the for loops and solving each K coefficient line-by-line. This code is displayed below the NON-WORKING CODE and is termed WORKING CODE. What's interesting here is that the two codes produce the same values for the K1 and K2 arrays but for the K3 array the values differ in the K3 array position. The working code calculates K3 = 0.5 (which I confirmed by calculation) while the non-working code calculates K3 = 0.49999995230. Why does it round?

Any help as to why the NON-WORKING CODE blows up while the WORKING CODE oscillates would be greatly apperciated.

NON-WORKING CODE

void setup () {
Serial.begin(9600);
}

float c = 1, B = 2.5, Y = 2.5, tau1 = 1, tau2 = 2*tau1, timestep = 0.1, M = 0.5*timestep;
float IC[] = {1,1,1,0}, K[] = {0,0,0,0}, K1, K2, K3, K4;
int i;

void loop() {

rungeKutta(tau1,tau2,c,B,Y,0,IC,IC,IC,IC,K); //note M has been replaced by zero
for(i=0;i<=3;i++)
K1 = K;

rungeKutta(tau1,tau2,c,B,Y,M,IC,IC,IC,IC,K);
for(i=0;i<=3;i++)
K2 = K;

rungeKutta(tau1,tau2,c,B,Y,M,IC,IC,IC,IC,K);
for(i=0;i<=3;i++)
K3 = K;

rungeKutta(tau1,tau2,c,B,Y,timestep,IC,IC,IC,IC,K); //note M has been replaced by timestep
for(i=0;i<=3;i++)
K4 = K;

for(i=0;i<=3;i++)
IC = IC + (1.0/6.0)*(K1 + 2.0*(K2 + K3) + K4)*timestep;

Serial.println(IC - IC,DEC);

}//end main loop

float rungeKutta(float tau1, float tau2, float c, float B, float Y, float M, float IC0, float IC1, float IC2, float IC3, double *K)
{
K = (1.0/tau1)*(c - (IC0 + M*K) - B*(IC1 + M*K) - Y*max(IC2 + M*K,0));
K = (1.0/tau2)*(max(IC0 + M*K,0) - IC1 + M*K);
K = (1.0/tau1)*(c - (IC2 + M*K) - (B*IC3 + M*K) - Y*max(IC0 + M*K,0));
K = (1.0/tau2)*(max(IC2 + M*K,0) - (IC3 + M*K));
}//end of rungeKutta function

WORKING CODE

void setup () {

Serial.begin(9600);

}

float c = 1, B = 2.5, Y = 2.5, tau1 = 1, tau2 = 2*tau1, timestep = 0.1, M = 0.5*timestep;;
float IC[] = {1,1,1,0}, K1, K2, K3, K4;

void loop() {

K1 = (1.0/tau1)*(c - IC - B*IC - Y*max(IC,0));
K1 = (1.0/tau2)*(max(IC,0) - IC);
K1 = (1.0/tau1)*(c - IC - B*IC - Y*max(IC,0));
K1 = (1.0/tau2)*(max(IC,0) - IC);

K2 = (1.0/tau1)*(c - (IC + M*K1) - B*(IC + M*K1) - Y*max(IC + M*K1,0));
K2 = (1.0/tau2)*(max(IC + M*K1,0) - IC + M*K1);
K2 = (1.0/tau1)*(c - (IC + M*K1) - (B*IC + M*K1) - Y*max(IC + M*K1,0));
K2 = (1.0/tau2)*(max(IC + M*K1,0) - (IC + M*K1));

K3 = (1.0/tau1)*(c - (IC + M*K2) - B*(IC + M*K2) - Y*max(IC + M*K2,0));
K3 = (1.0/tau2)*(max(IC + M*K2,0) - IC + M*K2);
K3 = (1.0/tau1)*(c - (IC + M*K2) - (B*IC + M*K2) - Y*max(IC + M*K2,0));
K3 = (1.0/tau2)*(max(IC + M*K2,0) - (IC + M*K2));

K4 = (1.0/tau1)*(c - (IC + timestep*K3) - B*(IC + timestep*K3) - Y*max(IC + timestep*K3,0));
K4 = (1.0/tau2)*(max(IC + timestep*K3,0) - IC + timestep*K3);
K4 = (1.0/tau1)*(c - (IC + timestep*K3) - (B*IC + timestep*K3) - Y*max(IC + timestep*K3,0));
K4 = (1.0/tau2)*(max(IC + timestep*K3,0) - (IC + timestep*K3));

IC = IC + (1.0/6.0)*(K1 + 2.0*K2 + 2.0*K3 + K4)*timestep;
IC = IC + (1.0/6.0)*(K1 + 2.0*K2 + 2.0*K3 + K4)*timestep;
IC = IC + (1.0/6.0)*(K1 + 2.0*K2 + 2.0*K3 + K4)*timestep;
IC = IC + (1.0/6.0)*(K1 + 2.0*K2 + 2.0*K3 + K4)*timestep;

Serial.println(IC - IC,DEC);

}//end main loop

You need to make your arrays of size 4, not of size three. This means, your array declarations should be:

``float IC[] = {1,1,1,0}, K[] = {0,0,0,0}, K1, K2, K3, K4; //notice 4 here.``

You basically has a problem of overlapping memory (the last element of K1 …

## All 2 Replies

You need to make your arrays of size 4, not of size three. This means, your array declarations should be:

``float IC[] = {1,1,1,0}, K[] = {0,0,0,0}, K1, K2, K3, K4; //notice 4 here.``

You basically has a problem of overlapping memory (the last element of K1 was at the same memory place as the first element of K2, as an example, but it could involve different elements). I'm pretty sure that would fix your problem.

>>K3 = 0.49999995230. Why does it round?
That's completely normal. You are using a "float" variable which is not expected to have better precision than to the 7-8th significant digit. This number is exactly what you would expect to get. You could use "double" for more precision (15 significant digits), but it's not sure that an Arduino uC would support that (sometimes floating point operations are not even supported at all).

Thanks a ton Mike 2000, you are exactly right.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.