Optimize Lookup Tables for Memory-Efficiency Programmatically

The following examples show how to generate memory-efficient lookup tables programmatically. Using the command-line equivalent of the Lookup Table Optimizer, you can:

  • Optimize an existing Lookup Table block.

  • Generate a lookup table from a Math Function block.

  • Generate a lookup table from a function or function handle.

  • Generate a lookup table from a Subsystem block.

Approximate a Function Using a Lookup Table

This example shows how to generate a memory-efficient lookup table that approximates the sin function. Define the approximation problem by creating a FunctionApproximation.Problem object.

P = FunctionApproximation.Problem('sin')
P = 

  1x1 FunctionApproximation.Problem with properties:

    FunctionToApproximate: @(x)sin(x)
           NumberOfInputs: 1
               InputTypes: "numerictype(0,16,13)"
         InputLowerBounds: 0
         InputUpperBounds: 6.2832
               OutputType: "numerictype(1,16,14)"
                  Options: [1x1 FunctionApproximation.Options]

The FunctionToApproximate and NumberOfInputs properties of the Problem object are inferred from the definition of the object, and cannot be edited after creation. All other properties are writable.

Edit the FunctionApproximation.Options object to specify additional constraints to use in the optimization process. For example, constrain the breakpoints of the generated lookup table to even spacing.

P.Options.BreakpointSpecification = 'EvenSpacing'
P = 

  1x1 FunctionApproximation.Problem with properties:

    FunctionToApproximate: @(x)sin(x)
           NumberOfInputs: 1
               InputTypes: "numerictype(0,16,13)"
         InputLowerBounds: 0
         InputUpperBounds: 6.2832
               OutputType: "numerictype(1,16,14)"
                  Options: [1x1 FunctionApproximation.Options]

Specify additional constraints, such as the absolute and relative tolerances of the output, and word length constraints.

P.Options.AbsTol = 2^-10;
P.Options.RelTol = 2^-6;
P.Options.WordLengths = [8,16];

Use the solve method to solve the optimization problem. MATLAB™ displays the iterations of the optimization process. The solve method returns a FunctionApproximation.LUTSolution object.

S = solve(P)
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) | 
|   0 |             48 |        0 |          2 |               8 |           16 |             EvenSpacing |     9.765625e-04, 1.007261e+00 |
|   1 |             32 |        0 |          2 |               8 |            8 |             EvenSpacing |     9.765625e-04, 1.007261e+00 |
|   2 |            672 |        0 |         41 |               8 |           16 |             EvenSpacing |     9.765625e-04, 3.322094e-02 |
|   3 |           1648 |        1 |        102 |               8 |           16 |             EvenSpacing |     9.765625e-04, 7.988139e-05 |
|   4 |            480 |        0 |         29 |               8 |           16 |             EvenSpacing |     9.765625e-04, 1.575496e-01 |
|   5 |           1104 |        0 |         68 |               8 |           16 |             EvenSpacing |     9.765625e-04, 1.970943e-03 |
|   6 |            352 |        0 |         21 |               8 |           16 |             EvenSpacing |     9.765625e-04, 3.322094e-02 |
|   7 |            320 |        0 |         19 |               8 |           16 |             EvenSpacing |     9.765625e-04, 9.553784e-02 |
|   8 |             64 |        0 |          2 |              16 |           16 |             EvenSpacing |     9.765625e-04, 9.853516e-01 |
|   9 |             48 |        0 |          2 |              16 |            8 |             EvenSpacing |     9.765625e-04, 9.853516e-01 |
|  10 |            640 |        1 |         38 |              16 |           16 |             EvenSpacing |     9.765625e-04, 6.281694e-04 |
|  11 |            624 |        0 |         37 |              16 |           16 |             EvenSpacing |     9.765625e-04, 3.435787e-03 |
|  12 |            496 |        0 |         29 |              16 |           16 |             EvenSpacing |     9.765625e-04, 9.943803e-04 |
|  13 |            480 |        0 |         28 |              16 |           16 |             EvenSpacing |     9.765625e-04, 1.238521e-03 |
|  14 |            560 |        0 |         33 |              16 |           16 |             EvenSpacing |     9.765625e-04, 1.970943e-03 |
|  15 |            592 |        0 |         35 |              16 |           16 |             EvenSpacing |     9.765625e-04, 3.679927e-03 |
|  16 |            608 |        0 |         36 |              16 |           16 |             EvenSpacing |     9.765625e-04, 2.703365e-03 |
|  17 |            352 |        1 |         20 |              16 |           16 |             EvenSpacing |     9.765625e-04, 6.281694e-04 |
|  18 |            336 |        0 |         19 |              16 |           16 |             EvenSpacing |     9.765625e-04, 1.325779e-03 |
|  19 |             48 |        0 |          2 |               8 |           16 |         EvenPow2Spacing |     9.765625e-04, 1.300802e+00 |
|  20 |             64 |        0 |          2 |              16 |           16 |         EvenPow2Spacing |     9.765625e-04, 1.300802e+00 |
|  21 |           1648 |        1 |        102 |               8 |           16 |         EvenPow2Spacing |     9.765625e-04, 7.988139e-05 |

