FunctionApproximation.Problem Class
Namespace: FunctionApproximation
Object defining the function to approximate, or the lookup table to optimize
Description
The FunctionApproximation.Problem
object defines the math function,
function handle, cfit
object, or Simulink® block to approximate with a lookup table, or the lookup table block to optimize.
After defining the problem, use the solve
method to
generate a FunctionApproximation.LUTSolution
object that contains the approximation.
Creation
creates a
problem
= FunctionApproximation.Problem()FunctionApproximation.Problem
object with default property values. When
no function
input is provided, the
FunctionToApproximate
property is set to
'sin'
.
creates a problem
= FunctionApproximation.Problem(function
)FunctionApproximation.Problem
object to approximate the math
function, function handle, cfit
object, or Simulink block, or the lookup table block to optimize, specified by
function
.
Input Arguments
function
— Function, object, or block to approximate, or lookup table block to
optimize
'sin'
(default) | math function | function handle | cfit
object | Math Function block | Subsystem block | Lookup Table block
Function, object, or block to approximate, or the lookup table block to optimize,
specified as a math function, function handle, cfit
(Curve Fitting Toolbox) object, Simulink block or subsystem, or one of the lookup table blocks (for example,
1-D Lookup Table, n-D Lookup Table).
If you specify a math function, a function handle, cfit
object,
or a Simulink block, the solve
method generates a lookup table
approximation of the input function or block.
If you specify one of the lookup table blocks, the solve
method generates an optimized lookup table.
The MATLAB® math functions supported for approximation are:
1./x
10.^x
2.^x
acos
acosh
asin
asinh
atan
atan2
atanh
cos
cosh
exp
log
log10
log2
sin
sinh
sqrt
tan
tanh
x.^2
Function handles must be on the MATLAB search path, or approximation fails.
Tip
The process of generating a lookup table approximation is faster for a function handle than for a subsystem. If a subsystem can be represented by a function handle, it is faster to approximate the function handle.
If you specify a cfit
object, use the fittype
(Curve Fitting Toolbox) function to specify a library model to approximate. For a list
of library models, see List of Library Models for Curve and Surface Fitting (Curve Fitting Toolbox).
Data Types: char
| string
| function_handle
Properties
FunctionToApproximate
— Function, object, or block to approximate, or lookup table block to optimize
'sin'
(default) | math function | function handle | cfit
object | Math Function block | Subsystem block | Lookup Table block
Function, object, or block to approximate, or the lookup table block to optimize,
specified as a math function, function handle, cfit
(Curve Fitting Toolbox) object, Simulink block or subsystem, or one of the lookup table blocks (for example, 1-D Lookup Table, n-D Lookup Table).
If you specify a math function, a function handle, cfit
object,
or a Simulink block, the solve
method generates a lookup table
approximation of the input function or block.
If you specify one of the lookup table blocks, the solve
method generates an optimized lookup table.
The MATLAB math functions supported for approximation are:
1./x
10.^x
2.^x
acos
acosh
asin
asinh
atan
atan2
atanh
cos
cosh
exp
log
log10
log2
sin
sinh
sqrt
tan
tanh
x.^2
Function handles must be on the MATLAB search path, or approximation fails.
Tip
The process of generating a lookup table approximation is faster for a function handle than for a subsystem. If a subsystem can be represented by a function handle, it is faster to approximate the function handle.
If you specify a cfit
object, use the fittype
(Curve Fitting Toolbox) function to specify a library model to approximate. For a list of
library models, see List of Library Models for Curve and Surface Fitting (Curve Fitting Toolbox).
Data Types: char
| string
| function_handle
NumberOfInputs
— Number of inputs to function approximation
1 | 2 | 3
Number of inputs to approximated function. This property is inferred from the
FunctionToApproximate
property, therefore it is not a writable
property.
If you are generating a Direct Lookup Table, the function to approximate can have no more than two inputs.
Data Types: double
InputTypes
— Desired data types of inputs to function approximation
numerictype
object | vector of numerictype
objects | Simulink.Numerictype
object | vector of Simulink.Numerictype
objects
Desired data types of the inputs to the approximated function, specified as a
numerictype
,
Simulink.Numerictype
, or a vector of numerictype
or
Simulink.Numerictype
objects. The number of
InputTypes
specified must match the
NumberOfInputs
.
Example: problem.InputTypes = ["numerictype(1,16,13)",
"numerictype(1,16,10)"];
InputLowerBounds
— Lower limit of range of inputs to function to approximate
scalar | vector
Lower limit of range of inputs to function to approximate, specified as a scalar or
vector. If you specify inf
, the InputLowerBounds
used during the approximation is derived from the InputTypes
property. The dimensions of InputLowerBounds
must match the
NumberOfInputs
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| fi
InputUpperBounds
— Upper limit of range of inputs to function to approximate
scalar | vector
Upper limit of range of inputs to function to approximate, specified as a scalar or
vector. If you specify inf
, the InputUpperBounds
used during the approximation is derived from the InputTypes
property. The dimensions of InputUpperBounds
must match the
NumberOfInputs
.
Data Types: single
| double
| int8
| int16
| int32
| int64
| uint8
| uint16
| uint32
| uint64
| fi
OutputType
— Desired data type of the function approximation output
numerictype
object | Simulink.Numerictype
object
Desired data type of the function approximation output, specified as a
numerictype
or Simulink.Numerictype
object. For
example, to specify that you want the output to be a signed fixed-point data type with
16-bit word length and best-precision fraction length, set the
OutputType
property to
"numerictype(1,16)"
.
Example: problem.OutputType = "numerictype(1,16)";
Options
— Additional options and constraints to use in approximation
FunctionApproximation.Options
object
Additional options and constraints to use in approximation, specified as a FunctionApproximation.Options
object.
Methods
Public Methods
solve | Solve for optimized solution to function approximation problem |
Examples
Create Problem Object to Approximate a Math Function
Create a FunctionApproximation.Problem
object specifying a math function to approximate.
problem = FunctionApproximation.Problem('log')
problem = 1x1 FunctionApproximation.Problem with properties: FunctionToApproximate: @(x)log(x) NumberOfInputs: 1 InputTypes: "numerictype(1,16,10)" InputLowerBounds: 0.6250 InputUpperBounds: 15.6250 OutputType: "numerictype(1,16,13)" Options: [1x1 FunctionApproximation.Options]
Create Problem Object to Approximate a Function Handle
Create a FunctionApproximation.Problem
object specifying a function handle that you want to approximate.
problem = FunctionApproximation.Problem(@(x,y) sin(x)+cos(y))
problem = 1x1 FunctionApproximation.Problem with properties: FunctionToApproximate: @(x,y)sin(x)+cos(y) NumberOfInputs: 2 InputTypes: ["numerictype('double')" "numerictype('double')"] InputLowerBounds: [-Inf -Inf] InputUpperBounds: [Inf Inf] OutputType: "numerictype('double')" Options: [1x1 FunctionApproximation.Options]
The FunctionApproximation.Problem
object, problem
, uses default property values.
Set the range of the function inputs to be between 0
and 2*pi
.
problem.InputLowerBounds = [0,0]; problem.InputUpperBounds = [2*pi, 2*pi]
problem = 1x1 FunctionApproximation.Problem with properties: FunctionToApproximate: @(x,y)sin(x)+cos(y) NumberOfInputs: 2 InputTypes: ["numerictype('double')" "numerictype('double')"] InputLowerBounds: [0 0] InputUpperBounds: [6.2832 6.2832] OutputType: "numerictype('double')" Options: [1x1 FunctionApproximation.Options]
Create Problem Object to Optimize a Lookup Table Block
Create a FunctionApproximation.Problem
object to
optimize an existing lookup table.
openExample('simulink_automotive/ModelingAFaultTolerantFuelControlSystemExample',... 'supportingfile','sldemo_fuelsys'); problem = FunctionApproximation.Problem('sldemo_fuelsys/fuel_rate_control/airflow_calc/Pumping Constant')
problem = 1×1 FunctionApproximation.Problem with properties: FunctionToApproximate: 'sldemo_fuelsys/fuel_rate_control/airflow_calc/Pumping Constant' NumberOfInputs: 2 InputTypes: ["numerictype('single')" "numerictype('single')"] InputLowerBounds: [50 0.0500] InputUpperBounds: [1000 0.9500] OutputType: "numerictype('single')" Options: [1×1 FunctionApproximation.Options]
The software infers the properties of the problem
object from the
model.
Create Problem Object to Approximate a Curve Fitting Object
Create a FunctionApproximation.Problem
object specifying a cfit
object to approximate.
ffun = fittype('exp1');
cfun = cfit(ffun,0.1,0.2);
problem = FunctionApproximation.Problem(cfun)
problem = 1x1 FunctionApproximation.Problem with properties: FunctionToApproximate: [1x1 cfit] NumberOfInputs: 1 InputTypes: "numerictype('double')" InputLowerBounds: -Inf InputUpperBounds: Inf OutputType: "numerictype('double')" Options: [1x1 FunctionApproximation.Options]
Search for Floating-Point Solution to Function Approximation Problem
Since R2023a
This example shows how to search for pure floating-point solutions to the function approximation problem.
Create a FunctionApproximation.Problem
object specifying a function to approximate.
problem = FunctionApproximation.Problem("sin");
Specify the input and output types to be a floating-point data type.
problem.InputTypes = [numerictype('Single')]; problem.OutputType = [numerictype('Single')];
Use the FunctionApproximation.Options
object to specify word lengths that can be used in the lookup table approximation. To search for floating-point solutions, specify word lengths corresponding to a single-precision or double-precision data type.
problem.Options.WordLengths = 32;
Use the solve
method to generate an approximation of the function.
solve(problem)
Searching for fixed-point solutions. | ID | Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification | Error(Max,Current) | | 0 | 128 | 0 | 2 | 32 | 32 | EvenSpacing | 7.812500e-03, 1.000000e+00 | | 1 | 1568 | 1 | 47 | 32 | 32 | EvenSpacing | 7.812500e-03, 2.331257e-03 | | 2 | 1536 | 1 | 46 | 32 | 32 | EvenSpacing | 7.812500e-03, 2.434479e-03 | | 3 | 1216 | 1 | 36 | 32 | 32 | EvenSpacing | 7.812500e-03, 4.021697e-03 | | 4 | 1184 | 1 | 35 | 32 | 32 | EvenSpacing | 7.812500e-03, 4.265845e-03 | | 5 | 832 | 1 | 24 | 32 | 32 | EvenSpacing | 7.812500e-03, 6.421237e-03 | | 6 | 800 | 1 | 23 | 32 | 32 | EvenSpacing | 7.812500e-03, 7.061585e-03 | | 7 | 448 | 0 | 12 | 32 | 32 | EvenSpacing | 7.812500e-03, 4.009663e-02 | | 8 | 608 | 0 | 17 | 32 | 32 | EvenSpacing | 7.812500e-03, 1.884634e-02 | | 9 | 704 | 0 | 20 | 32 | 32 | EvenSpacing | 7.812500e-03, 8.071933e-03 | | 10 | 736 | 0 | 21 | 32 | 32 | EvenSpacing | 7.812500e-03, 8.607101e-03 | | 11 | 768 | 1 | 22 | 32 | 32 | EvenSpacing | 7.812500e-03, 7.243455e-03 | | 12 | 128 | 0 | 2 | 32 | 32 | EvenPow2Spacing | 7.812500e-03, 1.315148e+00 | | 13 | 1152 | 1 | 18 | 32 | 32 | ExplicitValues | 7.812500e-03, 7.812380e-03 | | 14 | 1024 | 0 | 16 | 32 | 32 | ExplicitValues | 7.812500e-03, 1.202238e-02 | | 15 | 1152 | 0 | 18 | 32 | 32 | ExplicitValues | 7.812500e-03, 1.068657e-02 | | 16 | 1280 | 1 | 20 | 32 | 32 | ExplicitValues | 7.812500e-03, 7.278687e-03 | Searching for floating-point solutions. | 17 | 1536 | 1 | 46 | 32 | 32 | EvenSpacing | 7.812500e-03, 2.434489e-03 | | 18 | 128 | 0 | 2 | 32 | 32 | EvenPow2Spacing | 7.812500e-03, 1.315148e+00 | | 19 | 1152 | 1 | 18 | 32 | 32 | ExplicitValues | 7.812500e-03, 7.812365e-03 | | 20 | 1024 | 0 | 16 | 32 | 32 | ExplicitValues | 7.812500e-03, 1.202232e-02 | Best Solution | ID | Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification | Error(Max,Current) | | 11 | 768 | 1 | 22 | 32 | 32 | EvenSpacing | 7.812500e-03, 7.243455e-03 |
ans = 1x1 FunctionApproximation.LUTSolution with properties: ID: 11 Feasible: "true"
The solve
method returns all feasible solutions. In the table, fixed-point solutions are returned first, followed by floating-point solutions. The Lookup Table Optimizer selects a floating-point solution as the best solution when all of these conditions are met:
The floating-point solution requires equal or less memory than a fixed-point solution.
Both the
InputTypes
andOutputType
properties of theFunctionApproximation.Problem
object specify a floating-point data type.The
WordLengths
property of theFunctionApproximation.Options
object includes word lengths corresponding to a single-precision or double-precision data type.
Limitations
Lookup table objects and breakpoint objects are not supported in a model mask workspace.
Algorithms
Required Specifications
Functions and function handles that you approximate must meet the following criteria.
The function must be time-invariant.
The function must operate element-wise, meaning for each input there is one output.
The function must not contain states.
For more information, see Vectorization.
Infinite Upper and Lower Input Bounds
When a Problem
object specifies infinite input ranges and the input
type is non-floating-point, during the approximation, the software infers upper and lower
ranges based on the range of the input data type. The resulting FunctionApproximation.LUTSolution
object specifies the bounds that the algorithm
used during the approximation, not the originally specified infinite bounds.
Upper and Lower Input Bounds and Input Data Type Range
If the InputLowerBounds
or InputUpperBounds
specified for a Problem
object fall outside the range of the specified
InputTypes
, the algorithm uses the range of the data type specified
by InputTypes
for the approximation.
In cases where the BreakpointSpecification
property of the FunctionApproximation.Options
object is set to 'EvenSpacing'
,
but the InputUpperBounds
or InputLowerBounds
property of the FunctionApproximation.Problem
object is equal to the range of the
InputTypes
, the algorithm does not attempt to find a solution using
'EvenPow2Spacing'
.
Version History
Introduced in R2018aR2022a: Support for curve fitting objects
The FunctionApproximation.Problem
object now supports curve fitting
cfit
(Curve Fitting Toolbox) objects as valid inputs for
approximation.
R2022a: Improved memory reduction for 1D and flat interpolation
The Lookup Table Optimizer has an improved algorithm for lookup table value and breakpoint optimization for one-dimensional functions with flat interpolation. This enhancement can enable improved memory reduction of the optimized lookup table and faster completion of the lookup table optimization process.
This improvement applies when the function to approximate is one-dimensional and all of
these options are specified in FunctionApproximation.Options
:
Interpolation
is set toFlat
.BreakpointSpecification
is set toExplicitValues
.OnCurveTableValues
is set tofalse
.
R2021b: Generate an optimized lookup table approximation as a MATLAB function
You can now use the FunctionApproximation.Problem
object to generate an
optimized lookup table approximation as a MATLAB function. To generate MATLAB function, in a FunctionApproximation.Options
object, set the
ApproximateSolutionType
property to MATLAB
.
The generated MATLAB function is editable and supports C/C++ code generation using MATLAB Coder™.
R2021a: Lookup table optimization support for functions with scalar inputs
Previously, the FunctionApproximation.Problem
class required that
functions and function handles to approximate were vectorized, meaning that for each input,
there is exactly one output. Lookup table optimization now fully supports approximation of
Simulink blocks and subsystems that only allow scalar inputs.
R2021a: Improved lookup table value optimization
The Lookup Table Optimizer has an improved algorithm for lookup
table value optimization for the Flat
and Nearest
interpolation methods when off-curve table values are allowed. This enhancement can enable
faster completion of the lookup table optimization process and improved memory reduction of
the optimized lookup table.
See Also
Apps
Classes
FunctionApproximation.Options
|FunctionApproximation.LUTSolution
|FunctionApproximation.LUTMemoryUsageCalculator
Functions
solve
|approximate
|compare
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.
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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)