Lab 1 Exercise's
Lab 1 Exercise's
1
Getting Started with MATLAB: A Comprehensive Introduction
Objective:
To familiarize yourself with the MATLAB environment, understand basic operations, and
learn to use MATLAB for simple mathematical computations and visualizations.
Introduction:
The name MATLAB stands for MATrix LABoratory. MATLAB was written originally to
provide easy access to matrix software developed by the LINPACK (linear system package) and
EISPACK (Eigen system package) projects. MATLAB is a high-performance language for
technical computing. It integrates computation, visualization, and programming environment.
Furthermore, MATLAB is a modern programming language environment: it has sophisticated
data structures, contains built-in editing and debugging tools, and supports object-oriented
programming. These factors make MATLAB an excellent tool for teaching and research.
MATLAB has many advantages compared to conventional computer languages for solving
technical problems. MATLAB is an interactive system whose basic data element is an array that
does not require dimensioning. The software package has been commercially available since
1984 and is now considered as a standard tool at most universities and industries worldwide. It
has powerful built-in routines that enable a very wide variety of computations. It also has easy to
use graphics commands that make the visualization of results immediately available. Specific
applications are collected in packages referred to as toolbox. There are toolboxes for signal
processing, symbolic computation, control theory, simulation, optimization, and several other
fields of applied science and engineering. In addition to the MATLAB documentation, which is
mostly available online, we would recommend the following books: [2], [3], [4], [5], [6], [7], [8],
and [9]. They are excellent in their specific applications.
Starting with MATLAB:
After logging into your account, you can enter MATLAB by double-clicking on the
MATLAB shortcut icon (MATLAB 7.0.4) on your Windows desktop. When you start MATLAB,
a special window called the MATLAB desktop appears. The desktop is a window that contains
other windows. The major tools within or accessible from the desktop are:
1. The Command Window
2. The Command History
3. The Workspace
4. The Current Directory
5. The Help Browser
6. The Start button
1
Figure 1.1: The graphical interface to the MATLAB workspace
When MATLAB is started for the first time, the screen looks like the one that is shown in Figure
1.1. This illustration also shows the default configuration of the MATLAB desktop. You can
customize the arrangement of tools and documents to suit your needs.
Now, we are interested in doing some simple calculations. We will assume that you have
sufficient understanding of your computer under which MATLAB is being run.
You are now faced with the MATLAB desktop on your computer, which contains the prompt
(>>) in the Command Window. Usually, there are 2 types of prompts:
>> for full version
EDU> for educational version
Note: To simplify the notation, we will use this prompt, >>, as a standard prompt sign, though
our MATLAB version is for educational purposes.
Basic Commands in MATLAB:
Arithmetic Operations:
1. Addition: +
2. Subtraction: -
3. Multiplication: *
4. Division: /
5. Power: ^
2
Using MATLAB as a calculator:
As an example of a simple interactive calculation, just type the expression you want to
evaluate. Let’s start at the very beginning. For example, let’s suppose you want to calculate
the expression, 1 + 2 × 3. You type it at the prompt command (>>) as follows,
>>x=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
variable Ans is created (or overwritten, if it has already existed). To avoid this, you may
assign a value to a variable or output argument name. For example,
>>x=1+2*3
Ans=7
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. Therefore, computing 4x will result in
>> 4*x
Ans=28.0000
Quitting MATLAB:
To end your MATLAB session, type quit in the Command Window or select File −→Exit
MATLAB in the desktop main menu.
Creating and manipulating matrices and vectors
MATLAB works on matrices and vectors that may be real or complex. Variables do not have
to be “declared”. Results can be displayed in a variety of different formats.
Let us enter a row vector into the MATLAB workspace. Type in:
v = [2 4 7 5]
This creates a variable v whose current value is a row vector with four elements as shown.
After pressing “return” the value of v will have been echoed back to you. To suppress the
echo, one uses a semi-colon following the command line. Thus
w = [1 3 8 9];
Creates another row vector w but does not echo. To check that we have appeared in the
MATLAB workspace, type,
who
Which will display all the variables in the MATLAB workspace, and to check the value of w
simply type w
Operations on row vectors can be best illustrated by some simple exercises.
3
Exercise 1: Investigate the effect of the following commands:
v = [2 4 7 5]
w = [1 3 8 9]
(a) v (2)
Ans = 4
(b) sum = v + w
Ans = [3,7,15,14]
(c) diff = v – w
Ans = [1,1, -1, -4]
(d) vw = [v w]
Ans = [2,4,7,5,1,3,8,9]
(e) vw (2:6)
Ans = [4, 7, 5, 1, 3]
(f) v’
Ans = [2
4
7
5]
One way to generate a column vector is to take the transpose of a row vector, as you will
have found in exercise 1(f). Column vectors can be typed directly in one or two ways. For
example, to enter the command vector
[]
1
1
z=
0
0
you can type
z = [1; 1; 0; 0]; or
z = [1
1
0
0]
Exercise 2: Investigate the effect of the following commands.
(i) z’ (b) z*v (c) [v; w] (d) v*z (e) [z; v’] (f) z + v’
(a) z’
ans= [1 1 0 0]
(b) z*v
ans= [2 4 7 5
2 4 7 5
0 0 0 0
0 0 0 0]
(c) [v; w]
Ans= [2 4 7 5
1 3 8 9]
(d) v*z
4
ans= [6]
(f) z + v’
Ans= [3;5;7;5]
One way of creating matrices is by multiplying row and column vectors as seen in the
above exercises. There are various ways of entering matrices. For example, to enter the
matrix
M=
[ ]
1 2
3 4
The most obvious ways are to type
M = [1 2; 3 4]
but there are more perverse ways such as
M = [[1 3]’ [2 4]’]
Exercise 3: Investigate the effect of the following commands:
(a) N = inv(M) (b) M*N (c) I = eye (2) (d) M + I (e) M*z (1:2) (f) v (3:4) *M
(g) M (1,1) (h) M (1:2,1:2) (i) M (:1) (j) M (2, :)
(a) n= inv(m)
Ans= [-2.0000 1.0000;1.50000 -0.50000]
(b) m*n
Ans= [1.0000 0;0 1.0000]
(c) i= eye (2)
Ans= [1 0;0 1]
(d) m + i
Ans= [2 2;3 5]
(e) m*z (1:2)
Ans= [3;7]
(f) v (3:4) *m
ans= [22,34]
(g) m (1,1)
Ans= [1]
(h) m (1:2,1:2)
Ans= [1,2;3,4]
(i) m (:1)
Ans= [1;3]
(j) m (2:)
Ans= [3 4]