Difference between revisions of "MATLAB:Ordinary Differential Equations/Examples"
Line 110: | Line 110: | ||
===Population Growth=== | ===Population Growth=== | ||
− | |||
[[File:ODEPopDiffPlot.png|thumb|Result using rate of change proportional to measurement]] | [[File:ODEPopDiffPlot.png|thumb|Result using rate of change proportional to measurement]] | ||
For population growth, the rate of change of population is dependent | For population growth, the rate of change of population is dependent | ||
Line 163: | Line 162: | ||
</source> | </source> | ||
− | + | ||
− | + | ==Multiple Variable Models== | |
+ | [[File:ODETwoDiffPlot.png|thumb|Result for system with two variables]] | ||
+ | |||
It is possible to solve multiple-variable systems by making sure the | It is possible to solve multiple-variable systems by making sure the | ||
differential function returns values for each of the variables. For | differential function returns values for each of the variables. For | ||
Line 170: | Line 171: | ||
depends only on time while the second is dependent upon both time and | depends only on time while the second is dependent upon both time and | ||
the first variable: | the first variable: | ||
− | \begin{align | + | <center><math> |
+ | \begin{align} | ||
\frac{dy_1}{dt}&=k_1\cos(k_2t) & | \frac{dy_1}{dt}&=k_1\cos(k_2t) & | ||
\frac{dy_2}{dt}&=k_3y_1+k_4t | \frac{dy_2}{dt}&=k_3y_1+k_4t | ||
− | \end{align | + | \end{align} |
− | The differential file | + | </math></center> |
+ | The differential file <code>TwoDiff.m</code> for this system will have a 2x1 | ||
matrix as the output: | matrix as the output: | ||
− | + | <source lang="matlab"> | |
+ | function dydt = TwoDiff(t, y, k) | ||
+ | % Differential equations for two variables | ||
+ | % t is time | ||
+ | % y is the state vector | ||
+ | % k contains any required constants | ||
+ | % dydt must be a column vector | ||
+ | dydt = [... | ||
+ | k(1)*cos(k(2)*t);... | ||
+ | k(3)*y(1)+k(4)*t]; | ||
+ | </source> | ||
Finally, if you have systems with multiple dependent variables, just | Finally, if you have systems with multiple dependent variables, just | ||
be sure to put the initial conditions in a column vector. For | be sure to put the initial conditions in a column vector. For | ||
example, with the system defined as: | example, with the system defined as: | ||
− | \begin{align | + | <center><math> |
+ | \begin{align} | ||
\frac{dy_1}{dt}&=4\cos(3t) & | \frac{dy_1}{dt}&=4\cos(3t) & | ||
\frac{dy_2}{dt}&=-2y_1+0.5t | \frac{dy_2}{dt}&=-2y_1+0.5t | ||
− | \end{align | + | \end{align} |
− | you could use the following script called | + | </math></center> |
− | + | you could use the following script called <code>RunTwoDiff</code> to solve for both | |
− | + | <math>y_1</math> and <math>y_2</math> assuming <math>y_1</math> starts as 0 and <math>y_2</math> starts at -3: | |
− | + | <source lang="matlab"> | |
+ | % Initialize workspace and graph | ||
+ | clear; format short e; figure(1); clf | ||
+ | % Set name of file containing derivatives | ||
+ | DiffFileName = 'TwoDiff'; | ||
+ | |||
+ | % Set up time span, initial value(s), and constant(s) | ||
+ | % Note: Variables should be in columns | ||
+ | tspan = linspace(0, 5); | ||
+ | yinit = [0 -3]'; | ||
+ | k = [4 3 -2 0.5]; | ||
+ | |||
+ | % Determine if states should be plotted | ||
+ | PlotStates = 1; | ||
+ | |||
+ | %% Under the hood | ||
+ | % Use ODE function of choice to get output times and states | ||
+ | DE = eval(sprintf('@(t, y, k) %s(t,y,k)', DiffFileName)) | ||
+ | [tout, yout] = ode45(@(t,y) DE(t,y,k), tspan, yinit); | ||
+ | |||
+ | % Plot results | ||
+ | if PlotStates | ||
+ | StatePlotter(tout, yout) | ||
+ | end | ||
+ | |||
+ | </source> | ||
+ | <!-- | ||
\subsection{Example 5: Higher Order Differential Equations \label{ODE:ex:jerk}} | \subsection{Example 5: Higher Order Differential Equations \label{ODE:ex:jerk}} | ||
The system must be written in terms of first-order | The system must be written in terms of first-order |
Revision as of 20:00, 25 November 2009
The following examples show different ways of setting up and solving initial value problems in MATLAB. It is part of the page on Ordinary Differential Equations in MATLAB.
Contents
Examples
Note - each example began with the Templates provided at this web site. Some comments have been removed from the templates to conserve space while some comments may have been added to provide a clearer explanation of the process for a particular example.
Constant Rate of Change
If the dependent variable has a constant rate of change:
where \(k\) is some constant, you can provide the differential equation
with a function called ConstDiff.m
that contains the code:
function dydt = ConstDiff(t, y, k)
% Differential equation for constant growth
% t is time
% y is the state vector
% k contains any required constants
% dydt must be a column vector
dydt = k(1); % or just k since there is only one
You could calculate answers using this model with the following code
called RunConstDiff.m
,
which assumes there are 100 evenly spaced times between 0 and 10, the
initial value of \(y\) is 6, and the rate of change is 1.2:
% Initialize workspace and graph
clear; format short e; figure(1); clf
% Set name of file containing derivatives
DiffFileName = 'ConstDiff';
% Set up time span, initial value(s), and constant(s)
% Note: Variables should be in columns
tspan = linspace(0, 10);
yinit = 6;
k = 1.2;
% Determine if states should be plotted
PlotStates = 1;
%% Under the hood
% Use ODE function of choice to get output times and states
DE = eval(sprintf('@(t, y, k) %s(t,y,k)', DiffFileName))
[tout, yout] = ode45(@(t,y) DE(t,y,k), tspan, yinit);
% Plot results
if PlotStates
figure(1); clf
StatePlotter(tout, yout)
end
Time-dependent Rate of Change
If the dependent variable's rate of change is some function of time, this can be easily written using MATLAB. For example, if the differential equation is some quadratic function given as:
then the function providing the values of the derivative may be
written in a file called TimeDiff.m
function dydt = TimeDiff(t, y, k)
% Differential equation for time-based polynomial derivative
% t is time
% y is the state vector
% k contains any required constants
% dydt must be a column vector
dydt = polyval(k, t);
You could calculate answers using this model with the following code
called RunTimeDiff.m
,
which assumes there are 20 evenly spaced times between 0 and 4, the
initial value of \(y\) is 6, and the polynomial is defined by the vector
[2 -6 3]:
% Initialize workspace and graph
clear; format short e; figure(1); clf
% Set name of file containing derivatives
DiffFileName = 'TimeDiff';
% Set up time span, initial value(s), and constant(s)
% Note: Variables should be in columns
tspan = linspace(0, 4, 20);
yinit = 6;
k = [2 -6 3];
% Determine if states should be plotted
PlotStates = 1;
%% Under the hood
% Use ODE function of choice to get output times and states
DE = eval(sprintf('@(t, y, k) %s(t,y,k)', DiffFileName))
[tout, yout] = ode45(@(t,y) DE(t,y,k), tspan, yinit);
% Plot results
if PlotStates
StatePlotter(tout, yout)
end
Population Growth
For population growth, the rate of change of population is dependent upon the number of people as well as some constant of proportionality:
where \(k\) is again some constant.
In that case, the function may be written in a file called PopDiff.m
as follows:
function dydt = PopDiff(t, y, k)
% Differential equation for population growth
% t is time
% y is the state vector
% k contains any required constants
% dydt must be a column vector
dydt = k(1)*y(1); % or just k*y since both are 1x1
The following code, RunPopDiff.m
, will calculate the population for
a span of 3 seconds with 25
points for the population model above with an initial population of 10
and a constant of proportionality of 1.02:
% Initialize workspace and graph
clear; format short e; figure(1); clf
% Set name of file containing derivatives
DiffFileName = 'PopDiff';
% Set up time span, initial value(s), and constant(s)
% Note: Variables should be in columns
tspan = linspace(0, 3, 25);
yinit = 10;
k = 1.02;
% Determine if states should be plotted
PlotStates = 1;
%% Under the hood
% Use ODE function of choice to get output times and states
DE = eval(sprintf('@(t, y, k) %s(t,y,k)', DiffFileName))
[tout, yout] = ode45(@(t,y) DE(t,y,k), tspan, yinit);
% Plot results
if PlotStates
StatePlotter(tout, yout)
end
Multiple Variable Models
It is possible to solve multiple-variable systems by making sure the differential function returns values for each of the variables. For instance, in the following system the first variable's rate of change depends only on time while the second is dependent upon both time and the first variable:
The differential file TwoDiff.m
for this system will have a 2x1
matrix as the output:
function dydt = TwoDiff(t, y, k)
% Differential equations for two variables
% t is time
% y is the state vector
% k contains any required constants
% dydt must be a column vector
dydt = [...
k(1)*cos(k(2)*t);...
k(3)*y(1)+k(4)*t];
Finally, if you have systems with multiple dependent variables, just be sure to put the initial conditions in a column vector. For example, with the system defined as:
you could use the following script called RunTwoDiff
to solve for both
\(y_1\) and \(y_2\) assuming \(y_1\) starts as 0 and \(y_2\) starts at -3:
% Initialize workspace and graph
clear; format short e; figure(1); clf
% Set name of file containing derivatives
DiffFileName = 'TwoDiff';
% Set up time span, initial value(s), and constant(s)
% Note: Variables should be in columns
tspan = linspace(0, 5);
yinit = [0 -3]';
k = [4 3 -2 0.5];
% Determine if states should be plotted
PlotStates = 1;
%% Under the hood
% Use ODE function of choice to get output times and states
DE = eval(sprintf('@(t, y, k) %s(t,y,k)', DiffFileName))
[tout, yout] = ode45(@(t,y) DE(t,y,k), tspan, yinit);
% Plot results
if PlotStates
StatePlotter(tout, yout)
end
Questions
Post your questions by editing the discussion page of this article. Edit the page, then scroll to the bottom and add a question by putting in the characters *{{Q}}, followed by your question and finally your signature (with four tildes, i.e. ~~~~). Using the {{Q}} will automatically put the page in the category of pages with questions - other editors hoping to help out can then go to that category page to see where the questions are. See the page for Template:Q for details and examples.