Difference between revisions of "MATLAB:Ordinary Differential Equations/Templates"

From PrattWiki
Jump to navigation Jump to search
 
Line 1: Line 1:
The code below is a template for creating a script whose job is to solve a system of initial value problems based on ordinary differential equations.  Note that it takes advantage of
+
The codes below present a template for creating the function file responsible for computing values of the first derivatives of all the variables and the script whose job is to solve a system of initial value problems based on ordinary differential equations.  Note that the script takes advantage of
[[MATLAB:Flexible Programming|Flexible Programming]] in MATLAB such that the name of the function that calculates the actual derivatives is only required in the <code>DiffFileName</code> variable.  This program calls the <code>StatePlotter</code> program, available at [[MATLAB:Ordinary Differential Equations/State Plotter]].
+
[[MATLAB:Flexible Programming|Flexible Programming]] in MATLAB such that the name of the function that calculates the actual derivatives is only required in the <code>DiffFileName</code> variable.  This program calls the <code>StatePlotter</code> program, available at the bottom.
 
== Code ==
 
== Code ==
 +
=== Differential Equation File Template ===
 +
<source lang="matlab">
 +
function dydt = GeneralDiff(t, y, k)
 +
% Template for calculating first derivatives of state variables
 +
% t is time
 +
% y is the state vector
 +
% k contains any required constants
 +
% dydt must be a column vector
 +
dydt =
 +
</source>
 +
 +
=== Controlling Script Template ===
 
<source lang="matlab">
 
<source lang="matlab">
 
% Template for using an ODE solver in MATLAB
 
% Template for using an ODE solver in MATLAB
Line 37: Line 49:
 
</source>
 
</source>
  
 +
=== State Plotter ===
 +
The <code>StatePlotter.m</code> code will accept a vector of times and a matrix of states (which should have each state in a column).  It will determine how many states there are based on the number of columns, break the figure window up into the appropriate number of subplots, and graph each state.
 +
 +
<source lang="matlab">
 +
function StatePlotter(Time, States)
 +
 +
StateCount = size(States, 2);
 +
 +
NumCols = ceil(sqrt(StateCount));
 +
NumRows = ceil(StateCount / NumCols);
 +
clf;
 +
for PlotNumber = 1:StateCount
 +
        subplot(NumRows, NumCols, PlotNumber);
 +
        plot(Time, States(:,PlotNumber), 'ko:');
 +
        xlabel('Time');
 +
        ylabel(sprintf('y_{%0.0f}(t)', PlotNumber))
 +
        title(sprintf('y_{%0.0f}(t) vs. Time', PlotNumber));
 +
end
 +
</source>
  
 
== Questions ==
 
== Questions ==

Revision as of 19:35, 25 November 2009

The codes below present a template for creating the function file responsible for computing values of the first derivatives of all the variables and the script whose job is to solve a system of initial value problems based on ordinary differential equations. Note that the script takes advantage of Flexible Programming in MATLAB such that the name of the function that calculates the actual derivatives is only required in the DiffFileName variable. This program calls the StatePlotter program, available at the bottom.

Code

Differential Equation File Template

function dydt = GeneralDiff(t, y, k)
% Template for calculating first derivatives of state variables
% t is time
% y is the state vector
% k contains any required constants
% dydt must be a column vector
dydt =

Controlling Script Template

% Template for using an ODE solver in MATLAB
% tout and yout will be the time and state variables

% Be sure to change name of file containing derivatives,
% time span, initial values, and any constants, as well
% as setting the flag for whether to make state plots

% Initialize workspace and graph
clear; format short e; figure(1); clf

% Set name of file containing derivatives
DiffFileName = '';

% Set up time span, initial value(s), and constant(s)
% Note: Variables should be in columns
tspan = ;
yinit = ;
k     = ;

% 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

State Plotter

The StatePlotter.m code will accept a vector of times and a matrix of states (which should have each state in a column). It will determine how many states there are based on the number of columns, break the figure window up into the appropriate number of subplots, and graph each state.

function StatePlotter(Time, States)

StateCount = size(States, 2);

NumCols = ceil(sqrt(StateCount));
NumRows = ceil(StateCount / NumCols);
clf;
for PlotNumber = 1:StateCount
        subplot(NumRows, NumCols, PlotNumber);
        plot(Time, States(:,PlotNumber), 'ko:');
        xlabel('Time');
        ylabel(sprintf('y_{%0.0f}(t)', PlotNumber))
        title(sprintf('y_{%0.0f}(t) vs. Time', PlotNumber));
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.

External Links

References