Você está na página 1de 8

PLOTTING SINE AND COSINE WAVES

MATLAB MODELLING USING m files (or SCRIPT FILES)

Open MATLAB :-

Click on File/ New/ m-File

NOTE To use MATLAB in the program mode you must open the application as an m-File this will
produce a Script File

Example 1 To Plot a Simple Sine Wave y=A*sin(omega +/-)

Where A = amplitude, omega = angular velocity = 2*pi*f, and = phase.

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

plot(x,y),grid; tells MATLAB to plot x and y on a grid.

Dont forget to end each line with a semi colon (;).

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

Program (Script) Editing

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

Enter the following lines as far as the semi colons :-

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)

plot(x,y),grid; this tells MATLAB to plot x and y on a grid.

Save this with a different name and then click on Debug/Run to plot a Cosine wave that varies in x
and y.

Example 3 - To Plot One Cycle of a Time Varying 50Hz Sine-wave

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

f = 50; defines the frequency to be 50Hz

omega = 2*pi*f; defines the angular velocity

y = sin(omega*t); tells MATLAB to compute values of y at each time step

plot(t,y),grid; tells MATLAB to plot y against time on a grid

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.

Example 4 To Plot Five Cycles of a 50Hz Sinusoidal Waveform

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

f = 50; defines the frequency to be 50Hz

omega = 2*pi*f; defines the angular velocity

y = sin(omega*t); tells MATLAB to compute values of y at each time step

plot(t,y),grid; tells MATLAB to plot y against time on a grid

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

f = 50; defines the frequency to be 50Hz

omega = 2*pi*f; defines the angular velocity

y1 = sin(omega*t); tells MATLAB to compute sine values at each step

y2=cos(omega*t); tells MATLAB to compute cosine values at each step

plot(t,y1,t,y2),grid; tells MATLAB to plot y1 and y2 against time.

The two waveforms have the same frequencies and amplitudes they differ only in phase.

Example 6 Plotting Waveforms with Different Frequencies and Amplitudes

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

f = 50; defines the frequency to be 50Hz

omega = 2*pi*f; defines the angular velocity

y1 =sin(omega*t); tells MATLAB to compute a fundamental sine wave

y2=(1/3)*sin(omega*3*t); tells MATLAB to compute the 3rd harmonic

Y3=(1/5)*SIN(OMEGA*5*t); tells MATLAB to compute the 5th harmonic

plot(t,y1,t,y2,t,y3),grid; tells MATLAB to plot y1, y2 and y3 together.

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.

There is a relationship here can you find it?

t = 0: 0.001: 0.02; defines the time range 0 to 0.02 sec in steps of 0.001 sec

f = 50; defines the frequency to be 50Hz

omega = 2*pi*f; defines the angular velocity

y1 =sin(omega*t); tells MATLAB to compute sine values at each step

y2=cos(omega*t); tells MATLAB to compute cosine values at each step

y3=y1+y2; tells MATLAB to add y1 to y2

plot(t,y1,t,y2,t,y3),grid; tells MATLAB to plot y1, y2 and y3 against time.

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

f = 50; defines the frequency to be 50Hz

omega = 2*pi*f; defines the angular velocity

y1 =sin(omega*t); tells MATLAB to compute sine values at each step

y2=(1/3)*sin(omega*3*t); tells MATLAB to compute sine values of the harmonic

y3=y1+y2; tells MATLAB to add y1 to y2

plot(t,y1,t,y2,t,y3),grid; tells MATLAB to plot y1, y2 and y3 against time.

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.

Remember :- 2 radians = 3600, so radians = 1800 and therefore /2 radians = 900.

t = 0: 0.001: 0.02; defines the time range 0 to 0.02 sec in steps of 0.001 sec

f = 50; defines the frequency to be 50Hz

A=3; sets the amplitude to 3 instead of 1

omega = 2*pi*f; defines the angular velocity

y1 =A*sin(omega*t); tells MATLAB to compute sine values at each step

y2=A*sin(omega*t +pi); tells MATLAB to compute the phase shifted signal

plot(t,y1,t,y2),grid; tells MATLAB to plot y1 and y2 against time.

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.

Example 10 Plotting Multiple Waveforms and Adding them Fourier Synthesis

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

f = 50; defines the frequency to be 50Hz

omega = 2*pi*f; defines the angular velocity

y1 =sin(omega*t); tells MATLAB to compute sine values at each step

y2=(1/3)*sin(omega*3*t); tells MATLAB to compute sine values of the 3rd harmonic

y3=(1/5)*sin(omega*5*t); tells MATLAB to compute sine values of the 5rd harmonic

y4=(1/7)*sin(omega*7*t); tells MATLAB to compute sine values of the 7rd harmonic

y=y1+y2+y3+y4; tells MATLAB to add y1 to y2

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

Define the time interval to plot the waveform over eg t=1:0.0001:0.1;

Define A, f, omega, etc

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

6. Plot waveform plots the final value of Y

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

t=0:0.0001:0.01; defines time array (time span to plot waveform)

f=100; defines frequency = 100Hz.

A=1; defines amplitude = 1.

omega = 2*pi*f; defines angular velocity = 2f

Y=0; define y=0 at start of loop

for n=1:2:100; defines loop and counter n stats at 1 and jumps to 100 in steps of
2 at a time.

y = n*A*sin(n*omega*t); computes the value of the waveforms according to the counter


value

Y=Y+y; this adds the last value of Y to the previous value

end; this tell the loop to jump back around again

Plot(t,Y), grid finally the loop ends and the final waveform is plotted

MATLAB INFORMATION

Math Works Web Site

There is a web site full of MATLAB tutorials, examples and so on.

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

Você também pode gostar