MATlab

advertisement
MATLAB
CENTER
FOR
INTEGRATED RESEARCH
COMPUTING
http://info.circ.rochester.edu/Summer_School_Workshops/Matlab.html
Outline
Programming in Matlab
 Data Types
 Operators
 Arrays
 Plotting
 Control Structures
 For loops
 If statements
 Scripts
 Functions
Data Types
 Everything in matlab is at least a matrix of rank 2!
 help datatype
 Basic Datatypes

single, double, logical, int8, int16, ..., uint8, uint16, ..., char, complex
 Cells are arrays without a uniform type


a={1,[2,3,4],'hi'}
a{2}
 Structs have components referenced by name


card.suit='diamonds'
card.rank=8
 There are routines for converting between struct arrays, cell arrays,
and numeric arrays – cell2mat, struct2mat, num2cell
Arrays
 Arrays can be indexed using a single number





a=[1,2,3;4,5,6]
size(a)=[2,3]
a(row,column)
a(1,2)=2
a(2)=4
 Matlab is column major order – (columns are contiguous
in memory). The constructors are 'row major'

In memory 'a' looks like 1 4 2 5 3 6
Arrays
 Arrays can be constructed using









a=zeros(10,10) – or zeros(10)
a=rand(10,10) – or rand(10)
a=zeros(1,10) will give a 10 element 'vector'
a=[1,0,0;0,1,0;0,0,1] – 3x3 identity matrix
a=[1:100] – [1,2,3,4,5,6,7,8,9,...,100]
a=[0:.1:1] – [0,.1,.2,.3,.4,.5,.6,.7,.8,.9,1.0]
a=cat(3,[1,3;2,4],[5,7;6,8]) – [concatenates 2D matrices along 3rd dimension]
size(a)=[2,2,2]
size(size(a))=[1,3]
Operators
 +, -, ./, .*, .^
 .^, .*, ./ is element wise exponentiation, multiplication, and division.







(^, *, and / are reserved for matrix exponentiation, multiplication,
and division). For scalars (matrix of size 1x1) it doesn't matter.
.' is transpose
' is transposed conjugate
<, >, <=, >=, ==, ~=
~ is logical negation
& is logical and
| is logical or
&& and || are 'short-circuit' versions. Stops evaluations if possible
Control structures
for i=1:2:10
sum=sum+i
if (i==7)
break
end
end
Adds the numbers 1,3,5,7
Control structures
i=1
while i < 10
sum=sum+i
if (i == 7)
break
end
i=i+2
end
Control structures
if (a==b)
printf('%f == %f', a, b)
elseif (a < b)
printf('%f > %f', a, b)
else %(a > b)
printf('%f > %f', a, b)
end
Control structures
switch a
case 1
sprintf('a==1')
case 2
sprintf('a==2')
otherwise
sprintf('a ~= 1 and a ~= 2')
end
Mandelbrot Set
 The mandelbrot set is the set of numbers 'c' for which the
following sequence does not 'blow up' starting with z=0
2
n+1
n
 Write a program to calculate whether a given number is in
the mandelbrot set or not – and whether the sequence
reaches 1e6 in 1000 iterations.
z


= z +c
Determine whether the following numbers are in the 'mandelbrot set'
[-2, -1, i, 1/2, 1, 2]
Plotting
 You can plot vectors using

plot(x)
 You can make a scatter plot of two vectors using

plot(x,y)
 You can also an 'array of vectors' using

plot(A)
 Or you can plot a matrix using imagesc(A)
Functions and Scripts
 Matlab scripts do not take inputs or return outputs (except by writing to standard out




– or a data file etc...)
Usually a single script will call functions, which in turn will call other functions, etc...
Both scripts and functions are stored in '.m' files
Functions have their own 'workspaces' so variables in the parent function are not
available to the called functions unless explicitly declared global or passed as a
function argument.
Functions are usually stored in files with the same name. For example, fact.m would
contain
function f = fact(n)
f = prod(1:n);
Exercise 2
 Modify your mandelbrot script to call a mandelbrot function
which returns the number of iterations required to reach a
magnitude of 1e6 – (or the maximum number of iterations if the
sequence does not diverge)
 Re run your program and report the number of iterations
required to diverge for the same set of numbers
 For each pair of x=[-1.99:.02:1.99] and y=[-1.99:.02:1.99]
calculate the divergence rate for the complex number x+iy and
store the result in a 200 x 200 matrix A and plot the result.
Download