main content

fit smooth surfaces to investigate fuel efficiency -凯发k8网页登录

this example shows how to use curve fitting toolbox™ to fit a response surface to some automotive data to investigate fuel efficiency.

the toolbox provides sample data generated from a gtpower predictive combustion engine model. the model emulates a naturally aspirated spark-ignition, 2-liter, inline 4-cylinder engine. you can fit smooth lowess surfaces to this data to find minimum fuel consumption.

the data set includes the required variables to model response surfaces:

  • speed is in revolutions per minute (rpm) units.

  • load is the normalized cylinder air mass (the ratio of cylinder aircharge to maximum naturally aspirated cylinder aircharge at standard temperature and pressure).

  • bsfc is the brake-specific fuel consumption in g/kwh. that is, the energy flow in, divided by mechanical power out (fuel efficiency).

the aim is to model a response surface to find the minimum bsfc as a function of speed and load. you can use this surface as a table, included as part of a hybrid vehicle optimization algorithm combining the use of a motor and your engine. to operate the engine as fuel efficiently as possible, the table must operate the engine near the bottom of the bsfc bowl.

load and preprocess data

load the data from the xls spreadsheet. use the 'basic' command option for non-windows® platforms.

create a variable n that has all the numeric data in one array.

n = xlsread( 'engine_data_si_na_2l_i4.xls', 'si na 2l i4', '', 'basic' );

extract from the variable n the columns of interest.

speed = n(:,2);
load_cmd = n(:,3);
load = n(:,8);
bsfc = n(:,22);

process the data before fitting, to pick out the minimum bsfc values from each sweep. the data points are organized in sweeps on speed/load.

get a list of the speed/load sites:

sl = unique( [speed, load_cmd], 'rows' );
nruns = size( sl, 1 );

for each speed/load site, find the data at the site and extract the actual measured load and the minimum bsfc.

minbsfc  = zeros( nruns, 1 );
load     = zeros( nruns, 1 );
speed    = zeros( nruns, 1 );
for i = 1:nruns
    idx = speed == sl(i,1) & load_cmd == sl(i,2);
    minbsfc(i) = min(  bsfc(idx) );
    load(i)    = mean( load(idx) );
    speed(i)   = mean( speed(idx) );
end

fit a surface

fit a surface of fuel efficiency to the preprocessed data.

f1 = fit( [speed, load], minbsfc, 'lowess', 'normalize', 'on' )
     locally weighted smoothing linear regression:
       f1(x,y) = lowess (linear) smoothing regression computed from p
       where x is normalized by mean 3407 and std 1214
       and where y is normalized by mean 0.5173 and std 0.1766
     coefficients:
       p = coefficient structure

plot fit

plot( f1, [speed, load], minbsfc );
xlabel( 'speed [rpm]' );
ylabel( 'load [%]' );
zlabel( 'minimum bsfc [g/kwh]' );

remove problem points

review the resulting plot.

there are points where bsfc is negative because this data is generated by an engine simulation.

remove those problem data points by keeping points in the range [0, inf].

out = excludedata( speed, minbsfc, 'range', [0, inf] );
f2 = fit( [speed, load], minbsfc, 'lowess', ...
    'normalize', 'on', 'exclude', out )
     locally weighted smoothing linear regression:
       f2(x,y) = lowess (linear) smoothing regression computed from p
       where x is normalized by mean 3443 and std 1187
       and where y is normalized by mean 0.521 and std 0.175
     coefficients:
       p = coefficient structure

plot the new fit. note that the excluded points are plotted as red crosses.

plot( f2, [speed, load], minbsfc, 'exclude', out );
xlabel( 'speed [rpm]' );
ylabel( 'load [%]' );
zlabel( 'minimum bsfc [g/kwh]' );

zoom in

zoom in on the part of the z-axis of interest.

zlim([0, max( minbsfc )])

you want to operate the engine efficiently, so create a contour plot to see the region where the bsfc is low. use the plot function, and specify the name/value parameter pair 'style','contour'.

plot( f2, [speed, load], minbsfc, 'exclude', out, 'style', 'contour' );
xlabel( 'speed [rpm]' );
ylabel( 'load [%]' );
colorbar

create a table from the surface

generate a table by evaluating the model f2 over a grid of points.

create variables for the table breakpoints.

speedbreakpoints = linspace( 1000, 5500, 17 );
loadbreakpoints =  linspace( 0.2, 0.8, 13 );

