0% found this document useful (0 votes)
22 views4 pages

Lec 9

The document discusses defining variables in MATLAB. It explains that variables are assigned using the = operator and valid variable names. It also discusses using the ans variable when not explicitly assigning a result and gives examples of defining variables for expressions.

Uploaded by

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

Lec 9

The document discusses defining variables in MATLAB. It explains that variables are assigned using the = operator and valid variable names. It also discusses using the ans variable when not explicitly assigning a result and gives examples of defining variables for expressions.

Uploaded by

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

MATLAB Variables

Created by MathWorks for


Structural Dynamics

MathWorks

(Refer Slide Time: 00:04)

The first step to solving problems in MATLAB usually requires defining variables.
(Refer Slide Time: 00:11)

For example, suppose we need to evaluate this expression for different values of x and save the
results to use later.

(Refer Slide Time: 00:19)


You might guess that we use the equal sign to create a variable and that is exactly right. The =
sign is referred to as the assignment operator, since it assigns value to the variable. Giving
variables descriptive names helps us remember their meaning. But there are a few important
rules we must follow. Variable names must start with a letter and can contain only letters,
numbers and underscores. Variable names are also case sensitive.
Take a moment and try identifying the valid variable names in this list.

We see that both ‘b’ and ‘d’ are valid names. But ‘a’ violates rule 1 and c violates rule 2. Now,
back to that expression we need to evaluate. Variables are useful because they are stored in
memory. So, let us create a new variable y1 using the existing variable x. The expression to the
right of the assignment operator is evaluated using the current value of x. Then the result is
assigned to the variable.
Notice that y1 is in memory. But the result was not displayed. That is because we ended the
statement with a semicolon which prevents output from being shown. Now suppose we need to
find ‘y’ when ‘x’ is equal to pi. We could create a variable named pie pi. But to be accurate, we
need to remember pi too to many decimal places. No problem! pie Pi is so commonly used that it
is already included in MATLAB.

So, we don't have to define pie pi to use it in calculations. Notice anything different about this
last expression? We did not use the assignment operator. But the result was stored in the variable
‘ans’. The name and ‘ans’ is used by default to store a result when we do not use the assignment
operator. Create a few variables in MATLAB yourself. For example, try creating a variable y2
that is the value of the function when x=2 x is equal to 2.

Keywords: Defining variables, valid variable names, ans, MATLAB.

MathWorks

© 2015 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The
MathWorks, Inc. See www.mathworks.com / trademarks for a list of additional trademarks.
Other product or brand names may be trademarks or registered trademarks of their respective
holders.

You might also like