0% found this document useful (0 votes)
86 views9 pages

Matlab: Objective

MatLab is introduced to generate and plot various waveforms. Students are instructed to: 1) Enter basic MatLab commands to create arrays and solve equations. 2) Write a function to generate and plot a sine wave by varying frequency and amplitude. 3) Write another function to generate and plot a square wave in the same manner. Plots are saved for various frequency-amplitude combinations.

Uploaded by

Vinay Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views9 pages

Matlab: Objective

MatLab is introduced to generate and plot various waveforms. Students are instructed to: 1) Enter basic MatLab commands to create arrays and solve equations. 2) Write a function to generate and plot a sine wave by varying frequency and amplitude. 3) Write another function to generate and plot a square wave in the same manner. Plots are saved for various frequency-amplitude combinations.

Uploaded by

Vinay Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 9

EE 3010

MatLab
Objective
To introduce the student to MatLab programming, making them familiar with MatLabs
basic commands and scripts.
Procedure.
Open MatLab by double-clicking on the MatLab 7.. icon.
!ommand window " this is where MatLab commands are entered.
#orkspace " lists all current $ariables
%irectory " lists the files in the current working folder
!ommand history " lists all pre$ious commands
Some Basic MatLab Commands:
clear all !lears workspace of all $ariables
close all !loses all figure and plot windows
plot &,y' (lots $ector )y* $erses )*
+ ,eparates comments from script
help -sed to find the synta of a command
)help* is used in the following manner. help /command0, where /command0 is a
MatLab command.
1ample.
>> help close
CLOSE Close figure.
CLOSE(H) closes the window with handle H.
CLOSE, by itself, closes the current figure window.

CLOSE(na!e) closes the na!ed window.

CLOSE "LL closes all the open figure windows.
CLOSE "LL H#$$E% closes hidden windows as well.

S&"&'S ( CLOSE(...) returns ) if the specified windows
were closed
and * otherwise.

