具有与时间有关的输入参数的ODE 15s [英] ODE 15s with time dependent input parameters
问题描述
我有一个MATLAB代码可以解决以下类型的大规模ODE系统
I have a MATLAB code that solves a large scale ODE system of following type
function [F] = myfun(t,C,u)
u这里是时间相关向量,用作函数myfun中的输入.我当前的代码为:-
u here is a time dependent vector used as a input in the function myfun. My current code reads as:-
u = zeros(4,1);
u(1) = 0.1;
u(2) = 0.1;
u(3) = 5.01/36;
u(4) = 0.1;
C0 = zeros(15*4*20+12,1);
options = odeset('Mass',Mf,'RelTol',1e-5,'AbsTol',1e-5);
[T,C] = ode15s(@OCFDonecolumnA, [0,5000] ,C0,options,u);
我想将整个时域分为5个不同的部分,并让u的元素在不同时间采用不同的值(类似于多步函数).考虑到以后我想解决一个优化问题来确定域[0,5000]中每个时间间隔的u值,什么是最好的编码方式.
I would like to have the entire time domain split into 5 different sections and have the elements of u take different values at different times(something like a multiple step function). what would be the best way to code it considering that I later want to solve a optimization problem to determine values of u for each time interval in the domain [0,5000].
谢谢
推荐答案
ode15s
希望您的模型函数接受时间的参数t
和矢量x
(或您命名的C
)微分状态变量.由于ode15s
不允许我们传递控制输入的另一个向量u
,因此我通常将ODE函数ffcn
包装在model
函数中:
ode15s
expects your model function to accept a parameter t
for time and a vector x
(or C
as you named it) of differential state variables. Since ode15s
will not let us pass in another vector u
of control inputs, I usually wrap the ODE function ffcn
inside a model
function:
function [t, x] = model(x0, t_seg, u)
idx_seg = 0;
function y = ffcn(t, x)
% simple example of exponential growth
y(1) = u(idx_seg) * x(1)
end
...
end
在上面,model
的参数是初始状态值x0
,切换时间矢量t_seg
和不同段中控制输入值的矢量u
.您可以从ffcn
内部看到idx_seg
.这使我们可以将模型集成到所有段中(将上面的...
替换为以下内容):
In the above, the parameters of model
are the initial state values x0
, the vector of switching times t_seg
, and the vector u
of control input values in different segments. You can see that idx_seg
is visible from within ffcn
. This allows us to integrate the model over all segments (replace ...
in the above with the following):
t_start = 0;
t = t_start;
x = x0;
while idx_seg < length(t_seg)
idx_seg = idx_seg + 1;
t_end = t_seg(idx_seg);
[t_sol, x_sol] = ode15s(@ffcn, [t_start, t_end], x(end, :));
t = [t; t_sol(2 : end)];
x = [x; x_sol(2 : end, :)];
t_start = t_end;
end
在循环的第一次迭代中,t_start
为0,t_end
为第一个开关点.现在,我们使用ode15s
仅在此时间间隔内进行积分,而我们的ffcn
用u(1)
评估ODE.解决方案y_sol
和相应的时间点t_sol
附加到我们的总体解决方案(t
,x
).
In the first iteration of the loop, t_start
is 0 and t_end
is the first switching point. We use ode15s
now to only integrate over this interval, and our ffcn
evaluates the ODE with u(1)
. The solution y_sol
and the corresponding time points t_sol
are appended to our overall solution (t
, x
).
对于下一次迭代,我们将t_start
设置为当前段的末尾,并将新的t_end
设置为下一个切换点.您还可以看到t_seg
的最后一个元素必须是模拟结束的时间.重要的是,我们将模拟轨迹的当前尾部y(end, :)
传递给ode15s
作为下一段的初始状态向量.
For the next iteration, we set t_start
to the end of the current segment and set the new t_end
to the next switching point. You can also see that the last element of t_seg
must be the time at which the simulation ends. Importantly, we pass to ode15s
the current tail y(end, :)
of the simulated trajectory as the initial state vector of the next segment.
总而言之,函数model
将使用ode15s
逐段模拟模型,并返回总体轨迹y
及其时间点t
.您可以通过以下示例来说服自己
In summary, the function model
will use ode15s
to simulate the model segment by segment and return the overall trajectory y
and its time points t
. You could convince yourself with an example like
[t, x] = model1(1, [4, 6, 12], [0.4, -0.7, 0.3]);
plot(t, x);
对于我的指数增长示例,它应该产生
which for my exponential growth example should yield
为了进行优化运行,您将需要编写目标函数.该目标函数可以将u
传递给model
,然后通过查看x
来计算与u
关联的优点.
For your optimization runs, you will need to write an objective function. This objective function can pass u
to model
, and then calculate the merit associated with u
by looking at x
.
这篇关于具有与时间有关的输入参数的ODE 15s的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!