在每个时间步改变变量值 - Matlab ODE 函数

Change value of variable at each time step - Matlab ODE function

我要在 .m 文件中模拟以下微分方程组:

function dx = odefun(t,x)
    % display(x(1));
    % y = 5; 
    dx = [x(2); - y - 3*x(2) - 2*x(1)];
end

我正在模拟系统 运行 另一个 .m 文件,代码如下:

[t,x] = ode45(@odefun, [0 10], [0;0]);
% display(x(:,1));
plot(t,x(:,1));

我的问题是我希望 y 参数的值(恰好是我的系统的输出)在执行 ode(...) 函数时在每个时间步长中发生变化。我尝试发送另一个这样的参数:

[t,x] = ode45(@odefun, [0 10], [0;0], [some_elements]);
function dx = odefun(t,x,y)

但我收到错误:Not enough input arguments. 事实是我希望 y 参数在每个时间步从具有一百个元素的向量中取一个值。任何帮助将不胜感激。

未在 Matlab 上测试,只是为了提供帮助,主要来自 https://fr.mathworks.com/help/matlab/ref/ode45.html#bu00_4l_sep_shared-options ODE with Time-Dependent Terms

function dx = odefun(t,x,y,yt)
    f = interp1(yt,y,t);
    dx = [x(2); - f - 3*x(2) - 2*x(1)];
end

%define your array (or even function) y(yt)
yt = 0.0:0.1:10.0;
y = array; % array being your array of input values. (should contain the corresponding value for 0.0:0.1:10.0

[t,x] = ode45(@(t,x) odefun(t,x,y,yt), [0.0 10.0], [0;0]);

如果您希望在结果中包含特定的时间步长,请尝试以下操作:

[t,x] = ode45(@(t,x) odefun(t,x,y,yt), yt, [0;0]);

你也可以这样做,一定要让你的解对应0.0:0.1:10.0,

x100 = interp1(t,x,yt);

对于那些感兴趣的人,我 post 在这里我终于设法得到了我想要的东西。我有一个表示系统输入的 1x100 向量,我想取回一个由系统输出值组成的 1x100 向量。我想要一个值作为我输入的每个值的输出。

global f    
t = 0.1:0.1:10.0;
f = @create_signal;

[t,x] = ode45(@odefun, t, [0;0]);

odefun函数是这样的:

function dx = odefun(t,x)
    global f
    m = 15;
    b = 0.2;
    k = 2;
    u = f(t);

    dx = [x(2); u/m - (b/m)*x(2) - (k/m)*x(1)];

end

最后是创建输入值的函数:

function u = create_signal(t)

    u = 5*sin(t) + 10.5;

end

我应用了 MATLAB 的创建函数句柄功能。这是 mathworks 帮助的 link:https://www.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html