You are on page 1of 10

EXPERIMENT No.

1: INTRODUCTION TO MATLAB

1. Launch the MATLAB in LAPTOP and study the following :


(i) How to run MATLAB Program ?
(ii) Usage of the interactive graphical windows and practices on the applications of
Directory, Workspace & Termination commands.

OBJECTIVE:
The objective of this lab is to introduce you to the basic operations of MATLAB. Read the
instructions that are present in computer that has a MATLAB software. Practice each new
command by completing the examples and exercise. Turn-in the answers for all the exercise
problems as your lab report. When answering the problems, indicate the commands you entered
and the output displayed. It may be easier to open a word document, and after each command,
you may copy and paste the commands and outputs to the word document. Similarly, plots can
also be pasted to your word document
[1.] INTRODUCTION
In the first lab we will learn how to perform following
(i) Perform basic mathematical operations on simple variables, vectors, matrices and
complex numbers.
(ii) Generate 2-D plots. - use and write script files (MATLAB programs). MATLAB script
files have a file extension name .m and are, therefore, usually referred as M-files.
(iii)Use the ‘help’ and ‘lookfor’ commands to debug codes.
[2.] STARTING MATLAB SOFTWARE
Start MATLAB by clicking on it in the Start menu. Once the program is running, you will see a
screen that comprises following
a) Current directory
b) Command window
c) Workspace window
d) Command history window
e) Termination command
The Command Window:
The command window is where you type commands. Hit Enter to run the command you just
typed. But any statement typed in command window is not editable, that is writing of program is
not performed in command window it is performed in editor window of MATLAB software
The Current Directory:
This shows the directory that you are working in. This directory is where MATLAB expects to
find your files (M-files, audio, images, etc). If you encounter a ‘file not found’ error, it means the
file is not in your Current Directory. You can change the working directory by typing into it or
clicking through the file browser.
The Workspace Window:
It displays information about all the variables that are currently active. In particular, it will tell
you the type (int, double, etc) of variable, as well as the dimensions of the data structure (such as
a 2x2 or 8000x1 matrix). This information can be extremely useful for debugging! The
Command History Window: keeps track of the operations that you’ve performed recently. This is
handy for keeping track of what you have or haven’t already tried.
Termination command:
In command window or editor window where program is editable is terminate by the symbol ‘ ; ’
[3.] MATLAB COMMANDS
3.1 Help
MATLAB has two important help commands to find the right syntax and a description of a
command with its all options. Typing ‘help’ by itself on the command line gives you a list of
help topics. If you want to find more about the syntax of a certain command, you type in
>> help function_name

Where, the word function_name is in the above entry is substituted by the actual name of a
function for which description is sought. For example, we can type in

>> help plot

Moreover, MATLAB will display the description, syntax and options of the plot function.

The other helpful command is ‘lookfor’. If we are not sure the exact name of the function, we
can make a search for a string, which is related to the function, and MATLAB displays all m-
files (commands) that have a matching string. MATLAB searches for the string on the first
comment line of the m-file. For example:

>> lookfor inverse

It will display all m-files, which contain the string ‘inverse’ in the comment line. Two other
useful commands are

>> whos

Which lists all your active variables (this info also appears in your Workspace Window), and

>> clear

which clears and deletes all variables (for when you want to start over).

MATLAB has tab-completion for when you’re typing long function names repeatedly. This
means that you can type part of a command and then press and it will fill in the rest of the
command automatically. Moreover, MATLAB stores command history, and previous
commands can be searched by pressing the up or down arrow keys.

3.2 Matrix Operations

MATLAB is designed to operate efficiently on matrices (hence the name MATLAB = “Matrix
Laboratory). Consequently, MATLAB treats all variables as matrices, even scalars!

Like many other programming languages, variables are defined in MATLAB by typing:

<Variable Name>=<Assignment>

MATLAB will then acknowledge the assignment by repeating it back to you. The following are
what you would see if you defined a scalar x, a vector y, and a matrix z:

>> x = 3
x=3
>> y = [1, 2, 3]
y=1 2 3
>> z = [1, 2, 3 ; 4, 5 , 6]
z=1 2 3
4 5 6
You can see from the above examples that scalar variables require no special syntax; you just
type the value after the equals sign. Matrices are denoted by square brackets [ ]. Commas
separate the elements within a row, and semicolons separate different rows. A row array, such as
y, is just a special case of a matrix that has only one row.
EXERIMENT No. 2

OBJECTIVE

Syntax, and usage of various types of variables, vectors, constants, state-space variables, arrays,
matrices, determinants, and usage of arrays and matrices operations.

Syntax

It is the body language in which program is written. Description is given below

.m Files

MATLAB is a command-line driven program, which means it does not save commands once
they are typed. This is problematic when debugging programs because in the command window,
if a command is mistyped, previous commands may have to be retyped to get the program back
to the correct state. To alleviate this problem, and to save your commands for future use, you will
generally use script files. There are sometimes referred to as .m-files because their file names
end in .m. You can read more in Pratt Pundit about creating ‘.m files’ for scripts and functions -
specifically in the pages MATLAB: Script and MATLAB: User-defined Function - though the
basics needed for this laboratory are presented within this

