如何将随时间变化的参数传递给 SciLab ode?
How to pass a parameter that changes with time to SciLab ode?
我正在尝试使用 SciLab 的 ode 函数解决传热问题。问题是:其中一个参数随时间变化,h(t)。
ODE
我的问题是:如何将参数传递给随时间变化的 ode 函数?
ode
允许额外函数的参数作为列表:
It may happen that the simulator f needs extra arguments. In this
case, we can use the following feature. The f argument can also be a
list lst=list(f,u1,u2,...un) where f is a Scilab function with
syntax: ydot = f(t,y,u1,u2,...,un) and u1, u2, ..., un are extra
arguments which are automatically passed to the simulator simuf.
额外参数是t的函数
function y = f(t,y,h)
// define y here depending on t and h(t),eg y = t + h(t)
endfunction
function y = h(t)
// define here h(t), eg y = t
endfunction
// define y0,t0 and t
y = ode(y0, t0, t, list(f,h)) // this will pass the h function as a parameter
Extra 是一个向量,我们要为其提取相应的项。
因为 ode
只计算 y
在 t
的解。一个想法是在 ode
执行计算时寻找 Ti < t < Tj
并得到 Hi < h < Hj
.
这很丑陋但完全有效:
function y = h(t,T,H)
res = abs(t - T) // looking for nearest value of t in T
minres = min(res) // getting the smallest distance
lower = find(res==minres) // getting the index : T(lower)
res(res==minres)=%inf // looking for 2nd nearest value of t in T: nearest is set to inf
minres = min(res) // getting the smallest distance
upper = find(minres==res) // getting the index: T(upper)
// Now t is between T(lower) (nearest) and T(upper) (farest) (! T(lower) may be > T(upper))
y = ((T(upper)-t)*H(lower)+(t-T(lower))*H(upper))/(T(upper)-T(lower)) // computing h such as the barycenter with same distance to H(lower) and H(upper)
endfunction
function ydot=f(t, y,h,T,H)
hi = h(t,T,H) // if Ti< t < Tj; Hi<h(t,T,H)<Hj
disp([t,hi]) // with H = T, hi = t
ydot=y^2-y*sin(t)+cos(t) - hi // example of were to use hi
endfunction
// use base example of `ode`
y0=0;
t0=0;
t=0:0.1:%pi;
H = t // simple example
y = ode(y0,t0,t,list(f,h,t,H));
plot(t,y)
我正在尝试使用 SciLab 的 ode 函数解决传热问题。问题是:其中一个参数随时间变化,h(t)。 ODE
我的问题是:如何将参数传递给随时间变化的 ode 函数?
ode
允许额外函数的参数作为列表:
It may happen that the simulator f needs extra arguments. In this case, we can use the following feature. The f argument can also be a list lst=list(f,u1,u2,...un) where f is a Scilab function with syntax: ydot = f(t,y,u1,u2,...,un) and u1, u2, ..., un are extra arguments which are automatically passed to the simulator simuf.
额外参数是t的函数
function y = f(t,y,h)
// define y here depending on t and h(t),eg y = t + h(t)
endfunction
function y = h(t)
// define here h(t), eg y = t
endfunction
// define y0,t0 and t
y = ode(y0, t0, t, list(f,h)) // this will pass the h function as a parameter
Extra 是一个向量,我们要为其提取相应的项。
因为 ode
只计算 y
在 t
的解。一个想法是在 ode
执行计算时寻找 Ti < t < Tj
并得到 Hi < h < Hj
.
这很丑陋但完全有效:
function y = h(t,T,H)
res = abs(t - T) // looking for nearest value of t in T
minres = min(res) // getting the smallest distance
lower = find(res==minres) // getting the index : T(lower)
res(res==minres)=%inf // looking for 2nd nearest value of t in T: nearest is set to inf
minres = min(res) // getting the smallest distance
upper = find(minres==res) // getting the index: T(upper)
// Now t is between T(lower) (nearest) and T(upper) (farest) (! T(lower) may be > T(upper))
y = ((T(upper)-t)*H(lower)+(t-T(lower))*H(upper))/(T(upper)-T(lower)) // computing h such as the barycenter with same distance to H(lower) and H(upper)
endfunction
function ydot=f(t, y,h,T,H)
hi = h(t,T,H) // if Ti< t < Tj; Hi<h(t,T,H)<Hj
disp([t,hi]) // with H = T, hi = t
ydot=y^2-y*sin(t)+cos(t) - hi // example of were to use hi
endfunction
// use base example of `ode`
y0=0;
t0=0;
t=0:0.1:%pi;
H = t // simple example
y = ode(y0,t0,t,list(f,h,t,H));
plot(t,y)