How to solve differential equations with varying constants

4 views (last 30 days)
Ufuk Yavuz
Ufuk Yavuz on 12 Oct 2021
Edited: Ufuk Yavuz on 13 Oct 2021
I have 3 equations and 3 variables:
syms u(t) v(t) w(t):
I want to solve for u,v,w. However constants X, Y, Z and p,q,r have changing values over time and they are defined as arrays. For example X array is something like that:
X=[ -47.9440 19.0518 -14.4223 -47.9833 ......]
or p array is something like that:
p=[ 0.3266 0.0750 0.0111 -0.1975 -0.1741 ......]
Can you help me pls?

Answers (2)

Bjorn Gustavsson
Bjorn Gustavsson on 12 Oct 2021
For the case you describe you have to add information about how X and p varies with time. Are they constant over some fixed intervalls, are they varying continuously in some manner (piece-wise linearly, like polynomial splines)?
For many cases where I have time-varying parameters obtained at fixed times I typically integrate the system of ODEs numerically and interpolate the time-varying coefficients as best I can. For this case that would be something like:
function duvwdt = yourODE(t,uvw,t4interp,X,Y,Z,p,q,r)
u = uvw(1);
v = uvw(2);
w = uvw(3);
Xi = interp1(t4interp,X,t,'pchip');
Yi = interp1(t4interp,Y,t,'pchip');
Zi = interp1(t4interp,Z,t,'pchip');
pi = interp1(t4interp,p,t,'pchip');
qi = interp1(t4interp,q,t,'pchip');
ri = interp1(t4interp,r,t,'pchip');
du = Xi - qi*w + ri*v;
dv = Yi - ri*u - pi*w;
dw = Zi - pi*v - qi*u;
This way you can integrate the ODEs with arbitrary smoothly varying coefficients - provided that the interpolation makes an OK representation of the time-variations (whatever OK means).
Then you integrate this with ode45 or any of its siblings:
uvw0 = [1 2 3]; % you'll have to have some initial conditions
t_span = [0,10]; % and some time-interval of interest.
[t,uvw] = ode45(@(t,uvw) yourODE(t,uvw,t4interp,X,Y,Z,p,q,r),t_span,uvw0);
Here you would have to make sure that your parameter-arrays (X,Y,Z,p,q,r) are available for the entire time-interval and that t4interp are the same for all 6 parameters, if they are at given at different "sampling-times" the modification should be trivial.
If your parameters are constant at different time-intervalls - then you will have to solve the equations intervall-for-intervall either numerically (by using the final values of the solution of the previous intervall as initial conditions for the following intervall) of analytically (for which you'll have to patch the solutions together at the intervall boundaries).
  1 Comment
Ufuk Yavuz
Ufuk Yavuz on 13 Oct 2021
It gives an error saying:
Unrecognized function or variable 't4interp'.
And also I must say that my datas in these arrays are not very smooth.

Sign in to comment.

John D'Errico
John D'Errico on 12 Oct 2021
If a constant varies with time, then is it really a constant? Of course not.
X=[ -47.9440 19.0518 -14.4223 -47.9833]
X = 1×4
-47.9440 19.0518 -14.4223 -47.9833
If this variable varies with time, then at which times are those values associated?
You should learn to use an interpolation scheme. I would suggest here to use a tool like spline, makina, or pchip.
t_X = [0 1 2 3];
Xspl = spline(t_X,X);
this is now a function of time. You can pass it around, using a tool like fnval, or ppval to evaluate it at any given time in the domain of support, thus the interval [0,3].
ans = -29.0489

Community Treasure Hunt

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

Start Hunting!

Translated by