An Introduction to MATLABcsyliu/course/comp319/Lab/Lab1.pdf · An Introduction to MATLAB Lab tutor...
Transcript of An Introduction to MATLABcsyliu/course/comp319/Lab/Lab1.pdf · An Introduction to MATLAB Lab tutor...
Lab 1 of COMP 319Lab 1 of COMP 319
An Introduction to MATLAB
Lab tutor : Dennis Yang LIU
Email: [email protected]
Lab 1: Sept. 11, 2014
1
Introduction to the Lab
Schedule DateLab 1 Sept 11 (Thur ) and Sept 16 (Tue )Lab 1 Sept. 11 (Thur.) and Sept. 16 (Tue.)Lab 2 Sept. 18 (Thur.) and Sept. 23 (Tue.)Lab 3 Sept. 25 (Thur.) and Sept. 30 (Tue.)Lab 4 Oct. 7 (Tue.) and Oct. 9 (Thur.) Lab 5 Oct. 14 (Tue.) and Oct. 16 (Thur.) Lab 6 Oct. 21 (Tue.) and Oct. 23 (Thur.) ( ) ( )Lab 7 Oct. 28 (Tue.) and Oct. 30 (Thur.) Lab 8 Nov. 4 (Tue.) and Nov. 6 (Thur.) L b 9 N 11 (T ) d N 13 (Th )Lab 9 Nov. 11 (Tue.) and Nov. 13 (Thur.)
Lab 10 Nov. 18 (Tue.) and Nov. 20 (Thur.) Lab 11 Nov. 25 (Tue.) and Nov. 27 (Thur.)
4
Lab 12 Dec. 2 (Tue.) and Dec. 4 (Thur.)
Introduction to the Lab
• Time and place of our labs– Thur. 16:30 – 17:20 PQ 604B– Tue. 09:30 – 10:20 PQ 606
Th l b ill i l f M l b• The lab will mainly focus on Matlab– Basic concepts and useful skills of Matlab (6 labs)– Instructions to the lab project (6 labs: instructions and self-studies)p j ( )
• Directory of lab material– http://www4.comp.polyu.edu.hk/~csyliu/course/comp319/main.ht
ml
5
What is Matlab?
• Matlab– MATrix LABoratory
• The name is derived from MATrix LABoratory.
• A programming language for doing numerical computation.
• Originally designed for solving linear algebra type problems using matrices (suitable for image and video processing).
– Functionality of Matlab • built-in functions
• for solving problems requiring data analysis signal processing• for solving problems requiring data analysis, signal processing, optimization, and several other types of scientific computations.
• also contains functions for 2-D and 3-D graphics and animation.
8
What is Matlab – con’t
C id i M tl b t h• Considering Matlab at home
– Standard edition• Available for roughly 2 thousand dollars• Available for roughly 2 thousand dollars
– Student edition• Available for roughly 1 hundred dollars.• Some limitations, such as the allowable size of a matrix
9
Strengths of Matlab
M l b i l i l l• Matlab is relatively easy to learn• Matlab code is optimized to be relatively quick when
performing matrix operationsperforming matrix operations• Matlab may behave like a calculator or as a programming
language• Matlab is interpreted, errors are easier to fix
10
Weaknesses of Matlab
M tl b i i t t d l ( ki it f th t• Matlab is an interpreted language (making it for the most part slower than a compiled language such as C)
• Matlab is NOT a general purpose programming languageMatlab is NOT a general purpose programming language
• Matlab is designed for scientific computation and is not suitable for some things (such as parsing text)g ( p g )
11
Basic manipulation of Matlab
• Opening and closing Matlab
• The Matlab user interface
• M-file building, saving and running
13
Opening and closing Matlab
• Opening Matlab (for Windows XP):Single click ‘Start’ then– Single click Start , then
– Single click ‘Run’, then – Input ‘nalwin32’, then – Double click ‘COMP’, then– Double click ‘Network Application Packages’, then– Double click ‘Statistical & Mathematical’, then,– Double click ‘Matlab’– Select one version (e.g., R2012a), double click to open
14
Opening and closing Matlab
• Opening Matlab (for Windows 7):Single click ‘Start’ then– Single click Start , then
– Single click ‘Computer’, then – Double click disk ‘Y’, then– Double click ‘Win32’, then– Double click ‘Matlab’– Select one version (e.g., R2012a), then( g , ),– Double click ‘matlab.exe’ to open
• Closing MatlabT ‘ ’ ‘ ’ h M l b– Type ‘exit’ or ‘quit’ at the Matlab prompt; or
– Click the close icon ‘x’ in the top-right corner of the screen
15
Matlab user interface – con’t
• Command Window• Command Window– type commands
• Workspace• Workspace– view program variables– ‘clear’ to clear
d bl li k i bl t it i th A Edit– double click on a variable to see it in the Array Editor
• Command Historyi d– view past commands
17
Using the Command Window
• Do it yourself
1. To compute the value of 52, type this command:>> 5^2The following output will be displayed:The following output will be displayed:ans =
25
2. To find the cosine of pi, type:>> cos(pi)which result in the following output:g pans =
-1
18
Note: ‘ans’ is the default variable name given by Matlab.
Using the Command History
• Do it yourself
In the command window, type:
>> clc
This will clear the command window but leave the data in the command history. Try double-click:
cos(pi)
This will result in the following display in the command window:
ans = -1
19
Using the Workspace
• Do it yourself
• Enter the following two-dimensional matrix in the command window:
>> c= [1,2,3,4; 10,20,30,40; 5,10,15,20];
• c should now be listed in the workspace window. • Double clik it An array editor will be listed above the• Double clik it. An array editor will be listed above the
command window. • Then you can add or change values in the array editor.
20
Some useful notes• The Matlab environment is command oriented somewhat like UNIX.
• A prompt (>>) appears on the screen and a Matlab statement can be• A prompt (>>) appears on the screen and a Matlab statement can be entered.
• When the <ENTER> key is pressed, the statement is executed, and th tanother prompt appears.
• If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results will appear before the next prompt.
» a=5;» b=a/2
b =
2.5000
21»
Some useful notes – con’t
• In Matlab, the equal sign ( = ) is used to assign values to the variables. It does not mean ‘equal to’.
• In Matlab environment ‘equal to’ is expressed by two consecutive• In Matlab environment, equal to is expressed by two consecutive equal signs ( == ).
>> x = 5 means that assigning the value 5 to the variable x.
>> x == y means that the value of x is equal to the value of y.
22
Some useful notes – con’t
M tl b V i bl N
• Variable names are case sensitive
Matlab Variable Names
• Variable names can contain up to 63 characters (as of Matlab 6.5 and newer)
• Variable names must start with a letter followed by letters, digits, and underscores.
23
Some useful notes – con’t
• Matlab has some special (reserved) words that you may• Matlab has some special (reserved) words that you may not use…
for switchendifwhile
continueelsetrywhile
functionreturn
trycatchglobal
elseifcaseotherwise
persistentbreak
24
Matlab Help
• Different ways to find information
– help
– help general, help mean, sqrt...
– click ‘help’ iconp
– Search information from the internet via google
25
Some other useful Matlab commands
• who List known variables• whos List known variables plus their sizep• clear x y Clear variables x and y from workspace• clear Clear all variables from workspace• clc Clear the command indo• clc Clear the command window
27
M-file building and saving
• The command window offers an environment similar to a scratch pad.
• Using the command window allows you to save theUsing the command window allows you to save the values you calculate, but not the commands used to generate those values.
• If you want to save the command sequence you will• If you want to save the command sequence, you will need to use the edit window to create an M-file.
28
Explanation of the example
• Line 1: instructs Matlab to delete all variables in the working directorydirectory.
• Line 2: instructs Matlab to clear the command window. Any text that now appears in the command window will be the result of running this M file not the result of previous activitiesrunning this M-file, not the result of previous activities.
• Line 3: blank lines help improve the readability of a M-file.
• Line 4 and 5: Assign values to A and B The semicolon prevents• Line 4 and 5: Assign values to A and B. The semicolon prevents Matlab from displaying the result in the command window; the percent sign begins the legible comment.
• Line 6: Intermediate results with suitable names sometimes• Line 6: Intermediate results with suitable names sometimes improve the legibility of the algorithm.
• Line 7: Invokes the built-in function sqrt() to compute the final lt
32
result.
Run M-file (method 1)• Type the M-file’s name: “hypo” into command window and
then press the “enter” key.
35
Run M-file (method 2: step 2)• Double click the file “hypo”;• Or single click the file “hypo” and single click “open”
37
Summary of Lab 1
• Introduction to the LabIntroduction to the Lab
• Matlab overviewWh t i M tl b?– What is Matlab?
– Strengths and weaknesses of Matlab
• Basic manipulation in Matlab
– Start and quit Matlab
– The Matlab user interface
– M-file building, saving and running
40