MATLAB: Use of ODE 45

student

I'm taking a course in Matlab at my local college and I've been asked to solve the following system of ODE's. I've tried to set up the problem following the given example, but I'm getting an error that makes no sense. I don't want someone to tell me the answer, but rather to help me understand what I'm doing wrong.
Here's the problem:
The following system of differential equations is proposed as a model of the seasonal spread of infectious disease in a population. Given the following:
Where S , I , and R are the fractions of susceptible, infected, and recovered individuals in the population.
MEW is the birth and death rate (assumed to be equal).
LAMBDA is the recovery rate.
The function BETA (t) is the seasonally dependent infection rate.
BETA = @(t) BETA (0) * (1+sin( (2*pi*t) / 12)
dSdT = MEW (1-S) – BETA(t) * I * S;
dIdT = BETA (t) * I * S – ( LAMBDA MEW ) * I;
dRdT = LAMBDA * I – MEW* *R
Start by solving this system of ODEs for t = 0 to 36 months with the following initial conditions and parameter values:
Initial conditions of S(0) = 1, I(0) = 1.0 × 10−6, and R(0) = 0.
Initial infection rate BETA(0) = 3.0.
MEW Birth death rate = 0.1. LAMBDA Recovery rate = 1.5.
Here is how I've attempted to set up the ode45 solver:
MEW = 0.1;
LAMBDA = 1.5;
y0 = [1, 1*10^-6, 0];
tspan = [0:1:36];
BETA = @(t) 3 * (1+sin( (2*pi*t) / 12));
system = @(t,y,BETA,MEW,LAMBDA)[
MEW*(1-y(1)) - BETA(t) .* y(2) .* y(1);
BETA(t) .* y(2) .* y(1) - (LAMBDA - MEW) * y(2);
LAMBDA *y(2) - MEW *y(3)
];
[t1,y1] = ode45(system,tspan,y0,[],MEW,LAMBDA)
Here is the error I'm getting:
Subscript indices must either be real positive integers or logicals.
Error in
@(t,y,BETA,MEW,LAMBDA)[MEW*(1-y(1))-BETA(t).*y(2).*y(1);BETA(t).*y(2).*y(1)-(LAMBDA-MEW)*y(2);LAMBDA*y(2)-MEW*y(3);]
Error in odearguments (line 87)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 113)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, odeFcn, ...
Error in Project4_2 (line 13)
[t1,y1] = ode45(system,tspan,y0,[],MEW,LAMBDA)
Can anyone provide any insight?

Best Answer

Your ‘system’ anonymous funciton will pick up the other parameters from the workspace, so there is no need to pass them specifically in the ode45 call.
This works for me (without any other alterations in your code):
[t1,y1] = ode45(@(t,y) system(t,y,BETA,MEW,LAMBDA),tspan,y0);
The documentation for the ODE solvers explains that syntax.
Don’t forget to plot it to be sure it looks like you’re getting the correct output:
figure(1)
plot(t1, y1)
grid