Function does a thing for an amount of time, then another thing.

6 views (last 30 days)
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 Comment
Stephen23
Stephen23 on 18 Oct 2017
"Does anyone know why this isn't working?"
You replace all of the input values with zero.

Sign in to comment.

Answers (1)

Greg
Greg on 18 Oct 2017
Edited: Greg on 19 Oct 2017
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 Comments
Stephen23
Stephen23 on 19 Oct 2017
Edited: Stephen23 on 19 Oct 2017
"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:
Alasdair Robertson
Alasdair Robertson on 19 Oct 2017
The page I found advising initialisation of variables is linked below;
https://www.mathworks.com/matlabcentral/answers/39191-undefined-function-or-variable-the-first-assignment-to-a-local-variable-determines-its-clas
Again, I'm a complete novice programmer. The page linked above is the reason why I carried out that action, which I totally appreciate might have been incorrect. I was just trying to narrow down what my problem might actually be.
Sorry also for the delayed response. I only got the notifications when I woke this morning.
Thanks again!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!