Lec 9
Lec 9
MathWorks
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.
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.
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.