2. Simple Mathematics

Elementary Maths

All of the mathematical functions on your calculator are available in MATLAB , as well as many other elementary functions. To view a list of the common functions, click on “Help” at the top of the MATLAB Command Window, then click on “MATLAB Help”, and then double click on MATLAB Functions Listed by Category. Then click on Elementary Math Functions.

(Note there are also Specialized Math Functions beyond the scope of this introduction.)

For brief information about any of the elementary functions, merely double click on that function name. You can use the back-arrow near the top of the window to move back to previous windows. Of special significance, note there are the six trigonometric functions as well as their corresponding inverse functions. For example, the MATLAB expression sin(x) represents sin x. Note the use of brackets around the x. Of course, MATLAB only uses natural radian measure and never degrees. Type in the following (non-comment) commands:

theta=3*pi/4

y1=sin(theta), y2=cos(theta), y3=tan(theta)

% note than you can put more than one command on a given line,

% separated by commas. Now find the reciprocal of each:

z1=csc(theta), z2=sec(theta), z3=cot(theta)

% Find alpha, beta such that sin(alpha)=0.721 and tan(beta)=-1.78

alpha=asin(0.721), beta=atan(-1.78)

There are also the six hyperbolic functions and their inverse functions, to be discussed later in Chapter 6 of Grossman. Of special importance, note that

  • the exponential function ex is represented in MATLAB by exp(x)
  • the natural logarithm function ln x is represented in MATLAB by log(x)
  • the square-root function x is represented in MATLAB by sqrt(x)
  • the absolute value function |x| is represented in MATLAB by abs(x)

Exercises: Let x = 1.253. Show that the variables u, v, w, y, z have the values shown by typing in the corresponding MATLAB commands:

table

Other Useful Functions

round(x) rounds x to the nearest integer

floor(x) rounds x down to the nearest integer

ceil(x) rounds x up to the nearest integer

rem(y,x) the remainder after dividing y by x

sign(x) =−1 if x< 0 ; =0 if x=0 ; =1 if x> 0

rand generates a random number between 0 and 1

Exercise

You have $50. Each item costs $3.35. How many items can you buy and how much change will you have left?

You have $50. Each item costs $3.35. How many items can you buy and how

much change will you have left?

number_of_items=floor(50/3.35)

% change default format to one with two decimal places

format bank

change=rem(50,3.35)

Note: to view other possible formats, type in help format after the MATLAB prompt.

Exercise: There are 211 students to be arranged into tutorial classes which can hold 24 students per class. Use MATLAB commands to determine the number of tutorial classes needed and how many spare places are left.