# codegen

Generate MATLAB code for tunable gain surfaces

## Syntax

## Description

## Examples

### Generate Code from Tunable Surface

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

`GS`

— Tunable gain surface

`tunableSurface`

object

Tunable gain surface, specified as a `tunableSurface`

object.

## Output Arguments

`code`

— Generated code for gain surface

character array

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.

## Version History

**Introduced in R2017b**

## MATLAB 명령

다음 MATLAB 명령에 해당하는 링크를 클릭했습니다.

명령을 실행하려면 MATLAB 명령 창에 입력하십시오. 웹 브라우저는 MATLAB 명령을 지원하지 않습니다.

Select a Web Site

Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .

You can also select a web site from the following list:

## How to Get Best Site Performance

Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.

### Americas

- América Latina (Español)
- Canada (English)
- United States (English)

### Europe

- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)

- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)