How can I introduce a delay in an internal variable in ode15s solver?

1 view (last 30 days)
I'm using an ode15s solver to solve a system of differential equations. After that, I also calculate some internal variables without derivative. These internal variables need to be used within the function itself with a delay. I don't know how to keep track of these variables and use them with delay.
Let's say I have this system of equations to solve and I want to keep track of Qin and use it with delay to calculate something else within the function.
P = 100.0+20.0*cos(t);
Qin = (P-Y(1))/R1;
Qout = (Y(1)-P2)/R2;
Qc = Qin-Qout;
dY(1, 1) = Qc/C;
At the moment (to be able to store the internal variables and use the delay), I'm using a ode4 with fixed timestep and it is slow. I add at the end of my code these two lines:
Time=[Time;t];
Qintrack(length(Time),1) = (P-Y(1))/R1;
If I use the ode15s, 'Time'contains all the t values that the solver try to solve: some values are repeated and they are not in a crescent order.

Accepted Answer

Torsten
Torsten on 8 Apr 2016
Your equation is a delay-differential equation. Thus you should use an appropriate solver, e.g.
Best wishes
Torsten.
  2 Comments
Martina Sciola
Martina Sciola on 8 Apr 2016
Thanks for the quick reply.
I read about how the dde works and it seems the delay is included in the differential equation, but it is applied at the derivative function.
In my case, the delay is in an normal function (not differential). For example:
dde23 solves:
df/dt= f(t-D)+ Qin
while I would like to solve:
df/dt= f+ Qin (t-D)
Can I use in some way the dde23?
Torsten
Torsten on 8 Apr 2016
Your right-hand side function f is given by
f=(Qin-Qout)/C=((P-Y1(t-tau))/R1-(Y1(t)-P2)/R2)/C,
thus it is a function of t, Y1(t) and Y1(t-tau).
That's the kind of equation which can be solved with DDE23.
Best wishes
Torsten.

Sign in to comment.

More Answers (1)

Leo Sun
Leo Sun on 24 Aug 2018
Hi there, I have a problem solving stiff delay differential equations recently. I just found that there's a paper written by Vikas Agrawal's group and they used a solver called "dde15s" for there calculation.
We can find the paper at the following website: https://onlinelibrary.wiley.com/doi/10.1021/bp034226s
At the bottom of the website, click "Supporting Information", and download the zip file "bp034226ssi20030804_024052.zip", which the dde15s.m is inside (or you may download the attachment form this comment).
But I found a new problem. I try to use dde15s instead of dde23 in my code, then the error appears:
Error using vertcat
Dimensions of matrices being concatenated are not consistent.
Error in dde15s>solextend (line 155)
solout.idata.kvec = [sol.idata.kvec; solex.idata.kvec(2:end)];
Error in dde15s (line 139)
sol = solextend(sol,solex);
In dde15s.m, function "solextend" is for Concatenate two solution structures produced by ODE15S. And the Author said " This function works with ODE15S from MATLAB 6.5, but it may require modifications to work with other versions."
I think the error exist in my code because I use the Matlab 2017a version. So is there anyone can fix that error and make "dde15s.m" fit to the newest version of Matlab?
Thanks for all your help.

Community Treasure Hunt

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

Start Hunting!