Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

using MATLAB/SIMULINK, apply your designed controller to the original nonlinear

ID: 3832953 • Letter: U

Question

using MATLAB/SIMULINK, apply your designed controller to the original nonlinear system and

assume that the reference input and disturbance are given by

r(t) = 6 /180 . 1(t) (rad)

d(t) = 5 . 1(t) (Nm)

Suppose further that your initial conditions in the main file are set to

% Initial conditions theta_10 = 0; theta_20 = 0.01; omega_10 = v/r; omega_20 = 0;

(a) Plot the signals 1, 2, 1, 2, e and u over the time interval [0,20] (s), where u(t) is the controller output, that is u(t) + d(t) = (t).

function [dx] = nonlinear_Segway_PT(input_vector)
% The objective of this function is to implement the right- hand-side of the
% dynamical equation (15) for the nonlinear Segway PT. The input of
% this function is taken as a vector with 11 elements, whereas the output is
% the left-hand-side of the dynamical equation.
% Extract variables and parameters from the input vector
theta_1 = input_vector(1); % Position
theta_2 = input_vector(2); % Position
omega_1 = input_vector(3); % Velocity
omega_2 = input_vector(4); % Velocity

r = input_vector(11); % Radius
% Define quantities
g = 9.81;
d_11 = (m_1+m_2) * r^2 + I_1;
d_12 = m_2 * r * L * cos(theta_2); d_22 = m_2 * L^2 + I_2;

h_1 = -m_2 * r * L * sin(theta_2) * omega_2^2; h_2 = -m_2 * g * L * sin(theta_2);
Delta = d_11 * d_22 - d_12^2;
% Implement Eq. (15)

dtheta_1 = omega_1;
dtheta_2 = omega_2;
domega_1 = (d_22 * (tau-h_1) + d_12 * (tau + h_2))/Delta; domega_2 = (-d_12 * (tau-h_1) - d_11 * (tau + h_2))/Delta; % dx
dx = [dtheta_1; dtheta_2; domega_1; domega_2];
end

Explanation / Answer

[sys, xp] = CSTR_INOUT([],[],[],'sizes');

up = [10 298.15 298.15];

u = up(3);

tsave = [];

usave = [];

ysave = [];

rsave = [];

Ts = 1;

t = 0;

while t < 40

    yp = xp;

    % Linearize the plant model at the current conditions

    [a,b,c,d] = linmod('CSTR_INOUT',xp,up);  

    Plant = ss(a,b,c,d);

    Plant.InputGroup.ManipulatedVariables = 3;

    Plant.InputGroup.UnmeasuredDisturbances = [1 2];

    Model.Plant = Plant;

   

    % Set nominal conditions to the latest values

    Model.Nominal.U = [0 0 u];

    Model.Nominal.X = xp;

    Model.Nominal.Y = yp;

   

    dt = 0.001;

   

    simOptions.StartTime = num2str(t);

    simOptions.StopTime = num2str(t+dt);

    simOptions.LoadInitialState = 'on';

    simOptions.InitialState = 'xp';

    simOptions.SaveTime = 'on';

    simOptions.SaveState = 'on';

    simOptions.LoadExternalInput = 'on';

    simOptions.ExternalInput = '[t up; t+dt up]';

   

    simOut = sim('CSTR_INOUT',simOptions);

   

    T = simOut.get('tout');

    XP = simOut.get('xout');

    YP = simOut.get('yout');

    Model.Nominal.DX = (1/dt)*(XP(end,:)' - xp(:));

   

    % Define MPC controller for the latest model

    MPCobj = mpc(Model, Ts);

    MPCobj.W.Output = [0 1];

   

    % Ramp the setpoint

    r = max([8.57 - 0.25*t, 2]);

   

    % Compute the control action

    if t <= 0

        xd = [0; 0];

        x = mpcstate(MPCobj,xp,xd,[],u);

    end

   

    u = mpcmove(MPCobj,x,yp,[0 r],[]);

   

    % Simulate the plant for one control interval

    up(3) = u;

   

    simOptions.StartTime = num2str(t);

    simOptions.StopTime = num2str(t+Ts);

    simOptions.InitialState = 'xp';

    simOptions.ExternalInput = '[t up; t+Ts up]';

   

    simOut = sim('CSTR_INOUT',simOptions);

   

    T = simOut.get('tout');

    XP = simOut.get('xout');

    YP = simOut.get('yout');

   

    % Save results for plotting

    tsave = [tsave; T];

    ysave = [ysave; YP];

    usave = [usave; up(ones(length(T),1),:)];

    rsave = [rsave; r(ones(length(T),1),:)];

   

    xp = XP(end,:)';

   

    t = t + Ts;

end