Products & Services Solutions Academia Support User Community Company

Learn more about Simulink   

mdlProjection - Perturb the solver's solution of a system's states to better satisfy time-invariant solution relationships

Required

No

C Syntax

#define MDL_PROJECTION
void mdlProjection(SimStruct *S)

C Arguments

S

SimStruct representing an S-Function block.

M Syntax

Projection(s)

M Arguments

s

Instance of Simulink.MSFcnRunTimeBlock class representing the Level-2 M-File S-Function block.

Description

This method is intended for use with S-functions that model dynamic systems whose states satisfy time-invariant relationships, such as those resulting from mass or energy conservation or other physical laws. The Simulink engine invokes this method at each time step after the model's solver has computed the S-function's states for that time step. Typically, slight errors in the numerical solution of the states cause the solutions to fail to satisfy solution invariants exactly. Your mdlProjection method can compensate for the errors by perturbing the states so that they more closely approximate solution invariants at the current time step. As a result, the numerical solution adheres more closely to the ideal solution as the simulation progresses, producing a more accurate overall simulation of the system modeled by your S-function.

Your mdlProjection method's perturbations of system states must fall within the solution error tolerances specified by the model in which the S-function is embedded. Otherwise, the perturbations may invalidate the solver's solution. It is up to your mdlProjection method to ensure that the perturbations meet the error tolerances specified by the model. See Perturbing a System's States Using a Solution Invariant for a simple method for perturbing a system's states. The following articles describe more sophisticated perturbation methods that your mdlProjection method can use.

Perturbing a System's States Using a Solution Invariant

Here is a simple, Taylor-series-based approach to perturbing a system's states. Suppose your S-function models a dynamic system having a solution invariant, , i.e., is a continuous, differentiable function of the system states, , and time, , whose value is constant with time. Then

where

Given a continuous, differentiable invariant function for the system that your S-function models, this formula allows your S-function's mdlProjection method to compute a perturbation

of the solver's numerical solution, , that more closely matches the ideal solution, , keeping the S-function's solution from drifting from the ideal solution as the simulation progresses.

M Example

This example illustrates how the perturbation method outlined in the previous section can keep a model's numerical solution from drifting from the ideal solution as a simulation progresses. Consider the following model (open):

The PredPrey block references an S-function, predprey_noproj.m, that uses the Lotka-Volterra equations

to model predator-prey population dynamics, where is the population density of the predators and is the population density of prey. The ideal solution to the predator-prey ODEs satisfies the time-invariant function

where , , and are constants. The S-function assumes a = 1, c = 2, and d = 121.85.

The Invariant Residual block in this model computes the residual between the invariant function evaluated along the system's ideal trajectory through state space and its simulated trajectory:

where and are the values computed by the model's solver for the predator and prey population densities, respectively, at the current time step. Ideally, the residual should be zero throughout simulation of the model, but simulating the model reveals that the residual actually strays considerably from zero:

Now consider the following model (open):

This model is the same as the previous model, except that its S-function, predprey.m, includes a mdlProjection method that uses the perturbation approach outlined in Perturbing a System's States Using a Solution Invariant to compensate for numerical drift. As a result, the numerical solution more closely tracks the ideal solution as the simulation progresses as demonstrated by the residual signal, which remains near or at zero throughout the simulation:

Languages

C, C++, M

  


Related Products & Applications

Learn more about Simulink through this collection of videos, articles, technical literature and the Getting Started with Simulink Guide.

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