Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
ODE solver

Subject: ODE solver

From: Qiming

Date: 27 Dec, 2012 16:22:06

Message: 1 of 3

Hi:

I have a question using ODE solvers (say, ode45).

I want to save some parameter values inside the ODE function. For instance, I would like to save parameter "u" which is inside the ODE function for future use. I define the global "u", and also a counter "COUNT".

As I know, ode45 is essentially a "loop", it integrates the ODE function as an adaptive stepsize or a pre-defined fixed step size. The functions are given as below (The following codes are only for the purpose of testing):

Main function:

global u COUNT;
COUNT = 0;

t0 = 0; tf = 5;
tSpan = [ t0, tf ];
y0 = [ 1; 2; 3 ];
[t y] = ode45(@myode, tSpan, y0);



ODE function:

function ydot = myode(~,y)

global u COUNT;
COUNT = COUNT + 1;

    a = 0.01;
    b = 0.1;
    ydot (1) = -a*y(1)*y(2);
    ydot (2) = b*y(1)*y(2)-b*y(2);
    ydot (3) = b*y(2);
    
    u(COUNT) = y(1)+y(2)+y(3);
    
    ydot = [ydot (1) ydot (2) ydot (3)]';

The code can be ran without errors or warnings. But the things is, when I checked the workspace, "COUNT = 61", but "length(y) = 41". I think these two values should be the same. I also tried using a fixed stepsize, but they're still different. How come they're different?

Thanks for any help.

Qiming

Subject: ODE solver

From: Sadik

Date: 29 Dec, 2012 01:52:08

Message: 2 of 3

Hi Qiming,

Just take a look at the details of the Runge-Kutta method to see how many times you need to evaluate the function.

Best.

Subject: ODE solver

From: Qiming

Date: 5 Jan, 2013 16:08:08

Message: 3 of 3

"Sadik " <sadik.hava@gmail.com> wrote in message <kblic8$3sf$1@newscl01ah.mathworks.com>...
> Hi Qiming,
>
> Just take a look at the details of the Runge-Kutta method to see how many times you need to evaluate the function.
>
> Best.

Thanks Sadik, I figured out how to fix this problem. Actually I write my own ODE solver based on 4th-order RK algorithm, and I can set the stepsize by myself.

Best,

Qiming

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us