Built-in Functions

Download Report

Transcript Built-in Functions

Week 1: Variables, assignment, expressions
•
READING: 1.2 – 1.4
1
EECS 1541 -- Introduction to Computing for the Physical Sciences
Variables
• To store a value in a MATLAB program, a variable is used
• A variable stores a value that can be changed at any time
• To create a variable, we use an assignment statement:
variablename = expression
The variable is always on the left, followed by the =
symbol, followed by an expression
2
EECS 1541 -- Introduction to Computing for the Physical Sciences
Variables
• Example:
>> z = 6
z =
• This means a value of 6
is assigned to the
variable “z”
6
• The variable “z” is on the left, followed by the = symbol
3
EECS 1541 -- Introduction to Computing for the Physical Sciences
Variables
• Putting a semicolon at the end of a statement suppresses the output
• Example:
>> z = 6;
>>
• This would assign a value of 6 to the variable z, but the result is
not shown.
4
EECS 1541 -- Introduction to Computing for the Physical Sciences
Variables
1.
a variable name must start with a letter
2.
the rest of the name can include letters, digits, or underscores
3.
names are case sensitive, so “A” and “a” are two different
variables
4.
MATLAB has some reserved words called keywords that
cannot be used as variable names
• use the command iskeyword to get a list of keywords
5
EECS 1541 -- Introduction to Computing for the Physical Sciences
Variables
• use short, meaningful names
• a name that conveys the purpose of the variable is often useful for
others who need to read your code, e.g., use
massEarth
massSun
instead of
instead of
mE
mS
• exceptions to the rule:
• if you are solving a problem that contains variable names, you should try
to use the same names, e.g., in physics the following would likely be
common:
g, c, v0
(g = gravity, c = speed of light, v0 = initial velocity)
6
EECS 1541 -- Introduction to Computing for the Physical Sciences
Variables
• Examples:
valid variable
names
invalid variable
names
reason invalid
x
$
• does not begin with a letter
• $ is not allowed in variable names
x6
6x
• does not begin with a letter
lastValue
If
• if is a keyword
pi_over_2
pi/2
• punctuation marks are not allowed in
variable names
7
EECS 1541 -- Introduction to Computing for the Physical Sciences
Operators
• Numerical expressions are created using values, variables,
operators, built-in functions and parentheses.
• Common operators used with numerical expressions:
+ addition
- negative, subtraction
* Multiplication
/ division
^ exponentiation
()parentheses
8
EECS 1541 -- Introduction to Computing for the Physical Sciences
Operator Precedence Rules
• Just like normal mathematical computations, some operators
have precedence over others in MATLAB.
operator
(
)
name
Parentheses
^
Exponentiation
-
Negation
*, /, \
Multiplication and division
+, -
Addition and subtraction
precedence
• Highest
• Lowest
9
EECS 1541 -- Introduction to Computing for the Physical Sciences
Operators
• Example:
>> y = 6 - 5 * 8 + 9
y =
-25
• What about:
• First 5 is multiplied by 8,
then
the
result
is
subtracted by 6 and
added to 9
>> y = (6 – 5 * 8) + 9
y =
-25
10
EECS 1541 -- Introduction to Computing for the Physical Sciences
Operators
• What about:
>> y = 6 - 5 * 8 + 9;
>> y = y + 1
y =
-24
• The above computation where y = y + 1 is called
incrementing, it increases the previous value by 1
11
EECS 1541 -- Introduction to Computing for the Physical Sciences
Constants
• Recall that variables are used to store values that might change
• Constants are values that cannot be changed at any time. Some
constants that are pre-defined in MATLAB are:
Constant names
pi
Meaning/value
π or 3.1414
i
j
inf
∞
12
EECS 1541 -- Introduction to Computing for the Physical Sciences
Constants
• Example:
>> pi_over_2 = pi / 2
pi_over_2 =
1.5708
13
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions
• most MATLAB is provided through functions
• a function in MATLAB accepts a set of inputs and (usually)
calculates a set of outputs
• there can be 0 or more inputs
• there can be 0 or more outputs
• the user of the function provides the inputs
• the input values are called arguments to the function
• the function provides the outputs
• the user uses the name of the function to use the function
• we say that the user calls the function
14
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions
• A list of elementary math functions from MATLAB can be found
using the following command:
>> help elfun
15
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions
• This list introduces a long list of functions:
16
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions: Exponential functions
• What does the following expression mean?
>> exp(1)
• We can use help to search for the meaning of exp
>> help exp
17
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions: Exponential functions
>> help exp
18
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions: Exponential functions
• Since exp is the exponential function, exp(1)means evaluate
the exp function with an input argument of 1 (i.e. = e1)
>> exp(1)
ans =
2.7183
• Note: MATLAB uses a default variable named “ans” if an
expression is typed at prompt and it is not assigned to a variable
19
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions: Trigonometric functions
• Example:
>> help sin
20
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions: Trigonometric functions
• Consider the following expressions:
y = 2 * sin(pi/2);
z = y
z = pi
• What will the output values of the above program?
21
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions: Trigonometric functions
• Consider the following expressions:
y = 2 * sin(pi/2);
z = y
z = pi
• The first expression will evaluate the sin
function with an input argument of pi/2,
which is equal to 1, then multiply by 2
NOTE: “ ; ” is added after the statement, so
the final value “2” will not be displayed
22
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions: Trigonometric functions
• Consider the following expressions:
y = 2 * sin(pi/2);
z = y
z = pi
• The first expression will evaluate the sin
function with an input argument of pi/2,
which is equal to 1, then multiply by 2
NOTE: “ ; ” is added after the statement, so
the final value “2” will not be displayed
• The second expression simply means
whatever is in y is now also assigned to z
23
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions: Trigonometric functions
• Consider the following expressions:
y = 2 * sin(pi/2);
z = y
z = pi
• The first expression will evaluate the sin
function with an input argument of pi/2,
which is equal to 1, then multiply by 2
NOTE: “ ; ” is added after the statement, so
the final value “2” will not be displayed
• The second expression simply means
whatever is in y is now also assigned to z
• The last expression means to assign the
constant pi (or 3.1416) to variable z
24
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions: Trigonometric functions
• NOTE: the new value assigned to z will
overwrite the previous value
25
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions: Trigonometric functions
>> help sin
26
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions: Trigonometric functions
>> help sind
27
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions: Trigonometric functions
• Example:
>> y = sind(90)
y =
1
• What do you expect to see from:
>> y = cos(sind(360))
y =
1
28
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions
• If you are interested in other built-in functions in MATLAB:
http://www.mathworks.com/help/matlab/functionlist.html
29