Fit Data to N-D Lookup Table Using fitlookupn
Fit an N-D lookup table from scattered data using the fitlookupn 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]')
![Figure contains an axes object. The axes object with xlabel LOAD, ylabel Speed [rpm] contains a line object which displays its values using only markers.](../../examples/shared_autoblks_mbc/win64/FitlookupnExample_01.png)
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.^
-2smoothnessPenalty = 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]')
![Figure contains an axes object. The axes object with xlabel LOAD, ylabel Speed [rpm] contains 3 objects of type surface, line. One or more of the lines displays its values using only markers](../../examples/shared_autoblks_mbc/win64/FitlookupnExample_02.png)
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]')
![Figure contains an axes object. The axes object with xlabel LOAD, ylabel Speed [rpm] contains 3 objects of type surface, line. One or more of the lines displays its values using only markers](../../examples/shared_autoblks_mbc/win64/FitlookupnExample_03.png)
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])
![Figure contains an axes object. The axes object with xlabel Speed [rpm], ylabel LOAD contains 4 objects of type surface, line. One or more of the lines displays its values using only markers](../../examples/shared_autoblks_mbc/win64/FitlookupnExample_04.png)
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