Introduction To Matlab: Kadin Tseng Boston University Scientific Computing and Visualization
Introduction To Matlab: Kadin Tseng Boston University Scientific Computing and Visualization
MATLAB
Kadin Tseng
Boston University
Scientific Computing and Visualization
Introduction to MATLAB
2
It is developed by The Mathworks, Inc. (http://www.mathworks.com)
>>
>>
>>
>>
>>
>>
Introduction to MATLAB
4
Variables
case sensitive, NAME and Name are 2 distinct names.
• variable begins with a , A2z or a2z
• can be a mix of letters, digits, and underscores ( vector_A)
• reserved characters: % = + – ~ ; : ! [] () , @ # $& ^
• up to 63 characters
• Commands/Functions/scripts
• performs specific tasks; same naming rules as for variables
File names
• command files should be named with a suffix of ".m",
. An m-file typically contains a sequence of MATLAB
commands that will be executed in order
• A file may contain a collection of commands, functions
• A file may also be just data (strings, numbers
Introduction to MATLAB
5
,
• 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. They may have multiple uses.
• >>
>>
• >>
a=
3
• >>
>>
• >>
>>
• >>
d=
6
Introduction to MATLAB
6
• >>
x=
1 3 5 7 9
• >>
y=
3 4 5
• >>
X=
1 2 3
4 5 6
• >>
ans =
6
Introduction to MATLAB
7
>>
>>
>>
x=
1
2
3
>>
ans =
2 3
>>
ans =
6
Introduction to MATLAB
8
• >>
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
• >>
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
>>
Introduction to MATLAB
9
>>
>>
>>
5 7 9
>>
A=
1 2 3
4 5 6
>>
B=
1 4
2 5
3 6
Other ways to create B ? (hint: with and )
Introduction to MATLAB
10
>>
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
Introduction to MATLAB
11
>>
Name Size Bytes Class Attributes
A 2x3 48 double
B 3x2 48 double
C 2x2 32 double
D 2x3 48 double
E 2x3 48 double
a 1x3 24 double
b 1x3 24 double
c 1x3 24 double
>>
>>
Name Size Bytes Class
A 2x3 24 single
>>
Introduction to MATLAB
12
>>
Introduction to MATLAB
13
Scalar operation . . .
for j=1:3 % columns
for i=1:3 % rows
a(i,j) = rand; % a(i,j) = random number
b(i,j) = 0; % b(i,j) = 0 unless . . .
if a(i,j) > 0.5
b(i,j) = 1;
end
end
end
Related utilities:
Introduction to MATLAB
16
There are many types of files in MATLAB.
Only script-, function-, and mat-files are covered here:
• Other frequently used utilities that are available in both forms are:
• save, load
Introduction to MATLAB
28
>> Z = peaks; % generate data for plot; peaks returns function values
>> surf(Z) % surface plot of Z
m a ih m
a a ( i 1) h
i 1 i 1
mid-point of increment
cos(x) ; % range
; % # of increments
h ; % increment
Introduction to MATLAB
31
% integration with for-loop
tic
m = 100;
a = 0; % lower limit of integration
b = pi/2; % upper limit of integration
h = (b – a)/m; % increment length
integral = 0; % initialize integral
for i=1:m
x = a+(i-0.5)*h; % mid-point of increment i
integral = integral + cos(x)*h;
end
toc
a b
X(1) = a + h/2 X(m) = b - h/2
Introduction to MATLAB
32
% integration with vector form
tic
m = 100;
a = 0; % lower limit of integration
b = pi/2; % upper limit of integration
h = (b – a)/m; % increment length
x = a+h/2:h:b-h/2; % mid-point of m increments
integral = sum(cos(x))*h;
toc
a b
X(1) = a + h/2 X(m) = b - h/2
Introduction to MATLAB
33
1. Write a program (with editor) to generate the figure that describe the
integration scheme we discussed. (Hint: use plot to plot the cosine curve.
Use bar to draw the rectangles that depict the integrated value for each
interval. Save as plotIntegral.m
2. Compute the cosine integrals, from 0 to pi/2, using 10 different increment
sizes (10, 20, 30, . . . , 100). Plot these 10 values to see how the solution
converges to the analytical value of 1.
Introduction to MATLAB
34
a = 0; b=pi/2; % lower and upper limits of integration
m = 8; % number of increments
h = (b-a)/m; % increment size
x= a+h/2:h:b-h/2; % m mid-points
bh = bar(x,cos(x),1,'c'); % make bar chart with bars full width (1) and cyan (‘c’)
hold % all plots will be superposed on same figure
x = a:h/10:b; % use more points at which to evaluate cosine
f = cos(x); % compute cosine at x
ph = plot(x,f,'r'); % plots x vs f, in red
% Compute integral with different values of m to study convergence
for i=1:10
n(i) = 10+(i-1)*10;
h = (b-a)/n(i);
x = a+h/2:h:b-h/2;
integral(i) = sum(cos(x)*h);
end
figure % create a new figure
plot(n, integral)
Introduction to MATLAB
35
(www.bu.edu/tech/research)
www.bu.edu/tech/accounts/special/research/accounts
•
•
• Web-based tutorials
(www.bu.edu/tech/about/research/training/live-tutorials)
(MPI, OpenMP, MATLAB, IDL, Graphics tools)
• HPC consultations by appointment
• Kadin Tseng (kadin@bu.edu)
• Yann Tambouret (yannpaul@bu.edu)