El martes, 10 de marzo de 2015, 18:14:45 (UTC-6), Rafael Guariento escribió:
>
> Hi I am trying to run the following code but I get an error when I try to 
> run the model (evaluate the result variable (calling the ODE23))
>
> the error is: Error array could not be broadcast to a common size
>
> Does anyone have any idea why? Thanks in advance
>

Hi,

You have the order of the arguments to `ode23` wrong. Try the following 
version.
It has some extra cleanup; note, in particular, the added . after 9999 to 
make an array of the correct type (`Float64`s) ).

Try to avoid using the anonymous function syntax, and just pass the SIR 
function straight to ode23.
There is a way to pass the extra arguments (your `p`) to the SIR function 
straight from ode23, but I don't have time to check
how right now.

Best,
David.


using ODE

function SIR(t,x,p)
    S, I, R = x 
    beta, gamma = p
    N = S + I
    dS = -beta*S*I/N
    dI = beta*S*I/N - gamma*I
    dR = gamma*I
    
    [dS,dI,dR]
end

# Initialise model
t = linspace(0,500,101);
inits=[9999.,1,0];
p=[0.1,0.05];

# Run model
result=ode23((t,x)-> SIR(t,x,p), inits, t);
 

>
>
> # Load libraries
> using ODE
> using DataFrames
> using Gadfly
>
> # Define the model
> # Has to return a column vector
> function SIR(t,x,p)
>     S=x[1]
>     I=x[2]
>     R=x[3]
>     beta=p[1]
>     gamma=p[2]
>     N=S+I
>     dS=-beta*S*I/N
>     dI=beta*S*I/N-gamma*I
>     dR=gamma*I
>     return([dS;dI;dR])
> end
>
> # Initialise model
> t = linspace(0,500,101);
> inits=[9999,1,0];
> p=[0.1,0.05];
>
>
> # Run model
> result=ode23((t,x)-> SIR(t,x,p),t,inits);
>
>
>
>
>

Reply via email to