Implement Algorithm with Tunable Parameters

Introduction

This example shows how to implement an algorithm with tunable parameters by using a MATLAB System block.

System objects

System objects allow you to implement algorithms using MATLAB. System objects are a specialized kind of MATLAB object, designed specifically for implementing and simulating dynamic systems with inputs that change over time.

After you define a System object, you can include it in a Simulink model using a MATLAB System block.

Model Description

The MATLAB System block implements the System object TunableNontunableProperties that multiples input by a Gain parameter and adds a Bias parameter to the input. The input to MATLAB System block is provided by a Constant block and the Display block shows the result of applying the gain and bias. The Gain parameter is tunable and can be changed when the simulation is running. The Bias parameter is nontunable and cannot be changed during the simulation. When the model is running, you can change tunable properties, but you cannot change nontunable properties.

System Object Class Definition

You can access MATLAB source code used by the MATLAB System block by clicking the "Source Code" hyperlink from the block dialog. The System object TunableNontunableProperties implements the setupImpl, stepImpl and processTunedPropertiesImpl methods. The System object has two public properties: Gain and Bias. The Bias property has the attribute Nontunable, which makes this property read-only during simulation. The Gain property has no attributes, so by default, it is tunable and can be modified during the simulation.

The System object has a private property called pGain. pGain stores the value of the public Gain property after the range of Gain is restricted between 1 and 2. You can initialize pGain by copying the value of Gain in the setupImpl method. In this example, pGain is used in the stepImpl method to compute the output. In a System object, whenever public tunable properties are changed, processTunedPropertiesImpl is called. In this example, processTunedPropertiesImpl updates the private property pGain based on the value from the public Gain property.

classdef TunableNontunableProperties < matlab.System
% TunableNontunableProperties Multiply input by Gain and add Bias
    
    properties
        Gain = 1.5
    end
    
    properties(Nontunable)
        Bias = 0.1
    end
    
    properties(Access = private)
        pGain
    end

    methods(Access = protected)
        function setupImpl(obj, ~)
            % Copy public property value Gain to private property pGain and
            % restrict its range between 1 and 2
            obj.pGain = obj.Gain;
            if obj.pGain < 1
                obj.pGain = 1;
            elseif obj.pGain > 2
                obj.pGain = 2;
            end
        end
        
        function y = stepImpl(obj, u)
            y = u * obj.pGain + obj.Bias;
        end

        function processTunedPropertiesImpl(obj)
            % Update private property pGain from the public Gain property
            % and restrict its range between 1 and 2.
            obj.pGain = obj.Gain;
            if obj.pGain < 1
                obj.pGain = 1;
            elseif obj.pGain > 2
                obj.pGain = 2;
            end
        end
    end
end

Related Topics