MATLAB:Ordinary Differential Equations/Templates

From PrattWiki
Revision as of 00:42, 4 December 2018 by DukeEgr93 (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

The codes below present templates 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

Combined Template

If the matrix values for the first derivative(s) can be calculated in one line of code, the problem can be solved in one file with the derivative calculation(s) taking place in an anonymous function:

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

% Be sure to change the derivative calculations,
% 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

% Calculate derivatives - must be a column
dydt = @(t, y, c) ;

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

% Determine if states should be plotted
PlotStates = 1;

%% Under the hood
% Use ODE function of choice to get output times and states
[tout, yout] = ode45(@(t,y) dydt(t,y,C), tspan, yinit);

% Plot results
if PlotStates
    StatePlotter(tout, yout)
end

=== Broken into two files ===
If the derivative calculations take more than one line of code, the derivatives can be calculated in their own file and then referenced in the main script:
==== Differential Equation File Template ====
<source lang="matlab">
function dydt = GeneralDiff(t, y, C)
% Template for calculating first derivatives of state variables
% t is time
% y is the state vector
% C 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 = ;
C     = ;

% 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, C) %s(t,y,C)', DiffFileName))
[tout, yout] = ode45(@(t,y) DE(t,y,C), 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