www.gusucode.com > mbcdata 工具箱 matlab 源码程序 > mbcdata/mbcweoptimizer.m
function [bestafr, bestspk] = mbcweoptimizer(tq, afrrng, spkrng, res) %MBCWEOPTIMIZER An example of a user specific optimization % % MBCWEOPTIMIZER solves the problem "max TQ over (AFR, SPK)". The API % for this example function mimics that used by Optimization Toolbox % functions. % % [BESTAFR, BESTSPK] = MBCWEOPTIMIZER(TQ) finds a maximum (BESTAFR, % BESTSPK) to the function TQ. % % [BESTAFR, BESTSPK] = MBCWEOPTIMIZER(TQ, AFRRNG, SPKRNG) finds a maximum % (BESTAFR, BESTSPK) to the function TQ. AFRRNG and SPKRNG are 1-by-2 % row vectors containing search ranges for those variables. % % [BESTAFR, BESTSPK] = MBCWEOPTIMIZER(TQ, AFRRNG, SPKRNG, RES) finds a % maximum (BESTAFR, BESTSPK) to the function TQ. This optimization is % performed over a RES-by-RES grid of (AFR, SPK) values. If RES is not % specified, the default grid resolution is 25. % % Copyright 2000-2007 The MathWorks, Inc. and Ford Global Technologies, Inc. if nargin < 4 || isempty(res) res = 25; end if nargin < 3 || isempty(spkrng) spkrng = [-10 60]; end if nargin < 2 || isempty(afrrng) afrrng = [10 20]; end if nargin < 1 || ~isa(tq, 'function_handle') error(message('mbc:mbcweoptimizer:InvalidArgument')); end % Ensure that the tq model can be evaluated over the inputs try spkvals = 20*ones(10, 1); afrvals = 14.3*ones(10, 1); testeval = tq(afrvals, spkvals); catch error(message('mbc:mbcweoptimizer:InvalidArgument1')); end % Create the search grid in (afr, spk) afrvals = linspace(afrrng(1), afrrng(2), res); spkvals = linspace(spkrng(1), spkrng(2), res); % Create a grid of points to evaluate the TQ model at [gafrvals, gspkvals] = ndgrid(afrvals, spkvals); vafrvals = gafrvals(:); vspkvals = gspkvals(:); % Evaluate TQ torqueeval = tq(vafrvals, vspkvals); % Perform a search to find maximum torque [unused, index] = max(torqueeval); % Return best AFR and best SPK bestafr = vafrvals(index); bestspk = vspkvals(index);