dx/dt = -.1 x + .02 x y
dy/dt = .2 y - .025 x y
I am trying to figure out how to numerically solve this system of equations but my text book doesn't really explain how to do that. It just says "use a numerical solver". When I try to use MATLABs Lotka-Volterra or any of the previously asked questions I get the following errors;
>> function xdot = Lotka(t,x)
xdot = [x(1) - 0.05*x(1)*x(2); -0.5*x(2)-0.02*x(1)*x(2)];
function xdot = Lotka(t,x)
Error: Function definition not supported in this context. Create functions in code file.
>> xdot = lotka(t,x)
xdot = [x(1) - 0.05*x(1)*x(2); -0.5*x(2)-0.02*x(1)*x(2)];
Index exceeds array bounds.
Error in sym/subsref (line 859)
R_tilde = builtin('subsref',L_tilde,Idx);
Error in lotka (line 6)
yp = diag([1 - .01*y(2), -1 + .02*y(1)])*y;
I know my numbers don't match the code but I am trying to first figure out how to use Lotka or ODE45. My biggest issue is dealing with a system of equations with x, y, and t.
Any help is much appreciated.

 Akzeptierte Antwort

Star Strider
Star Strider am 14 Mär. 2021

0 Stimmen

Function definitions of the type you posted can be at the end of a script in recent MATLAB releases, and are not required to be separate function files. (The documentation on Function Basics discusses that.)
The easiest way to use your function is to create it as an anonymous function:
lotka = @(t,x) [x(1) - 0.05*x(1)*x(2); -0.5*x(2)-0.02*x(1)*x(2)];
It will work in the MATLAB ODE solvers, such as ode45 and the others. See the documentation on Anonymous Functions for details.

10 Kommentare

Matt Baron
Matt Baron am 17 Mär. 2021
Thank you, that makes sense.
Here is where I am stuck now;
lotka = @(t,x) [-.16*x(2) + 0.08*x(2)*x(4); 4.5*x(4)-0.9*x(2)*x(4)];
%The initial conditions are x(0)=2 and y(0)=4
%I don't understand the part where I replace x with x(2) and y with x(4) I
%know this comes from the initial conditions but it still doesn't make
%sense to me.
%here is what I currently get;
lotka(0,0)
Error using
@(t,x)[-.16*x(2)+0.08*x(2)*x(4);4.5*x(4)-0.9*x(2)*x(4)]
Index exceeds array bounds.
'lotka' appears to be both a function and a variable. If this is unintentional, use 'clear lotka' to remove the variable
'lotka' from the workspace.
%Am I supposed to create a funcion x as well?
%I want to graph this with something like t=0:24 to see if I can match the
%graph in the book. This is just the example question....
Star Strider
Star Strider am 17 Mär. 2021
My pleasure.
There were only two variables in the equation you posted, and now that function refers to four. The initial conditions vector must match the number of equations in the system.
Since I have no idea what the latest code refers to , I can’t offer any suggestions as to how to fix it.
Matt Baron
Matt Baron am 17 Mär. 2021
Me either... lol
Thats all my code currently has.
lotka = @(t,x) [-.16*x(2) + 0.08*x(2)*x(4); 4.5*x(4)-0.9*x(2)*x(4)];
lotka(0,0)
-----------------------------------------
Here is the example I am trying to figure out;
dx/dt = -.16x+.08xy
dy/dt = 4.5y-.9xy
x(0)=4, y(0)=4
"With the aid of a numerical solver, show the population curves for prey and predator"
This is the first one where the system of equations has both a dy and a dx.
It goes on to say "See chapter 10 for solving systems of non-linear differential equations, note chapter 10 is not included in this book......" Yet I am asked to do them....
Just so you know, I am working through the MATLAB online training and practicing daily but I need to progress in the course as well.
Thank you so much for all of your time and effort, I appreciate it.
You’ve essentially already done this.
Correcting the subscripting errors:
lotka = @(t,x) [-.16*x(1) + 0.08*x(1)*x(2); 4.5*x(2)-0.9*x(1)*x(2)];
x0 = [4 4];
tspan = [1 100];
[t,x] = ode45(lotka, tspan, x0);
figure
plot(t,x)
grid
legend('x(t)','y(t)')
xlabel('t')
ylabel('Populations')
.
Matt Baron
Matt Baron am 17 Mär. 2021
Bearbeitet: Matt Baron am 17 Mär. 2021
lotka = @(t,x) [-.16*x(1) + 0.08*x(1)*x(2); 4.5*x(2)-0.9*x(1)*x(2)];
x0 = [4 4]; %This is a vector that fulfills both the x and y conditions. If it way x(0)=1 and y(0)=2, this would say x0 = [1 2]; Correct?
tspan = [1 100]; %This is a vector that gives us t=1 through t=100.
[t,x] = ode45(lotka, tspan, x0); %This is the solver for the function lotka with variables tspan and x0
figure
plot(t,x)
grid
legend('x(t)','y(t)')
xlabel('t')
ylabel('Populations')
I need to get better in thinking in terms of vectors.
Thank you for your mentorship! Ill try it out tomorrow morning and report back.
Star Strider
Star Strider am 17 Mär. 2021
My pleasure!
x0 = [4 4]; %This is a vector that fulfills both the x and y conditions. If it way x(0)=1 and y(0)=2, this would say x0 = [1 2]; Correct?
Yes!
The other bolded statements are correct as well.
Matt Baron
Matt Baron am 19 Mär. 2021
I've worked through multiple Lotka problems now with ease.
Through this, I realized that the Lotka function;
lotka = @(t,x) [-.16*x(1) + 0.08*x(1)*x(2); 4.5*x(2)-0.9*x(1)*x(2)]
has the 2 functions as a column vector!
That combined with the x0 being a row vector has led me to a better understanding of how to better use MATLAB.
Star Strider
Star Strider am 19 Mär. 2021
Actually, whether the initial conditions vector is a row or column vector doesn’t matter. The MATLAB ODE numerical integration functions will work with it either way. (The boundary value and partial differential equation integrators do appear to require specific column-wise orientations for those and other data.)
Matt Baron
Matt Baron am 20 Mär. 2021
Right, I meant that I just figured out from your help this;
>> test
test =
function_handle with value:
@(x)[x+1;x+2]
>> x=[1 2]
x =
1 2
>> test(x)
ans =
2 3
3 4
That works, however it’s a bit different from using an initial conditions vector with the numeric ODE solvers, since in this instance:
test = @(x)[x+1;x+2];
x=[1 2];
q1 = test(x) % Row Vector Argument
q2 = test(x.') % Column Vector Argument
produce different results, however the ODE solvers automatically assign the appropriate elements of the initial conditions vector to the appropriate differential equations, regardless of the orientation of the initial conditions vector.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Mathematics finden Sie in Hilfe-Center und File Exchange

Produkte

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by