Question regarding the NMPC example of the pendulum
5 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
回答(1 个)
Divyajyoti Nayak
2025-2-25
The code provided is trying to calculate the states of the pendulum at time 'k+1' which is at time 'Ts'. The states are calculated discretely using the Euler method by dividing the sample time 'Ts' into 'M' intervals. The loop iterates through these 'M' intervals to calculate the states at each of these intervals using the discrete equation of Euler method till the final interval which gives the final states at time 'k + 1' .
for ct=1:M %Iterating through 'M' intervals
xk1 = xk1 + delta*pendulumCT0(xk1,uk); %Calculating new state values at each interval using Euler method
end
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Model Predictive Control Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!