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:
2.7e3which simply means \(2.7\times10^3\))
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\) and get:
>> 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 \(\pi\) from
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? \(c^2=a^2+b^2-2a b\, \cos(\theta)\). Find the length of the hypotenuse of a triangle with angle 30ο, and sides with lengths 10 and 20. The MATLAB trigonometric functions (cos, sin, tan) use radians, so you will need to convert using \(\pi\).
For guided practice and further exploration of how to use the command prompt, watch Video Lecture 2: The Command Prompt.