Main Content

FunctionApproximation.Problem Class

Namespace: FunctionApproximation

Object defining the function to approximate, or the lookup table to optimize

Description

The FunctionApproximation.Problem object defines the function 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.

Construction

approximationProblem = FunctionApproximation.Problem() creates a FunctionApproximation.Problem object with default property values. When no function input is provided, the FunctionToApproximate property is set to 'sin'.

approximationProblem = FunctionApproximation.Problem(function) creates a FunctionApproximation.Problem object to approximate the function, Math Function block, or lookup table specified by function.

Input Arguments

expand all

Function or block to approximate, or the lookup table block to optimize, specified as a function handle, a math function, a cfit (Curve Fitting Toolbox) object, a Simulink® block or subsystem, or one of the lookup table blocks (for example, 1-D Lookup Table, n-D Lookup Table).

If you specify one of the lookup table blocks, the solve method generates an optimized lookup table.

If you specify a math function, a function handle, cfit object, or a block, the solve method generates a lookup table approximation of the input function.

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).

Function handles must be on the MATLAB® search path, or approximation fails.

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

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.

Data Types: char | function_handle

Properties

expand all

Function or block to approximate, or the lookup table block to optimize, specified as a function handle, a math function, a Simulink block or subsystem, or one of the lookup table blocks (for example, 1-D Lookup Table, n-D Lookup Table).

If you specify one of the lookup table blocks, the solve method generates an optimized lookup table.

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).

If you specify a math function, a function handle, cfit object, or a block, the solve method generates a lookup table approximation of the input function.

Function handles must be on the MATLAB search path, or approximation fails.

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

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.

Data Types: char | function_handle

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

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)"];

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

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

Desired data type of the function approximation output, specified as a numerictype or Simulink.Numerictype. 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)";

Additional options and constraints to use in approximation, specified as a FunctionApproximation.Options object.

Methods

solveSolve for optimized solution to function approximation problem

Copy Semantics

Handle. To learn how handle classes affect copy operations, see Copying Objects.

Examples

collapse all

Create a FunctionApproximation.Problem object, specifying a function handle that you want to approximate.

problem = FunctionApproximation.Problem(@(x,y) sin(x)+cos(y))
problem = 

  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: [1×1 FunctionApproximation.Options]

The FunctionApproximation.Problem object, problem, uses default property values.

Set the range of the function inputs to be between zero and 2*pi.

problem.InputLowerBounds = [0,0];
problem.InputUpperBounds = [2*pi, 2*pi]
problem = 

  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: [1×1 FunctionApproximation.Options]

Create a FunctionApproximation.Problem object, specifying a math function to approximate.

problem = FunctionApproximation.Problem('log')
problem = 

  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: [1×1 FunctionApproximation.Options]

The math functions have appropriate input range, input data type, and output data type property defaults.

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: [1x1 cfit]
           NumberOfInputs: 1
               InputTypes: "numerictype('double')"
         InputLowerBounds: -Inf
         InputUpperBounds: Inf
               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 = 

  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.

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 wordlengths 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.411283e-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.249979e-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.278747e-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.249979e-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 and OutputType properties of the FunctionApproximation.Problem object specify a floating-point data type.

  • The WordLengths property of the FunctionApproximation.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

expand all

Version History

Introduced in R2018a

expand all