0

I want to compute the solution of a first order differentiable equation using ode45 in MATLAB.

But I want to compute the solution in precise times and knowing a input variable in those instants.

Let t be the vector with n time instants where I want to compute the solution, and u the input vector also with n values, where $u(k)$ is the value of $u$ at instant $k$, and $k$ belong to $t$.

I call the ode45 function as:

[t,T_dot]=ode45(@f_mass,t,.01,odeset,u);

and f_mass is:

function T_dot=f_mass(t,T,u)
T_dot=(1-u)*T;
end

How can I specify that u is u(t)?

Thanks

Rhei
  • 127
  • 11
Hugo
  • 13
  • 1
  • 5
  • Is `u` a continuous function of time `t` or does it change in steps at discrete points in time? – horchler Nov 13 '14 at 18:49
  • u changes i time, for exemple: t=[0 0.1 0.2 0.3] u=[1 1.1 1.5 1.8] In other words, how can I choose the instante time where ode45 is going to compute the solution? – Hugo Nov 13 '14 at 19:11
  • I realize that `u` changes in time. The question is how? Is it [piecewise constant](http://mathworld.wolfram.com/PiecewiseConstantFunction.html)? – horchler Nov 13 '14 at 19:16
  • No, u is the output of the impulse response of a system – Hugo Nov 13 '14 at 19:17
  • 1
    So `u(t)` is a continuously varying smooth function of time, but you only have a vector of discrete data values to represent it, not an equation. You'll need to use interpolation to create a lookup table. This question has been [asked and answered before](http://stackoverflow.com/questions/19732442/) so yours will need to marked as duplicate. [Also this](http://stackoverflow.com/questions/14343563/). See [this question](http://stackoverflow.com/questions/21800241/) on a potential issue that can arise. – horchler Nov 13 '14 at 19:27
  • 1
    Also, `odeset` is a function, not a parameter so you shouldn't be passing directly as argument to `ode45`. And you seem to be using an outdated way of passing values to your integration function. Unless you're using a truly ancient version of Matlab, you should not be passing variables as extra argument after the options returned by `odeset`. Instead, use an anonymous function and pass them via the first argument, e.g., `@(t,T)f_mass(t,T,u)`. Read more about [parametrizing functions here](http://www.mathworks.com/help/matlab/math/parameterizing-functions.html). – horchler Nov 13 '14 at 19:35

1 Answers1

0

Not tested on Matlab but just to help and mainly wrapped up from https://fr.mathworks.com/help/matlab/ref/ode45.html#bu00_4l_sep_shared-options ODE with Time-Dependent Terms

function T_dot=f_mass(t,T,u, ut)
f = interp1(ut,u,t);
T_dot=(1-f)*T;
end

then solve with ode 45

ut=[0 0.1 0.2 0.3];
u=[1 1.1 1.5 1.8];

[t,T] = ode45(@(t,T) odefun(t,T,u,ut), ut, 0.01, odeset);
PilouPili
  • 2,601
  • 2
  • 17
  • 31