我有以下功能:

function ypdiff = ypdiff(t,y)
    a = 0.01;
    b = 0.1;
    ypdiff(1) = -a*y(1)*y(2);
    ypdiff(2) = b*y(1)*y(2)-b*y(2);
    ypdiff(3) = b*y(2);
    ypdiff = [ypdiff(1) ypdiff(2) ypdiff(3)]';

如果我想解决这个问题,我将调用ode45函数,如下所示:
[t y] = ode45(@ypdiff, [to tf], yo);

但是如果我想给这个函数传递一个参数,我将如何使用ode45函数具体来说,我正在尝试以下公式:
function ypdiff = ypdiff(t,y,u)
    a = 0.01;
    b = 0.1;
    ypdiff(1) = -a*u*y(1)*y(2);
    ypdiff(2) = b*u*y(1)*y(2)-b*y(2);
    ypdiff(3) = b*u*y(2);
    ypdiff = [ypdiff(1) ypdiff(2) ypdiff(3)]';

最佳答案

您可以在matlab中使用anonymous function(类似于其他语言中的lambda函数):

u = 1.2;
[t y] = ode45(@(t, y) ypdiff(t, y, u), [to tf], yo);

08-25 07:55