Function does a thing for an amount of time, then another thing.
조회 수: 1 (최근 30일)
이전 댓글 표시
Basically, I'm trying to replicate a Closed Transfer Switch in a Matlab Function. I have two inputs (two power sources) and two outputs. For simplicities sake, I have set it up so that when the first inputs rises above a specific value, that input will then drop to 0 and input 2 will flow through output 2. However, the behaviour of Closed Transfer Switches results in a brief moment of paralleling, where, for 100ms or less, both power sources are connected, so therefore, in this case, both inputs flow through the outputs.
I have had a stab at programming a function to achieve this. I don't really understand why it doesn't seem to be working as I've used the "etime" function to determine the time in which both outputs should be flowing. This isn't working and I really don't understand why. I've used a couple of functions to try simplify things a bit for myself but again, doesn't seem to be working.
Closed Transfer Switch Function.
%Closed Transfer Switch Function
function [y1, y2] = CTS(u1, u2)
coder.extrinsic('clock')
t0=clock;
u1=0;
u2=0;
y1=0;
y2=0;
if abs(u1) > 20
[y1, y2] = Para(u1, u2, t0);
else
[y2, y1] = Para(u2, u1, t0);
end
Paralleling Function
function [out1, out2] = Para(in1, in2, s)
coder.extrinsic('clock')
coder.extrinsic('etime')
in1=0;
in2=0;
out1=0;
out2=0;
if etime(clock, s) < 0.1
out1=in1;
out2=in2;
else
out1=0;
out2=in2;
end
Does anyone know why this isn't working? Any help would be much appreciated! Thanks all for your time! Ali.
댓글 수: 1
Stephen23
2017년 10월 18일
"Does anyone know why this isn't working?"
You replace all of the input values with zero.
답변 (1개)
Greg
2017년 10월 18일
편집: Greg
2017년 10월 19일
In both of your functions, you're telling the user "Thanks for the inputs, but I'm gonna use 0."
MATLAB's Code Analyzer is probably telling you the exact same thing with orange squigglies under u1, u2 and in1, in2.
To try to put it one more way:
function CTS(u1,u2)
u1 = 0;
u2 = 0;
Has the same effect on u1 and u2 as:
u1 = 9;
u1 = 0;
u2 = 9;
u2 = 0;
The difference seems to be you aren't familiar with calling functions in MATLAB. You would call it from the command window(or another function/script, just like you did with Para):
CTS(9,9);
This tells CTS that u1 should initialize to 9, and u2 should initialize to 9. If you click the big green play triangle, or call it from command window as:
CTS();
You aren't initializing u1 or u2 to anything, so you'll get "Undefined Variable 'u1'" errors. -- I assume everything I said is just a re-hash of the content in the various links provided by Stephen.
댓글 수: 5
Stephen23
2017년 10월 19일
편집: Stephen23
2017년 10월 19일
"I've just realised that I shouldn't have those 0 initialisations in my 'Para' function but potentially just in my main CTS function"
Why do you think that you need them in the function CTS? Inside CTS you reallocate the input variables to be zero, thus making the function input arguments totally useless. And allocating the output variables to be zero does nothing at all because you simply redefine those variables a few lines later. What is the point of all that?
I highly recommend that you work through the MATLAB Getting Started tutorials, which teach very basic MATLAB usage and would help you a lot:
참고 항목
카테고리
Help Center 및 File Exchange에서 Whos에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!