1. Go to the File menu and choose the New option, followed by the Blank M-file option.
This will open a new editing window. You could also click on the icon that looks like a
blank piece of paper with an orange star in the corner to achieve the same result.
2. You can now type commands, just as if you were typing in the command window. The
difference is, these commands will not execute until you save the script and type its name
in the command window (or hit F5 while in the editing window, which both saves and
runs the active script). When the script is run, MATLAB will execute all the commands
in the file. Type the lines below in your new script. Note that your file does not yet have a
name.

You can define and write any types of variable in script and save it before ‘run’ each variable is
end by ‘;’ such as

x = 3;
y = 4;

z = 5;

x + y + z;

a = x + y;

b = y + z;

c = z + x;

3. Save and run this script as myTest.m by going to the Debug menu and choosing Save and
Run or by using the F5 shortcut key on the keyboard. Save the file in your current
directory.
4. Now type myTest in the MATLAB command window. You will notice that everything in
the script runs again, just as if you had typed each of the lines of code in the script at the
command prompt. The semi-colon still suppresses output, and any expression without an
assignment is still stored in the variable ans. You can see that the variables have all been
stored the same way by typing who or whos
5. First, determine what the ‘ans’ variable is equal to by typing ‘ans’ in MATLAB. Verify
for yourself that that value represents the last function or calculation performed that was
not assigned to some variable. Then check the rest of your guesses against the variable
values MATLAB has

VECTORS

ROW VECTORS

In MATLAB, you can create a row vector using square brackets [ ]. Elements of the vector may
be separated either by one or more blanks or a comma,

>> x = [1 -2 5]

OR

>> x = [1, -2, 5]

 Square brackets are use to create a row vector.


 The elements may be separated either by blanks or commas.

ACCESSING SEVERAL ELEMENTS OF A VECTOR

>> x ([1 3] )

OR

>> x ( [1, 3] )

 This displays elements 1 and 3 of the vector x, assuming x has already been defined. Note
that there is a blank between the 1 and the 3, so it is not x([13]).
 Although a space is commonly used to separate elements, to make it clearer a comma can
be used to separate the 1 and the 3.

NUMBER OF ELEMENTS IN A VECTOR

>> i2 = [1 3 2 1]
>> sample1 = x(i2)

>> numel(sample1)

The vector has four elements

>> length(sample1)

The vector has four elements

STATE SPACE

Initial condition of response of state-space model

Syntax of state-space

initial(sys,x0)
initial(sys,x0,Tfinal)
initial(sys,x0,t)
initial(sys1,sys2,...,sysN,x0)
initial(sys1,sys2,...,sysN,x0,Tfinal)
initial(sys1,sys2,...,sysN,x0,t)
[y,t,x] = initial(sys,x0)
[y,t,x] = initial(sys,x0,Tfinal)
[y,t,x] = initial(sys,x0,t)

Description:

initial(sys,x0) calculates the unforced response of a state-space (ss) model sys with an initial
condition on the states specified by the vector x0:

Constructing a Matrix in MATLAB

If you have a specific set of data, you can arrange the elements in a matrix using square brackets.
A single row of data has spaces or commas in between the elements, and a semicolon separates
the rows. For example, create a single row of four numeric elements. The size of the resulting
matrix is 1-by-4, since it has one row and four columns. A matrix of this shape is often referred
to as a row vector.

A = [12 62 95 -8]

A = 1 ROW and 4 COLUMNS

It can be find by following command

SZ = size (A)

SZ = [1 4]

ROW = 1

COLUMN = 4
Inverse of matrix can be obtained by following

inv(A)

Similarly, you can apply many commands in order to operation on matrices by seeing help with
reference to matrices in MATLAB

DETERMINANTS IN MATLAB

Syntax

B = det(A)
B = det(A,'Algorithm','minor-expansion')

DESCRIPTION

B = det(A) returns the determinant of the square matrix A.

B = det(A,'Algorithm','minor-expansion') uses the minor expansion algorithm to evaluate the


determinant of A.

EXAMPLE

Compute the determinant of a symbolic matrix.

syms a b c d
M = [a b; c d];
B = det(M)

B = a d−b c

Compute the determinant of a matrix that contain symbolic numbers.


A = sym([2/3 1/3; 1 1]);
B = det(A)

B=1/3

EXAMPLE REGARDING MINOR-EXPANSION

Create a symbolic matrix that contains polynomial entries.


syms a x
A = [1, a*x^2+x, x;
0, a*x, 2;
3*x+2, a*x^2-1, 0]
Compute the determinant of the matrix using minor expansion.
B = det(A,'Algorithm','minor-expansion')

B = 3 a x3+6 x2+4 x+2

You might also like