필터 지우기
필터 지우기

Why mpc function and mpc loop isn't same?

조회 수: 3 (최근 30일)
영민 공
영민 공 2023년 7월 24일
답변: Shushant 2023년 8월 1일
This is my mpc code, and it works well,
% Given state-space model
% function [out1, out2] = mpcmove_test(in1,in2)
%
% r = in1;
% y = in2;
num=[1 1];
den=[1 1 2];
[A,B,C,D] = tf2ss(num,den);
sys = ss(A,B,C,D);
Ts = 0.05;
p = 10;
m = 10;
r = 1;
y = 0;
mpc_contrller = mpc(sys,Ts,p,m);
state = mpcstate(mpc_contrller);
for i = 1:100
[u] = mpcmove(mpc_contrller,state,y,r);
y = C*state.plant;
out1 = u;
out2 = y;
y_p(i,1) = y;
end
plot(y_p)
but when i run this code like fuction. It works different. It can't follow reference value.
function [out1, out2] = mpcmove_test(in1,in2)
r = in1;
y = in2;
num=[1 1];
den=[1 1 2];
[A,B,C,D] = tf2ss(num,den);
sys = ss(A,B,C,D);
Ts = 0.05;
p = 10;
m = 10;
% r = 1;
% y = 0;
mpc_contrller = mpc(sys,Ts,p,m);
state = mpcstate(mpc_contrller);
% for i = 1:100
[u] = mpcmove(mpc_contrller,state,y,r);
y = C*state.plant;
out1 = u;
out2 = y;
% y_p(i,1) = y;
% end
% plot(y_p)
end
in2 = 0;
in1 = 1;
for j = 1:100
[out1, out2] = mpcmove_test(in1,in2);
in2 = out2;
y_p(j,1) = out2;
end
plot(y_p)
I don't know why this works different?

채택된 답변

Shushant
Shushant 2023년 8월 1일
I understand that when you try to execute two identical pieces of code, one using a regular loop and the other utilizing a function but there is disparity in outputs. The difference in outputs is due to the function "mpcmove".
The second argument "state" which is being passed to the function "mpcmove" gets updated every time the function is called. Refer to the following documentation for a detailed information-
While using the normal loop "state" gets updated after each iteration and we get the correct output. But in the implementation of the function, every time the function gets called the "state" gets reinitialized which result in different output.
As a workaround to this you can initialize the state before calling the function. Below is the code snippet for the same
in2 = 0;
in1 = 1;
% initializing the controller and state before calling the function
num=[1 1];
den=[1 1 2];
[A,B,C,D] = tf2ss(num,den);
sys = ss(A,B,C,D);
Ts = 0.05;
p = 10;
m = 10;
mpc_contrller = mpc(sys,Ts,p,m);
-->"Weights.ManipulatedVariables" is empty. Assuming default 0.00000. -->"Weights.ManipulatedVariablesRate" is empty. Assuming default 0.10000. -->"Weights.OutputVariables" is empty. Assuming default 1.00000.
state = mpcstate(mpc_contrller);
-->Converting model to discrete time. -->Assuming output disturbance added to measured output #1 is integrated white noise. -->"Model.Noise" is empty. Assuming white noise on each measured output.
for j = 1:100
% Also passing the controller and state to the function
[out1, out2] = mpcmove_test(in1,in2, mpc_contrller, state, C);
in2 = out2;
y_p(j,1) = out2;
end
plot(y_p)
function [out1, out2] = mpcmove_test(in1, in2, mpc_contrller, state, C)
r = in1;
y = in2;
[u] = mpcmove(mpc_contrller,state,y,r);
y = C*state.plant;
out1 = u;
out2 = y;
end
I hope this clarifies the doubts you may have had.
Thank you,
Shushant

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Model Predictive Control Toolbox에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by