MATLAB Answers

0

What to do if I want make parameters a vector in a ODE23?

Asked by Brian S. on 20 Nov 2019 at 0:50
Latest activity Commented on by Brian S. on 20 Nov 2019 at 9:50
Im trying to model prey/predator population dynamics with a range of parameters.
I have 3 scripts
(1) script with my parameters named 'preypredator_Parameters'
p.r = 0.1 ;
p.a = 0.01 ;
p.c = 0.9 ;
p.m = 0.03 ;
p.tSpan = [0 1000];
(2) script with my function named 'preypredator_ODE'
function dNdt = preypredator_ODE(t, N)
preypredator_Parameters ;
prey = N(1) ;
pred = N(2) ;
dNdt = [p.r * prey - p.a * prey * pred ; ...
p.c * p.a * prey * pred - p.m * pred] ;
(3) driver to run my ODE named 'preypredator_ODEdriver'
preypredator_Parameters ;
N0 = [10 10] ;
[t, N] = ode23s(@preypredator_ODE, p.tSpan, N0);
plot(t, N)
I would like to run the ODE with two of my parameters lets say p.m being a vector with a range of values (p.m = [0.03 0.05 0.07 0.09]) and p.r being a vector (p.r = [0.01 0.02 0.03 0.04]). As a final result I would like to produce 4 population dynamics graphs each using a different value of p.m and p.r while my other parameters stay constant, for example my first graph would use p.r(1) = 0.1, p.a = 0.01, p.c = 0.9, p.m(1) = 0.03, p.tSpan = [0 1000]; my second graph would use p.r(2) = 0.2, p.a = 0.01, p.c = 0.9, p.m(2) = 0.05, p.tSpan = [0 1000].
I know that if I was to just change the parameters to vectors there would be a 'Error using vertcat Dimensions of arrays being concatenated are not consistent.'. Im also not sure how to even produce the 4 graphs afterwards either.
In reality I have a lot more p.m and p.r values, so writing each value as a seperate parameter and creating a new ODEs each with a different set of parameters isnt too ideal.
Ive looked at several other discussions that appear to ask and answer a similar question to mine, however with my lack of experience with matlab, I cant seem to implement their sollutions to my situation.
Thanks in advance for any sort of help.

  0 Comments

Sign in to comment.

1 Answer

Answer by Steven Lord
on 20 Nov 2019 at 3:47

There's an example on the ode23s documentation page (open this by typing doc ode23s in the MATLAB Command Window) for how to pass additional parameters into your ODE function. Write your ODE function in that style, so it accepts the parameters over which you want to sweep as additional inputs, then just call ode23 in a loop (or a pair of loops) passing in the appropriate elements of your vectors of additional parameters.

  1 Comment

Thanks! ill take a look at the documentation for ode23

Sign in to comment.