You are on page 1of 3

Expressions: + (addition) - (substrction) * (multiplication) / (division) \ (left division of matrix)

^ (power) ‘ (complex conjugate transpose)

help elfun % list all elementary math functions


help specfun % list specific math functions
help elmat % list matrix functions

v = [1, 3, 5] % define vector v and print output


e=[0.1 0.2 0.4 0.6 0.8]; % define vector e but no print out
x = 0: 2: 8; % Generate vectors x giving [ 0 2 4 6 8]
x = linspace(a,b,n) % returns a vector with n elements in the interval [a,b]
x = logspace(a,b,n) % returns a vector with n elements in the interval[10a,10b]
A = [1 2 3; 4 5 6; 7 8 9] % generates a matrix by input
If an ASCII-file A.dat is created by an editor and has in it:
1 4 5
4 2 9
load A.dat A % load the file into matrix A
save B.dat A % saves the matrix A into file B.dat

A, B, C, D are matrices
A=eye (3) % generate a 3x3 Identity Matrix
B=zeros(2,4) % generate a 2x4 Zero matrix
C=ones(3,2) % generate a 3x2 matrix with one assigned to all elements

A’ % gives the Transpose of matrix A


A(1,3) % gives the (1,3) element of matrix A
V=A(1:4,2) % takes the elements (1,2),(2,2),(3,2),(4,2) from matrix A to form vector V
V=A(:,3) % takes the column 3 of matrix A to form vector V
V=A(:,end) % takes the last column of matrix A to form vector V
D=[A C] % concatenate matrix A & C (append the columns of C to the columns of A)
D=[A;C-2] % subtract 2 from each element of C and append it as the bottom rows of A
D=[A C; A-2 C] % concatenate to form new matrix
D(:,2) = [] % delete the second column of matrix D

sum(A) % gives the sum of each column of matrix A


diag(A) % gives the diagonal of matrix A
det(A) % return the determinant of matrix A
inv(A) % return the inverse of matrix A
eig(A) % return the eigen values of matrix A
poly(A) % returns the coefficients of the characteristic polynomial det(A-λI)

A*B % multiply matrix A by matrix B


A.*B % (i,j) element of the product matrix is A(i,j)*B(i,j)
A./B % (i,j) element of the result matrix is A(i,j)/B(i,j)
A.\B % element-by-element left division
A.^n % element-by-element raised to power n
Mathematical Functions:

abs(x)
sign(x)
sqrt(x)
f
pow2(x,f)=x*2
exp(x) log(x) log10(x) log2(x)
sin(x) asin(x) csc(x) acsc(x)
cos(x) acos(x) sec(x) asec(x)
tan(x) atan(x) atan2(x,y) cot(x) acot(x)
sinh(x) asinh(x) csch(x) acsch(x)
cosh(x) acosh(x) sech(x) asech(x)
tanh(x) atanh(x) coth(x) acoth(x)

round(x) gives the integer closest to x


fix(x) gives the integer closest to x in the direction towards zero
floor(x) gives the closest integer below x
ceil(x) gives the closest integer above x
rem(x,y) gives the remainder of the integer division x/y
rat(x) gives the continued fraction representation of x

complex numbers:
real(z) imag(z) abs(z) conj(z) angle(z)

Special Math Functions:


legendre(n,x) returns a n+1 vector of Assoc Legendre Functions of degree n (n=0,…,n) at x.
bessel(n,x) returns Bessel functions of the first kind
bessely(n,x) gives Bessel functions of the second kind
gamma(x)
gammainc(x,a) Incomplete Gamma Function
gammaln(x) natural log of the Gamma Function
erf(x) Error Function
erfinv(y) Inverse Error Function
erfc(x) 1-erf(x)

Signal Processing:
fft(x)
ifft(x) Inverse Fourier Transform
fft2(A) 2D FFT of matrix A
ifft2(A) Inverse 2D FFT
filter(b,a,x)
fftshift(x) shift the upper half of the series to become the lower part
PLOTTING
errorbar(x,y,e); % plots v vs x with error bars given by vector e
title(‘TEST PLOT’) % add a title centered at the top
xlabel(‘Time (sec)’); % add the x label
ylabel(‘Height (m)’); % add the y label
text(4,-1,’Wind’); % add text ‘Wind’ at position (x= 4, y= -1)
axis([xmin,xmax,ymin,ymax]); % set axis limits
axis on / off % set axis visibility on (or off)

plot(x,y,’color_style_marker’) ); % plots vector y versus vector x


where ’color_style_marker’ is a string containing 1-4 characters e.g. ‘r:+’ or ‘bs’
color: ‘c’=cyan, ‘m’=magenta, ‘y’=yellow, ’r’=red, ‘g’=green, ‘b’=blue, ‘w’=white, ‘k’=black
line style: ‘-‘=solid, ‘—‘=dash, ‘:’=dotted, ‘-.’=dash-dot, ‘none’=no line
marker: ‘+’ ‘o’ ‘*’ ‘x’ ‘s’=square, ‘d’=diamond , ‘^’=up triangle, ‘v’ ‘>’ ‘<’ for down,left &
right triangles, ‘p’=pentagram, ‘h’=hexagram, ‘none’ for no marker
hold on % add another plot to the graph
hold off % finish with the graph
subplot(n,m,p) % partition the window into n by m matrix of subplots, and select the pth
subplot
clf reset % clearing figure for new plot

contour(x,y,Z,n,’k’) % draw n black contour lines on surface Z with (x,y) coordinates


pcolor(x,y,Z) % ‘checker-board’ plot where color is dependent on the value
shading interp % interpolate the shading

C=contour(Z,p); % draws contour lines for levels specified in vector p


clabel(C,y); % label contours for levels specified in vector y

surf(x,y,Z); % draws a surface graph of Z with coordinates given by vectors (x,y)


surfc(x,y,Z); % same as above except that a contour plot is also drawn
view([6, 4 ,5]); % set the viewer at position [x=6, y=4, z=5]
s=[6,1,-4]; % set the light source position
surfl(x1,y1,Z,s) % does the same as surf(…) but put a light in the direction s
colorbar % add color bar

mesh(x,y,Z); % draw a mesh of the surface


alpha(r); % make surface transparent to varying degree r (0< r <1)
hidden off % transparent mesh

You might also like