主要内容

Simulink.Solver.FixedStepSolver Class

R2026b

Namespace: Simulink.Solver

Write fixed-step plugin solver for Simulink simulation

Since R2026b

Description

Simulink.Solver.FixedStepSolver is the base class for authoring fixed-step plugin solvers. Simulink® provides several built-in solvers that can simulate a wide range of systems. However, the built-in solver integration algorithms are not necessarily ideal for all domains and problems. Using the plugin solver interface, you can implement custom solver integration algorithms for Simulink simulations.

At each time step in the simulation, a fixed-step solver:

  • Integrates continuous states by solving an initial value problem (IVP).

    In the first time step, the initial state defines the initial value for the IVP. The solver computes the state at the next time step, which becomes the initial value in the next time step.

  • Advances time using a fixed step size.

    The Fixed-step size (fundamental sample time) configuration parameter of the simulated system defines the step size.

To write a plugin solver, you define only the integration algorithm in the step method. The Simulink environment provides solver services, such as zero-crossing detection. For more information about Simulink solvers, see Choose a Solver.

To simulate a system using a plugin solver:

  1. Save the class definition for the plugin solver in a location on the MATLAB® path or add the plugin solver location to the path.

  2. Register the plugin solver using the Simulink.Solver.register function.

  3. Specify the Solver parameter as the name of the plugin solver.

To implement a variable-step plugin solver, use the Simulink.Solver.VariableStepSolver class.

The Simulink.Solver.FixedStepSolver class is a handle class.

Class Attributes

Abstract
true

For information on class attributes, see Class Attributes.

Properties

expand all

Number of continuous states in simulated system, returned as a positive scalar integer. This property is available only during simulation.

The number of states in the simulated system determines the size of the state and state derivative input and output arguments for the step, forcingFunction, Jacobian, massMatrix, interpolateState, and reset methods.

Attributes:

GetAccess
public
SetAccess
private
Dependent
true

Data Types: double

Methods

expand all

Examples

collapse all

Write a class that defines a fixed-step backward Euler plugin solver. Then, register the plugin solver and simulate a model.

The class BackwardEuler defines the solver properties and integration algorithm of a fixed-step backward Euler solver. The property NewtonIterations defined in the class stores the number of Newton's iterations the integration algorithm performs. The getProperties method creates and returns a structure that specifies these solver properties:

  • DAE — The solver supports solving systems of differential-algebraic equations.

  • MassMatrix — The integration algorithm is implicit and uses the mass matrix of the simulated system.

  • Jacobian — The integration algorithm uses the Jacobian.

The integration algorithm defined in the step method:

  • Advances time by computing t1, the time of the next time step

  • Computes the Jacobian and mass matrix for the current time step by calling the Jacobian and massMatrix methods

  • Iteratively computes the state values in the next time step x1

  • Recomputes the mass matrix in each iteration if the mass matrix depends on the state values

classdef BackwardEuler < Simulink.Solver.FixedStepSolver
    properties
        NewtonIterations = 3;
    end

    methods (Static)
        function props = getProperties
            props = struct("MassMatrix",true,"DAE",true,"Jacobian",true);
        end
    end
    
    methods
        function x1 = step(slvr,t0,x0,h)
            t1 = t0 + h;
            J = slvr.Jacobian(t0,x0);
            M = slvr.massMatrix(t0,x0);
            W = M - h * J;

            x1 = x0;
            z = zeros(size(x0));

            for n = 1:slvr.NewtonIterations
                if slvr.isStateDependentMassMatrix && n > 1
                    M = slvr.massMatrix(t1,x1);
                end
                g = h * slvr.forcingFunction(t1,x1) - M * z;
                dg = W \ g;
                x1 = x1 + dg;
                z = z + dg;
            end
        end
    end
end

To simulate a system using the plugin solver, register the solver. Then, specify the SolverType and Solver configuration parameters in the system. For example, this code registers the plugin solver ODE12 and then uses the solver to simulate the system MyModel.

mdl = "MyModel";
Simulink.Solver.register("BackwardEuler")
set_param(mdl,SolverType="Fixed-step")
set_param(mdl,Solver="BackwardEuler")
out = sim(mdl);

Limitations

Plugin solvers do not support:

  • Rapid accelerator simulation

  • Software-in-the-loop (SIL) and processor-in-the-loop (PIL) simulation

  • Deployment with Simulink Compiler™

  • Production code generation using Simulink Coder™ or Embedded Coder®

Version History

Introduced in R2026b