Appendix A Quick Reference: Built-In MATLAB Functions
This appendix provides a quick reference for built-in MATLAB functions used throughout this textbook. Each entry includes a brief description and example usage. For more detailed coverage of
mod, see SubsectionΒ B.1. For floor, ceil, and round, see SubsectionΒ B.2. For min and max, see SubsectionΒ B.3.
Mathematical Functions.
-
abs -
Returns the absolute value of a number (its distance from zero).
abs(-7) % returns 7 abs(3.2) % returns 3.2 -
sqrt -
Returns the square root of a number.
sqrt(25) % returns 5 sqrt(2) % returns 1.4142 -
factorial -
Returns the factorial of a non-negative integer, i.e., \(n! = 1 \cdot 2 \cdot 3 \cdots n\text{.}\)
factorial(5) % returns 120 factorial(0) % returns 1 -
gcd -
Returns the greatest common divisor of two integers.
gcd(12, 8) % returns 4 gcd(7, 3) % returns 1 -
min -
Returns the smallest value & its location in a vector.
-
max -
Returns the largest value & its location in a vector.
Rounding Functions.
See SubsectionΒ B.2 for a detailed comparison of these three functions.
-
floor -
Rounds a number down (toward \(-\infty\)) to the nearest integer.
floor(3.7) % returns 3 floor(-2.3) % returns -3 -
ceil -
Rounds a number up (toward \(+\infty\)) to the nearest integer.
ceil(3.2) % returns 4 ceil(-2.7) % returns -2 -
round -
Rounds to the nearest integer. Halfway values round away from zero.
round(3.5) % returns 4 round(2.3) % returns 2
Trigonometric Functions.
MATLABβs trigonometric functions use radians, not degrees. A full circle is \(2\pi\) radians. To convert degrees to radians, multiply by \(\pi/180\text{.}\)
-
sin -
Returns the sine of an angle given in radians.
sin(0) % returns 0 sin(pi/2) % returns 1 -
cos -
Returns the cosine of an angle given in radians.
cos(0) % returns 1 cos(pi) % returns -1 -
tan -
Returns the tangent of an angle given in radians.
tan(0) % returns 0 tan(pi/4) % returns 1
Random Number Functions.
-
rand -
Generates uniformly distributed random numbers between 0 and 1.
rand % a single random number in [0, 1) rand(1, 5) % a 1-by-5 row vector of random numbers -
randi -
Generates uniformly distributed random integers. The first argument specifies the range.
randi(6) % random integer from 1 to 6 randi([0, 1]) % random integer: 0 or 1 (coin flip) randi(10, 1, 5) % five random integers from 1 to 10 -
rng -
Sets the random number seed so that results are reproducible. Using the same seed produces the same sequence of random numbers. Try it out!Press Evaluate multiple times. Notice that the same random numbers are generated each time.Now try it without
rng(42). Press Evaluate multiple times and see how the output changes each time.
Output/Display Functions.
-
fprintf -
Prints formatted text to the Command Window. Use
%dfor integers,%ffor decimals,%gfor general numbers,%sfor strings, and\nfor a new line.fprintf('The answer is %d\n', 42) fprintf('Pi is approximately %.4f\n', pi) -
disp -
Displays a value or string in the Command Window without the variable name.
disp(42) % displays: 42 disp('Hello World') % displays: Hello World
Input Functions.
-
input -
Prompts the user to enter a value at the Command Window. The program pauses until the user types a response and presses Enter.The following command expects a numeric input. If the user types a number, it is stored in variable
x.x = input('Enter a number: ');When you want text input, add the second argument βsβ. Now, if the user types text, it is stored in variablename.name = input('Enter your name: ', 's');
