using fminimax with a simulink model -凯发k8网页登录
this example shows how to tune the parameters of a simulink® model. the model, optsim
, is included when you run this example. the model includes a nonlinear process plant modeled as a simulink block diagram.
plant with actuator saturation
the plant is an under-damped third-order model with actuator limits. the actuator limits are a saturation limit and a slew rate limit. the actuator saturation limit cuts off input values greater than 2 units or less than –2 units. the slew rate limit of the actuator is 0.8 units/sec. the closed-loop response of the system to a step input is shown in . you can see this response by opening the model (type optsim
at the command line or click the model name), and selecting run from the simulation menu. the response plots to the scope.
closed-loop response
the problem is to design a feedback control loop that tracks a unit step input to the system. the closed-loop plant is entered in terms of the blocks where the plant and actuator are located in a hierarchical subsystem block. a scope block displays output trajectories during the design process.
closed-loop model
to optimize this system, minimize the maximum value of the output at any time between 0 and 100. (in contrast, in the example lsqnonlin with a simulink model, the solution involves minimizing the error between the output and the input signal.)
the code for this example is contained in the helper function runtrackmm
at the the objective function is simply the output yout
returned by the sim
command. but minimizing the maximum output at all time steps might force the output to be far below unity for some time steps. to keep the output above 0.95 after the first 20 seconds, the constraint function trackmmcon
contains the constraint yout >= 0.95
from t = 20
to t = 100
. because constraints must be in the form , the constraint in the function is g = -yout(20:100) 0.95
.
both trackmmobj
and trackmmcon
use the result yout from sim
, calculated from the current pid values. to avoid calling the simulation twice, runtrackmm
has nested functions so that the value of yout
is shared between the objective and constraint functions. the simulation is called only when the current point changes.
call runtrackmm
.
[kp,ki,kd] = runtrackmm
objective max line search directional iter f-count value constraint steplength derivative procedure 0 5 0 1.11982 1 11 1.184 0.07978 1 0.482 2 17 1.012 0.04285 1 -0.236 3 23 0.9995 0.007058 1 -0.0186 hessian modified twice
4 29 0.9997 9.708e-07 1 0.00716 hessian modified local minimum possible. constraints satisfied. fminimax stopped because the size of the current search direction is less than twice the value of the step size tolerance and constraints are satisfied to within the value of the constraint tolerance.
kp = 0.5910
ki = 0.0606
kd = 5.5383
the last value in the objective value
column of the output shows that the maximum value for all the time steps is just under 1. the closed-loop response with this result is shown in the figure .
this solution differs from the solution obtained in lsqnonlin with a simulink model because you are solving different problem formulations.
closed-loop response using fminimax
helper function
the following code creates the runtrackmm
helper function.
function [kp, ki, kd] = runtrackmm optsim % initialize simulink(r) pid0 = [0.63 0.0504 1.9688]; % a1, a2, yout are shared with trackmmobj and trackmmcon a1 = 3; a2 = 43; % initialize plant variables in model yout = []; % give yout an initial value pold = []; % tracks last pid opt = simset('solver','ode5','srcworkspace','current'); options = optimset('display','iter',... 'tolx',0.001,'tolfun',0.001); pid = fminimax(@trackmmobj,pid0,[],[],[],[],[],[],... @trackmmcon,options); kp = pid(1); ki = pid(2); kd = pid(3); function f = trackmmobj(pid) % track the output of optsim to a signal of 1. % variables a1 and a2 are shared with runtrackmm. % variable yout is shared with runtrackmm and % runtrackmmcon. updateifneeded(pid) f = yout; end function [c,ceq] = trackmmcon(pid) % track the output of optsim to a signal of 1. % variable yout is shared with runtrackmm and % trackmmobj updateifneeded(pid) c = -yout(20:100) .95; ceq=[]; end function updateifneeded(pid) if ~isequal(pid,pold) % compute only if needed kp = pid(1); ki = pid(2); kd = pid(3); [~,~,yout] = sim('optsim',[0 100],opt); pold = pid; end end end