Undefined function t for autonomous ODE

3 ビュー (過去 30 日間)
David
David 2014 年 7 月 11 日
コメント済み: Sara 2015 年 2 月 17 日
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 Center および File ExchangeNumerical 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