Main Content

fitlookupn

Fit N-D lookup table from data

Since R2023b

    Description

    example

    values = fitlookupn(breakPoints, inputData, outputData) fits an N-D lookup table to data specified by breakPoints, inputData, and outputData and returns a lookup table as an N-D array with a default smoothness penalty of one.

    values = fitlookupn(breakPoints, inputData, outputData, smoothnessPenalty) fits an N-D lookup table to data and returns a lookup table as an N-D array with the smoothness penalty specified by smoothnessPenalty.

    example

    values = fitlookupn(___, Name=Value) specifies additional options using one or more name-value arguments. For example, you can specify the bounds on the lookup table.

    Examples

    collapse all

    Fit an N-D lookup table from scattered data using the fitlookupn (Model-Based Calibration Toolbox) function.

    Load Data and Create a 3-D Plot

    Load the example data file dgasData.mat. Use the plot3 function to create a 3-D plot.

    load data.mat GasolineData
    plot3(GasolineData.LOAD,GasolineData.Speed,GasolineData.BSFC,'x')
    grid on
    xlabel('LOAD')
    ylabel('Speed [rpm]')
    zlabel('BSFC [g/Kwh]')

    Define Breakpoints

    Specify load and speed breakpoints in a cell array.

    loadBreakpoints = 0.2:0.1:1.4;
    speedBreakpoints = 500:500:5000;
    breakPoints = { loadBreakpoints , speedBreakpoints };

    Define Smoothness Penalty

    Specify the smoothness penalty, smoothnessPenalty, using the slider.

    Note that the slider is on a log scale, as the smoothness penalty usually requires adjusting by several orders of magnitude.

    smoothnessPenalty = 10.^-2
    smoothnessPenalty = 0.0100
    

    Define Input Data

    Set the load and speed as inputData.

    inputData = [GasolineData.LOAD,GasolineData.Speed];

    Fit Lookup Table

    Fit the lookup table using the fitlookupn function and plot result.

    [tableValues,rmse] = fitlookupn(breakPoints, inputData, ...
        GasolineData.BSFC, smoothnessPenalty, ShowPlot=true);
    grid on
    xlabel('LOAD')
    ylabel('Speed [rpm]')
    zlabel('BSFC [g/Kwh]')

    Calculate Lookup Table Output

    Use the griddedInterpolant function to calculate the output from the lookup table. Calculate the lookup table value for LOAD=0.45 and Speed=2250rpm and show on the plot.

    Note: You can use the interpn function as an alternative to griddedInterpolant.

    rmse
    rmse = 5.9891
    
    %yinterpn = interpn(loadBreakpoints,speedBreakpoints,tableValues,x(1),x(2))
    g = griddedInterpolant(breakPoints,tableValues);
    x = [0.45, 2250];
    y= g(x)
    y = 269.5463
    

    Specify Bounds

    Specify bounds on the table values.

    Note: An Optimization Toolbox™ license is required to fit lookup tables with bounds.

    tableValues = fitlookupn(breakPoints, inputData, ...
        GasolineData.BSFC, 0.1, Bounds = [0 350], ...
        ShowPlot=true);
    grid on
    xlabel('LOAD')
    ylabel('Speed [rpm]')
    zlabel('BSFC [g/Kwh]')

    Set Row-Major Order

    You can use the IsRowMajor option to change the order of the inputs (first input=columns, second input=rows). The resulting lookup table is the transpose of the default value IsRowMajor=false.

    tableValuesRM = fitlookupn(breakPoints, inputData, ...
        GasolineData.BSFC, 0.1, ...
        IsRowMajor=true, ...
        ShowPlot=true);
    grid on
    ylabel('LOAD')
    xlabel('Speed [rpm]')
    zlabel('BSFC [g/Kwh]')

    Use the griddedInterpolant function to calculate the output from the lookup table. Calculate the lookup table value for LOAD=0.45 and Speed=2250rpm and show the results on the plot. You must swap the breakpoints and input order when IsRowMajor=true.

    breakPointsRM = {speedBreakpoints, loadBreakpoints};
    g = griddedInterpolant(breakPointsRM,tableValuesRM);
    xrm = [2250 0.45];
    y= g(xrm)
    y = 273.6243
    
    hold on
    plot3(xrm(1),xrm(2),y,'.r','MarkerSize',20)
    hold off
    view([-20 37])

    When you use the interp2 function, the inputs are already in row-major (meshgrid) order.

    yinterpn = interpn(speedBreakpoints,loadBreakpoints, ...
        tableValuesRM,xrm(1),xrm(2))
    yinterpn = 273.6243
    
    yinterp2 = interp2(loadBreakpoints,speedBreakpoints, ...
        tableValuesRM,x(1),x(2))
    yinterp2 = 273.6243
    

    Input Arguments

    collapse all

    Breakpoints, specified as a cell array. Each element of breakPoints must be strictly increasing vectors of doubles.

    Note

    The order of the first two dimensions is specified by the IsRowMajor name-value argument.

    Input data, specified as a matrix for the lookup table. The number of columns in inputData must equal the number of columns in breakPoints.

    Note

    Extrapolation is not supported.

    Output data, specified as a vector. The number of rows in outputData must equal number of rows in inputData.

    Smoothness penalty for fitting the lookup table, specified as a real positive scalar.

    The smoothness penalty is measured by second divided differences of lookup tables. The larger the smoothness penalty, the smoother the lookup table will be, but the match between the data and the lookup table will be worse. You might need to experiment with several values for smoothness to find an acceptable compromise between the goodness of fit and lookup table smoothness.

    Name-Value Arguments

    Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

    Example: values = fitlookupn(---, Bounds=[-inf,inf],RegularizerType='laplacian')

    Lower and upper bounds for the lookup table elements, specified as a 1-by-2 array, in the range [min,max].

    Example: Bounds=[-100,100]

    Note

    An Optimization Toolbox™ license is required to specify bounds.

    Option to use row-major order, specified as true or false.

    When IsRowMajor=true, the first dimension is a columns and the second dimension is a row.

    Setting IsRowMajor=true is useful for graphics applications where the first dimension is X and the second dimension is Y.

    Example: IsRowMajor=true

    Option to show fitted 1-D or 2-D lookup tables and data, specified as true or false.

    Example: ShowPlot=true

    Output Arguments

    collapse all

    N-D lookup table, specified as an N-D double array, where the number of dimensions is the number of breakpoints.

    Version History

    Introduced in R2023b

    See Also

    (Model-Based Calibration Toolbox)