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));用simulink如何实现筛选出最小的四个状态
时间: 2023-07-15 15:11:53 浏览: 93
可以使用MATLAB Function模块和sort函数来实现。首先,在MATLAB Function模块中,将上述代码复制到函数中,然后使用sort函数对g向量进行排序,得到排序后的向量g_sort。然后,取g_sort的前四个元素作为最小的四个状态,将其存储到一个向量中。最后,将这个向量作为输出变量,连接到MATLAB Function模块的输出端口上。在Simulink中,将这个MATLAB Function模块放置在适当的位置,并连接它与其他模块。这样,就可以实现筛选出最小的四个状态的功能。
相关问题
g(1)=abs(i2refa-i2a-K1*(uca-udc0))+abs(i2refb-i2b-K1(ucb-udc0))+abs(i2refc-i2c-K1(ucc-udc0)); 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-udc0))+abs(i2refb-i2b-K1(ucb-udc0))+abs(i2refc-i2c-K1(ucc-udc*0))用simulink如何实现筛选出计算所得值最小的输入状态
你可以使用 MATLAB Function Block 来实现这个功能。首先,将上述代码复制到 MATLAB Function Block 中,并将输入和输出端口设置为所需的大小和数据类型。然后,在 MATLAB Function Block 中添加以下代码来计算 g 值:
```
function [g_min, state] = fcn(i2refa, i2a, i2refb, i2b, i2refc, i2c, uca, ucb, ucc, udc0, K1)
g = zeros(1,8);
g(1) = abs(i2refa-i2a-K1*(uca-udc0))+abs(i2refb-i2b-K1*(ucb-udc0))+abs(i2refc-i2c-K1*(ucc-udc0));
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-udc0))+abs(i2refb-i2b-K1*(ucb-udc0))+abs(i2refc-i2c-K1*(ucc-udc*0));
[g_min, state] = min(g);
end
```
该函数将计算出每个状态的 g 值,并返回最小值及其对应的状态编号。接下来,在 Simulink 模型中,将 MATLAB Function Block 插入到合适的位置,并将输入端口连接到控制系统的输出,将输出端口连接到一个 MinMax Block。在 MinMax Block 中,选择“Minimum”作为操作,即可实现筛选出计算所得值最小的输入状态的功能。
>> 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输出最小的四个输入状态
您可以使用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
阅读全文