Best Solution
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) |
|  17 |            352 |        1 |         20 |              16 |           16 |             EvenSpacing |     9.765625e-04, 6.281694e-04 |


S = 

  1x1 FunctionApproximation.LUTSolution with properties:

          ID: 17
    Feasible: "true"

Compare the numerical behavior of the original function with the numerical behavior of the generated lookup table stored in the solution, S.

err = compare(S)
err = 

  struct with fields:

    Breakpoints: [51473x1 double]
       Original: [51473x1 double]
    Approximate: [51473x1 double]

You can access the lookup table data stored in the LUTSolution object.

t = S.TableData
t = 

  struct with fields:

       BreakpointValues: {[1x20 double]}
    BreakpointDataTypes: [1x1 embedded.numerictype]
            TableValues: [1x20 double]
          TableDataType: [1x1 embedded.numerictype]
          IsEvenSpacing: 1
          Interpolation: Linear

To access the generated Lookup Table block, use the approximate method.

S.approximate

Optimize an Existing Lookup Table

This example shows how to optimize an existing Lookup Table block for memory efficiency. Open the model containing the Lookup Table block that you want to optimize.

load_system('sldemo_fuelsys');
open_system('sldemo_fuelsys/fuel_rate_control/airflow_calc');

Create a FunctionApproximation.Problem object to define the optimization problem and constraints.

P = FunctionApproximation.Problem('sldemo_fuelsys/fuel_rate_control/airflow_calc/Pumping Constant')
P = 

  1x1 FunctionApproximation.Problem with properties:

    FunctionToApproximate: 'sldemo_fuelsys/fuel_rate_control/airflow_calc/Pumping Constant'
           NumberOfInputs: 2
               InputTypes: [1x2 string]
         InputLowerBounds: [50 0.0500]
         InputUpperBounds: [1000 0.9500]
               OutputType: "numerictype('single')"
                  Options: [1x1 FunctionApproximation.Options]

Specify additional constraints by modifying the Options object associated with the Problem object, P.

P.Options.BreakpointSpecification = "EvenSpacing"
P = 

  1x1 FunctionApproximation.Problem with properties:

    FunctionToApproximate: 'sldemo_fuelsys/fuel_rate_control/airflow_calc/Pumping Constant'
           NumberOfInputs: 2
               InputTypes: [1x2 string]
         InputLowerBounds: [50 0.0500]
         InputUpperBounds: [1000 0.9500]
               OutputType: "numerictype('single')"
                  Options: [1x1 FunctionApproximation.Options]

Solve the optimization problem.

