MatLab 2008-1m

advertisement
The Hong Kong Polytechnic University
Industrial Centre
MatLAB
Lesson 1 : Overview & Environment
Edward Cheung
email: icec@polyu.edu.hk
Room W311g
2008
1
Introduction
•
•
•
•
•
MatLAB stands for Matrix Laboratory
A Scientific calculator
A programming language
Good in Matrix calculation and graph plotting
written in Fortran and rewritten in C
 common programming languages in Science & Engineering
 MatLAB becomes a standard tool for scientists & engineers
• Other computational tools; Maple, Mathematica, MathCad
• Only Minor change since MatLAB 6
• Latest version is MatLab ver.7 (R2008a/b)
2
What we’ll cover?
Topic
Lesson
1
Overview & Development Environment
2
Mat rix Comput ing
3
Graphics
4
Polynomials
5
Symbolic Mat hemat ics & file I/O
6
M-file Programming & Funct ions
7
M-file Programming Cont rol & Debugging
8
GUI
9
GUI
10
Q & A, Quiz & Report
3
Start Up – UI, Windows & Environment
• Interactive, interpretative & programmable
• Command, command history & workspace window
• Transfer any command from the command history window
to the command window by double clicking (also executes
the command) or by click & drag
• Launch Pad shows all available products and launch them
• Workspace window keep track the variables defined in the
programme
• MatLAB 7 provided more information on variable like
size, bytes, Max, Min, etc. in addition to Name, Value and
Class (right click at the Workspace Browser)
• The default data type use in calculation is double precision
floating point numbers
4
Workspace Browser
• Data is not save on exit unless execute “save Workspace
As” from the File Menu
• Or using the save function to save all to a MAT-file
>>save
% by default, stores all workspace variables
in a binary format in the current directory
in a file named matlab.mat
• Can also save in ASCII format (readable but larger in size)
>> Save filename.dat {variables} –ascii
>> help save
5
Some keys
• Arrow key to run previous command
• clear screen of command window while keeping history
>> clc
>> clear % delete all variables
 Command History Window can be cleared from Edit Menu
• Who & whos
 give variable information in the command window
• Double clicking on a variable listed in the workspace
window launches a document window called
 the array editor
• A semicolon (;) at the end of a command will suppress the
output, try
>>x=9; x=x+9
6
Keywords
• You can redefine keywords
>iskeyword
% list reserved names
• You can redefine function as variables
• Example
>> sin=3;
>> sin(90)
??? Index exceeds matrix dimensions.
>> which sin
% check if sin is a function
sin is a variable.
>> clear sin
% reset sin back to a function
>> which sin % check again
sin is a built-in function.
• Most easy overlook is redefining i & j
7
Helpful Commands
>> Help % give help topics
>> Lookfor %Search all M-files for keyword
• Try
>> help format
>> format compact % no linefeed
>> format loose % default with linefeed
>> format long % fixed-pt 15 digits
>> format rat % rational format
>> format blank % 2 decimal places
 e.g. Get pi with the above format
• No matter what format is selected, calculation are
performed using double precision floating point numbers.
>> format
% reset to format default
8
Variables
• Variables are created with
>> variable = expression
• Type of variables




Scalars
Vectors
Matrices
Strings
9
Variables
• All name must starts with a letter
• No special character except underscore, case sensitive X≠x
• Convention – lowercase for variable & uppercase for
constants but there are exceptions (eg speed of light=c)
• MatLab 6 max length 31 characters
 Namelengthmax = 63 (ver. 7 function)
• isvariablename – check the validity of variable name
(boolean T=1; F=0), for example:>> isvarname polyu
>> isvarname poly-u
>> isvarname poly_u
10
Scalar Variables
• A variable with one row and one column
• Creating scalars
>> A=1;
%creates a scaler
>> B=1:10; % creates a vector
>> C=[1,2;3,4]; creates a 2D matrix
• end can be use to terminate a function or indicate last
array index
>> iskeyword end
ans =
1
>> x=[2 3 4];
>> x(end+2)=7
x =
2
3
4
0
7
11
Scalar Operations
>>
>>
>>
>>
>>
>>
>>
>>
x=5;
y=3;
z=x+y;
s=x-y;
a=x*y;
d=x/y;
d=y\x;
p=x^y;
12
Vector
• A vector can be a column matrix (nx1)or row matrix (1xn)
• Helpful function in vector creation
• Ones() & Zeros()
>> x=ones(1,5)
x = 1
1
1
>> x=zeros(5,1)
x =
0
0
0
0
0
1
1
13
Create vector with scaling between elements
>> x=linspace(1,5,5)
x =
1
2
3
4
5
>> y=logspace(1,5,5)
y =
10 100 1000 10000 100000
>> y(3)
% addressing vector elements
ans = 1000
>> x(7)=-9
x = 1 2 3
% increase the size of a vector
4 5 0 -9
Array operators:
Dot multiply or dot star .* -array multiplication
dot divide or dot slash ./ -array division
Dot power or dot caret
.^ -array power
14
The colon operator
• Create vector from a matrix
>>x= xbegin:dx:xend
Xbegin & xend = begin & end value of x
dx = optional increment (default is 1)
• Example:>> x=1.1:5.1
x = 1.1000 2.1000
3.1000
4.1000
5.1000
• Transpose operator creates a column vector
>> x=(1:5)'
x =
1
2
3
4
5
>> b=0:2:10 % a vector starts at 0 ended at 10 with an increment 2
15
Matrices
• In MatLab, a matrix is a variable with more than 1 row and 1 column
 Variables with a dimension 1x1 is a scalar
• Example:- create a 2x2 matrix and assigned to variable A
>> A=[1 2;3 4]
A =
1
2
3
4
(looks like a table)
>> A=[1 2 3;4 5 6;7 8 9]
A =
1
2
3
4
5
6
7
8
9
>> A(2,3)
ans =
6
16
Matrix Sum
>> lookfor sum
TRACE Sum of diagonal elements.
CUMSUM Cumulative sum of elements.
SUM Sum of elements.
SUMMER Shades of green and yellow colormap.
……….
>> help sum
SUM Sum of elements.
For vectors, SUM(X) is the sum of the elements of X. For
matrices, SUM(X) is a row vector with the sum over each
column. ………
17
String
>> a='polyu';
>> b=' ';
>> c='ic';
>> [a b c]
ans =
polyu ic
>> disp(a)
polyu
>> % display array
>> disp('Solution 1.1')
Solution 1.1
18
MatLab File Types
• M-file
 plain ASCII text that is interpreted at run time
 parsed once and "just-in-time" compiled
 best for development as well as platform independency
• File Menu  New  M-file
 % = comment statement
>> what % give M-files & Mat files in current directory
• Filename restriction
 No special character including space except underscore
‘_’
19
MatLab File Types
• Pcode file
 a preparsed and encoded version of the M-file
 Fast function load time; for large file, this is important
 hide the source code from others.
 Cannot convert Pcode back to the M-file source
 Pcode is also platform independent
• MEX file
 native C or C++ files that are dynamically linked directly into the
MATLAB application at runtime.
 Need to be compiled for different system hardware
 Fastest runtime but can crash the MatLab application
20
Download