Function output not displayed on scope.
61 views (last 30 days)
Show older comments
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;
3 Comments
Answers (2)
Walter Roberson
on 17 Oct 2025 at 18:06
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
Walter Roberson
on 18 Oct 2025 at 18:56
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.
Mathieu NOE
on 20 Oct 2025 at 12:44
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 !
8 Comments
Mathieu NOE
about 4 hours ago
hello again
as I aleady explained above, if you use a counter , the max value depends of the lowest frequency you want to generate AND the sampling rate
a sinewave of 50 Hz (period = 20 milli second) with a samping time of 50 microseond means 1 period = 400 samples , so no surprise that with a counter limited to 360 (again this value has no relation with 360 degrees revolution) you just truncate your signal because the last 40 samples are not generated.
the 2nd method does not has this limitation as you also noticed !
See Also
Categories
Find more on Sources 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!