See also delete.
O+erloaded functions or !ethods (ones with the sa!e
na!e in other directories)
help serial,close.!
help ftp,close.!
help a+ifile,close.!
help tlchandle,close.!
-eference page in Help browser
doc close
Basic Arithmetic Operators
2 . 3rithmetic addition
- . 3rithmetic subtraction
4. 3rithmetic multiplication
5. 3rithmetic di$ision
6. 1ponent or power &7 6 8 9 7
8
'
.4. 1lement by element &for arrays'
Built-in Waveform Functions
cos&'. cosine
sin&'. sine
s:uare&'. generates a s:uare wa$e
sawtooth&'. generates a sawtooth
wa$e
sawtooth&t,;.<'. generates a triangle
wa$e
Part A: Basic MatLab Commands (command line
1nter the following array into MatLab

=
= >
; =
A
?y entering the following command.
39@= ;A> -=B
Cow, enter this array.

=
; 7
= 7
B
Decord the command used to create this array in the %ata ,heet.
Cow, find B A and A B . 1nter the results for both in the %ata ,heet.
,ol$e the following e:uation for E using MatLab:
C X A = ,

=
>
8
C

>
8
= >
; =
>
=
x
x
This reminder may help you. C A X A A
= =
=
1nter your results in the %ata ,heet. Fa$e your T3 $erify your MatLab results.
n order to plot a !aveform, we need $ectors representing both the independent &t' and
dependent &f@tB' $ariables.
f@tB can easily be defined after t as shown below.
out 9 f@tBA
1ample.
out 9 >4cos&t'A
Cow, create an array named t with $alues starting at ; and ending at >, incremented by
;.;;;=.
t needs to be defined first.
To %1GHC1 a $ector, MatLab uses the following synta.
t 9 @; ;.= ;.> ;.7 ;.8BA which produces a $ector t with fi$e elements, ;, ;.=, ;.>, ;.7 and
;.8.
This may be simple enough for short $ectors, but we need microsecond resolution to
show our periodic signals clearly. -sing the defining statement abo$e would re:uire
hundreds of thousands of entries to co$er only one secondI
Gortunately, MatLab has a solution.
3 $ector can be created and filled incrementally as shown below.
array ( start.incre!ent.finish
This creates $ector, filling it with $alues starting at )start,* ending at )finish* and
incremented by )increment.*
1ample.
inc ( )./.)*
inc ( 0) 1 2 )*3
1nter this command. t(0*.*.***).435
Cow, lets create an associated array for a cosine wa$e, with a fre:uency of > FJ. 1nter
this command. cosine(cos(46pi646t)5 This creates an array with a $alue for
each inde of t. This will be important later.
Cow, create an array for a sine wa$e named )sine* with a fre:uency of > FJ, using the
same array for t.
On the %ata ,heet, enter the $alues for this array at indices 8<, =<;K and =LLL>.
)sine(17)*
Cow, we will plot the sine wa$e. 1nter )help plot* for details on the plot function.
(lot the sine wa$e you ha$e created. 1nter. plot(t,cosine)5
3 window similar to the one shown below should appear.
Figure ": MatLab #lot
This shows your sine wa$e, but nothing else.
To make it look nicer, add a title, -ais label, and y-ais label.
title(Sine wa+e. 8re9uency 4H:)5
;label(ti!e (s))5
ylabel(<agintude)5
(rint your plot and turn it in with your report.
Part B: !ine"ave #eneration.
!ommands can also be called as scripts. 3 MatLab script has the etension ).m*
To create a new )M-file* MatLab script, !lick Gile Cew M-Gile.
Mou will now use a MatLab function to plot a sine wa$e. !reate an M-file and sa$e it as
)sinewa$e.m*
1nter the following code into the M-file editor.
= EE /*)*
= >eneration of sine wa+e with gi+en fre9uency,
a!plitude, phase, and $C offset.
clear all5
close all5
fre9uency ( 4***5 = re9uired fre9uency
period ( ),fre9uency5 = re9uired period
a!plitude ( ) 5 = re9uired a!plitude
phase ( *5 = re9uired phase angle
offset ( *5 = re9uired $C offset
t ( *.*.****).46period5
output ( a!plitude 6 sin(46pi6fre9uency6t ? phase) ?
offset5
plot(t,output)5
To eecute the code, do OC1 of the following.
,elect all Dight click 1$aluate selection
%ebug Dun
(ress G<
Type the file name in the !ommand #indow and press )1nter*
Cow, add code to the M-Gile that sets the title to ),ine #a$e %emo,* the -ais label to
)Time &s',* and the y-ais label to )Noltage &N'.*
3dd a title to the plot by entering. title&O,ine #a$e %emo'A
Cow, add a label to the -ais. label&OTime &t''A
3nd a y-ais label. ylabel&ONoltage &N''A ,a$e this plot figure to your disk.
Cow, change the fre:uency to =;;;, and sa$e this new plotA
Cow, you will produce the plots of all combinations of the following fre:uencies and
amplitudes. Gre:uencies. <;;,7;;;, <;;; FJA 3mplitudes. =, >.<, 8.
Fowe$er, there is a way to do this without changing and resa$ing the script for each set
of $alues. Ht would be simpler to enter the abo$e as an argument to the script.
,inewa$e&<;;, ='A Hn order to do this, the script must be configured as a function.
A MatLab function is defined in the follo!ing manner:
function output@+ariable function@na!e(argu!ent),argu!ent4,A)
3dd the following line to the TO( of your file.
function output ( sinewa+e(fre9uency, a!plitude)
!hange )clear allA* to )clear global5B &This pre$ents your argument $ariables
from being deleted.'
Demo$e the lines.
fre9uency ( 4***5 = re9uired fre9uency
a!plitude ( )5 = re9uired a!plitude
Mour code should now look as below.
function output ( sinewa+e(fre9uency, a!plitude)

= EE /*)*
= >eneration of sine wa+e with gi+en fre9uency,
a!plitude, phase, and $C offset.
clear global5
close all5
period ( ),fre9uency5 = re9uired period
phase ( *5 = re9uired phase angle
offset ( *5 = re9uired $C offset
t ( *.*.****).46period5
output ( a!plitude 6 sin(46pi6fre9uency6t ? phase) ?
offset5
plot(t,output)5
title(Sine Ca+e $e!o)5
;label(&i!e (s))5
ylabel(Doltage (D))5

,a$e the function, and try it by entering. sinewa+e(7**,))5
Ht would be con$enient for each plot to ha$e a title identifying it with the gi$e parameters.
1mbedded title and label statements are needed. To make a title that includes the
specified fre:uency and amplitude, we first need to con$ert these numbers to strings, then
concatenate &Poin' them with other strings to make the title.
Cumbers are transformed into strings by the nu!4str() function.
Hn MatLab, ,trings are concatenated in this manner.
string ( 0string), string4, A35
Demo$e the eisting title line, and insert the following code at the end of your function.
title(0S#%E C"DE E 8re9uency ,
nu!4str(fre9uency),H:, "!plitude. ,
nu!4str(a!plitude),D3)5
Mour code should now look as below.
function output ( sinewa+e(fre9uency, a!plitude)

= EE /*)*
= >eneration of sine wa+e with gi+en fre9uency,
a!plitude, phase, and $C offset.
clear global5
close all5
period ( ),fre9uency5 = re9uired period
phase ( *5 = re9uired phase angle
offset ( *5 = re9uired $C offset
t ( *.*.****).46period5
output ( a!plitude 6 sin(46pi6fre9uency6t ? phase) ?
offset5
plot(t,output)5
;label(&i!e (s))5
ylabel(Doltage (D))5
title(0S#%E C"DE E 8re9uency ,
nu!4str(fre9uency),H:, "!plitude. ,
nu!4str(a!plitude),D3)5
Fa$e your T3 $erify that your code works
Cow, using this function, sa$e the nine plots of the fre:uency5amplitude combinations as
specified abo$e.
Cet, enter. )help sinewa$eA* Mou will get something like the below.
00 help sinewa$e
11 7;=;
Qeneration of sine wa$e with gi$en fre:uency, amplitude, phase, and %! offset.
This help tet is generated from the commented lines at the beginning of the script.
1%HT these comment lines to describe to the user what this function does and how to use
it.
Part C. !$uare "ave %eneration
-se what you ha$e learned about MatLab programming to write a function that generates
a s:uare wa$e with user specified fre:uency and amplitude. The title should read
),:uare #a$e %emo,* and gi$e the fre:uency and amplitude entered. The and y
labels should be the same as abo$e. ,a$e plots with combinations of three different
fre:uencies and three different amplitudes. %emonstrate to your T3 that your function
works properly.
&iscussion.
Hnclude your == plots from part ? and your code and L plots from part ? in your &hand in,
reportR' hand in to your T3.
=. #hat did you find easy about this labR #hat did you find hardR
>. %iscuss how you de$eloped the code re:uired for part !, especially gi$ing any
difficulties encountered.
&ata !'eet
Came SSSSSSSSSSSSSSSSSSSSSS ,ection SSSSSSSSSSSSSSSSSSSS

Part A:
? 9 SSSSSSSSSSSSSSSSSSSSSSS &MatLab command'
B A 9


A B 9

E 9

Nerify use of MatLab, not calculator T3 SSSSSSSS


sine(17) 9 SSSSSSSSSSSSSS
sine()7*F) 9 SSSSSSSSSSSSSS
sine()GGG4) 9 SSSSSSSSSSSSSS
Part B
T3 SSSSSSSS
Part C
T3 SSSSSSSS

You might also like