Math 012/ Math 012B Numerical Methods and Analysis Matlab Activity 1.1 Operators and Numerical Functions
Math 012/ Math 012B Numerical Methods and Analysis Matlab Activity 1.1 Operators and Numerical Functions
Math 012/ Math 012B Numerical Methods and Analysis Matlab Activity 1.1 Operators and Numerical Functions
NAME :
SCORE:
Operators
Operator is an alternate name that describes symbols such as +, -, and <. Operators are normally broken into usage-
based categories
1. Arithmetic Operators- Used on numbers and return a number. They are similar to functions in your
calculator.
2. Relational Operators- make a comparison which results in a logical value. A logical value has only two
possible states; TRUE and FALSE which is represented by 1 and 0 respectively.
3. Logical Operators – used on logical values and result in a logical value. These operators test for true or false
and outputs either 1 (true) or 0 (false). In coding, both the use of 1 and 0 or keywords true or false are
acceptable.
EXERCISE 2.1
1. Work through the following commands. Observe and indicate the outputs from Matlab. If there is no
output, write a short explanation.
Output
Command Window Input
w=5;
x=3;
y=2;
z=2;
x==y
y==z
x==(w-x) | y==z
x==y | y==z
z==w | x==y
w==(y+z) | x==z
~(x == y) & y == z
X Y X and Y Explanation
true(1) true(1)
true (1) false (0)
false (0) true(1)
false (0) false (0)
4. The operator | (or) returns true when either or both of the logical values it compares are true.
Summarize the possible outputs from the comparison of two statements X and Y when the | operator
(or) is used. Indicate an explanation e.g. X | Y is true if X is true and Y is true.
X Y X and Y Explanation
true(1) true(1)
true (1) false (0)
false (0) true(1)
false (0) false (0)
Double is the default numerical data type in MATLAB. It stores values between ± 3.4 x 10 38.
>> class(y)
Output: _________________________________
To check whether a value is of type logical, use the MATLAB function islogical().
Type these lines into the Command Window:
>> islogical(x)
>> islogical(y)
While MATLAB returns the logical 1 and 0 in the Command Window, in your code you can also use the MATLAB
constants true and false, which are the same as logical(1) and logical(0).
Operator Precedence
Operator precedence is vital to know, because the result of an operation can be wildly different than intended if
precedence isn’t understood and addressed. For instance, enter the following two lines into the Command Window
and note the different results:
>> 3 + 2 * 6
Output: __________________________
>> (3 + 2) * 6
Output: _________________________
Multiplication has a higher precedence than addition, so the first line is equivalent to 3 + (2 * 6). Parentheses
override precedence, so the second line first adds 3 + 2 and then multiplies the result by 6. Someone who
understands the precedence rules might use line one, but someone who assumes that everything is done left-to-
right might use the first when they meant the second. Use parentheses to ensure the correct precedence!
Order of Operations
1. Parentheses
4. Addition, subtraction
5. Relational operators (<, <=, >, >=, ==, ~=)
6. Logical AND (&)
7. Logical OR (|) In the case of a tie, operations are usually executed from left to right.
There are many more operators and you can see the full list by searching for “operator precedence” in the MATLAB
help. Write below a few other operators found in MATLAB and their uses.
____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________
____________________________________________________________________________________________________________________________________
_________________________________________________________________________________________________________________________________.
NUMERICAL FUNCTIONS
There are more built-in functions in MATLAB for manipulating numbers. Here are some functions that might be
useful.
EXERCISE 2.2
1. Based on operator precedence, predict the result of the calculation below without using Matlab.
8 3 2 10
5 2
2 1
Prediction: ___________________________________________
Once you think you know the answer, enter the statement in the Command Window.
Matlab Syntax/input:
>>
Output: __________________________________
y=5
Prediction: _________________________________________________________________________
3. Type the following commands into the Command Window and note the output that results.
Command Window Entry MATLAB Output
>>ceil(3.4)
>>ceil(-3.4)
>>floor(3.4)
>>floor(-3.4)
>>fix(3.4)
>>fix(-3.4)
>>round(3.4)
>>round(-3.4)
>>mod(5, 2)
>>rem(5, 2)
>>mod(5, -2)
>>rem(5, -2)
>>mod(-5, 2)
>>rem(-5, 2)
If you forgot to put the parenthesis when typing the commands, i.e. instead of ceil(3.4) you only typed ceil
3.4, what happens to the output?
_________________________________________________________________________________________________________________________
4. On your own, browse the MATLAB hep for Elementary Math then select Trigonometry from the results to
see the full list of trigonometric functions. Complete the following table. Note angle must be converted to
radians using MATLAB also.
sine of
Cosine of
tangent of
cotangent of
secant of
cosecant of