Introduction to Matlab: Features and Program Structure
Matlab offers a simple calculator-like syntax with a strong library of functions for numerical analysis and extensive toolboxes for various fields. It is a universally recognized language in engineering with open-source options available. The program structure includes functions, conditionals, looping, and mathematical functions like trigonometry, hyperbolic, and special functions.
Download Presentation
Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. Download presentation by click this link. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
E N D
Presentation Transcript
L02-Introduction to Matlab Christopher Crawford PHY 404G 2020-08-17
Features of Matlab Simple calculator-like syntax with minimum boilerplate Almost everything is a matrix of complex doubles Powerful library of builtin functions for numerical analysis Extensive toolbox for application-specific fields Universally recognized language of engineering Open-source clones and cloud versions available 2020-08-17 2/27
Punctuation [ a, b; c, d] a(1,2) f(x,y) S={1,'s'}; S{1,2} 1:2:10 a(:,2) end .*'.' x.a x.('a') @(x)x^2 fn1, fn2 fn1; fn2; 'str'"str" 1.6e-19 pi, e, i, j, eps ans % comment !pwd Matrix concatenation (space=',' and newline=';') Matrix indexing, function evaluation Cell array and indexing (heterogenous elements) Range, [whole] subrange, largest index value Element operation, transpose, structure member Anonymous (unnamed) function Command separator, ; silences output Command continuation to next line String constants (either ' or " ) Numeric constant in scientific notation Numeric constants: 3.14.., 2.71.., sqrt(-1), 2e-52 Result of previous calculation First comment block used for 'help' documentation Shell (system) command 2020-08-17 3/27
Operator precedence () .' .^ ' ^ + - ! * / \ + - : < <= > >= == ~= Comparison & | && || Parentheses (manual precedence) Transpose, Power, Hermitian transpose, Matrix power Sign, Negative, Logical negation Multiplication, Right division, Left division Addition, Subtraction Colon operator (range) Element-wise AND Element-wise OR Short-circuit AND Short-circuit OR 2020-08-17 4/27
Program structure function [y1 y2 ] = myfun (x1, x2, ) nargin/out, varargin/out{} z=x1+x2 y1=z; y2=z^2 end SUBROUTINE % variable # of arguments % local variable % output variables % saved in file myfun.m CONDITIONAL if a==b a=a+1 elseif a>b a=a-1 end LOOPING for k=1:5 a(k)=2*k end switch k case 1 a=a+1 case 2 a=a+2 otherwise a=0 end k=5 while k>0 k=k-1 end break, continue 2020-08-17 5/27
Functions Mathematics cos(x), sin, tan, acos, asin, atan - trig atan2(y,x) - knows the quadrant cosd(x), - angles in degrees cosh(x), - hyperbolic functions exp(x), log(x), log10(x), log2(x) - exponent sqrt(x) - square root abs(x) - magnitude (absolute val.) sign(x) - x/abs(x) airy(n,x) - Airy function besselX(n,x) - Bessel func. X=j,y,i,k,h erf, erfc, erfcx, erfinv gamma(x) - ? = ? 1 ! expint(x) - exponential integral legendre(n,x) - Legendre polynomials factorial(x) - factorial Matrices eye, zeros, ones, diag, magic - constructor rand, randn, randperm - random, normal size, length, numel, rows, cols, ndims reshape, squeeze - repack matrix flipud, fliplr, circshift, shiftdim - elements dot, cross, kron - products, (Kronecker) norm, vecnorm - magnitude (cols) inv, det, trace - inverse, determinant, trace [v,d]=eig(a) - eigenvectors, eigenvalues [u,v,d]=svd(a) - singular value decomp QR, LU, rref, rcond, cond - other decomp fft, ifft - fast Fourier transform, inverse conv , deconv, xcor - convolution (+poly) sum, max, min, median, mean, std stats is{matrix,vector,scalar,empty,column,row} - error functions 2020-08-17 6/27
Functions Conversion, Text I/O ceil, floor, round, fix - real to integer real, imag, angle, abs, conj - complex clear , cls, clf - clear vars, screen, figure load, save, {csv,dlm,xls}read/write - files fread/fwrite - raw binary file I/O printf, fprintf, disp - print output input, textscan - read input char - convert ASCII to char strfind, strsplit, strjoin, strcat, replace, contains, cellstr, regexp - string fns num2string, int2str, mat2str, str2num cell2struct, struct2cell - conversions cell2mat, mat2cell - partitioned matrix num2cell - matrix/rows/cols to cell array Plotting, Analysis meshgrid, ndgrid, linspace - domain X,Y plot, plot3, hist, bar, pie, area, contour, quiver, scatter, compass, rose, semilogx, loglog, stem, stairs, image, imagesc - plots axis, title, x/y/zlabel, legend, view - format figure, subplot, clf, saveas - plot window line, fill, text, rectangle - annotations sort, issorted, sortrows - sort array find - find elements of array fminsearch - find minimum of function fzero - find the zeros of function interp1 - interpolate X,Y arrays diff, gradient, trapz, quad - calculus ode{23,45,113,23s,15s} - ODE integrator 2020-08-17 7/27