Function output not displayed on scope.

61 views (last 30 days)
Roy
Roy on 17 Oct 2025 at 11:29
Commented: Mathieu NOE about 19 hours ago
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
Roy
Roy on 17 Oct 2025 at 17:49
Probably a couple of reasons, my background is embedded C so loop constructs are common and I am new to Matlab. Please elaborate on how you could do it much simpler and would this result in the signal being displayed on the scope ?
Mathieu NOE
Mathieu NOE on 20 Oct 2025 at 12:25
hello again
see my answer below

Sign in to comment.

Answers (2)

Walter Roberson
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
Roy
Roy on 18 Oct 2025 at 9:37
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 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.

Sign in to comment.


Mathieu NOE
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
Roy
Roy about 2 hours ago
Thanks for that, it has been a while since working with the Nyquist sampling theorem and is something I need to get refreshed as it is obviously central to Simulink and a major difference from working with just a clock. I have set dt to 50 micro seconds with a stop time of 0.08 and with the model using the 0 to 359 clock you get this shown below but the model without a clock works fine.
I think this must have something to do with that clock resetting. If I use a dt value of 55.6 micro seconds, my period of 0.02 / 360 then both models work fine as shown below.
Mathieu NOE
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 !

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!