>> function [sa,sb,sc] = fcn(Ts,L1,L2,C2,udc,uca,ucb,ucc,i1a,i1b,i1c,i2a,i2b,i2c,i2refa,i2refb,i2refc,ucrefa,ucrefb,ucrefc,ea,eb,ec,i1refa,i1refb,i1refc) K1=Ts/L2;temp=0;C=0;P=0;mpc=1;sa=0;sb=0;sc=0;K2=Ts/C2;K3=Ts/L1; w1=1; w2=0.7; w3=20; g=[0 0 0 0 0 0 0 0]; h=[0 0 0 0 0 0 0 0]; k=[0 0 0 0 0 0 0 0]; z=[0 0 0 0 0 0 0 0]; g(1)=abs(i2refa-i2a-K1*(uca-udc*0))+abs(i2refb-i2b-K1*(ucb-udc*0))+abs(i2refc-i2c-K1*(ucc-udc*0)); g(2)=abs(i2refa-i2a-K1*(uca-udc*(-1/3)))+abs(i2refb-i2b-K1*(ucb-udc*(-1/3)))+abs(i2refc-i2c-K1*(ucc-udc*(2/3))); g(3)=abs(i2refa-i2a-K1*(uca-udc*(-1/3)))+abs(i2refb-i2b-K1*(ucb-udc*(2/3)))+abs(i2refc-i2c-K1*(ucc-udc*(-1/3))); g(4)=abs(i2refa-i2a-K1*(uca-udc*(-2/3)))+abs(i2refb-i2b-K1*(ucb-udc*(1/3)))+abs(i2refc-i2c-K1*(ucc-udc*(1/3))); g(5)=abs(i2refa-i2a-K1*(uca-udc*(2/3)))+abs(i2refb-i2b-K1*(ucb-udc*(-1/3)))+abs(i2refc-i2c-K1*(ucc-udc*(-1/3))); g(6)=abs(i2refa-i2a-K1*(uca-udc*(1/3)))+abs(i2refb-i2b-K1*(ucb-udc*(-2/3)))+abs(i2refc-i2c-K1*(ucc-udc*(1/3))); g(7)=abs(i2refa-i2a-K1*(uca-udc*(1/3)))+abs(i2refb-i2b-K1*(ucb-udc*(1/3)))+abs(i2refc-i2c-K1*(ucc-udc*(-2/3))); g(8)=abs(i2refa-i2a-K1*(uca-udc*0))+abs(i2refb-i2b-K1*(ucb-udc*0))+abs(i2refc-i2c-K1*(ucc-udc*0)); % for P=1:8 % z(P)=w1*g(P)+w2*h(P)+w3*k(P); % end % temp=z(1); % for C=1:8 % if z(C)<temp % temp=z(C); % mpc=C; % end % end switch mpc case 1 sa=0;sb=0;sc=0; case 2 sa=0;sb=0;sc=1; case 3 sa=0;sb=1;sc=0; case 4 sa=0;sb=1;sc=1; case 5 sa=1;sb=0;sc=0; case 6 sa=1;sb=0;sc=1; case 7 sa=1;sb=1;sc=0; case 8 sa=1;sb=1;sc=1; otherwise sa=0;sb=0;sc=0; end 这是matlab function中的一段代码如何修改代码找出使g输出最小的四个输入状态
时间: 2023-07-16 07:12:58 浏览: 125
您可以使用Matlab中的sort函数找出使g输出最小的四个输入状态。具体方法是将g向量排序并找出前四个最小的元素的索引,然后在switch语句中使用这四个索引进行判断。修改后的代码如下:
function [sa,sb,sc] = fcn(Ts,L1,L2,C2,udc,uca,ucb,ucc,i1a,i1b,i1c,i2a,i2b,i2c,i2refa,i2refb,i2refc,ucrefa,ucrefb,ucrefc,ea,eb,ec,i1refa,i1refb,i1refc)
K1=Ts/L2;temp=0;C=0;P=0;mpc=1;sa=0;sb=0;sc=0;K2=Ts/C2;K3=Ts/L1;
w1=1; w2=0.7; w3=20;
g=[0 0 0 0 0 0 0 0];
h=[0 0 0 0 0 0 0 0];
k=[0 0 0 0 0 0 0 0];
z=[0 0 0 0 0 0 0 0];
g(1)=abs(i2refa-i2a-K1*(uca-udc*0))+abs(i2refb-i2b-K1*(ucb-udc*0))+abs(i2refc-i2c-K1*(ucc-udc*0));
g(2)=abs(i2refa-i2a-K1*(uca-udc*(-1/3)))+abs(i2refb-i2b-K1*(ucb-udc*(-1/3)))+abs(i2refc-i2c-K1*(ucc-udc*(2/3)));
g(3)=abs(i2refa-i2a-K1*(uca-udc*(-1/3)))+abs(i2refb-i2b-K1*(ucb-udc*(2/3)))+abs(i2refc-i2c-K1*(ucc-udc*(-1/3)));
g(4)=abs(i2refa-i2a-K1*(uca-udc*(-2/3)))+abs(i2refb-i2b-K1*(ucb-udc*(1/3)))+abs(i2refc-i2c-K1*(ucc-udc*(1/3)));
g(5)=abs(i2refa-i2a-K1*(uca-udc*(2/3)))+abs(i2refb-i2b-K1*(ucb-udc*(-1/3)))+abs(i2refc-i2c-K1*(ucc-udc*(-1/3)));
g(6)=abs(i2refa-i2a-K1*(uca-udc*(1/3)))+abs(i2refb-i2b-K1*(ucb-udc*(-2/3)))+abs(i2refc-i2c-K1*(ucc-udc*(1/3)));
g(7)=abs(i2refa-i2a-K1*(uca-udc*(1/3)))+abs(i2refb-i2b-K1*(ucb-udc*(1/3)))+abs(i2refc-i2c-K1*(ucc-udc*(-2/3)));
g(8)=abs(i2refa-i2a-K1*(uca-udc*0))+abs(i2refb-i2b-K1*(ucb-udc*0))+abs(i2refc-i2c-K1*(ucc-udc*0));
% 找出前四个最小元素的索引
[~,idx] = sort(g);
idx = idx(1:4);
for P=1:8
h(P)=abs(ea-g(P));
k(P)=abs(i1refa-i1a-K3*(ea-ea*0)-K2*ea);
end
for P=1:4 % 只在前四个最小元素中搜索最小值
z(P)=w1*g(idx(P))+w2*h(idx(P))+w3*k(idx(P));
end
[temp,mpc] = min(z); % 找出最小值和对应的输入状态
switch mpc
case idx(1)
sa=0;sb=0;sc=0;
case idx(2)
sa=0;sb=0;sc=1;
case idx(3)
sa=0;sb=1;sc=0;
case idx(4)
sa=0;sb=1;sc=1;
case idx(5)
sa=1;sb=0;sc=0;
case idx(6)
sa=1;sb=0;sc=1;
case idx(7)
sa=1;sb=1;sc=0;
case idx(8)
sa=1;sb=1;sc=1;
otherwise
sa=0;sb=0;sc=0;
end
end
阅读全文