Outputting more than one value into Simulink system using Matlab function block
4 次查看(过去 30 天)
显示 更早的评论
Hi guys,
I have an issue with my matlab code. I am currently working on a solar MPPT system. I am using a Matlab function block for my algorithm which inputs the power from the solar PV and it will output the duty cycle according to my code. Currently, I wish to inject 4 duty cycles of 0.2, 0.4, 0.6, 0.8 one at a time and I need to save the power generated for each of these duty cycles. HOWEVER, my code is only able to output the first duty cycle. How do I code it to get it to run each duty cycle and to save the power generated for each of the duty cycle? For and while loops are not allowed in matlab function blocks cuz its a recursive call.
function D = BAT(P)
D = 0;
DPold = [0.2;0.4;0.6;0.8];
Pold = [0;0;0;0];
if D < 0.2
D = DPold(1);
Pold(1) = P;
elseif D < 0.4 & D >= 0.2
D = DPold(2);
Pold(2) = P;
elseif D < 0.6 & D >= 0.4
D = DPold(3);
Pold(3) = P;
elseif D < 0.8 & D >= 0.6
D = DPold(4);
Pold(4) = P;
else
end
Thanks guys!
0 个评论
回答(2 个)
Raj
2019-7-8
There are two things here.
1) Your function code is wrong at many places. You will have to rewrite it:
function D = BAT(P) % So D is the output and P is the input
D = 0; % D is initlalized as zero
DPold = [0.2;0.4;0.6;0.8]; % fine
Pold = [0;0;0;0]; % fine
if D < 0.2 % condition will pass
D = DPold(1); % D=0.2
Pold(1) = P; % Polt(1)=whatever value of P you are giving
elseif D < 0.4 & D >= 0.2 % i think you meant D < 0.4 && D >= 0.2; condition pass
D = DPold(2); % D=0.4
Pold(2) = P;% Polt(2)=whatever value of P you are giving
elseif D < 0.6 & D >= 0.4 % i think you meant D < 0.6 && D >= 0.4; condition pass
D = DPold(3);% D=0.6
Pold(3) = P;% Polt(3)=whatever value of P you are giving
elseif D < 0.8 & D >= 0.6 % i think you meant D < 0.8 && D >= 0.6; condition pass
D = DPold(4);% D=0.8
Pold(4) = P;% Polt(4)=whatever value of P you are giving
else % not required
end
% function end missing here
%% so basically your function outputs D=0.8
2) I wish to inject 4 duty cycles of 0.2, 0.4, 0.6, 0.8 one at a time: First of all correct your function as i explained above so that you get an array D=[0.2,0.4,0.6,0.8] as output. Then you can use bus to transmit all the values at once to the next block.
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!