This is machine translation

Translated by Microsoft
Mouseover text to see original. Click the button below to return to the English version of the page.

Note: This page has been translated by MathWorks. Click here to see
To view all translated materials including this page, select Country from the country navigator on the bottom of this page.

Zero-Crossing Detection

A variable-step solver dynamically adjusts the time step size, causing it to increase when a variable is changing slowly and to decrease when the variable changes rapidly. This behavior causes the solver to take many small steps in the vicinity of a discontinuity because the variable is rapidly changing in this region. This improves accuracy but can lead to excessive simulation times.

The Simulink® software uses a technique known as zero-crossing detection to accurately locate a discontinuity without resorting to excessively small time steps. Usually this technique improves simulation run time, but it can cause some simulations to halt before the intended completion time.

Two algorithms are provided in the Simulink software: Nonadaptive and Adaptive. For information about these techniques, see Zero-Crossing Algorithms.

Demonstrating Effects of Excessive Zero-Crossing Detection

The Simulink software comes with three models that illustrate zero-crossing behavior: sldemo_bounce_two_integrators, sldemo_doublebounce, and sldemo_bounce.

  • The sldemo_bounce_two_integrators model demonstrates how excessive zero crossings can cause a simulation to halt before the intended completion time unless you use the adaptive algorithm.

  • The sldemo_bounce model uses a better model design than sldemo_bounce_two_integrators.

  • The sldemo_doublebounce model demonstrates how the adaptive algorithm successfully solves a complex system with two distinct zero-crossing requirements.

The Bounce Model with Two Integrators

  1. At the MATLAB® command prompt, type sldemo_bounce_two_integrators to load the example.

  2. Once the block diagram appears, set the Model Configuration Parameters > Solver > Algorithm parameter to Nonadaptive.

  3. Also in the Solver pane, set the Stop time parameter to 20 s.

  4. Run the model. In the Simulink Editor, select Simulation > Run.

  5. After the simulation completes, click the Scope block window to see the results.

    You may need to click on Autoscale to view the results in their entirety.

  6. Use the scope zoom controls to closely examine the last portion of the simulation. You can see that the velocity is hovering just above zero at the last time point.

  7. Change the simulation Stop time edit box in the Simulink Editor toolbar to 25 seconds, and run the simulation again.

  8. This time the simulation halts with an error shortly after it passes the simulated 20 second time point.

    Excessive chattering as the ball repeatedly approaches zero velocity has caused the simulation to exceed the default limit of 1000 for the number of consecutive zero crossings allowed. Although you can increase this limit by adjusting the Model Configuration Parameters > Solver > Number of consecutive zero crossings parameter. In this case, making that change does not allow the simulation to simulate for 25 seconds.

  9. Also in the Solver pane, from the Algorithm pull down menu, select the Adaptive algorithm.

  10. Run the simulation again.

  11. This time the simulation runs to completion because the adaptive algorithm prevented an excessive number of zero crossings from occurring.

Bounce Model with a Second-Order Integrator

The Double-Bounce Model

  1. At the MATLAB command prompt, type sldemo_doublebounce to load the example. The model and an animation window open. In the animation window, two balls are resting on two platforms.

  2. In the animation window, click the Nonadaptive button to run the example using the nonadaptive algorithm. This is the default setting used by the Simulink software for all models.

  3. The ball on the right is given a larger initial velocity. Consequently, the two balls hit the ground and recoil at different times.

  4. The simulation halts after 14 seconds because the ball on the left exceeded the number of zero crossings limit. The ball on the right is left hanging in mid air.

  5. An error message dialog opens. Click OK to close it.

  6. Click on the Adaptive button to run the simulation with the adaptive algorithm.

  7. Notice that this time the simulation runs to completion, even after the ground shifts out from underneath the ball on the left at 20 seconds.

How the Simulator Can Miss Zero-Crossing Events

The bounce and double-bounce models show that high-frequency fluctuations about a discontinuity ('chattering') can cause a simulation to prematurely halt.

It is also possible for the solver to entirely miss zero crossings if the solver error tolerances are too large. This is possible because the zero-crossing detection technique checks to see if the value of a signal has changed sign after a major time step. A sign change indicates that a zero crossing has occurred, and the zero-crossing algorithm will then hunt for the precise crossing time. However, if a zero crossing occurs within a time step, but the values at the beginning and end of the step do not indicate a sign change, the solver steps over the crossing without detecting it.

The following figure shows a signal that crosses zero. In the first instance, the integrator steps over the event because the sign has not changed between time steps. In the second, the solver detects change in sign and so detects the zero-crossing event.

Preventing Excessive Zero Crossings

