kadin tseng scientific computing and visualization group ...• double click on the matlab icon to...
TRANSCRIPT
Summer 2011
Kadin Tseng
Scientific Computing and Visualization Group
Boston University
Summer 2011
• Log on with your BU userid and Kerboros password
• Double click on the MATLAB icon to start MATLAB
• You will see a splash screen as MATLAB is starting
• When a >> prompt appears, MATLAB is ready for action
• MATLAB commands may be entered at >> for execution
• Feel free to try the commands appearing in the subsequent
slides to get a feel for how they work. Be adventurous !
Summer 2011
• It is developed by The Mathworks, Inc. (http://www.mathworks.com)
• It is an interactive, integrated, environment
• for numerical computations
• for symbolic computations (MuPAD)
• for scientific visualizations
• other applications
• For program development and debugging, runs in interactive,
interpreted mode; for production, run in the background, or batch.
• Slow (compared with FORTRAN or C) because it is interpreted.
• Automatic memory management; no need to declare arrays.
• Intuitive, easy to use.
• Compact notations.
• Shorter program development and debugging time than
traditional programming languages such as FORTRAN and C.
• Integrated: computation and visualization.
Summer 2011
Latest version is MATLAB 2011a
For Windows: double click MATLAB icon (on desktop or from “Start”)
For Linux Cluster: katana% matlab
Either case spawns a MATLAB window with >> prompt.
>>
>>
>>
Online help: Mathworks.com/support has additional help materials.
Summer 2011
Variable/Function name
– is case sensitive, NAME and Name are 2 distinct variables.
– variable begins with a , A2z or a2z.
– can be a mix of letters, digits, and underscores ( vector_A)
– up to 63 characters (31 for older versions).
– Cannot have reserved characters (e.g., %, +, -)
File name
– command files should be named with a suffix of ".m",
myfile.m. An m-file typically consists of a sequence of
MATLAB commands that will be executed in order.
Summer 2011
,
• Some characters are by for various purposes.
Some as arithmetic or matrix operators: =, +, -, *, / , \ and others
are used to perform a multitude of operations. Reserved
characters cannot be used in variable or function names.
• >>
>>
• >>
a =
3
• >>
>>
• >>
>>
• >>
d =
6
Summer 2011
• >>
x =
1 3 5 7 9
• >>
y =
3 4 5
• >>
X =
1 2 3
4 5 6
• >>
ans =
6
Summer 2011
>>
>>
x =
1
2
3
>>
ans =
2 3
>>
ans =
6
Summer 2011
• >>
Volume in drive C has no label.
Volume Serial Number is 6860-EA46
Directory of C:\Program Files\MATLAB704\work
01/31/2007 10:56 AM <DIR> .
01/31/2007 10:56 AM <DIR> ..
06/13/2006 12:09 PM 12 foo.exe
06/13/2006 08:57 AM 77 mkcopy.m
06/13/2006 08:53 AM 486 mkcopy.p
• >>
total 0
-rw-r--r-- 1 kadin scv 0 Jan 19 15:53 file1.m
-rw-r--r-- 1 kadin scv 0 Jan 19 15:53 file2.m
-rw-r--r-- 1 kadin scv 0 Jan 19 15:53 file3.m
>>
Summer 2011
>>
c =
5 7 9
>>
A =
1 2 3
4 5 6
>>
B =
1 4
2 5
3 6
Other ways to create B ? (hint: with a, b, )
Summer 2011
>>
C =
14 32
32 77
>>
D =
1 4 9
16 25 36
>>
E =
1 1 1
1 1 1
>>
Your variables are:
A B C D E a b d
Summer 2011
>>
Name Size Bytes Class
A 2x3 48 double array
B 3x2 48 double array
C 2x2 32 double array
D 2x3 48 double array
E 2x3 48 double array
a 1x3 24 double array
b 1x3 24 double array
c 1x3 24 double array
Grand total is 37 elements using 296 bytes
>>
Name Size Bytes Class
A 2x3 24 single array
>>
Utilities to initialize or define arrays:
Trigonometric and hyperbolic functions :
These utilities can be used on scalar or vector inputs
>>
Spring 2011
Scalar operation . . .
use utility to generate a random number and
save in
Equivalent vector operations . . .
A is a 3x3 random number double array
Initialize as a 3x3 array of zeroes
% set to all elements of with corresponding
Spring 2011
Purpose of cell array is to collect arrays of
different sizes and data types into a single entity.
>>
C =
[2x2 double] [1x3 double]
[2x1 double] 'this is a string'
{1x1 cell } [ ]
>>
ans =
1 3
4 2
>>
Related utilities:
Spring 2011
Purpose : to store arrays with mixed types and sizes, similar to cell.
>>
name =
1x2 struct array with fields:
last
first
sex
age
>>
ans =
Linda
ans =
John
Related utilities:
Spring 2011
Summer 2011
There are many types of files in MATLAB. Only script-, function-,
and mat-
files are covered here:
1. script m-files (.m) -- group of commands; all I/O in base
workspace
2. function m-files (.m) -- like script m-file but encapsulated;
parameters passed as input, output arguments; own workspace
3. mat files (.mat) -- binary (or text) files generated typically with
4. mex files (.mex) -- runs C/FORTRAN codes from m-file
5. eng files (.eng) -- runs m-file from C/FORTRAN code
6. C codes (.c) – C codes generated by MATLAB compiler
7. P codes (.p) – converted m-files to hide source for security
Summer 2011
This type of m-files is useful for automating a group of MATLAB
commands that are used repeatedly. All variables stay in the base
workspace.
• The following script m-file mytrig.m contains these commands
• To use it :
>>
a = 0.70711
b = 0.70711
• Pros:
• all variables stay in base workspace; they are readily accessible
• Cons:
• each time the script is used, each command is loaded and interpreted
(slow)
• new value will cause and to be overwritten
• risks of variables overridden unintentionally; lack of modularity
Summer 2011
• It is declared with the key word “function”. It takes input and/or
output variables. All variables within function reside in its own
workspace.
Use MATLAB editor to create file: >> edit average.m
Save the above with File/Save
•
• It may be called from a script, a function, or on command line:
• >>
a =
2
Summer 2011
end
• MATLAB passes input arguments either “by value” or “by
reference” (lazy copy).
• Contiguous statements beginning with % immediately after the
function statement serves as help doc for function.
>> help average
• Always strive to extend the range of applications of your
function.
• Here, computes the sum of a vector or matrix; how would
you extend the capability to handle multi-dimensional arrays ?
• Internally, MATLAB treats all arrays as one-dimensional. Better
to replace sum(x) with sum(x(:)). This works regardless of # of
dims.
Summer 2011
• Multiple output should be enclosed in [ ],
• Pros:
– Scope of function’s variables is confined to within
function. No worry for name conflict with those outside of
function.
– What comes in and goes out are highly controllable.
– Compiled the first time it is used; runs faster next time.
– Easily be deployed in another project.
– Auto cleaning of temporary variables.
• Cons:
– I/O are highly regulated, if the function requires many pre-
defined variables, it is cumbersome to pass in and out of
the function – a script m-file is more convenient.
Summer 2011
On occasions, one wants to comment out an entire block of
lines.
:
• Select statement block with mouse, then
– press Ctrl r keys to insert % to column 1 of each line.
– press Ctrl t keys to remove % on column 1 of each line.
• %{
n = 3000;
x = rand(n);
%}
• if 0
n = 3000;
x = rand(n);
end
Summer 2011
>>
Summer 2011
• Line plot
• Bar graph
• Surface plot
• Contour plot
• Animation (movies)
Summer 2011
Summer 2011
Summer 2011
Summer 2011
Generally, MATLAB’s default graphical settings are adequate which make
plotting fairly effortless. For more customized effects, use the and
commands to change the behavior of specific rendering properties.
Summer 2011
Summer 2011
• To add a legend, either use the legend command or via insert in the
Menu Bar on the figure. Many other actions are available in Tools.
• It is convenient to use the Menu Bar to change a figure’s properties
interactively. However, the set command is handy for background
processing, such as with an m-file. Also, set is better if you need to
perform property changes to multiple figures in the background.
• Similarly, save a graph via the Menu Bar’s File / Save as or
>> print –djpeg 'mybar' % file mybar.jpg saved in current dir
Summer 2011
Many MATLAB utilities are available in both command and function
forms.
For this example, both forms produce the same effect:
For this example, the command form yields an unintentional
outcome:
>>
Other frequently used utilities that are available in both forms are:
Summer 2011
Try these commands also:
Summer 2011
Summer 2011
If you see this error:
??? Error using ==> movie
Could not create movie frame
Translation: it means you don’t have enough memory.
Summer 2011
There are a number of ways:
• Buy your own student version for $99.
http://www.bu.edu/tech/desktop/site-licensed-
software/mathsci/matlab/faqs/#student
• Check your own department to see if there is a computer
lab with installed MATLAB
• With a valid BU userid, the engineering grid will let you
gain access remotely.
http://collaborate.bu.edu/moin/GridInstructions
If you have a Mac, Windows PC or laptop, you may have to
sync it with Active Directory (AD) first:
http://www.bu.edu/tech/accounts/remote/away/ad/
• acs-linux.bu.edu, katana.bu.edu
http://www.bu.edu/tech/desktop/site-licensed-
software/mathsci/mathematica/student-resources-at-bu
http://scv.bu.edu/survey/tutorial_evaluation.html
(www.bu.edu/tech/research)
www.bu.edu/tech/accounts/special/research/accounts
–
•
– Web-based tutorials
(www.bu.edu/tech/research/training/tutorials)
(MPI, OpenMP, MATLAB, IDL, Graphics tools)
– HPC consultations by appointment
• Kadin Tseng ([email protected])
• Doug Sondak ([email protected])
Summer 2011