MATLAB Cheat Sheet: Functions, Operators, Loops and Plotting Syntax
Posted by Anonymous and classified in Mathematics
Written on in
English with a size of 4.74 KB
Trigonometric Functions
Trig functions (in radians): sin(x), cos(x), tan(x), acos(x), asin(x), atan(x), cosh(x), sinh(x), csc(x), sec(x), and more.
Logarithm and Exponential Functions
exp(x): exlog(x): natural logarithm (loge(x))log10(x): base-10 logarithmlog2(x): base-2 logarithmsqrt(x): square root
Complex Numbers
iorj: reserved for √(-1) unless otherwise designatedabs(x): absolute valuereal(x): real part of a numberimag(x): imaginary part of a number
Rounding Functions
fix(x): round towards zerofloor(x): round towards -∞ceil(x): round towards +∞round(x): round to the nearest integer
Predefined Constants
pi= 3.14159...i,j= −1 (imaginary unit)Inf= infinity
Arrays and Matrices
x = [1 2 3 4 5 6 7 8 9 10];: row vector with 10 columnsx = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];: column vector with 10 rowsA = [1 2; 3 4];: 2×2 matrix.*,./,.^: element-wise operations on arrayslinspace(a,b,c): creates a linearly spaced array from a to b with c pointslogspace(a,b,c): creates a logarithmically spaced vector from 10a to 10b with c pointsones(m,n)andzeros(m,n): create an m×n matrix of ones or zeros
Plotting
figure: generates a new figure windowplot: creates a two-dimensional plotxlabel('Label'),ylabel('Label'),title('Title')
Line Styles
-solid:dotted-.dash-dot--dashednoneno line
Colors
bblueggreenrredccyanmmagentayyellowkblackwwhite
Markers
.pointocirclexx-mark+plus*starssquareddiamondvtriangle (down)^triangle (up)<triangle (left)>triangle (right)ppentagramhhexagram
Workspace and Command Window Commands
help topic: help on a specific topicwho: list variables in the workspacewhos: list variables with their sizesclear: clears the workspaceclear x y: clears variables x and yclc: clears the command windowclf: clears the figurepwd: shows the current directorycd: changes directorydirorls: lists directory contentsmkdir: makes a directoryclock: gets the current timedate: gets the current datequitorexit: quits MATLAB
Relational and Logical Operators
>greater than<less than~=not equal to==equal to<=less than or equal to>=greater than or equal to&&and (both true)||or (one or both true)~notxorexclusive or (one true, one false)
Control Flow Syntax
if value > othervalue
actions
elseif value ~= othervalue
actions2
else
actions3
end
for variable = 1:1:n
actions
end
while x <= 10
actions
endCommon Loop Patterns
y = zeros(1,10);
for count = 1:10
y(count) = _______
end
A = zeros(3,3);
for i = 1:3
for j = 1:3
A(i,j) = ____
end
end
sumx = 0;
for i = 1:n
sumx = sumx + x(i);
end