to generate values for the table, evaluate the model over a grid of points.

[tspeed, tload] = meshgrid( speedbreakpoints, loadbreakpoints );
tbsfc = f2( tspeed, tload );

examine the rows and columns of the table at the command line.

tbsfc(1:2:end,1:2:end)
ans =
  columns 1 through 7
  722.3280  766.7608  779.4296  757.4574  694.5378  624.4095  576.5235
  503.9880  499.9201  481.7240  458.2803  427.7338  422.1099  412.1624
  394.7579  364.3421  336.1811  330.1550  329.1635  328.1810  329.1144
  333.7740  307.7736  295.1777  291.2068  290.3637  290.0173  287.8672
  295.9729  282.7567  273.8287  270.8869  269.8485  271.0547  270.5502
  273.7512  264.5167  259.7631  257.9215  256.9350  258.3228  258.6638
  251.5652  247.6746  247.2747  247.4699  247.3570  248.2433  248.8139
  columns 8 through 9
  532.1533  466.9610
  396.3209  398.0199
  335.3871  346.3882
  286.3077  291.0075
  269.6837  272.2054
  258.0298  260.5269
  249.0083  250.4165

plot the table against the original model

the grid on the model surface shows the table breakpoints.

h = plot( f2 );
h.edgecolor = 'none';
hold on
mesh( tspeed, tload, tbsfc, ...
    'linestyle', '-', 'linewidth', 2, 'edgecolor', 'k', ...
    'facecolor', 'none', 'facealpha', 1 );
hold off
xlabel( 'speed [rpm]' );
ylabel( 'load [%]' );
zlabel( 'minimum bsfc [g/kwh]' );

check the table accuracy

view the difference between the model and the table by plotting the difference between them on a finer grid. then, use this difference in prediction accuracy between the table and the model to determine the most efficient table size for your accuracy requirements.

the following code evaluates the model over a finer grid and plots the difference between the model and the table.

[tfspeed, tfload] = meshgrid( ...
    linspace( 1000, 5500, 8*17 1 ), ...
    linspace( 0.2, 0.8, 8*13 1 ) );
tfbsfc_model = f2( tfspeed, tfload );
tfbsfc_table = interp2( tspeed, tload, tbsfc, tfspeed, tfload, 'linear' );
tfdiff = tfbsfc_model - tfbsfc_table;
surf( tfspeed, tfload, tfdiff, 'linestyle', 'none'  );
hold on
mesh( tspeed, tload, zeros( size( tbsfc ) ), ...
    'linestyle', '-', 'linewidth', 2, 'edgecolor', 'k', ...
    'facecolor', 'none', 'facealpha', 1 );
hold off
axis tight
xlabel( 'speed [rpm]' );
ylabel( 'load [%]' );
zlabel( 'difference between model and table [g/kwh]' );
title( sprintf( 'max difference: %g', max( abs( tfdiff(:) ) ) ) );

create a table array including breakpoint values

after creating a table by evaluating a model fit over a grid of points, it can be useful to export your table data from matlab. before exporting, create a table array that includes the breakpoint values in the first row and column. the following command reshapes your data to this table format:

  • x (speedbreakpoints) is a (1 x m) vector

  • y (loadbreakpoints) is an (n x 1) vector

  • z (tbsfc) is an (m x n) matrix

table = [
    {'load\speed'}, num2cell(speedbreakpoints(:).' )
    num2cell(loadbreakpoints (:) ), num2cell( tbsfc )
    ];

export table to spreadsheet file

you can use the xlswrite function to export your table data to a new excel spreadsheet. execute the following command to create a spreadsheet file.

xlswrite( 'tabledata.xlsx', table )

create a lookup table block

if you have simulink® software, you can create a look up table block as follows. execute the following code to try it out.

1. create a model with a 2-d lookup table block.

simulink
new_system( 'my_model' )
open_system( 'my_model' )
add_block( 'simulink/lookup tables/2-d lookup table', 'my_model/surfaceblock' )

2. populate the lookup table with speed breakpoints, load breakpoints, and a lookup table.

set_param( 'my_model/surfaceblock',...
    'breakpointsfordimension1', 'loadbreakpoints',...
    'breakpointsfordimension2', 'speedbreakpoints',...
    'table', 'tbsfc' );

3. examine the populated lookup table block.

网站地图