问题
I am currently solving a system of nonlinear ODE's. It is a set of kinematic motion equations, where I need to calculate the position angles with given anguar velocities
I found out, how to add a function dependent on time using the list, but the question is, how to add a parameter which is also time dependent, but given as a vector.
Simplified is it written in the following code. c(t) is a time function.
function dx = f(t, x, c)
dx(1) = x(1)*sin(x(2))
dx(2) = c*x(2)*cos(x(1))
dx(3) = t*cos(x(3))
endfunction
c = 1:32; // values from measured signal, here simplyfied
y0 = [0.1;0.1;0.1];
t0 = 0;
t = 0:0.1:%pi;
y = ode(y0, t0, t, list (f, c));
回答1:
See
- https://help.scilab.org/doc/5.5.2/en_US/interp1.html and
- https://help.scilab.org/doc/5.5.2/en_US/interpln.html
on how to convert a function table into an interpolating function.
using interpln
linear interpolation
Essentially, in the right side function dx=f(t,x,tc_arr)
you evaluate
function dx=f(t,x,tc_arr)
c = interpln(tc_arr, t)
dx(1) = x(1)*sin(x(2))
dx(2) = c*x(2)*cos(x(1))
dx(3) = t*cos(x(3))
endfunction
where tcarr
contains arrays of sampling times and sampling values.
an example signal for tests
As an example signal, take
t_sam = -1:0.5:4;
c_sam = sin(2*t_sam+1);
tc_arr = [ t_sam; c_sam ];
Note that you always will need the sample times to find out how the input time t
relates to the value array.
t = 1.23;
c = interpln(tc_arr, t)
returns c = - 0.2719243
.
using interp1
1D interpolation (with splines)
You could as well use the other function, which has more options in terms of interpolation method.
function dx=f(t,x,t_sam,c_sam)
c = interp1(t_sam, c_sam, t, "spline", "extrap")
dx(1) = x(1)*sin(x(2))
dx(2) = c*x(2)*cos(x(1))
dx(3) = t*cos(x(3))
endfunction
回答2:
For a more general solution than that of LutzL, you just need to define c(t)
and call it from inside de ODE:
function v = c(t)
//definition of c(t)
v = ...
endfunction
function dx = f(t, x, c)
dx(1) = x(1)*sin(x(2))
dx(2) = c(t)*x(2)*cos(x(1))
dx(3) = t*cos(x(3))
endfunction
回答3:
Putting all ideas together gives this working script :
function v = c(tc_arr,t)
//definition of c(t)
v = interpln(tc_arr,t)
endfunction
function dx = f(t, x, c, tc_arr)
dx(1) = x(1)*sin(x(2))
dx(2) = c(tc_arr, t)*x(2)*cos(x(1))
dx(3) = t*cos(x(3))
endfunction
// measured signal (e.g. 10 samples here)
t_sam = linspace(0, %pi, 10)
c_sam = sin(2*t_sam+1)
tc_arr = [t_sam; c_sam]
y0 = [0.1;0.1;0.1];
t0 = 0;
t = 0:0.1:%pi;
y = ode(y0, t0, t, list(f, c, tc_arr));
plot(t,y)
As explained previously, using interpolation (linear here) allows to compute c(t)
values at arbitrary values of t
, which are needed by the ode solver.
来源:https://stackoverflow.com/questions/57392624/how-to-include-time-dependent-variables-into-ode-solver-in-scilab