Computational Lab (1)
Computational Lab (1)
Computational Lab (1)
Sindri, Dhanbad
Department of Electrical Engineering
>> 1+2*3
ans = 7
You will have noticed that if you do not specify an output variable, MATLAB uses a default
variable ans, short for answer, to store the results of the current calculation. Note that the
variable ans is created (or overwritten, if it is already existed). To avoid this, you may assign a
value to a variable or output argument name.
will result in x being given the value 1 + 2 × 3 = 7. This variable name can always be used to
refer to the results of the previous computations.
>> 4*x
ans = 28.0000
Before we conclude this minimum session, Table 1.1 gives the partial list of arithmetic operators.
+ Addition 2+3
- Subtraction 3-2
* Multiplication 2*3
/ Division 3/2
For operators of equal precedence, evaluation is from left to right. Now, consider another
example:
In MATLAB, it becomes
>> 1/(2+3^2)+4/5*6/7
ans = 0.7766
ans = 10.1857
So here what we get: two different results. Therefore, we want to emphasize the importance of
precedence rule in order to avoid ambiguity.
Experiment 2: Managing the Workspace
The contents of the workspace persist between the executions of separate commands. Therefore,
it is possible for the results of one problem to have an effect on the next one. To avoid this
possibility, it is a good idea to issue a clear command at the start of each new independent
calculation.
>> clear
The command clear or clear all removes all variables from the workspace. This frees up system
memory. In order to display a list of the variables currently in the memory, type
>> who
while, whos will give more details which include size, space allocation, and class of the
variables.
It is possible to keep track of everything done during a MATLAB session with the diary
command. >> diary or give a name to a created file,
>> diary
The function diary is useful if you want to save a complete MATLAB session. They save all
input and output as they appear in the MATLAB window. When you want to stop the recording,
enter diary off. If you want to start recording again, enter diary on. The file that is created is a
simple text file. It can be opened by an editor or a word processing program and edited to
remove extraneous material, or to add your comments. You can use the function type to view the
diary file or you can edit in a text editor or print. This command is useful, for example in the
process of preparing a homework or lab submission.
To view the online documentation, select MATLAB Help from Help menu or MATLAB Help
directly in the Command Window. The preferred method is to use the Help Browser. The Help
Browser can be started by selecting the ? icon from the desktop toolbar. On the other hand,
information about any command is available by typing
Another way to get help is to use the lookfor command. The lookfor command differs from the
help command. The help command searches for an exact function name match, while the lookfor
command searches the quick summary information in each function for a match. For example,
suppose that we were looking for a function to take the inverse of a matrix. Since MATLAB
does not have a function named inverse, the command help inverse will produce nothing. On the
other hand, the command lookfor inverse will produce detailed information, which includes the
function of interest, inv.
Note - At this particular time of our study, it is important to emphasize one main point. Because
MATLAB is a huge program; it is impossible to cover all the details of each function one by one.
However, we will give you information how to get help.
• In the current version (MATLAB version 7), the doc function opens the on-line version of the
help manual. This is very helpful for more complex commands.
There is a long list of mathematical functions that are built into MATLAB. These functions are called
x
built-ins. Many standard mathematical functions, such as sin(x), cos(x), tan(x), e , ln(x), are evaluated
by the functions sin, cos, tan, exp, and log respectively in
MATLAB
We illustrate here some typical examples which related to the elementary functions previously defined.
for a = 5, x = 2, and y = 8
is computed by
>> a = 5; x = 2; y = 8;
>> log(142)
ans = 4.9558
>> log10(142)
ans = 2.1523
Note the difference between the natural logarithm log(x) and the decimal logarithm (base 10) log10(x).
To calculate sin(π/4) and e 10, we enter the following commands in MATLAB,
>> sin(pi/4)
ans = 0.7071
>> exp(10)
ans = 2.2026e+004
Experiment 4: Curve Plotting, Labeling and its analysis
The vectors x = (1, 2, 3, 4, 5, 6) and y = (3, −1, 2, 4, 5, 1) produce the picture shown in Figure 2.1.
>> x = [1 2 3 4 5 6];
>> y = [3 -1 2 4 5 1];
>> plot(x,y)
Note: The plot functions has different forms depending on the input arguments. If y is a vector plot(y)
produces a piecewise linear graph of the elements of y versus the index of the elements of y. If we specify
two vectors, as mentioned above, plot(x,y) produces a graph of y versus x. For example, to plot the
function sin (x) on the interval [0, 2π], we first create a vector of x values ranging from 0 to 2π, then
compute the sine of these values, and finally plot the result:
>> x = 0:pi/100:2*pi;
>> y = sin(x);
>> plot(x,y)
Notes: • 0:pi/100:2*pi
yields a vector that – starts at 0, – takes steps (or increments) of π/100, – stops when 2π is reached. • If
you omit the increment, MATLAB automatically increments by 1.
MATLAB enables you to add axis labels and titles. For example, using the graph from the previous
example, add an x- and y-axis labels. Now label the axes and add a title. The character \pi creates the
symbol π. An example of 2D plot is shown in Figure 2.2.
Multiple (x, y) pairs arguments create multiple graphs with a single call to plot. For example, these
statements plot three related functions of x: y1 = 2 cos(x), y2 = cos(x), and y3 = 0.5 cos(x), in the
interval 0 ≤ x ≤ 2π.
>> x = 0:pi/100:2*pi;
>> y1 = 2*cos(x);
>> y2 = cos(x);
>> y3 = 0.5*cos(x);
>> plot(x,y1,’--’,x,y2,’-’,x,y3,’:’)
>> legend(’2*cos(x)’,’cos(x)’,’0.5*cos(x)’)
>> v = [1 4 7 10 13] v = 1 4 7 10 13
Column vectors are created in a similar way, however, semicolon (;) must separate the components of a
column vector,
>> w = [1;4;7;10;13]
w = 1 4 7 10 13
On the other hand, a row vector is converted to a column vector using the transpose operator. The
transpose operation is denoted by an apostrophe or a single quote (’).
>> w = v’ w = 1 4 7 10 13
Thus, v(1) is the first element of vector v, v(2) its second element, and so forth. Furthermore, to access
blocks of elements, we use MATLAB’s colon notation (:). For example, to access the first three elements
of v, we write,
Or, all elements from the third through the last elements,
>> v(3,end) ans = 7 10 13 where end signifies the last element in the vector. If v is a vector, writing
Entering a matrix
To enter a matrix A, such as, A = [1 2 3 4 5 6 7 8 9]
>> A = [1 2 3; 4 5 6; 7 8 9]
MATLAB then displays the 3 × 3 matrix as follows, A = 1 2 3 4 5 6 7 8 9 Note that the use of semicolons
(;) here is different from their use mentioned earlier to suppress output or to write multiple commands in a
single line. Once we have entered the matrix, it is automatically stored and remembered in the
Workspace. We can refer to it simply as matrix A. We can then view a particular element in a matrix by
specifying its location. We write,
>> A(2,1) ans = 4 A(2,1) is an element located in the second row and first column. Its value is 4.
We select elements in a matrix just as we did for vectors, but now we need two indices. The element of
row i and column j of the matrix A is denoted by A(i,j). Thus, A(i,j) in MATLAB refers to the element
Aij of matrix A. The first index is the row number and the second index is the column number. For
example, A(1,3) is an element of first row and third column. Here, A(1,3)=3. Correcting any entry is easy
through indexing. Here we substitute A(3,3)=9 by A(3,3)=0. The result is
>> A(3,3) = 0 A = 1 2 3 4 5 6 7 8 0
>> A(3,:) = [] A = 1 2 3 4 5 6
Third row of matrix A is now deleted. To restore the third row, we use a technique for creating a matrix
>> A = [A(1,:);A(2,:);[7 8 0]] A = 1 2 3 4 5 6 7 8 0
Ax = b
where there are as many equations as unknown. A is a given square matrix of order n, b is a
given column vector of n components, and x is an unknown column vector of n components. In
linear algebra we learn that the solution to Ax = b can be written as x = A−1 b, where A−1 is the
inverse of A. For example, consider the following system of linear equations
x + 2y + 3z = 1
4x + 5y + 6z = 1
7x + 8y = 1
>> A = [1 2 3; 4 5 6; 7 8 0];
>> x = inv(A)*b
2. The second one is to use the backslash (\)operator. The numerical algorithm behind this
operator is computationally efficient. This is a numerically reliable way of solving system
of linear equations by using a well-known process of Gaussian elimination.
>> A = [1 2 3; 4 5 6; 7 8 0];
>> x = A\b
y1 = 2 cos(x), y2 = cos(x), and y3 = 0.5 cos(x), in the interval 0 ≤ x ≤ 2π. This example has
been presented in previous Chapter. Here we put the commands in a file.
x = 0: pi/100:2*pi;
y1 = 2*cos(x);
y2 = cos(x);
y3 = 0.5*cos(x);
plot(x,y1,’--’,x,y2,’-’,x,y3,’:’)
ylabel(’Cosine functions’)
legend(’2*cos(x)’,’cos(x)’,’0.5*cos(x)’)
Let us illustrate the “if” statement through a simple example. Suppose we would like to
define and plot the piecewise defined function
This is done with the use of the “if” statement in MATLAB as follows. First we define the
“domain” vector x from –1 to 1 with increment 0.01 to produce a smooth enough curve.
» x=-1:0.01:1;
Next, we loop through the values of x and for each one we create the corresponding function
value F as a vector.
» for i=1:length(x)
F(i) = x(i)^2;
else
F(i) = 0.25;
end
end
» plot(x, F,’-k’)
Experiment 9: Laplace Transform of Symbolic Expression
syms a t
f = exp(-a*t);
laplace(f)
syms x y
f = 1/sqrt(x);
laplace(f)
ans =
1/(a + s)
ans =
pi^(1/2)/s^(1/2)
Experiment 10: Solution of Eigen values and Eigen Vector of a matrix
A = gallery('lehmer',4)
A = 4×4
e = eig(A)
e = 4×1
0.2078
0.4078
0.8482
2.5362
D = eig(A,'matrix')
D = 4×4
0.2078 0 0 0
0 0.4078 0 0
0 0 0.8482 0
0 0 0 2.5362
A = gallery('circul',3)
A = 3×3
1 2 3
3 1 2
2 3 1
V = 3×3 complex
D = 3×3 complex