Why mpc function and mpc loop isn't same?
Show older comments
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?
Accepted Answer
More Answers (0)
Categories
Find more on Controller Creation in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!