lsqnonlin 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 closed-loop response. 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 solve this problem, minimize the error between the output and the input signal. (in contrast, in the example using fminimax with a simulink model, the solution involves minimizing the maximum value of the output.) the variables are the parameters of the proportional integral derivative (pid) controller. if you only need to minimize the error at one time unit, you would have a scalar objective function. but the goal is to minimize the error for all time steps from 0 to 100, thus producing a multiobjective function (one function for each time step).
use lsqnonlin
to perform a least-squares fit on the tracking of the output. the tracking is performed by the function tracklsq
, which is nested in runtracklsq
at the end of this example. tracklsq
returns the error signal yout
, the output computed by calling sim
, minus the input signal 1.
the function runtracklsq
sets up all required values and then calls lsqnonlin
with the objective function tracklsq
. the variable options
passed to lsqnonlin
defines the criteria and display characteristics. the options specify to have no displayed output, to use the 'levenberg-marquardt'
algorithm, and the options give termination tolerances for the step and objective function on the order of 0.001.
to run the simulation in the model optsim
, you must specify the variables kp
, ki
, kd
, a1
, and a2
(a1
and a2
are variables in the plant block). kp
, ki
, and kd
are the variables to be optimized. the function tracklsq
is nested inside runtracklsq
so that the variables a1
and a2
are shared between the two functions. the variables a1
and a2
are initialized in runtracklsq
.
the objective function tracklsq
runs the simulation. you can run the simulation either in the base workspace or the current workspace, that is, the workspace of the function calling sim
, which in this case is the workspace of tracklsq
. in this example, the srcworkspace
option is set to 'current'
to tell sim
to run the simulation in the current workspace. runtracklsq
runs the simulation to 100 seconds.
when the simulation is complete, runtracklsq
creates the myobj
object in the current workspace (that is, the workspace of tracklsq
). the outport block in the block diagram model puts the yout
field of the object into the current workspace at the end of the simulation.
when you run runtracklsq
, the optimization gives the solution for the proportional, integral, and derivative (kp
, ki
, kd
) gains of the controller.
[kp, ki, kd] = runtracklsq
kp = 3.1330
ki = 0.1465
kd = 14.3918
the scope shows the optimized closed-loop step response.
closed-loop response after lsqnonlin
note: the call to sim
results in a call to one of the simulink ordinary differential equation (ode) solvers. you need to choose which type of solver to use. from the optimization point of view, a fixed-step ode solver is the best choice if it is sufficient to solve the ode. however, in the case of a stiff system, a variable-step ode method might be required to solve the ode.
the numerical solution produced by a variable-step solver, however, is not a smooth function of parameters, because of step-size control mechanisms. this lack of smoothness can prevent an optimization routine from converging. the lack of smoothness is not an issue when you use a fixed-step solver. (for a further explanation, see .)
simulink design optimization™ software is recommended for solving multiobjective optimization problems in conjunction with simulink variable-step solvers. this software provides a special numeric gradient computation that works with simulink and avoids introducing a problem of lack of smoothness.
helper function
the following code creates the runtracklsq
helper function.
function [kp,ki,kd] = runtracklsq % runtracklsq demonstrates using lsqnonlin with simulink. mdl = 'optsim'; open_system(mdl) % load the model in = simulink.simulationinput(mdl); % create simulation input object in = in.setmodelparameter('stoptime','100'); % stop time 100 pid0 = [0.63 0.0504 1.9688]; % initial gain values a1 = 3; a2 = 43; % initialize model plant variables options = optimoptions(@lsqnonlin,'algorithm','levenberg-marquardt',... 'display','off','steptolerance',0.001,'optimalitytolerance',0.001); % optimize the gains set_param(mdl,'fastrestart','on'); % fast restart pid = lsqnonlin(@tracklsq,pid0,[],[],options); set_param(mdl,'fastrestart','off'); % return the gains kp = pid(1); ki = pid(2); kd = pid(3); function f = tracklsq(pid) % track the output of optsim to a signal of 1 % set the simulation input object parameters in = in.setvariable('kp',pid(1),'workspace',mdl); in = in.setvariable('ki',pid(2),'workspace',mdl); in = in.setvariable('kd',pid(3),'workspace',mdl); % simulate out = sim(in); f = out.get('yout') - 1; end end
凯发官网入口首页 copyright 2019–2020 the mathworks, inc.