close

Вход

Забыли?

вход по аккаунту

?

MATlab

код для вставкиСкачать
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.
Документ
Категория
Без категории
Просмотров
11
Размер файла
120 Кб
Теги
1/--страниц
Пожаловаться на содержимое документа