# Question regarding the NMPC example of the pendulum

3 views (last 30 days)
Christos Patrinos on 23 Jun 2020
Hello,
I was looking at the NMPC example of the pendulum (https://www.mathworks.com/help/mpc/ug/swing-up-control-of-a-pendulum-using-nonlinear-model-predictive-control.html) and I have a question regarding the definition of the custom discretization method. In particular, the code used is the following:
function xk1 = pendulumDT0(xk, uk, Ts)
%% Discrete-time nonlinear dynamic model of a pendulum on a cart at time k
%
% 4 states (xk):
% cart position (z)
% cart velocity (z_dot): when positive, cart moves to right
% angle (theta): when 0, pendulum is at upright position
% angular velocity (theta_dot): when positive, pendulum moves anti-clockwisely
%
% 1 inputs: (uk)
% force (F): when positive, force pushes cart to right
%
% xk1 is the states at time k+1.
%
% Copyright 2018 The MathWorks, Inc.
%#codegen
% Repeat application of Euler method sampled at Ts/M.
M = 10;
delta = Ts/M;
xk1 = xk;
for ct=1:M
xk1 = xk1 + delta*pendulumCT0(xk1,uk);
end
% Note that we choose the Euler method (first oder Runge-Kutta method)
% because it is more efficient for plant with non-stiff ODEs. You can
% choose other ODE solvers such as ode23, ode45 for better accuracy or
% ode15s and ode23s for stiff ODEs. Those solvers are available from
% MATLAB.
My question is why do they have the following loop and what does this loop mean. Do they iterate through time until 10sec? Why do we need this if we want the discretization method to be done online?
for ct=1:M
xk1 = xk1 + delta*pendulumCT0(xk1,uk);
end