Arithmetic in MATLAB
Arithmetic operations are the foundation of mathematics. They include:
- Addition
- Subtraction
- Multiplication
- Division
- Exponentiation
When a mathematical expression contains more than one arithmetic operation, the order of operations is used to determine which are performed first. This page reviews both the arithmetic operations and the order in which they are performed.
Addition and Subtraction
The two most fundamental arithmetic operations are addition and subtraction.
To add two numbers in MATLAB, put the +
sign between them.
Space between the numbers and the +
sign is optional; add as much or as little as you need for visual clarity.
Example: Altitude Above Sea Level
Question
A plane is flying 7,500 ft above Deep Creek Lake, where the terrain is elevated 2,461 ft above sea level. Using MATLAB, what is the plane’s altitude above sea level?
Solution
To find the altitude above sea level, we add the height from sea level to Deep Creek Lake to the height from the lake to the plane. In MATLAB, this looks like:
>> 7500 + 2461
ans =
9961
Subtraction is carried out with the -
sign, in the same manner as addition.
Example: Red Hot Steel
Question
A steel part is heated up and glowing cherry red, corresponding to a temperature of 1100 K. Given that 0 degrees Celsius is 273.15 K, what is the part’s temperature in Celsius?
Solution
To find the temperature in degrees Celsius, we subtract 273.15 from 1100 K. In MATLAB, this looks like:
>> 1100 - 273.15
ans =
826.8500
Multiplication and Division
Multiplication is performed with the *
sign,
while division is performed with the /
sign.
The x
key does not work for multiplication in MATLAB,
and there is no ÷ for division.
MATLAB also does not support implicit multiplication, so 0.5 10
will not work but 0.5 * 10
will.
Lastly, the %
symbol has special meaning in MATLAB and does not turn a number into a percent.
Example: Tangential Speed
Question
The earth revolves around the sun at a rate of $7.173\times 10^-4$ rad/hr, with an average radius of $92.96\times 10^6$ miles. Use MATLAB to find the tangential speed of the earth, in units of mph, given the formula:
\[v = \omega r\]Solution
To find the tangential speed, we plug in values for the rotation rate, $\omega$, and the radius, $r$. In MATLAB, this looks like:
>> 7.173e-4 * 92.96e6
ans =
6.6680e+04
Example: Mach Number
Question
\[M = \frac{v}{a}\]The Mach number is the ratio of an object’s speed to the local speed of sound. When the Apollo capsule returned from the Moon, it was initially traveling 10,973 m/s in air where the local speed of sound was 289 m/s. Use MATLAB to calculate the Mach number of the capsule.
Solution
To find the Mach number, we plug in values for the speed, $v$, and the speed of sound, $a$. In MATLAB, this looks like:
>> 10973 / 289
ans =
37.9689
Exponentiation
Exponentiation raises one number to the power of another, and
is performed in MATLAB with the ^
sign.
The number to the left of the ^
is raised to the power of the number on the right.
Example: Volume of a Cube
Question
A concrete cube compression test uses a standard cube with side lengths of 15 cm. Use MATLAB to calculate the volume of the cube, in units of cubic centimeters.
Solution
To find the volume, we raise the side length to the power 3. In MATLAB, this looks like:
>> 15 ^ 3
ans =
3375
Order of Operations
MATLAB follows standard PEMDAS for order of operations. Their Order of Precedence goes into full details including other operators that are not part of the PEMDAS mnemonic.
P Parentheses
E Exponentiation
MD Multiplication and Division
AS Addition and Subtraction
Since parentheses are at the top of the list, lower-precedence operations can be promoted using parentheses. If an operation does not need to be promoted because the precedence is correct, using parentheses is unnecessary.
Example: Asset Depreciation
Question
You operate a factory containing $250M of equipment that depreciates in value at a rate of 10% per year. Use MATLAB to calculate the value of that equipment after 5 years of operations, in millions of dollars. The formula for the future value is:
\[FV = PV (1-r)^n\]Solution
To find the future value of the equipment, we plug the givens into the above formula. In MATLAB, this looks like:
>> 250 * (1-0.1)^5
ans =
147.6225
Reading Questions
- Which symbol indicates multiplication in MATLAB?
- How does MATLAB determine order of operations when evaluating expressions?
- How would you convert 4 feet to meters in MATLAB?
- How would you convert 80 degrees Fahrenheit to degrees Celsius?
- What mistakes might occur if parentheses are not included in an arithmetic expression?
- When are parentheses unnecessary in an arithmetic expression?