Examples that Use Standard Algorithms Evaluate the vector functio evens =2: 2: n: (n,1); F(odds, 1)=1-x(odds); F(evens, 1)=10.*(x(evens )-x(odds). 2) Evaluate the Jacobian matrix if nargout >1 if nargout 1 ones(n/2, 1): C sparse(odds, odds, c, n, n); es(n/2,1);D= sparse(evens s, d, n, n); e=-20*x(odds); E= sparse(evens, odds, e, n, n); J=C+D+E nd Step 2: Call the solve routine for the system of equations options-optimset('Display, 'iter,'Jacobian','on)i [X, F, exitflag, output, JAc]= fsolve(abananaobj, x0, options ); Use the starting point x(i)=-1.9 for the odd indices, and x(i)=2 for the even indices. Accept the fsolve default'off' for the Largescale parameter, nd the default medium-scale nonlinear equation algorithm 'dogleg.Then set Jacobian to'on'to use the Jacobian defined in bananaob j m. The fsolve function generates the following output First-order Trust-region Iteration Func-count f(x) optimality radius 4281.92 615 1 1546.86 23456 112.552 106.24 6.25 34.1 6.25 51.3854 1.5625 6.39 1.56 789 51.3854 43.87220.976562 0.977 8 37.0713 2.44141 6.2 2.44 37.0713 2.44141 2.44 26.24850.610352 1.52
Examples that Use Standard Algorithms 2-19 % Evaluate the vector function odds = 1:2:n; evens = 2:2:n; F = zeros(n,1); F(odds,1) = 1-x(odds); F(evens,1) = 10.*(x(evens)-x(odds).^2); % Evaluate the Jacobian matrix if nargout > 1 if nargout > 1 c = -ones(n/2,1); C = sparse(odds,odds,c,n,n); d = 10*ones(n/2,1); D = sparse(evens,evens,d,n,n); e = -20.*x(odds); E = sparse(evens,odds,e,n,n); J = C + D + E; end Step 2: Call the solve routine for the system of equations. n = 64; x0(1:n,1) = -1.9; x0(2:2:n,1) = 2; options=optimset('Display','iter','Jacobian','on'); [x,F,exitflag,output,JAC] = fsolve(@bananaobj,x0,options); Use the starting point for the odd indices, and for the even indices. Accept the fsolve default 'off' for the LargeScale parameter, and the default medium-scale nonlinear equation algorithm 'dogleg'. Then set Jacobian to 'on' to use the Jacobian defined in bananaobj.m . The fsolve function generates the following output: Norm of First-order Trust-region Iteration Func-count f(x) step optimality radius 0 1 4281.92 615 1 1 2 1546.86 1 329 1 2 3 112.552 2.5 34.8 2.5 3 4 106.24 6.25 34.1 6.25 4 5 106.24 6.25 34.1 6.25 5 6 51.3854 1.5625 6.39 1.56 6 7 51.3854 3.90625 6.39 3.91 7 8 43.8722 0.976562 2.19 0.977 8 9 37.0713 2.44141 6.27 2.44 9 10 37.0713 2.44141 6.27 2.44 10 11 26.2485 0.610352 1.52 0.61 x i( ) = –1.9 x i( ) = 2
2 Tutorial 20.6649 52588 4.63 6.97 2345 13 34567 8.48582 1.52588 4.69 4.08398 1.52588 1.77589 1.52588 3.56 1.53 16 0.692381 1.52588 3.31 0.109777 1.16206 19 00.0468565 1.53 Optimization terminated successfully First-order optimality is less than options. TolFun Nonlinear Equations with Finite-Difference Jacobian In the preceding example, the function bananaobj evaluates F and computes the Jacobian J. What if the code to compute the Jacobian is not available? By default, if you do not indicate that the Jacobian can be computed in the objective function(using the Jacobian parameter in options), fsolve, sqnonlin, and lsqcurvefit instead use finite differencing to approximate the Jacobian. This is the default Jacobian option. You can select finite differencing by setting the Jacobian parameter to'off' in optimset This example uses bananaobj from the preceding example as the objective function, but sets the jacobian parameter to 'off so that fsolve approximates the Jacobian and ignores the second bananaobjoutput. It accepts the fsolve default ' for the LargeScale parameter, and the default nonlinear equation medium-scale algorithm'dogleg n=64; x0(1:n,1) 0(2:2:n options-optimset('Display','iter, Jacobian,off); [X, F, exitflag, output, JAc]= fsolve(abananaobj, x0, options) The example produces the following output Norm of First-order Trust-region Iteration Func-count f(x) step optimality 4281.92 615 1 130 1546.86 329 106.24 106.24 6.25 34.1 2-20
2 Tutorial 2-20 11 12 20.6649 1.52588 4.63 1.53 12 13 17.2558 1.52588 6.97 1.53 13 14 8.48582 1.52588 4.69 1.53 14 15 4.08398 1.52588 3.77 1.53 15 16 1.77589 1.52588 3.56 1.53 16 17 0.692381 1.52588 3.31 1.53 17 18 0.109777 1.16206 1.66 1.53 18 19 0 0.0468565 0 1.53 Optimization terminated successfully: First-order optimality is less than options.TolFun Nonlinear Equations with Finite-Difference Jacobian In the preceding example, the function bananaobj evaluates F and computes the Jacobian J. What if the code to compute the Jacobian is not available? By default, if you do not indicate that the Jacobian can be computed in the objective function (using the Jacobian parameter in options), fsolve, lsqnonlin, and lsqcurvefit instead use finite differencing to approximate the Jacobian. This is the default Jacobian option.You can select finite differencing by setting the Jacobian parameter to 'off' in optimset. This example uses bananaobj from the preceding example as the objective function, but sets the Jacobian parameter to 'off' so that fsolve approximates the Jacobian and ignores the second bananaobjoutput. It accepts the fsolve default 'off' for the LargeScale parameter, and the default nonlinear equation medium-scale algorithm 'dogleg': n = 64; x0(1:n,1) = -1.9; x0(2:2:n,1) = 2; options=optimset('Display','iter','Jacobian','off'); [x,F,exitflag,output,JAC] = fsolve(@bananaobj,x0,options); The example produces the following output: Norm of First-order Trust-region Iteration Func-count f(x) step optimality radius 0 65 4281.92 615 1 1 130 1546.86 1 329 1 2 195 112.552 2.5 34.8 2.5 3 260 106.24 6.25 34.1 6.25 4 261 106.24 6.25 34.1 6.25
Examples that Use Standard Algorithms 5 326 51.3854 6.39 327 51.3854 3.90625 6.39 3.91 392 43.87220.976562 2.19 0.97 678901234567 37.0713 2.44141 37.0713 2.44141 6.2 2.44 26.24850.610352 0.61 588 20.66491.52588 4.63 653 17.2558 1.52588 6.97 718 8.48582 4.69 783 4.08398 1.52588 3.77 1.53 1.775 1.52588 3 913 0.692381 1.52588 3.31 978 0.109777 1043 Optimization terminated successfully First-order optimality is less than options. TolFun The finite-difference version of this example requires the same number of iterations to converge as the analytic Jacobian version in the preceding example. It is generally the case that both versions converge at about the same rate in terms of iterations. However the finite-difference version requires many additional function evaluations. The cost of these extra evaluations might or might not be significant, depending on the particular problem. Multiobjective Examples The previous examples involved problems with a single objective function. This section demonstrates solving problems with multiobjective functions using lsqnonlin, fminimax, and fgoalattain. Included is an example of how to optimize parameters in a Simulink model Simulink Example Let's say that you want to optimize the control parameters in the Simulink model optsim mdl. (This model can be found in the Optimization Toolbox optim directory. Note that Simulink must be installed on your system to load this model. )The model includes a nonlinear process plant modeled as a Simulink block diagram shown in Figure 2-1, Plant with Actuator Saturation. 221
Examples that Use Standard Algorithms 2-21 5 326 51.3854 1.5625 6.39 1.56 6 327 51.3854 3.90625 6.39 3.91 7 392 43.8722 0.976562 2.19 0.977 8 457 37.0713 2.44141 6.27 2.44 9 458 37.0713 2.44141 6.27 2.44 10 523 26.2485 0.610352 1.52 0.61 11 588 20.6649 1.52588 4.63 1.53 12 653 17.2558 1.52588 6.97 1.53 13 718 8.48582 1.52588 4.69 1.53 14 783 4.08398 1.52588 3.77 1.53 15 848 1.77589 1.52588 3.56 1.53 16 913 0.692381 1.52588 3.31 1.53 17 978 0.109777 1.16206 1.66 1.53 18 1043 0 0.0468565 0 1.53 Optimization terminated successfully: First-order optimality is less than options.TolFun The finite-difference version of this example requires the same number of iterations to converge as the analytic Jacobian version in the preceding example. It is generally the case that both versions converge at about the same rate in terms of iterations. However, the finite-difference version requires many additional function evaluations. The cost of these extra evaluations might or might not be significant, depending on the particular problem. Multiobjective Examples The previous examples involved problems with a single objective function. This section demonstrates solving problems with multiobjective functions using lsqnonlin, fminimax, and fgoalattain. Included is an example of how to optimize parameters in a Simulink model. Simulink Example Let’s say that you want to optimize the control parameters in the Simulink model optsim.mdl. (This model can be found in the Optimization Toolbox optim directory. Note that Simulink must be installed on your system to load this model.) The model includes a nonlinear process plant modeled as a Simulink block diagram shown in Figure 2-1, Plant with Actuator Saturation
2 Tutorial Actuator Model Plant 50s3+a2.s2+a1.s+1 Rate igure 2-1: 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 offinput values greater than 2 units or less than-2 units The slew rate limit of the actuator is 0. 8 units/sec. The open-loop response of the system to a step input is shown in Figure 2-2, 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 Start from the Simulation menu. The response plots to the scope 2-22
2 Tutorial 2-22 Figure 2-1: 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 open-loop response of the system to a step input is shown in Figure 2-2, 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 Start from the Simulation menu. The response plots to the scope. Actuator Model 1 Rate y 1.5 50s +a2.s +a1.s+1 3 2 Plant Limit 1 u
Examples that Use Standard Algorithms 舀A日 Time offset: 0 Figure 2-2: 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 have been placed in a hierarchical Subsystem block. a Scope block displays output trajectories during the design process. See Figure 2-3, Closed-Loop Model Tunable Variables are PID gains, Kp, Ki, and K 口却 p ant& Actuate igure 2-3: Closed 2-23
Examples that Use Standard Algorithms 2-23 Figure 2-2: 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 have been placed in a hierarchical Subsystem block. A Scope block displays output trajectories during the design process. See Figure 2-3, Closed-Loop Model. Figure 2-3: Closed-Loop Model