Use the following table to prevent excessive zero-crossing errors in your model.

Make this change...How to make this change...Rationale for making this change...

Increase the number of allowed zero crossings

Increase the value of the Number of consecutive zero crossings. option on the Solver pane in the Configuration Parameters dialog box.

This may give your model enough time to resolve the zero crossing.

Relax the Signal threshold

Select Adaptive from the Algorithm pull down and increase the value of the Signal threshold option on the Solver pane in the Configuration Parameters dialog box.

The solver requires less time to precisely locate the zero crossing. This can reduce simulation time and eliminate an excessive number of consecutive zero-crossing errors. However, relaxing the Signal threshold may reduce accuracy.

Use the Adaptive Algorithm

Select Adaptive from the Algorithm pull down on the Solver pane in the Configuration Parameters dialog box.

This algorithm dynamically adjusts the zero-crossing threshold, which improves accuracy and reduces the number of consecutive zero crossings detected. With this algorithm you have the option of specifying both the Time tolerance and the Signal threshold.

Disable zero-crossing detection for a specific block

  1. Clear the Enable zero-crossing detection check box on the block's parameter dialog box.

  2. Select Use local settings from the Zero-crossing control pull down on the Solver pane of the Configuration Parameters dialog box.

Locally disabling zero-crossing detection prevents a specific block from stopping the simulation because of excessive consecutive zero crossings. All other blocks continue to benefit from the increased accuracy that zero-crossing detection provides.

Disable zero-crossing detection for the entire model

Select Disable all from the Zero-crossing control pull down on the Solver pane of the Configuration Parameters dialog box.

This prevents zero crossings from being detected anywhere in your model. A consequence is that your model no longer benefits from the increased accuracy that zero-crossing detection provides.

If using the ode15s solver, consider adjusting the order of the numerical differentiation formulas

Select a value from the Maximum order pull down on the Solver pane of the Configuration Parameters dialog box.

For more information, see Maximum order.

Reduce the maximum step size

Enter a value for the Max step size option on the Solver pane of the Configuration Parameters dialog box.

This can insure the solver takes steps small enough to resolve the zero crossing. However, reducing the step size can increase simulation time, and is seldom necessary when using the Adaptive algorithm.

Zero-Crossing Algorithms

The Simulink software includes two zero-crossing detection algorithms: Nonadaptive and Adaptive.

To choose the algorithm, either use the Algorithm option in the Solver pane of the Configuration Parameter dialog box, or use the ZeroCrossAlgorithm command. The command can either be set to 'Nonadaptive' or 'Adaptive'.

The Nonadaptive algorithm is provided for backwards compatibility with older versions of Simulink and is the default. It brackets the zero-crossing event and uses increasingly smaller time steps to pinpoint when the zero crossing has occurred. Although adequate for many types of simulations, the Nonadaptive algorithm can result in very long simulation times when a high degree of 'chattering' (high frequency oscillation around the zero-crossing point) is present.

The Adaptive algorithm dynamically turns the bracketing on and off, and is a good choice when:

  • The system contains a large amount of chattering.

  • You wish to specify a guard band (tolerance) around which the zero crossing is detected.

The Adaptive algorithm turns off zero-crossing bracketing (stops iterating) if either of the following are satisfied:

  • The zero crossing error is exceeded. This is determined by the value specified in the Signal threshold option in the Solver pane of the Configuration Parameters dialog box. This can also be set with the ZCThreshold command. The default is Auto, but you can enter any real number greater than zero for the tolerance.

  • The system has exceeded the number of consecutive zero crossings specified in the Number of consecutive zero crossings option in the Solver pane of the Configuration Parameters dialog box. Alternatively, this can be set with the MaxConsecutiveZCs command.

Understanding Signal Threshold

The Adaptive algorithm automatically sets a tolerance for zero-crossing detection. Alternatively, you can set the tolerance by entering a real number greater than or equal to zero in the Configuration Parameters Solver pane, Signal threshold pull down. This option only becomes active when the zero-crossing algorithm is set to Adaptive.

This graphic shows how the Signal threshold sets a window region around the zero-crossing point. Signals falling within this window are considered as being at zero.

The zero-crossing event is bracketed by time steps Tn-1 and Tn. The solver iteratively reduces the time steps until the state variable lies within the band defined by the signal threshold, or until the number of consecutive zero crossings equals or exceeds the value in the Configuration Parameters Solver pane, Number of consecutive zero crossings pull down.

It is evident from the figure that increasing the signal threshold increases the distance between the time steps which will be executed. This often results in faster simulation times, but might reduce accuracy.

How Blocks Work with Zero-Crossing Detection

