Products & Services Solutions Academia Support User Community Company

Learn more about SimBiology   

Desktop Example — Using User-Defined Functions in Expressions

Prerequisites

To work through the example, these sections assume you have a working knowledge of the following:

Overview

You can use custom defined functions in reaction rate, rule, and event expressions. When you use a function in a SimBiology expression, the solver calls the function every time the expression is evaluated during simulation.

Therefore, if you have complex reaction rate expressions you can define the reaction rate with a function.

Requirements For Using User-Defined Functions in SimBiology Expressions

The requirements do not have to be executed in any specific order, but you might find it more convenient to start with the first two items before calling the function from within the SimBiology expression. This is because colored cues for model verification in the SimBiology desktop will show the expression as invalid if the function has not yet been defined, or is not on the path or the working directory.

The example below shows how to create and call user-defined functions in SimBiology expressions. More specifically, the example shows how to use a user-defined function in a rule expression. You can use user-defined functions similarly in event functions (EventFcns property), event triggers (Trigger property) and in reaction rate expressions (ReactionRate property). For example, you can define a function that gives the reaction rate expression as the output, and use the function in a reaction rate expression.

About the Example Model

This example uses the model from Modeling a G Protein Cycle in the SimBiology Model Reference documentation.

This table shows the reactions used to model the G protein cycle and the corresponding rate parameters (rate constants) for each reaction. For reversible reactions, the forward rate parameter is listed first.

No.NameReactionRate Parameters
1Receptor-ligand interactionL + R <-> RLkRL,kRLm
2Heterotrimeric G protein formationGd + Gbg -> GkG1
3G protein activationRL + G -> Ga + Gbg + RLkGa
4Receptor synthesis and degradationR <-> nullkRdo, kRs
5Receptor-ligand degradationRL -> nullkRD1
6G protein inactivationGa -> GdkGd

Assumptions

For the purpose of this example, assume that:

Creating an M-File Function

  1. In the MATLAB desktop, select File > New > M-File, to open a new M-file in the MATLAB Editor.

  2. Copy and paste the following function declaration:

    % inhibvalex.m
    function Cp = inhibvalex(t, Cpo, kel)
    
    % This function takes the input arguments t, Cpo, and kel 
    % and returns the value of the inhibitor Cp.
    % You can define the input arguments in a 
    % SimBiology rule expression. 
    % For example in the rule, define:
    % t as time (a keyword recognized as simulation time), 
    % Cpo as a parameter that represents the initial 
    % amount of inhibitor and kel as a parameter 
    % that governs the amount of inhibitor.
    
    if  t < 400   
        Cp = Cpo*exp(-kel*(t));
    else
        Cp = Cpo*exp(-kel*(t-400));
    end
  3. Save the M-file (name the file inhibvalex.m) in a directory that you can access, or that is on the path.

  4. If the location of the M-file is not on the path, change the working directory to the M-file location.

Calling the Function in a Rule Expression

Opening and Saving the Example Model

  1. Load the example project by typing the following at the command line:

    sbioloadproject gprotein

    The model is stored in a variable called m1.

  2. Open the SimBiology desktop with the model loaded by typing:

    simbiology(m1)

    The desktop opens with Model Session-Heterotrimeric_G_Protein_wt.

  3. Select File > Save Project As. The Save SimBiology Project dialog box opens.

  4. Specify a name (for example, gprotein_ex) and location for your project, and click Save.

Adding User-Defined Functions to the Example Model

The previously defined function inhibvalex in Creating an M-File Function lets you specify how the inhibitor amount changes over time. This section shows you how to specify the input values for the function in a rule expression. As defined in the function, the output value is the amount of inhibitor.

Define a new rule to assign the inhibitor value.

  1. In the Project Explorer, expand SimBiology Model and click Rules to open the Rules pane.

  2. In the Enter Rule box, type the following expression and press Enter:

    Inhib = inhibvalex(time, Cpo, Kel)

    The Rule Variables dialog box opens for you to define the rule variables.

  3. From the Type list, select species for Inhib, and parameter for Cpo and Kel. The SimBiology desktop creates the two species and the parameter.

      Note   If inhibvalex is on the list of undefined variables in the Rule Variables dialog box, this means that you have not yet put the M-file on the path or changed the working directory to the location of the M-file.

      Leave inhibvalex undefined in the Rule Variables dialog box and click OK. In the MATLAB desktop, change the Current Directory field to the location of the M-file.

  4. Click OK.

  5. In the Rules pane from the RuleType list, select repeatedAssignment.

  6. In the Settings tab, in Parameters being used by Rule, find the row containing the parameter Kel. Double-click the Value column, type 0.01, and press Enter.

  7. Find the row containing the parameter Cpo. Double-click the Value column, type 250 and press Enter.

      Note   You do not have to set a value for the species Inhib because it is being specified by a repeatedAssignment rule.

  8. Save the project by selecting File > Save Project.

Defining a Rule to Affect Parameter Value

As described in About the Example Model , the parameter kGd should be affected by the amount of inhibitor present in the system. Add a rule to describe this action, but first change the Scope and ConstantValue properties of the parameter kGd so that it can be varied by a rule.

  1. In the Project Explorer, expand SimBiology Model and click Parameters to open the Parameters pane.

  2. Select the row containing the parameter kGd.

  3. Right-click and select Change Parameter Scope. Notice that the Scope column now shows the model name for this parameter.

  4. In the Settings tab, clear the ConstantValue check box for kGd, as this parameter is being varied by a rule.

  5. In the Project Explorer, click Rules to open the Rules pane.

  6. In the Enter Rule box, type the following expression and press Enter:

    kGd = 1/Inhib

    In the Settings tab you should see a green square indicating that the rule variables have been previously defined, and that there are no other warnings or errors associated with this rule.

  7. For the new rule, in the Rules pane from the RuleType list, select repeatedAssignment.

  8. Save the project by selecting File > Save Project.

Simulating the Modified Model

  1. In the Project Explorer, right-click SimBiology Model and select Run Simulation.

    The simulation runs to completion and plots the result in a figure. The plot does not show the species of interest due to a wide range in species amounts/concentrations. Follow the next steps to view the species of interest.

  2. In the Project Explorer, under Simulation, click Data to open the Data pane for the most recent simulation run.

  3. Click the Plots tab.

  4. In the Arguments section, click . The Select Values for y dialog box opens.

  5. Clear the check box for the following species:

    RL
    L
    R
    Gbg

      Note   Species names are prefixed with the name of the compartment to which the species belongs. The default compartment is 'unnamed'.

  6. In the Plot Type box, select Time and click Add Plot Type.

  7. Select the row containing the new plot, and then in the Arguments section, click . The Select Values for y dialog box opens.

  8. Click Clear All, and then select the check box for the species Inhib.

  9. Click OK.

  10. Click Plot. Your plots should resemble the following:

    Notice the change in profile of species Ga at time = 400 seconds (simulation time) when the inhibitor amount is changed to reflect the re-addition of inhibitor to the model.

See Also

To learn about...Refer to...
The SimBiology desktopGetting Started in the SimBiology Desktop in the SimBiology Getting Started Guide.
M-file functionsfunction in the MATLAB Function Reference.
Changing the working directory to the directory containing your M-filecd command in the MATLAB Function Reference.
Adding the directory containing your M-files to the MATLAB search pathaddpath in the MATLAB Function Reference or Adding Folders to the Search Path in MATLAB Desktop Tools and Development Environment.

  


Recommended Products

Includes the most popular MATLAB recorded presentations with Q&A sessions led by MATLAB experts.

 © 1984-2009- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS