Escolar Documentos
Profissional Documentos
Cultura Documentos
Open MATLAB :-
NOTE To use MATLAB in the program mode you must open the application as an m-File this will
produce a Script File
Enter the following lines as far as the semi colons the other bits are explanations :-
x = 0: pi/100: 2*pi; defines the range of x values (0 to 2*pi) and the step size 100/pi.
y = sin(x); tells MATLAB to compute values of y for each step of x (amplitude A=1 and
phase = zero
Next:- Click on File/ Save As and choose a name to save the file eg Sinewave1.m
Now click on Debug / Run and this should plot a simple sine-wave
If you wish to edit the simple program, place the cursor in the program area to change the script
When you have changed the script, go to Debug and click on Save and Run this should plot a
new graph with the new script details.
For example try changing the amplitude A from 1 to 2 or change the step size from pi/100 to
pi/50 - and see the difference.
1
Example 2 Example 1 To Plot a Simple Cosine Wave
x = 0: pi/100: 2*pi; defines the range of x values (0 to 2*pi) and the step size 100/pi.
y = cos(x); this tells MATLAB to compute values of y for each step of x (A=1 and =0)
Save this with a different name and then click on Debug/Run to plot a Cosine wave that varies in x
and y.
The above example of one cycle of a simple sine-wave was one which changed in x and y. In real life,
electrical signals vary with time and we need to be able to plot a sine wave that changes in time so
that we can see the effects of frequency, amplitude and phase.
To plot waves that vary in time we must be careful about the time scales, frequencies and step sizes
otherwise we could end up with a very strange looking graph indeed.
For example, we will plot one cycle of a sine-wave with a frequency of 50Hz.
Note with a frequency of 50Hz, the periodic time will be T=1/f = 1/50 = 0.02 or 20 ms. This means
that if we wish to plot just one cycle of this waveform we will require time values from 0 to 0.02sec.
Also, if we wish to see a nice smooth curve, we should choose lots of steps per cycle 1000 steps
would be fine for a smooth curve but 10 would not try changing the number of steps for yourself
to see the difference.
For one cycle of a 50Hz waveform we should use a time interval of 0.02sec or 20ms.
For a smooth curve we will use 1000 steps so each step has a time interval of 1/1000 = 0.001sec.
2
t = 0: 0.001: 0.02; defines the time range 0 to 0.02 sec in steps of 0.001 sec
Save this with a suitable name and click on Debug / Run to plot the time varying sinewave.
Now try changing the frequency in the above example to 1000 and run the program again dont
forget to click on Debug Save and Run first.
For five cycles of a 50Hz waveform we should use a time interval of 5 x 0.02 =0.1sec.
Also, in the following program we could set the amplitude to something other than 1 say 5.
Edit the above script to that shown below, click on Debug Save and Run and see the difference
t = 0: 0.001: 0.1; defines the time range 0 to 0.1 sec in steps of 0.001 sec
3
Example 5 Plotting Waveforms of the Same Frequency and Amplitude Together Differing only
in Phase
Now that you have some experience at plotting waveforms we can begin to look at some more
interesting items such as plotting different waveforms together and then adding them together.
This example will plot 5 cycles of a sine wave and a cosine wave on the same graph.
t = 0: 0.001: 0.02; defines the time range 0 to 0.02 sec in steps of 0.001 sec
The two waveforms have the same frequencies and amplitudes they differ only in phase.
In this example, a signal is plotted alongside another one which is one third the amplitude of the first
and three times its frequency. In other words, the second signal is the third harmonic of the first
(fundamental) and it has one third the amplitude of the fundamental.
t = 0: 0.001: 0.02; defines the time range 0 to 0.02 sec in steps of 0.001 sec
4
Example 7 Plotting Waveforms Differing in Phase and Adding them Together
This example is the same as Example 6 except that it also adds two waves together (a sinewave and
a cosine wave)and plots the sum as well as the original waveforms.
NOTE From the plot, it can be seen that if the frequency and amplitude are the same and the
waveforms differ only in phase, then the sum of the two is also a sine wave with the same frequency
but different amplitude and phase.
t = 0: 0.001: 0.02; defines the time range 0 to 0.02 sec in steps of 0.001 sec
Example 8 Plotting Waveforms with Different Frequencies and Amplitudes and Adding them
Together
In this example, a signal is plotted alongside another one which is one third the amplitude of the first
and three times its frequency. In other words, the second signal is the third harmonic of the first
(fundamental) and it has one third the amplitude of the fundamental.
t = 0: 0.001: 0.02; defines the time range 0 to 0.02 sec in steps of 0.001 sec
NOTE - What has happened to the new waveform it has changed shape and is no longer sinusoidal
it has become a complex waveform.
5
Example 9 Plotting Waveforms with Different Phase
In this example, a signal is plotted alongside another one which is shifted in phase by pi/2 radians. In
other words, the second signal is out of phase with respect to the first signal by 900. NOTE This is
similar to Example 5 except that we are using sine waves instead of sinewaves and cosine waves.
t = 0: 0.001: 0.02; defines the time range 0 to 0.02 sec in steps of 0.001 sec
This code produces two signals one (y1) starting at t=0, another (y2) phase shifted by pi/2 radians
(or 900). Look at the differences.
When we add together sine waves (or cosine waves or both) of different frequencies and
amplitudes we can create a wave with a different shape non-sinusoidal. This is known as Fourier
Sunthesis and it is the opposite of Fourier Analysis where a complex (non-sinusoidal signal) is broken
down to see its composite sine and cosine waves.
ALSO Watch out for overshoots when synthesising digital waveforms see Gibbs Phenomena for
more information.
t = 0: 0.001: 0.02; defines the time range 0 to 0.02 sec in steps of 0.001 sec
plot(t,y),grid; tells MATLAB to plot the sum of y1,y2,y3 & y4 against time.
6
Example 10 Using a Controlled Loop to Sum up Lots of Sine Waves
MATLAB can also be used to synthesise complex waveforms by adding multiple sine waves or cosine
waves or both in a controlled loop to save us writing out the equation for each term . The PREVIOUS
process can be simplified considerably by using a loop and counter
The idea is to set up a counter inside a loop to run from say 1 to n where n is a large number (say
100 or 10000). Each time the loop is executed, one more waveform is added to the previous one
until the sum is obtained.
NOTE the higher the loop counter, the better the waveform BUT higher loop numbers take longer
to compute so the process needs to be optimised between a quick execution time and a good
waveform shape. You must decide the optimum number.
We can automate the summing process to make thousands (if necessary) of additions. Computers
are good at this sort of thing because they can do repetitive things very quickly without complaining
and without getting tired.
The idea is to set y to zero to start, and then define the loop parameters to loop around say 100
times, and do a new calculation each time. As the program loops around we start with the first
calculation and loop back and make another one with a different frequency. Then we want to add in
the last calculation to the previous one. As the program loops through the different sine waves
become added together until we reach the end. Note - the end point is where enough calculations
have been done to obtain the end result but no more otherwise it is a waste of computer time. You
have to decide how many loops is enough by looking at the end product. When the end result has
been reached, the program will jump out of the loop and print the final result
This is a rough guide to the method and you can use different numbers to suit:-
1. Set Y = 0
2. Define loop parameters:- say - n starts at 0 and increases to 100 in steps of 2 at a time.)
There are other variations of this.
3. Define the fundamental signal y = (A*n)*sin(omega*n*t)
4. Y=Y+y this adds the new y to the old one.
5. End loop
7
NOTE for odd harmonics we want n to take on values of 0, 1, 3, 5, 7, 9 etc and this can done by
using (2n-1) in the equation for y. Check it for yourself:-
N 2n-1
0 0
1 1
2 3
3 5
4 7
Example Code:-
for n=1:2:100; defines loop and counter n stats at 1 and jumps to 100 in steps of
2 at a time.
Plot(t,Y), grid finally the loop ends and the final waveform is plotted
MATLAB INFORMATION
There is even an academic folder with lots of tips and hints for students. Go to the MathWorks web
site at :-www.MathWorks.com
MATLAB Book:- There is a cheap MATLAB paperback book called :-MATLAB De Mystified by David
McMahon, published by McGraw Hill 2007, ISBN 978-0-07-148551-7. Price - 12.99
8