Undefined function t for autonomous ODE

5 次查看(过去 30 天)
I am new to matlab, and I am attempting to learn about modeling with ODE's with this helpful tutorial:http://matlabgeeks.com/tips-tutorials/modeling-with-odes-in-matlab-part-3/
But I am having problems running the code here. I don't know if it was created in a different version or what. I am using R2013b.
Below is the code all together with comments. When I run this, I am first prompted for input arguments. It is suggested that I run it as lokta_volterra(t,x). However, when I do this, I get Undefined function or variable 't'.
Now of course t is not "used" as this is, as the comments state, this is an autonomous equation. But I'm not sure how I'm supposed to run this to display the graph indicated on the help site.
% lotka_volterra.m
%
% Imlements the Lotka-Volterra function
% dx/dt = alpha x - beta xy
% dy/dt = delta xy - gamma y
%
% Inputs:
% t - Time variable: not used here because our equation
% is independent of time, or 'autonomous'.
% x - Independent variable: this contains both populations (x and y)
% Output:
% dx - First derivative: the rate of change of the populations
function dx = lotka_volterra(t, x)
dx = [0; 0];
alpha = 1;
beta = .05;
delta = .02;
gamma = .5;
dx(1) = alpha * x(1) - beta * x(1) * x(2);
dx(2) = delta * x(1) * x(2) - gamma * x(2);
% Set our preferences for ode45
% The default relative tolerance is 1e-3.
% To set our output to non-negative, we provide an array listing
% each population that we want to constrain. Since this example
% has two populations, we pass the array [1 2]
options = odeset('RelTol', 1e-4, 'NonNegative', [1 2]);
% Use ode45 to solve our ODE
% Place the time points in a vector 't'
% Place the solution in a vector 'x'
[t,x] = ode45('lotka_volterra', [0 20], [10 10], options);
plot(t,x);
legend('prey', 'predators');

采纳的回答

Sara
Sara 2014-7-11
You need to put this part in a separate function and run the new function you create
options = odeset('RelTol', 1e-4, 'NonNegative', [1 2]);
% Use ode45 to solve our ODE
% Place the time points in a vector 't'
% Place the solution in a vector 'x'
[t,x] = ode45('lotka_volterra', [0 20], [10 10], options);
plot(t,x);
legend('prey', 'predators');
  3 个评论
Beth
Beth 2015-2-15
Which part specifically needs to be made into a separate function?
Sara
Sara 2015-2-17
The part that calls the ode45 solver

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile 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!

Translated by