S = solve(P)
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) | 
|   0 |          12128 |        1 |    [18 19] |         [32 32] |           32 |          ExplicitValues |     7.812500e-03, 0.000000e+00 |
|   1 |          11840 |        1 |    [18 19] |         [16 32] |           32 |          ExplicitValues |     7.812500e-03, 0.000000e+00 |
|   2 |             80 |        0 |      [2 2] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 1.047640e-01 |
|   3 |           3088 |        0 |    [19 20] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.296377e-02 |
|   4 |           2928 |        0 |    [18 20] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.404382e-02 |
|   5 |          11592 |        0 |    [37 39] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 2.700279e-02 |
|   6 |          11280 |        0 |    [36 39] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 2.700279e-02 |
|   7 |           1848 |        0 |    [15 15] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.396479e-02 |
|   8 |           1728 |        0 |    [14 15] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.528193e-02 |
|   9 |           1728 |        0 |    [15 14] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.396479e-02 |
|  10 |           1616 |        0 |    [14 14] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.528193e-02 |
|  11 |           7008 |        0 |    [29 30] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.208317e-02 |
|  12 |           6768 |        0 |    [28 30] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.208317e-02 |
|  13 |           9024 |        0 |    [33 34] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 2.954298e-02 |
|  14 |          10968 |        0 |    [35 39] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 2.700278e-02 |
|  15 |            928 |        0 |    [10 11] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.983925e-02 |
|  16 |            840 |        0 |     [9 11] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 4.118272e-02 |
|  17 |            848 |        0 |    [10 10] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.983925e-02 |
|  18 |            768 |        0 |     [9 10] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 4.118272e-02 |
|  19 |           3392 |        0 |    [19 22] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.296377e-02 |
|  20 |           3216 |        0 |    [18 22] |          [16 8] |            8 |             EvenSpacing |     7.812500e-03, 3.404382e-02 |
|  21 |            128 |        0 |      [2 2] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.049957e-01 |
|  22 |           3136 |        0 |    [19 20] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.326864e-02 |
|  23 |           2976 |        0 |    [18 20] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.458861e-02 |
|  24 |           2984 |        0 |    [19 19] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.326864e-02 |
|  25 |           2832 |        0 |    [18 19] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.458861e-02 |
|  26 |          11640 |        1 |    [37 39] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 2.918795e-03 |
|  27 |          11328 |        1 |    [36 39] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 2.918795e-03 |
|  28 |          11040 |        1 |    [36 38] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 2.047341e-03 |
|  29 |           6368 |        0 |    [28 28] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 9.548876e-03 |
|  30 |           8288 |        1 |    [32 32] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 6.038850e-03 |
|  31 |           7296 |        0 |    [30 30] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 7.898808e-03 |
|  32 |           7784 |        1 |    [31 31] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 7.366333e-03 |
|  33 |           1896 |        0 |    [15 15] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.769516e-02 |
|  34 |           1776 |        0 |    [14 15] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.885424e-02 |
|  35 |           1776 |        0 |    [15 14] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 2.017076e-02 |
|  36 |           1664 |        0 |    [14 14] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 2.088643e-02 |
|  37 |           6824 |        0 |    [29 29] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 8.238737e-03 |
|  38 |           6592 |        0 |    [28 29] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 9.153552e-03 |
|  39 |           6592 |        0 |    [29 28] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 9.313788e-03 |
|  40 |            976 |        0 |    [10 11] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 3.163798e-02 |
|  41 |            888 |        0 |     [9 11] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 3.296046e-02 |
|  42 |            896 |        0 |    [10 10] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 3.414129e-02 |
|  43 |            816 |        0 |     [9 10] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 3.545698e-02 |
|  44 |           3288 |        0 |    [19 21] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.326864e-02 |
|  45 |           3120 |        0 |    [18 21] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.458861e-02 |
|  46 |           5096 |        0 |    [25 25] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.065261e-02 |
|  47 |           2144 |        0 |    [16 16] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.675013e-02 |
|  48 |           2656 |        0 |    [16 20] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.675013e-02 |
|  49 |           3168 |        0 |    [16 24] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.675013e-02 |
|  50 |           3680 |        0 |    [16 28] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.675013e-02 |
|  51 |           4064 |        0 |    [16 31] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.675013e-02 |
|  52 |           2656 |        0 |    [20 16] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.218531e-02 |
|  53 |           3296 |        0 |    [20 20] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.206348e-02 |
|  54 |           3936 |        0 |    [20 24] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.206348e-02 |
|  55 |           4576 |        0 |    [20 28] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.304035e-02 |
|  56 |           5056 |        0 |    [20 31] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.206348e-02 |
|  57 |           3168 |        0 |    [24 16] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.113475e-02 |
|  58 |           3936 |        0 |    [24 20] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 9.480579e-03 |
|  59 |           4704 |        0 |    [24 24] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.130392e-02 |
|  60 |           5472 |        0 |    [24 28] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 1.113148e-02 |
|  61 |           6048 |        0 |    [24 31] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 9.743482e-03 |
|  62 |           3680 |        0 |    [28 16] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 9.836901e-03 |
|  63 |           4576 |        1 |    [28 20] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 6.340916e-03 |
|  64 |           4064 |        0 |    [31 16] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 8.757599e-03 |
|  65 |            112 |        0 |      [2 2] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 1.054412e-01 |
|  66 |           3648 |        0 |    [15 15] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 3.462848e-02 |
|  67 |           3408 |        0 |    [14 15] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 3.596578e-02 |
|  68 |           3408 |        0 |    [15 14] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 3.462848e-02 |
|  69 |           3184 |        0 |    [14 14] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 3.596578e-02 |
|  70 |           4144 |        0 |    [16 16] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 3.329117e-02 |
|  71 |           1808 |        0 |    [10 11] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 4.057692e-02 |
|  72 |           1632 |        0 |     [9 11] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 4.191638e-02 |
|  73 |           1648 |        0 |    [10 10] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 4.057692e-02 |
|  74 |           1488 |        0 |     [9 10] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 4.191638e-02 |
|  75 |           2752 |        0 |    [13 13] |          [16 8] |           16 |             EvenSpacing |     7.812500e-03, 3.730308e-02 |
|  76 |            160 |        0 |      [2 2] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 1.056905e-01 |
|  77 |           3696 |        0 |    [15 15] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 1.852884e-02 |
|  78 |           3456 |        0 |    [14 15] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 1.976016e-02 |
|  79 |           3456 |        0 |    [15 14] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 2.026647e-02 |
|  80 |           3232 |        0 |    [14 14] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 2.101223e-02 |
|  81 |           4192 |        0 |    [16 16] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 1.729794e-02 |
|  82 |           1856 |        0 |    [10 11] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 3.189704e-02 |
|  83 |           1680 |        0 |     [9 11] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 3.319502e-02 |
|  84 |           1696 |        0 |    [10 10] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 3.423219e-02 |
|  85 |           1536 |        0 |     [9 10] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 3.554761e-02 |
|  86 |           2800 |        0 |    [13 13] |         [16 32] |           16 |             EvenSpacing |     7.812500e-03, 2.411811e-02 |
|  87 |             80 |        0 |      [2 2] |          [16 8] |            8 |         EvenPow2Spacing |     7.812500e-03, 8.761361e-02 |
|  88 |           1848 |        0 |    [15 15] |          [16 8] |            8 |         EvenPow2Spacing |     7.812500e-03, 3.133052e-02 |
|  89 |            128 |        0 |      [2 2] |         [16 32] |            8 |         EvenPow2Spacing |     7.812500e-03, 8.876663e-02 |
|  90 |           1896 |        0 |    [15 15] |         [16 32] |            8 |         EvenPow2Spacing |     7.812500e-03, 1.705435e-02 |
|  91 |            112 |        0 |      [2 2] |          [16 8] |           16 |         EvenPow2Spacing |     7.812500e-03, 8.819825e-02 |
|  92 |           3648 |        0 |    [15 15] |          [16 8] |           16 |         EvenPow2Spacing |     7.812500e-03, 3.195386e-02 |
|  93 |            160 |        0 |      [2 2] |         [16 32] |           16 |         EvenPow2Spacing |     7.812500e-03, 8.949983e-02 |
|  94 |           3696 |        0 |    [15 15] |         [16 32] |           16 |         EvenPow2Spacing |     7.812500e-03, 1.740768e-02 |
|  95 |            176 |        0 |      [2 2] |          [16 8] |           32 |         EvenPow2Spacing |     7.812500e-03, 8.819933e-02 |
|  96 |            224 |        0 |      [2 2] |         [16 32] |           32 |             EvenSpacing |     7.812500e-03, 1.056914e-01 |
|  97 |           3616 |        0 |    [10 11] |         [16 32] |           32 |             EvenSpacing |     7.812500e-03, 3.189653e-02 |
|  98 |           3264 |        0 |     [9 11] |         [16 32] |           32 |             EvenSpacing |     7.812500e-03, 3.319453e-02 |
|  99 |           3296 |        0 |    [10 10] |         [16 32] |           32 |             EvenSpacing |     7.812500e-03, 3.423204e-02 |
| 100 |           2976 |        0 |     [9 10] |         [16 32] |           32 |             EvenSpacing |     7.812500e-03, 3.554735e-02 |
| 101 |           3968 |        0 |    [11 11] |         [16 32] |           32 |             EvenSpacing |     7.812500e-03, 3.070421e-02 |
| 102 |            224 |        0 |      [2 2] |         [16 32] |           32 |         EvenPow2Spacing |     7.812500e-03, 8.950080e-02 |

