MATLAB Notes
MATLAB Notes
Character Description
+ Addition
- Subtraction
* Multiplication (Scalar and array)
/ Division
^ Exponential
: Creates vectors with equally spaced elements, represents rang of elements in arrays
= Assignment operator
() Parentheses; enclose input arguments in functions and subscripts of arrays
[] Brackets; forms arrays, enclose output arguments in functions
, Separates array subscripts and function arguments; separates commands in the same
line
; Suppresses display; ends row in array
‘ Single quote; matrix transpose; creates string
… Ellipsis; continuation of line
% Percent; denotes a comment, specifies output format
Function Description
Managing compounds
Command Description
Predefined variables
Variable Description
pi 𝜋
eps Smallest difference between 2 numbers
inf or Inf Infinity
i √−1
ans Value of last expression
NaN or nan Not a number. Used to express mathematically undefined values
Creating arrays
Function Description
Array functions
Function Description
dot (A,B) Dot product; computes inner product of 2 vectors. (a and b → MUST same size)
det (A) Calculate the determinant of matrix A
inv Inverse of square matrix
max Maximum value
min Minimum value
median Median value
sum(v) sum
sort(v) elements sorted into ascending order (by column first)
std standard deviation
cross(v,w) cross product, (v and w → must have 3 elements)
Input to a script file
- variable_name=input(‘prompt’)
- prompt → text that input command displays in command window
- put a (1) space or (2) a colon and a space at the end of prompt → separated from the prompt
Command Description
- Method 1
• plot (x,y,u,v,t,h)
• plot y vs x, v vs u, h vs t
• vectors of each pair must be same size
• can use line specifiers by putting in triplets
plot (x,y,’-b’,u,v,’—r’, t,h,’g :’)
- Method 2
• Draw the first graph with plot
• Issue the command hold on
• Call plot for each of the remaining graphs
• Issue the command hold off
- Method 3
• Use line command adds additional graphs to an existing plot
line (x,y,’PropertyName’,’PropertyValue’)
• Example: plot the function y=3x3-26x+6 for −2 ≤ 𝑥 ≤ 4 together with its first and second
derivatives
Plot
Command Description
Character Description
True 1
False 0
| OR Either 1 true.
If either one or both true → result: true (1)
Otherwise, result false (0)
switch Switches among several cases based on expression (very specific input –
yes/no/maybe) NO RELATIONAL OPERATORS
case Conditionally execute commands
otherwise Conditionally execute commands (doesn’t make all the values in case stated)
for Repeats execution of a group of commands (specified loops; when you know
how many times it will run)
end Terminates conditional statements and loops
while Repeats execution of a group of commands (runs when user give the correct
command/answer)
for-end loops
• To keep values without overlapping → introduce new variables (in matrix) and link it to
equation
3D line plots Surface mesh plot
Strings