Week 1-1
Week 1-1
Variables
variablename = expression
2
Variables
• Example: >> z = 6
• This means a value of 6
z = is assigned to the
variable “z”
6
3
Variables
4
Variables
5
>>
Initializing, Incrementing, and
Decrementing
• Putting the first or initial value in a variable is called initializing
• Adding to a variable is called incrementing.
• Subtracting from a variable is called decrementing.
num = i =
4 10
incrementing
decrementing
6
Removing Variables
2- Right click on
the variable in
the workspace
7
Removing Variables
>> clear name1 name2 … removes name1, name2, etc from workspace
8
Variable Names
3. names are case sensitive, so “A” and “a” are two different variables
• 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 mE
massSun instead of mS
• Examples:
12
Operators
13
Operator Precedence Rules
^ Exponentiation
- Negation
*, /, \ Multiplication and division
+, - Addition and subtraction • Lowest
14
Operators
• Example: >> y = 10 - 5 * 2
• First 5 is multiplied by 2,
y = 10 - 10 then the result is
subtracted by 10
0
y = 3 - 4 * 3
3 - 12
-9
15
Operators
• Example: >> y = - 6 ^ 2 / 4
y =
-9
>> y = (- 6) ^ 2 / 4
y =
9
16
Constants
• Recall that variables are used to store values that might change
17
Constants
pi_over_2 =
1.5708
>> a = 0.5
a =
0.5000
18
Constants
a =
0.141120008059867
>> y = 3 + 2 + 1 * 5 ...
/ 5 - 2
y =
20
Built-in Functions
22
Built-in Functions
23
Built-in Functions: Exponential functions
24
Built-in Functions: Exponential functions
25
Quick Question!
>> sin=10
sin =
10
>> exp(1)
ans =
2.7183
27
Built-in Functions: Trigonometric
functions
28
Built-in Functions
>> A=10;
>> 2A is an invalid command
>> 2*A is valid
29
Built-in Functions
>> y = 3*cos(exp(0)*pi*2)^3
30
Built-in Functions: Trigonometric
functions
y = 2 * sin(pi/2)
z = y
z = z ^ y + y
31
Built-in Functions
http://www.mathworks.com/help/matlab/functionlist.html
32