Export ODEs to MATLAB (pysb.export.matlab)

A class for converting a PySB model to a set of ordinary differential equations for integration in MATLAB.

Note that for use in MATLAB, the name of the .m file must match the name of the exported MATLAB class (e.g., robertson.m for the example below).

For information on how to use the model exporters, see the documentation for pysb.export.

Output for the Robertson example model

Information on the form and usage of the generated MATLAB class is contained in the documentation for the MATLAB model, as shown in the following example for pysb.examples.robertson:

classdef robertson
    % A simple three-species chemical kinetics system known as "Robertson's
    % example", as presented in:
    % H. H. Robertson, The solution of a set of reaction rate equations, in Numerical
    % Analysis: An Introduction, J. Walsh, ed., Academic Press, 1966, pp. 178-182.
    % A class implementing the ordinary differential equations
    % for the robertson model.
    % Save as robertson.m.
    % Generated by pysb.export.matlab.MatlabExporter.
    % Properties
    % ----------
    % observables : struct
    %     A struct containing the names of the observables from the
    %     PySB model as field names. Each field in the struct
    %     maps the observable name to a matrix with two rows:
    %     the first row specifies the indices of the species
    %     associated with the observable, and the second row
    %     specifies the coefficients associated with the species.
    %     For any given timecourse of model species resulting from
    %     integration, the timecourse for an observable can be
    %     retrieved using the get_observable method, described
    %     below.
    % parameters : struct
    %     A struct containing the names of the parameters from the
    %     PySB model as field names. The nominal values are set by
    %     the constructor and their values can be overriden
    %     explicitly once an instance has been created.
    % Methods
    % -------
    % robertson.odes(tspan, y0)
    %     The right-hand side function for the ODEs of the model,
    %     for use with MATLAB ODE solvers (see Examples).
    % robertson.get_initial_values()
    %     Returns a vector of initial values for all species,
    %     specified in the order that they occur in the original
    %     PySB model (i.e., in the order found in model.species).
    %     Non-zero initial conditions are specified using the
    %     named parameters included as properties of the instance.
    %     Hence initial conditions other than the defaults can be
    %     used by assigning a value to the named parameter and then
    %     calling this method. The vector returned by the method
    %     is used for integration by passing it to the MATLAB
    %     solver as the y0 argument.
    % robertson.get_observables(y)
    %     Given a matrix of timecourses for all model species
    %     (i.e., resulting from an integration of the model),
    %     get the trajectories corresponding to the observables.
    %     Timecourses are returned as a struct which can be
    %     indexed by observable name.
    % Examples
    % --------
    % Example integration using default initial and parameter
    % values:
    % >> m = robertson();
    % >> tspan = [0 100];
    % >> [t y] = ode15s(@m.odes, tspan, m.get_initial_values());
    % Retrieving the observables:
    % >> y_obs = m.get_observables(y)

        function self = robertson()
            % Assign default parameter values
            self.parameters = struct( ...
                'k1', 0.040000000000000001, ...
                'k2', 30000000, ...
                'k3', 10000, ...
                'A_0', 1, ...
                'B_0', 0, ...
                'C_0', 0);

            % Define species indices (first row) and coefficients
            % (second row) of named observables
            self.observables = struct( ...
                'A_total', [1; 1], ...
                'B_total', [2; 1], ...
                'C_total', [3; 1]);

        function initial_values = get_initial_values(self)
            % Return the vector of initial conditions for all
            % species based on the values of the parameters
            % as currently defined in the instance.

            initial_values = zeros(1,3);
            initial_values(1) = self.parameters.A_0; % A()
            initial_values(2) = self.parameters.B_0; % B()
            initial_values(3) = self.parameters.C_0; % C()

        function y = odes(self, tspan, y0)
            % Right hand side function for the ODEs

            % Shorthand for the struct of model parameters
            p = self.parameters;

            % A();
            y(1,1) = -p.k1*y0(1) + p.k3*y0(2)*y0(3);
            % B();
            y(2,1) = p.k1*y0(1) - p.k2*power(y0(2), 2) - p.k3*y0(2)*y0(3);
            % C();
            y(3,1) = p.k2*power(y0(2), 2);

        function y_obs = get_observables(self, y)
            % Retrieve the trajectories for the model observables
            % from a matrix of the trajectories of all model
            % species.

            % Initialize the struct of observable timecourses
            % that we will return
            y_obs = struct();

            % Iterate over the observables;
            observable_names = fieldnames(self.observables);
            for i = 1:numel(observable_names)
                obs_matrix = self.observables.(observable_names{i});
                if isempty(obs_matrix)
                    y_obs.(observable_names{i}) = zeros(size(y, 1), 1);
                species = obs_matrix(1, :);
                coefficients = obs_matrix(2, :);
                y_obs.(observable_names{i}) = ...
                                y(:, species) * coefficients';
class pysb.export.matlab.MatlabExporter(model, docstring=None)[source]

A class for returning the ODEs for a given PySB model for use in MATLAB.

Inherits from pysb.export.Exporter, which implements basic functionality for all exporters.


Generate a MATLAB class definition containing the ODEs for the PySB model associated with the exporter.


String containing the MATLAB code for an implementation of the model’s ODEs.