MATLAB
MATLAB
Lab. No. 1
OBJECTIVES
The students are able to describe the characteristics and purposes of the different windows in
MATLAB.
• The command window is MATLAB’s main window and opens when MATLAB is
started.
• This window is used to enter variables and run functions and M- files.
• The window where you type commands and non-graphic output is displayed. A ‘>>’
prompt shows you the system is ready for input.
2.Command History
• Command history records commands given that session and recent sessions.
Can be used for reference or to copy and paste commands.
3.Launch Pad
• MATLAB’s Launch Pad provides easy access to tools, demos, and documentation.
4.Help browser
• The Help browser is used to search and view documentation for all your Math Works
products.
• To open the Help browser, click the help button in the toolbar, or Type helpbrowser in
the Command Window.
5.MATLAB Workspace
• The MATLAB workspace consists of the set of variables built up during a MATLAB
session and stored in memory.
6.Current Directory Browser
• MATLAB file operations use the current directory and the search path as reference
points.
• Any file you want to run must either be in the current directory or on the search
• path.
Exercises
4. IfA1=[2768910]andB1=[643234],get
a. (a) C1 = A1.*B1
b. (b) D1 = A1./B1
5. Ifr1=[735]ands1=[243],get
(a) q1 = r1.^s1
(b) q2 = r1.^2
OBJECTIVES:
The students are able to create and address arrays in one and two-dimensional rows or
columns.
DISCUSSION:
The vector is created by typing the elements (numbers) inside the square brackets [ ].
Row vector : To create a row vector type the elements with a space or a comma between the
elements inside the square brackets.
Column vector: T create a column vector type the left square bracket [ and then enter the elements
with a semicolon between them, or press the ENTER key after each element. Type the right square
bracket] after the last element.
Example:
>>yr=[1984 1986 1988 1990 1992 1994 1996] àthe list of years is assigned to a row vector
name yr.
yr =
1984 1986 1988 1990 1992 1994 1996
>>pop=[ 127; 130; 133; 135; 137; 139; 141] à the population data is assigned to a column
vector name pop.
pop=
127
130
133
135
137
139
141
Creating vector with constant spacing by specifying the first term, the spacing and the last
term.
example:
>>X=[1:2:13] àfirst element 1, the is spacing 2 and the last element is 13.
X=
1 3 5 7 9 11 13
>>Y=[1.5:0.1:2.1] àfirst element 1.5,spacing 0.1, last element 2.1
Y=
1.5000 1.6000 1.7000 1.8000 1.9000 2.0000 2.1000
Creating a vector with constant spacing by specifying the first and last terms, and the number
of terms:
ab=
0 1.6000 3.2000 4.8000 6.4000 8.0000
cd=
30 28 26 24 22 20 18 16 14 12 10
variable_name=[ 1st row elements; 2nd row elements; 3rd row elements;….; last row elements]
Example:
A=
1 2 3 4
5 6 7 8
9 10 11 12
>>B=[ 3 4 10 8 9 7
9 2 1 4 6 10
5 0 1 3 5 7] à the enter key is pressed before a new line is entered
B=
3 4 10 8 9 7
9 2 1 4 6 10
5 0 1 3 5 7
The zeros, ones and eye commands
The zeros (m,n), the ones (m,n), and the eye (n) commands can be used to create matrices
that have elements with special values. The zeros (m,n) and the ones (m,n) commands create matrix
with m rows and n columns, in which all the elements are the number 0 and 1. The eye (n) command
creates a square matrix with n rows and n columns in which diagonal elements are equal to 1, and
rest of the elements are 0.
Example:
>>K=zeros(3,4)
K=
0 0 0 0
0 0 0 0
0 0 0 0
>>R=ones(4,3)
R=
1 1 1
1 1 1
1 1 1
1 1 1
>>N=eye(5)
N=
1 0 0 0 0
0 1 0 0 0
0 0 1 0 0
0 0 0 1 0
0 0 0 0 1
Example:
>>AR=[ 1 2 3] àdefine a row vector AR
AR =
1 2 3
1. Using the ones and zeros commands, create a 4 x 5 matrix in which the first two rows
are 0’s and the next two rows are 1’s.
Solution:
>>A(1:2,:)=zeros(2,5)
A=
0 0 0 0 0
0 0 0 0 0
>>A(3:4,:)=ones(2,5)
A=
0 0 0 0 0
0 0 0 0 0
1 1 1 1 1
1 1 1 1 1
2. Create a 6 x 6 matrix in which the middle two rows, and the middle two columns are 1’s
and the rest are 0’s.
5. If R = 10 Ohms and the current is increased from 0 to 10 A with increments of 2A, write
a MATLAB m-file program to generate a table of current, voltage and power dissipation.
Mathematical Operations with Arrays
Lab No. 3
OBJECTIVES:
The students will learn the basic and most common mathematical operations that
MATLAB performs using arrays. They will apply the basic operations like multiplication,
division and exponentiation in different ways.
Function File
Lab No. 4
The input to the function is x and the output is f(x). Use the function to calculate:
a) f(x) for x = 6.
b) f(x) for x = 1, 3, 5, 7, 9, and 11.
2. Write a function file to solve the equivalent resistance of series connected resistors, R1, R2,
R3, ..., Rn. Use the function to calculate: the series equivalent resistance of 10, 20, 15, 16 and
5 ohms.
3. plot, xlabel, ylabel, title, and axis commands;