0% found this document useful (0 votes)
10 views32 pages

Week 1-1

The document provides an overview of variables in MATLAB, including how to create, initialize, increment, and remove them. It also discusses variable naming conventions, operators, operator precedence, constants, and built-in functions. Additionally, it highlights the importance of avoiding naming conflicts between variables and functions.

Uploaded by

kisla05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views32 pages

Week 1-1

The document provides an overview of variables in MATLAB, including how to create, initialize, increment, and remove them. It also discusses variable naming conventions, operators, operator precedence, constants, and built-in functions. Additionally, it highlights the importance of avoiding naming conflicts between variables and functions.

Uploaded by

kisla05
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 32

1

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


(assignment operator), followed by an expression

2
Variables

• Example: >> z = 6
• This means a value of 6
z = is assigned to the
variable “z”
6

• The variable “z” is on the left, followed by the assignment


operator

3
Variables

4
Variables

• Putting a semicolon at the end of a statement suppresses the output

• This would assign a value of 6 to the variable z, but the result is


not shown.

With Semicolon Without Semicolon

>> z=6; >> z=6


>>
z =

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=5; initializing >> i=10;

>> num=num-1 >> i=i+1

num = i =

4 10

incrementing
decrementing

6
Removing Variables

2- Right click on
the variable in
the workspace

1- Use clear function

7
Removing Variables

>> clear name1 removes name1 from workspace

>> clear name1 name2 … removes name1, name2, etc from workspace

>> clear removes all variables from the workspace

>> clc clears Command Window, but not variables

8
Variable Names

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


9
10
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 instead of mE
massSun 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)


11
Variables

• Examples:

valid variable invalid variable reason invalid


names names
x $, # • does not begin with a letter
• $, # are 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

12
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

13
Operator Precedence Rules

• 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

14
Operators

• Example: >> y = 10 - 5 * 2
• First 5 is multiplied by 2,
y = 10 - 10 then the result is
subtracted by 10
0

• What about: >> y = 3 – 2 ^ 2 * 3

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

• Constants are values that cannot be changed at any time. Some


constants that are pre-defined in MATLAB are:

17
Constants

• Example: >> pi_over_2 = pi / 2

pi_over_2 =

1.5708

• Note: the default in MATLAB is to display numbers


that have decimal points with four decimal places

>> a = 0.5

a =

0.5000
18
Constants

• The format command can be used to specify the output format

• Changing the format to long will result in 15 decimal places


>> format long
>> a = sin(3)

a =

0.141120008059867

• This will remain in effect until the format is changed back to


short
19
Continuation Operator

• Particularly long expressions can be continued on the next line by


typing three (or more) periods, which is the continuation operator.

>> y = 3 + 2 + 1 * 5 ...
/ 5 - 2

y =

20
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
21
Built-in Functions

• A list of elementary math functions from MATLAB can be found


using the following command:
>> help elfun

22
Built-in Functions

23
Built-in Functions: Exponential functions

• We can use help to search for the meaning of exp


>> help exp

24
Built-in Functions: Exponential functions

25
Quick Question!

• What would happen if you use the name of a function, for


example, sin, as a variable name?

>> sin (pi/3)


ans =
0.8660

>> sin=10
sin =
10

>> sin (pi/3)


Array indices must be positive integers or logical values.

'sin' appears to be both a function and a variable. If this is


unintentional, use 'clear sin' to remove the variable 'sin' from
the workspace.
26
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

27
Built-in Functions: Trigonometric
functions

• Example: >> help sin

28
Built-in Functions

• Write out the MATLAB statement that performs the following:


 
1 sin 2  
 2
y e
2
>> y = 0.5*exp((sin(pi/2))^2)

Don’t forget to put Multiplication (*)

>> A=10;
>> 2A is an invalid command
>> 2*A is valid
29
Built-in Functions

• What is the output with the following MATLAB statement?

>> y = 3*cos(exp(0)*pi*2)^3

30
Built-in Functions: Trigonometric
functions

• Consider the following statements:

y = 2 * sin(pi/2)
z = y
z = z ^ y + y

• What are the final values of y and z?

31
Built-in Functions

• If you are interested in other built-in functions in MATLAB, visit:

http://www.mathworks.com/help/matlab/functionlist.html

32

You might also like