Matlab ODE45 with parameters which depend on the solution

I am trying to solve an ordinary differential equation with Matlabs ODE45 function. This works very fine with constant parameters. Now, I want to declare my parameters such that they depend on the solution at the last time step. The code looks like this
[t,q] = ode45(@ems,[t_begin t_end],initCondition);
In my function ems, there is a parameter which depends on q at the last time step. ODE45 solves my function for the entire time span. I am not interested in, how to "pause" the solver at each time step in order to assign the solution q to my function to be solved.

Risposte (2)

Torsten
Torsten il 19 Mag 2017
Modificato: Torsten il 19 Mag 2017
If you want to reset a parameter after each successful time step taken by the solver, you can use the "OutputFcn" routine (see the options structure of ODE45).
But note that MATLAB's ODE-integrators use adaptive time stepping. So the length of the time steps vary during integration. It might be better to use your own solver with fixed time stepping instead.
Best wishes
Torsten.

5 Commenti

Or show us the mathematical form of the differential equation you're trying to solve. You may have a delay-differential equation rather than an ordinary differential equation, and there are specific functions for DDEs that may be better suited to the problem you're trying to solve.
Hi Steven, below you can see the equation I try to solve. The parameters m and d are constant whereas k depends on the solved q(1). var_k is a function in which a lookup-table describes to relation between q(1) and k.
function dq = ems(t,q,m,d,table)
k = var_k(q(1),table);
dq = zeros(2,1);
dq(1) = q(2);
dq(2) = -(k/m)*q(1)-(d/m)*q(2);
And what's the problem with the code from above ?
Best wishes
Torsten.
Hi Torsten, since my results seem odd, I am not sure if I implemented it correctly. Especially line k = var_k(q(1),table);
The problem is that my number of step size are much higher when k is dependent on q.
We don't know what function "var_k" does and how you call the ODE solver, but if "var_k" interpolates the value of k to the value of q(1), the code should work.
Best wishes
Torsten.

Accedi per commentare.

Jan
Jan il 22 Mag 2017
var_k(q(1),table) could mean in interpolation. If this is linear, remember that Matlab's integrators are designed to integrate differentiable functions only. Otherwise the stepsize controller is driven out of its purpose. See also http://www.mathworks.com/matlabcentral/answers/59582#answer_72047 .
Are you aware that the details about the function var_k matter the problem? Then it would be useful to show us, what happens inside this function.

1 Commento

Hi Jan, indeed there is a linear interpolation in that function
function c_out = var_k(displacement,table)
table_c = zeros(size(table,1),size(table,2));
table_c(:,1) = table(:,1);
table_c(:,2) = gradient(table(:,2));
cq = interp1(table_c(:,1),table_c(:,2),displacement);
c_out = cq;
And as you suggested, the step size gets very small which indicates a problem in the integration scheme (your third point at the referenced thread). How can I fix this problem?

Accedi per commentare.

Tag

Richiesto:

il 19 Mag 2017

Modificato:

il 22 Mag 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by