Lecture2 Notes
Lecture2 Notes
Lecture2 Notes
Matlab does not use the “=” sign for expression evaluation, say, 1+2 =. It returns
an error.
In Matlab the “=” sign is reserved for assigning a value (numeric or text) to a
variable:
Note: The instruction clc clears the Command Window from all previously typed
commands and answers, but it does not clear the defined variables. Another useful
instruction is the home instruction that scrolls the Command Window and sends the
cursor to the upper left hand side of the window. Try it.
Rules on variable names:
Command Outcome
clear Removes all variables from memory
clear x y z Removes only variables x, y and z from memory
who Displays a list of variables currently in memory
whos Displays a detailed list of variables currently in memory
Examples:
Creating and Manipulating Arrays
One of the main advantages of using Matlab for numeric calculations and
programming is its relative ease of creating and manipulating arrays (vectors and
matrices), and its efficient array computations (the name Matlab stands for Matrix
Laboratory).
One can selectively assign one element of an array to a new variable by using
an“index” that points to the element of interest. For example, to assign the third
element of vector A to a variable d, we use the instruction d=A(3)
For a matrix:
Matlab has a powerful way for processing the individual components of a vector or
a matrix. For example, if x is a row (or column) vector, the expression 2*x
multiplies each individual component of x by 2. Similarly, 1+x and sqrt(x) are
designed to compute the element-by-element addition of 1 and the element-by-
element square root of vector x, respectively. This is true for all Matlab elementary
(and trig) built-in functions. Here is an example:
However, care must be used when trying to perform exponentiation (say, x^2) of
the components of x. Here, the “ ^” operator must be used:
Continuing with the x and y vectors defined in the above example, Matlab returns
the following for y*x (no dot) and x*y. Why?
So, the rule is that when performing (element-by-element multiplication (*),
division (/) or exponentiation (^) of a vector we must use the “ *”, “ /”, and “ ^”
operators. The dot notation is optional for multiplication and division by a scalar
(constant). Here are few more examples:
Examples:
Use the help utility to learn more about these Matlab commands and their available
options.
Vector Generation with Function and “ ” colon Operator
The linspace(x1, x2, n) function provides a handy tool to generate a row vector of n
equally spaced points between x1 and x2. If the integer n is omitted, the function
generates 100 points (by default). The increment between consecutive values is
given by
Can you find a formula for the intermediate values generated by the command
logspace(x1, x2, n)?
The Colon Operator “:”
The colon operator is a powerful tool for creating and manipulating arrays. The
Matlab expression x = 1:6 generates a row vector with values 1, 2, 3, ... 6. The
expression y = 1:0.5:3.2 generates a row vector with values 1, 1.5, 2, 2.5, 3. Here,
the 0.5 is the increment. Here are few more examples:
In the last example, the instruction format short g suppresses the redundant zeros on
the right of all displayed numerical results.
The colon operator can also be used as a wildcard to select the individual rows and
columns of a matrix (or vector). So, if x = [1 2 4 3 -1 10], then x(2:4)= x([2 3 4])
returns the row vector [2 4 3]. Here are two examples:
Your turn: Explain the result returned by the following Matlab instructions:
>>eye(3,4)
>>v=[1 2 3];
>>A=diag(v)
>>B=[ 1 2 3; 4 5 6; 7 8 9];
>> diag(B)
>>x=0:6;
>>x(1:3)=[]
>>A=[1 2 3 ; 2 -3 4; 0 2 -1];
>>A(:,1)=[];
>>A
>>x=[1 2 3 -1 0];
>>y=fliplr(x)
Application of “:” in summing series:
Find the sum of the first ten terms of the following geometric series, and compare it
to the exact value, 3/7.
0.3 3
= (0.3) = =
1 − 0.3 7
Matlab solution: