codegen
Generate MATLAB code for tunable gain surfaces
Syntax
Description
Examples
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 GS
. 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
Tunable gain surface, specified as a tunableSurface
object.
Output Arguments
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 Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
Website auswählen
Wählen Sie eine Website aus, um übersetzte Inhalte (sofern verfügbar) sowie lokale Veranstaltungen und Angebote anzuzeigen. Auf der Grundlage Ihres Standorts empfehlen wir Ihnen die folgende Auswahl: .
Sie können auch eine Website aus der folgenden Liste auswählen:
So erhalten Sie die bestmögliche Leistung auf der Website
Wählen Sie für die bestmögliche Website-Leistung die Website für China (auf Chinesisch oder Englisch). Andere landesspezifische Websites von MathWorks sind für Besuche von Ihrem Standort aus nicht optimiert.
Amerika
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)