MATLAB:如何将参数传递给函数?

时间:2010-02-13 02:12:50

标签: matlab parameters function calculus

我有以下功能:

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

1 个答案:

答案 0 :(得分:8)

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

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