FunctionApproximation.Problem Class
R2026bNamespace: 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, sfit 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. You can specify multiple functions to approximate as a cell
array: {function1, function2, ..., functionN}. When you specify multiple
functions, they must share the same input signal.
Input Arguments
The function, object, or block to approximate, or the lookup table block to
optimize, specified as a math function, function handle, cfit (Curve Fitting Toolbox) or sfit (Curve Fitting Toolbox) object, Simulink block or subsystem, lookup table block (for example, 1-D Lookup Table, n-D Lookup Table), or a cell array
specifying two or more of these input function types. If you specify multiple inputs
in a cell array, all of the input functions must share the same input signal.
This argument sets the FunctionToApproximate property. See
FunctionToApproximate for supported
types, behavior, and additional details.
Data Types: char | string | function_handle
Properties
Function, object, or block to approximate, or the lookup table block to optimize,
specified as a math function, function handle, cfit (Curve Fitting Toolbox) or sfit (Curve Fitting Toolbox) object, Simulink block or subsystem, lookup table block (for example, 1-D Lookup Table, n-D Lookup Table), or a cell array
specifying two or more of these input function types. If you specify multiple functions
in a cell array, all of the input functions must share the same input signal.
The solve method generates one lookup table for each input
function. If you specify a math function, a function handle, cfit or
sfit object, or a Simulink block, the solve method generates a lookup table
approximation of the input function or block. If you specify a lookup table block, the
solve
method generates an optimized lookup table.
The MATLAB® math functions supported for approximation are:
1./x10.^x2.^xacosacoshasinasinhatanatan2atanhcoscoshexploglog10log2sinsinhsqrttantanhx.^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 or sfit 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
Number of inputs to the approximated function or functions. 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
Desired data types of the inputs to the approximated function or functions,
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)"];
Lower limit of range of inputs to the function or functions 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
Upper limit of range of inputs to the function or functions 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
Desired data types of the function approximation outputs, specified as a
numerictype, Simulink.Numerictype, or a vector of
numerictype or Simulink.Numerictype objects.
For problem objects that approximate multiple functions, provide the output data types as an ordered array. Each entry in the array corresponds to the function at the same position.
Example: problem.OutputType = "numerictype(1,16)";
Example:
problem.OutputType = ["numerictype(1,16,13)",
"numerictype(1,16,10)"];
Additional options and constraints to use in approximation, specified as a FunctionApproximation.Options object.
Methods
solve | Solve for optimized solution to function approximation problem |
Examples
Create a FunctionApproximation.Problem object specifying a math function to approximate.
problem = FunctionApproximation.Problem('log')problem =
1×1 FunctionApproximation.Problem with properties:
FunctionToApproximate: @(x)log(x)
NumberOfInputs: 1
NumberOfOutputs: 1
InputTypes: "numerictype(1,16,10)"
InputLowerBounds: 0.6250
InputUpperBounds: 15.6250
OutputType: "numerictype(1,16,13)"
Options: [1×1 FunctionApproximation.Options]
Create a FunctionApproximation.Problem object to
optimize multiple functions.
funcs = {@(x) sin(x), @(x) exp(-x)};
multiFunctionProblem = FunctionApproximation.Problem(funcs)multiFunctionProblem =
1×1 FunctionApproximation.Problem with properties:
FunctionToApproximate: {[@(x)sin(x)] [@(x)exp(-x)]}
NumberOfInputs: 1
NumberOfOutputs: 2
InputTypes: "numerictype(0,16,13)"
InputLowerBounds: 0
InputUpperBounds: 6.2832
OutputType: ["numerictype(1,16,14)" "numerictype(1,16,14)"]
Options: [1×1 FunctionApproximation.Options]
Create a FunctionApproximation.Problem object specifying a function handle that you want to approximate.
problem = FunctionApproximation.Problem(@(x,y) sin(x)+cos(y))
problem =
1×1 FunctionApproximation.Problem with properties:
FunctionToApproximate: @(x,y)sin(x)+cos(y)
NumberOfInputs: 2
NumberOfOutputs: 1
InputTypes: ["numerictype('double')" "numerictype('double')"]
InputLowerBounds: [-Inf -Inf]
InputUpperBounds: [Inf Inf]
OutputType: "numerictype('double')"
Options: [1×1 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 =
1×1 FunctionApproximation.Problem with properties:
FunctionToApproximate: @(x,y)sin(x)+cos(y)
NumberOfInputs: 2
NumberOfOutputs: 1
InputTypes: ["numerictype('double')" "numerictype('double')"]
InputLowerBounds: [0 0]
InputUpperBounds: [6.2832 6.2832]
OutputType: "numerictype('double')"
Options: [1×1 FunctionApproximation.Options]
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 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 =
1×1 FunctionApproximation.Problem with properties:
FunctionToApproximate: [1×1 cfit]
NumberOfInputs: 1
NumberOfOutputs: 1
InputTypes: "numerictype('double')"
InputLowerBounds: -Inf
InputUpperBounds: Inf
OutputType: "numerictype('double')"
Options: [1×1 FunctionApproximation.Options]
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.071929e-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.309205e-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 =
1×1 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
InputTypesandOutputTypeproperties of theFunctionApproximation.Problemobject specify a floating-point data type.The
WordLengthsproperty of theFunctionApproximation.Optionsobject 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
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.
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.
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 R2018aYou can now specify multiple functions to approximate in a
FunctionApproximation.Problem object. To create a multi-function lookup
table optimization problem, all functions must share the same input signal. The function
approximations will share breakpoints and prelookup tasks.
The FunctionApproximation.Problem object now supports curve fitting
cfit (Curve Fitting Toolbox) objects as valid inputs for
approximation.
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:
Interpolationis set toFlat.BreakpointSpecificationis set toExplicitValues.OnCurveTableValuesis set tofalse.
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™.
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.
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)