A block can register a set of zero-crossing variables, each of which is a function of a state variable that can have a discontinuity. The zero-crossing function passes through zero from a positive or negative value when the corresponding discontinuity occurs. The registered zero-crossing variables are updated at the end of each simulation step, and any variable that has changed sign is identified as having had a zero-crossing event.

If any zero crossings are detected, the Simulink software interpolates between the previous and current values of each variable that changed sign to estimate the times of the zero crossings (that is, the discontinuities).

Note

The Zero-Crossing detection algorithm can bracket zero-crossing events only for signals of data type double

Blocks That Register Zero Crossings

The following table lists blocks that register zero crossings and explains how the blocks use the zero crossings:

BlockDescription of Zero Crossing

Abs

One: to detect when the input signal crosses zero in either the rising or falling direction.

Backlash

Two: one to detect when the upper threshold is engaged, and one to detect when the lower threshold is engaged.

Compare To Constant

One: to detect when the signal equals a constant.

Compare To Zero

One: to detect when the signal equals zero.

Dead Zone

Two: one to detect when the dead zone is entered (the input signal minus the lower limit), and one to detect when the dead zone is exited (the input signal minus the upper limit).

Enable

One: If an Enable port is inside of a Subsystem block, it provides the capability to detect zero crossings. See the Enable Subsystem block for details Using Enabled Subsystems.

From File

One: to detect when the input signal has a discontinuity in either the rising or falling direction

From Workspace

One: to detect when the input signal has a discontinuity in either the rising or falling direction

Hit Crossing

One or two. If there is no output port, there is only one zero crossing to detect when the input signal hit the threshold value. If there is an output port, the second zero crossing is used to bring the output back to 0 from 1 to create an impulse-like output.

If

One: to detect when the If condition is met.

Integrator

If the reset port is present, to detect when a reset occurs.

If the output is limited, there are three zero crossings: one to detect when the upper saturation limit is reached, one to detect when the lower saturation limit is reached, and one to detect when saturation is left.

MinMax

One: for each element of the output vector, to detect when an input signal is the new minimum or maximum.

Relational Operator

One: to detect when the specified relation is true.

Relay

One: if the relay is off, to detect the switch-on point. If the relay is on, to detect the switch-off point.

Saturation

Two: one to detect when the upper limit is reached or left, and one to detect when the lower limit is reached or left.

Second-Order Integrator

Five: two to detect when the state x upper or lower limit is reached; two to detect when the state dx/dt upper or lower limit is reached; and one to detect when a state leaves saturation.

Sign

One: to detect when the input crosses through zero.

Signal Builder

One: to detect when the input signal has a discontinuity in either the rising or falling direction

Step

One: to detect the step time.

Switch

One: to detect when the switch condition occurs.

Switch Case

One: to detect when the case condition is met.

Trigger

One: If a Triggered port is inside of a Subsystem block, it provides the capability to detect zero crossings. See the Triggered Subsystem block for details: Using Triggered Subsystems.

Enabled and Triggered Subsystem

Two: one for the enable port and one for the trigger port. See the Enabled and Triggered Subsystem block for details: Using Enabled and Triggered Subsystems

Note

Zero-crossing detection is also available for a Stateflow® chart that uses continuous-time mode. See Configure a Stateflow Chart for Continuous-Time Simulation (Stateflow) for more information.

Implementation Example: Saturation Block

An example of a Simulink block that registers zero crossings is the Saturation block. Zero-crossing detection identifies these state events in the Saturation block:

  • The input signal reaches the upper limit.

  • The input signal leaves the upper limit.

  • The input signal reaches the lower limit.

  • The input signal leaves the lower limit.

Simulink blocks that define their own state events are considered to have intrinsic zero crossings. Use the Hit Crossing block to receive explicit notification of a zero-crossing event. See Blocks That Register Zero Crossings for a list of blocks that incorporate zero crossings.

The detection of a state event depends on the construction of an internal zero-crossing signal. This signal is not accessible by the block diagram. For the Saturation block, the signal that is used to detect zero crossings for the upper limit is zcSignal = UpperLimit - u, where u is the input signal.

Zero-crossing signals have a direction attribute, which can have these values:

  • rising — A zero crossing occurs when a signal rises to or through zero, or when a signal leaves zero and becomes positive.

  • falling — A zero crossing occurs when a signal falls to or through zero, or when a signal leaves zero and becomes negative.

  • either — A zero crossing occurs if either a rising or falling condition occurs.

For the Saturation block's upper limit, the direction of the zero crossing is either. This enables the entering and leaving saturation events to be detected using the same zero-crossing signal.

See Also

| |