Documentation

# codegen

Generate MATLAB code for tunable gain surfaces

## Syntax

``code = codegen(GS)``

## Description

example

````code = codegen(GS)` generates MATLAB® code for the tunable surface `GS`. The generated code is a function that takes the scalar values of the scheduling variables and returns the scalar-valued or matrix-valued gain, depending on `GS`. ```

## Examples

collapse all

Create a tunable surface that represents a scalar gain with a bilinear dependence on two scheduling variables. Suppose that the scheduling variables are `alpha`, ranging from 0-15 degrees, and `V`, ranging from 300-600 m/s. The tunable surface covers a linearly spaced grid in this operating range.

```[alpha,V] = ndgrid(0:3:15,300:50:600); domain = struct('alpha',alpha,'V',V); shapefcn = @(x,y) [x,y,x*y]; GS0 = tunableSurface('K',1,domain,shapefcn);```

Usually, you use `GS0` to parameterize a scheduled gain and tune the surface coefficients with systune. For this example, instead of tuning, manually set the coefficients to non-zero values.

`GS = setData(GS0,[100,28,40,10]);`

Generate MATLAB code that computes the scalar gain as a function of scheduling variables.

`code = codegen(GS)`
```code = 'function Gain_ = fcn(alpha_,V_) %#codegen % Type casting ZERO = zeros(1,1,'like',alpha_+V_); alpha_ = cast(alpha_,'like',ZERO); V_ = cast(V_,'like',ZERO); % Tuned gain surface coefficients Coeffs = cast([100 28 40 10],'like',ZERO); Offsets = cast([7.5 450],'like',ZERO); Scalings = cast([7.5 150],'like',ZERO); % Normalization alpha_ = (alpha_ - Offsets(1))/Scalings(1); V_ = (V_ - Offsets(2))/Scalings(2); % Compute weighted sum of terms Y = [ alpha_ , V_ , alpha_*V_ ]; Gain_ = Coeffs(1); for i=1:numel(Y) Gain_ = Gain_ + Coeffs(i+1) * Y(i); end ' ```

The resulting code is a function, `fcn`, that takes two scheduling variables and returns a scalar gain. The function includes the `%#codegen `directive, so that it can be used for further code generation, such as implementing a tuned gain schedule in hardware.

The function includes four sections. The first section ensures that the scheduling variables are cast to the same type. The second section encodes the gain coefficients and the offsets and scalings that the software extracts from `GS`. These values are hard-coded into `fcn`, which can compute the gain surface without reference to G`S`. The third section uses these values to compute the normalized scheduling variables. (See `tunableSurface` for more information about normalization.)

The last section computes the gain by summing up all the terms in the polynomial expression for the gain surface.

## Input Arguments

collapse all

Tunable gain surface, specified as a `tunableSurface` object.

## Output Arguments

collapse all

Generated code for the gain surface, returned as a character array. The code contains a function, `Gain_ = fcn(x1_,x2_,...,xN_)`, that computes the gain from the scheduling variables `x1_,x2_,...,xN_` of `GS`. The expression relating the gain to the scheduling variables, the coefficients of the expression, and the normalization of the scheduling variables are all taken from `GS`, and the code can run without further reference to `TS`. The returned function includes the `%#codegen` directive so that it can be used for further code generation, such as implementing a tuned gain schedule in hardware.

When you use `writeBlockValue` to write tuned gain-surface coefficients from an `slTuner` interface to a MATLAB Function block, it uses this generated MATLAB code.

## See Also

### Topics

#### Learn how to automatically tune PID controller gains

Download code examples