Undefined function t for autonomous ODE
3 views (last 30 days)
Show older comments
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');
0 Comments
Accepted Answer
Sara
on 11 Jul 2014
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 Comments
More Answers (0)
See Also
Categories
Find more on Numerical Integration and Differential Equations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!