At its heart, MATLAB® is a big calculator. To calculate something simply type it in at the “command prompt” and press Enter. Thus, to calculate 1 + 1 we type it in and press Enter. The screen should show:
>> 1+1
ans =
2
meaning that the answer is 2.
Exercise 1. Run MATLAB, find the command window and the blinking cursor. Find the answer to the following arithmetic problems:
(The operator for “power” is the circumflex ^, usually found by pressing Shift ⇑ 6- How far is
from its approximation (Remember that , thus the answer should be ±1_)_ - Find an approximation to 1/73
- Find an approximation to
(while you can of course use the fact that , you can also “look for” a dedicated function square root by learning how to use thelookfor
command….) - If you get 5% interest-rate (yearly) on a loan, compounded monthly, and you start with $1000, how much money will you have after 20 years? (don’t be confused by an answer of the form
2.7e3
which simply means ) - If two sides of a right triangle have lengths 31 and 45, what is the length of the hypotenuse?
You may have noticed in the exercises that the answer is only given with 5 digits of accuracy (at most). For example, we can ask MATLAB for the value of
>> pi
ans =
3.1416
Internally, MATLAB keeps a 16 (more-or-less) digit version of the number it shows us, but to keep things orderly, it only displays the answer rounded to show 5 digits (by default). We can change this by issuing a command:
>> format long
>> pi
ans =
3.141592653589793
We can see this, by subtracting part of ans
, which always holds the full, unrounded answer to the previous, unassigned expression:
>> format short
>> pi
ans =
3.1416
>> ans-3.1415
ans =
9.2654e-05
>> ans - 9.2653e-5
ans =
5.8979e-10
Exercise 2. Remember the cosine rule?
For guided practice and further exploration of how to use the command prompt, watch Video Lecture 2: The Command Prompt.