Best Solution
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) |
|  63 |           4576 |        1 |    [28 20] |         [16 32] |            8 |             EvenSpacing |     7.812500e-03, 6.340916e-03 |


S = 

  1x1 FunctionApproximation.LUTSolution with properties:

          ID: 63
    Feasible: "true"

Compare the numerical behavior of the original lookup table, with the optimized lookup table.

compare(S)
ans = 

  1x2 struct array with fields:

    Breakpoints
    Original
    Approximate

Generate the new Lookup Table block using the approximate method.

S.approximate

Visualize Pareto Front for Memory Optimization Versus Absolute Tolerance

When you want to optimize for both memory and absolute tolerance, it is helpful to visualize the tradeoffs between the two. This example creates a lookup table approximation of the function 1-exp(-x) with varying levels of absolute tolerance and creates a plot of each solution found. In the final plot you can view the tradeoffs between memory efficiency and numeric fidelity.

nTol = 32; % Initialize variables
solutions = cell(1,nTol);
objectiveValues = cell(1,nTol);
constraintValues = cell(1,nTol);
memoryUnits = 'bytes';

% Options for absolute tolerance
absTol = 2.^linspace(-12,-4,nTol);

% Relative tolerance is set to 0
relTol = 0;

% Initialize options
options = FunctionApproximation.Options( ...
    'RelTol', relTol, ...
    'BreakpointSpecification', 'EvenSpacing', ...
    'Display', false, ...
    'WordLengths', 16);

