14
C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB http://info.circ.rochester.edu/Summer_School_Workshops/Matlab.html

C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

Embed Size (px)

Citation preview

Page 1: C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

CENTER FOR INTEGRATED RESEARCH

COMPUTING

MATLAB

http://info.circ.rochester.edu/Summer_School_Workshops/Matlab.html

Page 2: C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

Outline

Programming in Matlab Data Types Operators Arrays Plotting Control Structures

For loops If statements

Scripts Functions

Page 3: C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

Data Types

Everything in matlab is at least a matrix of rank 2!help datatypeBasic Datatypes

single, double, logical, int8, int16, ..., uint8, uint16, ..., char, complexCells 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

Page 4: C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

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

Page 5: C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

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]

Page 6: C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

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

Page 7: C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

Control structures

for i=1:2:10 sum=sum+i if (i==7) break endend

Adds the numbers 1,3,5,7

Page 8: C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

Control structures

i=1while i < 10 sum=sum+i if (i == 7) break end i=i+2end

Page 9: C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

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

Page 10: C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

Control structures

switch a case 1 sprintf('a==1') case 2 sprintf('a==2') otherwise sprintf('a ~= 1 and a ~= 2')end

Page 11: C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

Mandelbrot Set

The mandelbrot set is the set of numbers 'c' for which the following sequence does not 'blow up' starting with z=0

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. Determine whether the following numbers are in the 'mandelbrot set' [-2, -1, i, 1/2, 1, 2]

Page 12: C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

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)

Page 13: C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

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);

Page 14: C ENTER FOR I NTEGRATED R ESEARCH C OMPUTING MATLAB

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.