Function output not displayed on scope.

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

why a for loop ? you can do the same function much simpler
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 ?
hello again
see my answer below
"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

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

and this is an alternative that does not use a counter block (simple addition : angle (new) = angle(old) + delta_angle (modulo 2pi)
Thanks for all the replies and suggestions which I am now going to replicate and try and get a proper understanding of how they work. Everything seems to point to my problems being the understanding of Simulink itself and the way the "Solvers" interpret the model. Based on the comment by Mathieu
"why a for loop ? you can do the same function much simpler " and Walters suggestion of using a clock input I came up with this function that produced an output on a scope but also highlighted my problems with the step times in Simulink.
function [X,Y,Z] = ThreePhase(Magnitude,Frequency,t) % t is the clock input
W = 2*pi*Frequency;
X = Magnitude*sin((W*t));
Y = Magnitude*sin((W*t)+ 120);
Z = Magnitude*sin((W*t)- 120);
It is also interesting to see that three phase waveform, I have had similar displays and to me it shows insufficient data points to produce a smooth waveform but again this must be something I am not setting up correctly in the solver.
just a minor comment
matlab sin, cos, tan assume angle are in radian - you have now sind, cosd,... if you use degrees (and for lazy people)
Y = Magnitude*sin((W*t)+ 120); is therefore incorrect as you are mixing (W*t) which is in radian and the shift (120) in degrees
=> Y = Magnitude*sin((W*t)+ 2*pi/3)
Nicely spotted, my original function used t = period / 360 but after using the clock for t I overlooked multiplying my 120 degrees by pi / 180. I have replicated both models put forward and now looking to fully understand both methods, with and without the counter.
I have constructed both the models and cannot smooth out the sinewave but both models produce exactly the same output. I also can only achieve an output with frequency = 1 and not with it set to 50.
Currently re-visiting the Matlab Onramp courses because I need more detail on how the models are solved by the solvers as this is I believe one of the issues.
NB that dt =0.02 s (or expressed differently, samplig freq Fs = 1/dt = 50 Hz , Nyquist frequency = Fs/2 = 25 Hz. Shannon theorem states that you cannot generate a sine wave with less than two samples per period (and the shape is going to be like staircases , not a nice sine wave).
  • when generating a 1 Hz sinewave you have 50 samples per period and the signal looks like a sine indeed
  • we you increase the sine freq, the number o samples per period decreases and you get a less smooth sine , up to the point of when you reach the Nyquist frequency (here 25 Hz you have a very crude output with only 2 samples / period and at 50 Hz it's just outside the valid range as you are above the Nyquist frequency
  • reduce dt (/ increase the sampling rate ) as you want higher frequency sine waves (think about having at least 10 samples per period if you want something that actually looks like a sinewave and not large staircases)
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.
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 !
I think the original question has been answered and I have added an output to the model that gives me the required angle in degree's . Many thanks to both Walter and Mathieu who have shown that my method using Matlab code in a function block was not the best solution and gave alternative Simulink models. My understanding of using Simulink has been greatly improved along the way and I have now got a much deeper understanding behind the solutions which is as important as the solution itself. The only outstanding question is why define Dt in the Matlab command window and not as a constant in the model ?
The only outstanding question is why define Dt in the Matlab command window and not as a constant in the model ?
you can either directly initialize the simulink file & the solver parameters with a numerical value , but IMHO, especially if you have a lot of variables I prefer to initialize them from a script file.
NB that you can also run your simulation from the same matlab script , using sim - Run and script programmatic simulations of Simulink models - MATLAB
this way you can do more complex simulations like making a for loop , change parameters , run the new simulation (using sim) retrieve the data , and so forth...
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)

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

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.
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

Asked:

Roy
on 17 Oct 2025

Edited:

on 27 Oct 2025

Community Treasure Hunt

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

Start Hunting!