MATLAB Fundamentals - Cheat Sheet - Tools Course ETH Z Urich
MATLAB Fundamentals - Cheat Sheet - Tools Course ETH Z Urich
Cheat Sheet for MATLAB Tools Course at ETH Zürich, by Res Jöhr
Graphics Relational and logical operations Interpolation and fitting
Plotting == Check equality ∼= Check inequality interp1(x,v,xq) 1-D data interpolation (table lookup)
plot(x,y) Plot y vs. x > greater than >= greater or equal to interp2(X,Y,V,Xq,Yq) 2D interpolation for meshgrid data
axis equal Scale axes equally < less than <= less or equal to interp3(X,..V,..Zq) 3D interpolation for meshgrid data
title(’A Title’) Add title to the plot &, && logical AND ∼ logical NOT pchip(x,v,xq) Piecew. cubic Hermite poly interpol
xlabel(’x axis’) Add label to the x axis |, | | logical OR xor logical exclusive-OR spline(x,v,xq) Cubic spline data interpolation
ylabel(’y axis’) Add label to the y axis if, elseif Conditions ppval(pp,xq) Evaluate piecewise polynomial
legend(’foo’, ’bar’) Label 2 curves for the plot mkpp(breaks,coeffs) Make piecewise polynomial
if n<10
grid Add a grid to the plot disp('n smaller 10')
unmkpp(pp) Extract piecewise polynomial details
hold on / off Multiple plots on single figure elseif n<20
disp('n between 10 and 20')
xlim /ylim / zlim get or set axes range else
Differential equations
figure Start a new plot disp('n larger than 20') ode45(ode,tspan,y0) Solve system of nonstiff ODE
Plot types end % control structures terminate with end ode15s(ode,tspan,y0) Solve system of stiff ODE
Switch Case pdepe(m,pde,ic,bc,xm,ts) Solve 1D PDEs
pdeval(m,xmesh,usol,xq) Interpolate num. PDE solution
n = input('Enter a number: ');
switch n
case -1 Optimization
disp('negative one') fminbnd(fun,x1,x2) Find minimum of fun(x) in [x1 , x2 ]
case 0
disp('zero') fminsearch(fun,x0) Find minimum of function
case {1,2,3} %check three cases together lsqnonneg(C,d) Solve non-neg. lin. least-squares prob.
disp('positive one')
otherwise
fzero(fun,x0) Root of nonlinear function
disp('other value') optimget(opt,’par’) Optimization options values
end % control structures terminate with end optimset(’opt’,val) Define optimization options
For-Loop
Descriptive Statistics
%loop a specific number of times, and keep track of each ...
iteration with an incrementing index variable bounds(A) Smallest and largest elements
%parfor might be used to parallelize the execution max(A) Maximum elements of an array
for i = 1:3 min(A) Minimum elements of an array
disp('cool'); % comment with some LATEXin it: πx2
end % control structures terminate with end
mode(A) Most frequent values in array
mean(A) Average or mean value of array
While-Loop median(A) Median value of array
%loops as long as a condition remains true std(A) Standard deviation
n = 1; var(A) Variance
nFactorial = 1;
while nFactorial < 1e100
hist(X) calculate and plot histogram
n = n + 1; corrcoef(A) Correlation coefficients
nFactorial = nFactorial * n; cov(A) Covariance
end % control structures terminate with end
xcorr(x,y) Cross-correlation
Further programming commands xcov(x,y) Cross-covariance
break exit the current loop (combine with if) rand Uniformly distributed random numbers
continue go to next iteration (combine with if) randn Normally distributed random numbers
try, catch Execute statements and catch errors randi Uniformly distributed pseudorandom integers
further functions: movmax, movmin, cummax, cummin,
Plot gallery: mathworks.com/products/matlab/plot-gallery Special Topics movprod, movsum, cumsum, cumprod, movmean,
Polynomials movmedian, movstd, movvar.
poly(x) Polynomial with roots x
Programming methods poly(A) Characteristic polynomial of matrix Discrete Math
Functions polyeig(x) Polynomial eigenvalue problem factor(n) Prime factors
% defined in m-file polyfit(x,y,d) Polynomial curve fitting factorial(n) Factorial of input
% File must have the same name as the function residue(b,a) Partial fraction expansion/decomposition gcd(n,m) Greatest common divisor
function output = addNumbers(x, y) roots(x) Polynomial roots lcm(n,m) least common multiple
output = x + y; %multiple or var nr of args possible
end polyval(p,x) Evaluate poly p at points x mod(a,m) Remainder after division (modulo operation)
conv(u,v) Convolution and polynomial multiplication ceil(X) Round toward positive infinity
Anonymous Functions
deconv(u,v) Deconvolution and polynomial division fix(X) Round toward zero
% defined via function handles polyint(p,k) Polynomial integration floor(X) Round toward negative infinity
f = @(x) cos(x.ˆ2)./(3*x); polyder(p) Polynomial differentiation round(X) Round to nearest decimal or integer
Cheat Sheet for MATLAB Tools Course at ETH Zürich, by Res Jöhr