# Outputting more than one value into Simulink system using Matlab function block

조회 수: 3(최근 30일)
Bryan 2019년 7월 6일
답변: Nisar Ahmed 2020년 5월 6일
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!

댓글을 달려면 로그인하십시오.

### 답변(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.
##### 댓글 수: 1표시숨기기 없음
Bryan 2019년 7월 10일
Hi Raj.
Thanks for replying. I have edited my coded accordingly as you mentioned in the comment above. I am still getting an output of 0.2 only. Apart from that, would like to know if it is necessary to include a bus and also how do I go about it as my D has only one output from the function block.
Thanks!

댓글을 달려면 로그인하십시오.

Nisar Ahmed 2020년 5월 6일
send me the code
nisark37@gmail.com

댓글을 달려면 로그인하십시오.

### 범주

Find more on Simscape Electrical in Help Center and File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by