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

时间:2018-11-10 21:57:26

标签: matlab ode differential-equations

我有以下微分方程组,可以在.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参数在每个时间步长中从具有一百个元素的向量中取一个值。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

尚未在Matlab上进行测试,仅用于提供帮助,并且主要是由https://fr.mathworks.com/help/matlab/ref/ode45.html#bu00_4l_sep_shared-options ODE(带有时变术语)组成的

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);

答案 1 :(得分:0)

对于那些感兴趣的人,我在这里发布了我最终设法获得想要的东西的方式。我有一个表示系统输入的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的create function handle功能。这是mathworks帮助的链接:https://www.mathworks.com/help/matlab/matlab_prog/creating-a-function-handle.html

相关问题