将其他依赖于迭代的输入传递给ode45 [英] Passing additional iteration-dependent inputs to ode45
问题描述
我正在尝试使用ode45
函数求解微分方程.考虑以下代码,
I'm trying to solve differential equation using the ode45
function. Consider the following code,
[t1,X2] = ode45(@(t,x)fun(t,x,C1,C2,C3,C4),t0,X01);
其中参数C1
,C2
,C3
和C4
是列向量,对于ode45
所引用的功能(fun.m
)应该可用.我希望值在每次迭代后都发生变化,例如,在开始时,我要输入的C1
项是C1(1)
,在下一次迭代中是C1(2)
,依此类推.
where parameters C1
, C2
, C3
and C4
are column vectors, which should be available to the function that ode45
is referring to (fun.m
). I want the values to change after every iteration, so for example, at the beginning the entry of C1
I want in is C1(1)
, in the next iteration it's C1(2)
, etc.
我该如何实现?
推荐答案
You may have noticed that the official docs are not too helpful in this scenario (as they pretty much force you to use global
variables - which is doable, but discouraged). Instead, I'll show you how this can be done with classes and function handles. Consider the following:
classdef SimpleQueue < handle
%SIMPLEQUEUE A simple FIFO data structure.
properties (Access = private)
data
position
end
methods (Access = public)
function obj = SimpleQueue(inputData)
%SIMPLEQUEUE Construct an instance of this class
obj.data = inputData;
rewind(obj);
end % constructor
function out = pop(obj, howMany)
%POP return the next howMany elements.
if nargin < 2
howMany = 1; % default amount of values to return
end
finalPosition = obj.position + howMany;
if finalPosition > numel(obj.data)
error('Too many elements requested!');
end
out = obj.data(obj.position + 1 : obj.position + howMany);
obj.position = finalPosition;
end % pop
function [] = rewind(obj)
%REWIND restarts the element tracking
% Subsequent calls to pop() shall return elements from the beginning.
obj.position = 0;
end % rewind
end % methods
end % classdef
如何使用它?简单:
C1q = SimpleQueue(C1);
C2q = SimpleQueue(C2);
C3q = SimpleQueue(C3);
C4q = SimpleQueue(C4);
[t1,X2] = ode45(@(t,x)fun(t,x,@C1q.pop,@C2q.pop,@C3q.pop,@C4q.pop),t0,X01);
如您所见,在fun
内部,我们使用C1q()
而不是C1
.
As you can see, inside fun
we use C1q()
instead of C1
.
这篇关于将其他依赖于迭代的输入传递给ode45的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!