MATH/CMSC 206 - Introduction to Matlab

Announcements Syllabus Tutorial Projects Submitting

Using Matlab Like a Calculator

Contents

Arithmetic and More

To get the ball rolling, let's ask Matlab to do a little arithemetic for us. It's the least it could do for us (we mean that literally -- wait till you see the fancy stuff!) Try the following command to harness the enormous computing power of Matlab:

2 + 2
ans =

     4

Are you impressed yet? Matlab is pretty smart. Note that Matlab, like most programming languages, uses the * symbol for multiplication and the ^ symbol for exponentiation. Try these commands:

10 * 20
ans =

   200

sqrt(2)
ans =

    1.4142

sin(2 * pi / 3)
ans =

    0.8660

2^10
ans =

        1024

log(3)
ans =

    1.0986

Notice that results are returned as decimals which means you often get just an approximation as is the case with sqrt(2) and the sin and log functions.

Also notice that the log function represents the natural logarithm. Computer scientists and mathematicians differ on notation sometimes and this is one of those places. If you want Matlab to do base 10 logarithms you must use log10.

Can you figure out what the following commands do? Try them all!

factorial(5) 
round(pi * 6) 
primes(40)
            
factor(600) 
gcd(120, 90) 
date 
calendar 
Try executing these two commands, one after the other:
tic
toc

Self-Test

  1. Consider a right-triangle with one leg of length 13, and the other leg of length 27. Write a single Matlab command that you could use to compute the length of the hypotenuse.
  2. Write a single Matlab command that would compute the area of a circle having a radius of 73.
  3. Write a single Matlab command that will compute the number of seconds there are in 1 year (assuming there are 365 days per year).
  4. You are standing on the ground, pointing to a window that is high above the ground and far away. The angle between the ground and your outstretched arm is 0.5 radians. The length of the direct line from your finger to the window is 392 feet. Write a single Matlab command that will compute the height of the window above the ground.

Answers to Self-Test

Next Topic: A Few Other Basic Commands