Post on 13-Jan-2016
Variables
2
• To store a value in a MATLAB program, a variable is used
• 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
• A variable stores a value that can be changed at any time
EECS 1541 -- Introduction to Computing for the Physical Sciences
Variables
3
• Example:
• The variable “z” is on the left, followed by the = symbol
>> z = 6
z =
6
• This means a value of 6 is assigned to the variable “z”
EECS 1541 -- Introduction to Computing for the Physical Sciences
Variables
4
• 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.
EECS 1541 -- Introduction to Computing for the Physical Sciences
Variables
5
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
EECS 1541 -- Introduction to Computing for the Physical Sciences
Variables
6
• 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 instead of mEmassSun instead of 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)
EECS 1541 -- Introduction to Computing for the Physical Sciences
Variables
7
• 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
EECS 1541 -- Introduction to Computing for the Physical Sciences
Operators
8
• Numerical expressions are created using values, variables, operators, built-in functions and parentheses.
+ addition- negative, subtraction* Multiplication/ division ^ exponentiation ()parentheses
• Common operators used with numerical expressions:
EECS 1541 -- Introduction to Computing for the Physical Sciences
Operator Precedence Rules
9
• Just like normal mathematical computations, some operators have precedence over others in MATLAB.
operator name precedence
( ) Parentheses • Highest
^ Exponentiation
- Negation
*, /, \ Multiplication and division
+, - Addition and subtraction • Lowest
EECS 1541 -- Introduction to Computing for the Physical Sciences
Operators
10
• Example: >> y = 6 - 5 * 8 + 9
y =
• First 5 is multiplied by 8, then the result is subtracted by 6 and added to 9
• What about: >> y = (6 – 5 * 8) + 9
y =
-25
EECS 1541 -- Introduction to Computing for the Physical Sciences
-25
Operators
11
• What about: >> y = 6 - 5 * 8 + 9;
>> y = y + 1
y =
• The above computation where y = y + 1 is called incrementing, it increases the previous value by 1
-24
EECS 1541 -- Introduction to Computing for the Physical Sciences
Constants
12
• 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:
EECS 1541 -- Introduction to Computing for the Physical Sciences
13
• Example: >> pi_over_2 = pi / 2
pi_over_2 =
1.5708
Constants
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions
14
• 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
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions
15
• A list of elementary math functions from MATLAB can be found using the following command:
>> help elfun
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions
16
• This list introduces a long list of functions:
EECS 1541 -- Introduction to Computing for the Physical Sciences
17
• What does the following expression mean?
• We can use help to search for the meaning of exp
>> exp(1)
>> help exp
Built-in Functions: Exponential functions
EECS 1541 -- Introduction to Computing for the Physical Sciences
18
Built-in Functions: Exponential functions
EECS 1541 -- Introduction to Computing for the Physical Sciences
>> help exp
19
• 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
Built-in Functions: Exponential functions
EECS 1541 -- Introduction to Computing for the Physical Sciences
Built-in Functions: Trigonometric functions
20
>> help sin• Example:
EECS 1541 -- Introduction to Computing for the Physical Sciences
21
• Consider the following expressions:
y = 2 * sin(pi/2);z = yz = pi
• What will the output values of the above program?
Built-in Functions: Trigonometric functions
EECS 1541 -- Introduction to Computing for the Physical Sciences
22
• Consider the following expressions:
y = 2 * sin(pi/2);z = yz = pi
Built-in Functions: Trigonometric functions
EECS 1541 -- Introduction to Computing for the Physical Sciences
NOTE: “ ; ” is added after the statement, so the final value “2” will not be displayed
• The first expression will evaluate the sin function with an input argument of pi/2, which is equal to 1, then multiply by 2
23
• Consider the following expressions:
y = 2 * sin(pi/2);z = yz = pi
• The second expression simply means
whatever is in y is now also assigned to z
Built-in Functions: Trigonometric functions
EECS 1541 -- Introduction to Computing for the Physical Sciences
NOTE: “ ; ” is added after the statement, so the final value “2” will not be displayed
• The first expression will evaluate the sin function with an input argument of pi/2, which is equal to 1, then multiply by 2
24
• Consider the following expressions:
y = 2 * sin(pi/2);z = yz = pi
• The second expression simply means
whatever is in y is now also assigned to z
Built-in Functions: Trigonometric functions
EECS 1541 -- Introduction to Computing for the Physical Sciences
• 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 last expression means to assign the constant pi (or 3.1416) to variable z
25
Built-in Functions: Trigonometric functions
EECS 1541 -- Introduction to Computing for the Physical Sciences
• NOTE: the new value assigned to z will overwrite the previous value
Built-in Functions: Trigonometric functions
26
>> help sin
EECS 1541 -- Introduction to Computing for the Physical Sciences
27
Built-in Functions: Trigonometric functions
EECS 1541 -- Introduction to Computing for the Physical Sciences
>> help sind
28
Built-in Functions: Trigonometric functions
• Example: >> y = sind(90)
y =
1
• What do you expect to see from:
>> y = cos(sind(360))
y =
1
EECS 1541 -- Introduction to Computing for the Physical Sciences