Why mpc function and mpc loop isn't same?

1 回表示 (過去 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 Center および File ExchangeModel Predictive Control Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by