% Setup the approximation problem
problem = FunctionApproximation.Problem( ...
    @(x) 1 - exp(-x), ...
    'InputTypes',numerictype(0,16), ...
    'OutputType',numerictype(1,16,14), ...
    'InputLowerBounds',0, ...
    'InputUpperBounds',5, ...
    'Options',options);

% Execute to find solutions with different tolerances
for iTol = 1:nTol
    problem.Options.AbsTol = absTol(iTol);
    solution = solve(problem);
    objectiveValues{iTol} = arrayfun(@(x) x.totalMemoryUsage(memoryUnits), solution.AllSolutions);
    constraintValues{iTol} = arrayfun(@(x) x.Feasible, solution.AllSolutions);
    solutions{iTol} = solution;
end


% Plot results

h = figure();
hold on;

for iTol = 1:nTol
    for iObjective = 1:numel(objectiveValues{iTol})
        if constraintValues{iTol}(iObjective)
            markerColor = 'g';
        else
            markerColor = 'r';
        end
        plot(absTol(iTol),objectiveValues{iTol}(iObjective), ...
            'Marker', '.' ,'LineStyle', 'none', ...
            'MarkerSize', 24, ...
            'MarkerEdgeColor', markerColor)
    end
end

xlabel('AbsTol')
ylabel(['MemoryUsage (',memoryUnits,')'])
h.Children.XScale = 'log';
h.Children.YMinorGrid = 'on';
grid on
box on
hold off;

