Arithmetic & Built-in Math
MATLAB is a very capable calculator. The usual operators work on single numbers (scalars), and in later lessons you'll see they work on whole arrays too.
| Operator | Meaning | Example | Result |
|---|---|---|---|
+ - |
Add, subtract | 7 - 2 |
5 |
* |
Multiply | 7 * 2 |
14 |
/ |
Divide | 7 / 2 |
3.5 |
\ |
Left divide (a\b is b/a) |
2 \ 7 |
3.5 |
^ |
Power | 7 ^ 2 |
49 |
disp(7 / 2)
disp(2 \ 7)
disp(2 ^ 10)
disp(8 ^ (1/3))
disp(-2 ^ 2)
disp((-2) ^ 2)3.5000
3.5000
1024
2
-4
4
Operator precedence
From highest to lowest: parentheses, then ^ (and transpose), then unary minus, then * / \, then + -. Operators of equal precedence are evaluated left to right. That's why -2 ^ 2 is -4: the power happens before the negation. Use parentheses whenever you are unsure.
disp(2 + 3 * 4)
disp((2 + 3) * 4)
disp(2 ^ 3 ^ 2) % left to right: (2^3)^2
disp(12 / 4 / 3) % (12/4)/314
20
64
1
Note:
2^3^2is evaluated left to right in MATLAB, giving 64. In mathematics and in many other languages, powers group right to left (giving 512). Add parentheses to make your intent obvious.
Integer division and remainders
There is no special integer-division operator. Combine / with floor or fix, and use mod or rem for the remainder:
minutes = 135;
h = floor(minutes / 60);
m = mod(minutes, 60);
fprintf('%d min = %d h %d min\n', minutes, h, m);
fprintf('mod(-7, 3) = %d, rem(-7, 3) = %d\n', mod(-7, 3), rem(-7, 3));135 min = 2 h 15 min
mod(-7, 3) = 2, rem(-7, 3) = -1
mod takes the sign of the divisor and rem the sign of the dividend; for positive numbers they agree.
Elementary functions
| Category | Functions |
|---|---|
| Roots and powers | sqrt, nthroot, exp, power |
| Logarithms | log (natural), log10, log2 |
| Trigonometry (radians) | sin, cos, tan, asin, acos, atan, atan2 |
| Trigonometry (degrees) | sind, cosd, tand, asind, … |
| Rounding | round, floor, ceil, fix |
| Other | abs, sign, factorial, gcd, lcm, hypot |
fprintf('sqrt(2) = %.4f\n', sqrt(2));
fprintf('exp(1) = %.4f\n', exp(1));
fprintf('log(100) = %.4f\n', log(100));
fprintf('log10(100) = %g\n', log10(100));
fprintf('sin(pi/6) = %.4f\n', sin(pi/6));
fprintf('sind(30) = %.4f\n', sind(30));
fprintf('atan2(1, -1) = %.4f\n', atan2(1, -1));
fprintf('nthroot(-27, 3) = %g\n', nthroot(-27, 3));
fprintf('factorial(5) = %d, gcd(12, 18) = %d, lcm(4, 6) = %d\n', factorial(5), gcd(12, 18), lcm(4, 6));sqrt(2) = 1.4142
exp(1) = 2.7183
log(100) = 4.6052
log10(100) = 2
sin(pi/6) = 0.5000
sind(30) = 0.5000
atan2(1, -1) = 2.3562
nthroot(-27, 3) = -3
factorial(5) = 120, gcd(12, 18) = 6, lcm(4, 6) = 12
There is no function called e: write exp(1) for Euler's number. Note also that (-27)^(1/3) gives a complex result, because MATLAB returns the principal complex root; nthroot returns the real one.
A worked example
Compound interest: €2,500 invested at 3.2% a year, compounded monthly, for 6 years. The formula is A = P(1 + r/n)^(n·t).
P = 2500; % principal
r = 0.032; % yearly rate
n = 12; % compounding periods per year
t = 6; % years
A = P * (1 + r/n)^(n*t);
fprintf('Final amount: %.2f\n', A);
fprintf('Interest earned: %.2f\n', A - P);
yearsToDouble = log(2) / (n * log(1 + r/n));
fprintf('Doubling time: %.1f years\n', yearsToDouble);Final amount: 3028.40
Interest earned: 528.40
Doubling time: 21.7 years
Test yourself
What is the value of -3 ^ 2?
- 9
- -9
- 6
- -6
Which expression gives Euler's number e ≈ 2.718?
eexp(1)log(1)euler()
What is mod(17, 5)?
- 3
- 2
- 3.4
- 12