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): ex
  • log(x): natural logarithm (loge(x))
  • log10(x): base-10 logarithm
  • log2(x): base-2 logarithm
  • sqrt(x): square root

Complex Numbers

  • i or j: reserved for √(-1) unless otherwise designated
  • abs(x): absolute value
  • real(x): real part of a number
  • imag(x): imaginary part of a number

Rounding Functions

  • fix(x): round towards zero
  • floor(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 columns
  • x = [1; 2; 3; 4; 5; 6; 7; 8; 9; 10];: column vector with 10 rows
  • A = [1 2; 3 4];: 2×2 matrix
  • .*, ./, .^: element-wise operations on arrays
  • linspace(a,b,c): creates a linearly spaced array from a to b with c points
  • logspace(a,b,c): creates a logarithmically spaced vector from 10a to 10b with c points
  • ones(m,n) and zeros(m,n): create an m×n matrix of ones or zeros

Plotting

  • figure: generates a new figure window
  • plot: creates a two-dimensional plot
  • xlabel('Label'), ylabel('Label'), title('Title')

Line Styles

  • - solid
  • : dotted
  • -. dash-dot
  • -- dashed
  • none no line

Colors

  • b blue
  • g green
  • r red
  • c cyan
  • m magenta
  • y yellow
  • k black
  • w white

Markers

  • . point
  • o circle
  • x x-mark
  • + plus
  • * star
  • s square
  • d diamond
  • v triangle (down)
  • ^ triangle (up)
  • < triangle (left)
  • > triangle (right)
  • p pentagram
  • h hexagram

Workspace and Command Window Commands

  • help topic: help on a specific topic
  • who: list variables in the workspace
  • whos: list variables with their sizes
  • clear: clears the workspace
  • clear x y: clears variables x and y
  • clc: clears the command window
  • clf: clears the figure
  • pwd: shows the current directory
  • cd: changes directory
  • dir or ls: lists directory contents
  • mkdir: makes a directory
  • clock: gets the current time
  • date: gets the current date
  • quit or exit: 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)
  • ~ not
  • xor exclusive 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
end

Common 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

Related entries: