MATLAB Answers

Question regarding the NMPC example of the pendulum

5 views (last 30 days)
Christos Patrinos
Christos Patrinos on 23 Jun 2020
I was looking at the NMPC example of the pendulum ( 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.
% 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);
% 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
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);

Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!