Help with Lotka-Volterra error codes
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
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.
채택된 답변
Star Strider
2021년 3월 14일
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
Matt Baron
2021년 3월 17일
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
2021년 3월 17일
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
2021년 3월 17일
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
2021년 3월 17일
편집: Matt Baron
2021년 3월 17일
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
2021년 3월 17일
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
2021년 3월 19일
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
2021년 3월 19일
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
2021년 3월 20일
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.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Numerical Integration and Differential Equations에 대해 자세히 알아보기
제품
태그
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)
