Function output not displayed on scope.

45 views (last 30 days)
Roy
Roy on 17 Oct 2025
Edited: Stephen23 on 27 Oct 2025 at 10:38
The following function block is supposed to output three sine waves X, Y & Z with a 120 degree phase shift between each along with Angle A. Only looking at one phase at present. The way I thought this works is I have a For loop that runs from 0 to 360 and with each itteration of the loop a point on the sinewave is produced. I have confirmed the equation for X using Excel and the correct values are shown in the Simulink viewer. The problem is that this sinewave is not being shown on the scope and I am not an experienced user of Matlab and cannot find the reason, help appreciated.
function [X,Y,Z, A] = ThreePhase(Magnitude,Frequency,t)
Angle = 1:1:360; % Array of Angles
W = 2*pi*Frequency; % Frequency = 50 W = 314.1593
Ts = t / 360; % Time per Degree Ts = 55.6 E-6
Y = 0;
Z = 0;
for index = 1:1:360
X = Magnitude * (sin( W * (Angle(index) *Ts) ) ); % Calculate a value for the given angle.
Q = [index, X]; % Test purposes only
disp(Q);
end
A = Angle;
  4 Comments
Mathieu NOE
Mathieu NOE on 20 Oct 2025
hello again
see my answer below
Stephen23
Stephen23 on 27 Oct 2025 at 10:17
Edited: Stephen23 on 27 Oct 2025 at 10:38
"Please elaborate on how you could do it much simpler"
The name MATLAB comes from MATrix LABoratory. It is often simpler to write MATLAB code in terms of matrices (or vectors or arrays): https://www.mathworks.com/help/matlab/matlab_prog/vectorization.html
function [X, Y, Z, A] = ThreePhase(Magnitude, Frequency, t)
A = 0:360; % fixed starting angle
W = 2 * pi * Frequency;
X = Magnitude * sin(W * A * t / 360);
Y = 0;
Z = 0;
end

Sign in to comment.

Accepted Answer

Mathieu NOE
Mathieu NOE on 20 Oct 2025
hello again
assuming dt = 0.02 is your application sampling rate and this is what we use in Simulink solver options (discrete / fixed steps)
so dt is no more a block input , but if needed you can easily change this setting as your initialize variables in your workspace (like dt) - so dt can be changed anytime .
assuming you have executed
dt = 0.02;
in your matlab command window , the simulink part is fairly simple (and can be executed) :
top level block (nothing interesting indeed)
sub-function description (here we are)
so we have a counter that goes from 0 to 359 (360 steps) and this allows us to increment the angle by angular steps = 2*pi*dt (NB that the angular precision is not defined by the counter max value, but the counter limit must be larger than 1/dt otherwise you never reach 2*pi)
then we add 2*pi/3 twice to get the two additionnal angular outputs shifted by 120 degrees and from there simply do a 2pi modulo operation and apply the sin operator (in my code I used the look up table option). Then multiply by the magnitude value.
here the scope outputs for dt = 0.02, Freq = 1, Magnitude = 2 , simulation duration time = 1 s
attached the simulink file
hope it helps !
  12 Comments
Roy
Roy on 27 Oct 2025 at 9:05
Yes I do have a long way to go on the learning curve but good progress has been made and just from this problem a big advance has been made.

Sign in to comment.

More Answers (1)

Walter Roberson
Walter Roberson on 17 Oct 2025
The scope would show the output signal. The output is after the for loop has run. So the scope would show the final X, final Y, final Z -- corresponding to Magnitude * (sin( W * (Angle(360) *Ts) ) ), and 0, and 0.
It would not especially surpise me if Magnitude * (sin( W * (360 *Ts) ) ) came out as 0 as well.
The contents of MATLAB Function Blocks are evaluated once every time step. (The size of a timestep can be variable, depending on whether you configured fixed step solver or variable step solver.) It is the ending values of the outputs that become the output values of the signals. Everything in one invocation is considered to take place at the same time.
You should consider using a clock input block, and inside the MATLAB Function Block, calculate the outputs only for the current clock value.
  2 Comments
Roy
Roy on 18 Oct 2025
Thanks Walter as I now realise that my issue is because there is no output from a loop until it has completed which is different from the loops used in C. The final evaluation of X for 360 degree's is as you say also zero.
What does " calculate the outputs only for the current clock value. " actually do ? I need to look deeper at how Matlab evaluates expressions and function blocks because this is more complex than I assumed and is raising more questions.
Walter Roberson
Walter Roberson on 18 Oct 2025
Constant Block1 --->| Constant Block2 ---> |
| multiply block1 ---> Trig block (sin)-+ | multiply block2 ---> scope
Clock ------------->| |--------------------> |
Simulink will automatically evaluate this series of blocks repeatedly, with different times on the clock. The action will be to evaluate CONSTANT2 .* sin(CONSTANT1 .* TIME) and display the current result via the scope.

Sign in to comment.

Products


Release

R2025a

Community Treasure Hunt

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

Start Hunting!