Solutions that are infeasible, meaning they do not meet the required absolute tolerance are marked in red. Solutions that are feasible are marked in green. As the absolute tolerance increases, the approximation finds solutions which use less memory. When the absolute tolerance is lower, indicating higher numerical fidelity, the required memory also increases.

Compare Approximations Using On Curve and Off Curve Table Values

This example compares the lookup table approximations generated for the sin function when the OnCurveTableValues property of the FunctionApproximation.Options object is set to true and false. The OnCurveTableValues property specifies whether the table values of the optimized lookup table approximation must be equal to the quantized output of the original function being approximated. In some cases, by setting this value to false, the generated lookup table approximation can maintain the same error tolerances while reducing the memory used by the lookup table.

Create a Lookup Table Approximation Using On Curve Table Values

Use the FunctionApproximation.Problem object to define a function to approximate with a lookup table. By default, the OnCurveTableValues property of the associated Options object is set to true.

P1 = FunctionApproximation.Problem('sin');
P1.Options.OnCurveTableValues
ans = logical
   0

Generate the lookup table approximation.

S1 = solve(P1)
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) | 
|   0 |             64 |        0 |          2 |              16 |           16 |             EvenSpacing |     7.812500e-03, 1.000000e+00 |
|   1 |            784 |        1 |         47 |              16 |           16 |             EvenSpacing |     7.812500e-03, 5.388912e-03 |
|   2 |            768 |        1 |         46 |              16 |           16 |             EvenSpacing |     7.812500e-03, 4.534419e-03 |
|   3 |            608 |        1 |         36 |              16 |           16 |             EvenSpacing |     7.812500e-03, 4.089765e-03 |
|   4 |            592 |        1 |         35 |              16 |           16 |             EvenSpacing |     7.812500e-03, 4.272461e-03 |
|   5 |            416 |        1 |         24 |              16 |           16 |             EvenSpacing |     7.812500e-03, 6.192382e-03 |
|   6 |            400 |        1 |         23 |              16 |           16 |             EvenSpacing |     7.812500e-03, 6.939196e-03 |
|   7 |             64 |        0 |          2 |              16 |           16 |         EvenPow2Spacing |     7.812500e-03, 1.315166e+00 |
|   8 |            576 |        0 |         18 |              16 |           16 |          ExplicitValues |     7.812500e-03, 7.827414e-03 |
|   9 |            640 |        1 |         20 |              16 |           16 |          ExplicitValues |     7.812500e-03, 7.784160e-03 |
|  10 |            576 |        0 |         18 |              16 |           16 |          ExplicitValues |     7.812500e-03, 7.866281e-03 |
|  11 |            576 |        0 |         18 |              16 |           16 |          ExplicitValues |     7.812500e-03, 7.849443e-03 |
|  12 |            640 |        1 |         20 |              16 |           16 |          ExplicitValues |     7.812500e-03, 7.244422e-03 |

