views:

233

answers:

1

I have the following function:

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

If I want to solve this, I would call the ode45 function as follows:

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

But if I want to pass a parameter to this function, how would I use the ode45 function? Specifically, I am trying for the following formulation:

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)]';
+3  A: 

You can use an anonymous function in matlab (similar to lambda functions in other languages):

u = 1.2;
[t y] = ode45(@(t, y) ypdiff(t, y, u), [to tf], yo);
catchmeifyoutry
Great... Thanks a lot.. However, I couldn't get it working until I removed the ':' character
Legend
yep, that was a typo, sorry ;)
catchmeifyoutry
Oh... Thanks for the clarification :)
Legend