Best Solution
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) |
|   6 |            400 |        1 |         23 |              16 |           16 |             EvenSpacing |     7.812500e-03, 6.939196e-03 |
S1 = 
  1x1 FunctionApproximation.LUTSolution with properties:

          ID: 6
    Feasible: "true"

Create a Lookup Table Approximation Using Any Table Values

Create another FunctionApproximation.Problem object. Set the OnCurveTableValues property of this object to false to allow the optimization to optimize the table values as well as the breakpoints.

P2 = FunctionApproximation.Problem('sin');
P2.Options.OnCurveTableValues = 0
P2 = 
  1x1 FunctionApproximation.Problem with properties:

    FunctionToApproximate: @(x)sin(x)
           NumberOfInputs: 1
               InputTypes: "numerictype(0,16,13)"
         InputLowerBounds: 0
         InputUpperBounds: 6.2832
               OutputType: "numerictype(1,16,14)"
                  Options: [1x1 FunctionApproximation.Options]

Generate the lookup table approximation.

S2 = solve(P2)
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) | 
|   0 |             64 |        0 |          2 |              16 |           16 |             EvenSpacing |     7.812500e-03, 1.000000e+00 |
|   1 |            784 |        1 |         47 |              16 |           16 |             EvenSpacing |     7.812500e-03, 5.388912e-03 |
|   2 |            768 |        1 |         46 |              16 |           16 |             EvenSpacing |     7.812500e-03, 4.534419e-03 |
|   3 |            608 |        1 |         36 |              16 |           16 |             EvenSpacing |     7.812500e-03, 4.089765e-03 |
|   4 |            592 |        1 |         35 |              16 |           16 |             EvenSpacing |     7.812500e-03, 4.272461e-03 |
|   5 |            416 |        1 |         24 |              16 |           16 |             EvenSpacing |     7.812500e-03, 6.192382e-03 |
|   6 |            400 |        1 |         23 |              16 |           16 |             EvenSpacing |     7.812500e-03, 6.939196e-03 |
|   7 |             64 |        0 |          2 |              16 |           16 |         EvenPow2Spacing |     7.812500e-03, 1.315166e+00 |
|   8 |            576 |        0 |         18 |              16 |           16 |          ExplicitValues |     7.812500e-03, 7.827414e-03 |
|   9 |            640 |        1 |         20 |              16 |           16 |          ExplicitValues |     7.812500e-03, 7.784160e-03 |
|  10 |            576 |        0 |         18 |              16 |           16 |          ExplicitValues |     7.812500e-03, 7.866281e-03 |
|  11 |            576 |        0 |         18 |              16 |           16 |          ExplicitValues |     7.812500e-03, 7.849443e-03 |
|  12 |            640 |        1 |         20 |              16 |           16 |          ExplicitValues |     7.812500e-03, 7.244422e-03 |

Best Solution
|  ID |  Memory (bits) | Feasible | Table Size | Breakpoints WLs | TableData WL | BreakpointSpecification |             Error(Max,Current) |
|   6 |            400 |        1 |         23 |              16 |           16 |             EvenSpacing |     7.812500e-03, 6.939196e-03 |
S2 = 
  1x1 FunctionApproximation.LUTSolution with properties:

          ID: 6
    Feasible: "true"

View Results

Compare the best solutions for each lookup table approximation.

compare(S1)

ans = struct with fields:
    Breakpoints: [51473x1 double]
       Original: [51473x1 double]
    Approximate: [51473x1 double]

compare(S2)

ans = struct with fields:
    Breakpoints: [51473x1 double]
       Original: [51473x1 double]
    Approximate: [51473x1 double]

The maximum error between the original function and the two lookup table approximations are approximately equal, however the memory used by the lookup table that was not constrained to using only on curve table values is significantly lower.

percent_reduction = S2.totalMemoryUsage/S1.totalMemoryUsage
percent_reduction = 1

See Also

Apps

Classes

Related Topics