0% found this document useful (0 votes)
42 views

MATLAB

Here are the function files and sample code to call the functions for the given problems: 1) chp6one.m function y = chp6one(x) y = x^2 - 5*x + 6; % Call the function f1 = chp6one(6) f2 = chp6one([1 3 5 7 9 11]) 2) seriesResistance.m function R = seriesResistance(Rlist) R = sum(Rlist); % Call the function R = seriesResistance([10 20 15 16 5]) 3) plotCommands.m x = 0:pi/10

Uploaded by

Audrey Cruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
42 views

MATLAB

Here are the function files and sample code to call the functions for the given problems: 1) chp6one.m function y = chp6one(x) y = x^2 - 5*x + 6; % Call the function f1 = chp6one(6) f2 = chp6one([1 3 5 7 9 11]) 2) seriesResistance.m function R = seriesResistance(Rlist) R = sum(Rlist); % Call the function R = seriesResistance([10 20 15 16 5]) 3) plotCommands.m x = 0:pi/10

Uploaded by

Audrey Cruz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

Starting with MATLAB

Lab. No. 1

OBJECTIVES
The students are able to describe the characteristics and purposes of the different windows in
MATLAB.

Explain the main windows in matlab desktop


1. Command Window

• 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

Perform the following exercises:

1. Enter the following matrix, A=1342 2016 4127


(a) Get the matrix size
(b) Get the matrix transpose

2. We have the following two matrices B = [2 2 3; 4 0 6; 8 1 5] C = [1 1 2; 6 3 5; 1 9 1] Calculate:


(a) D=B-C
(b) E=B+C
(c) F= E+2
(d) G=B*C
(e) H= B.*C
3. If V1 = 5v, V2 = 6v, Z11=2, Z12=1, Z21=3 Z22=4, get the value of I1 and I2?

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

6. State if the following statements are true or false,


(a) If a MATLAB statement ends with a semicolon (;) MATLAB evaluates the statement but
suppresses the display of the results.
(b) The end of each row in entering a matrix, is indicated by a semicolon (;)
(c) MATLAB is case sensitive in naming variables only.
Creating Arrays
Lab. No. 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 [ ].

variable_name = [ type vector elements ]

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.

variable_name=[m:q:n] or variable_name = m:q:n

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:

variable_name=linspace (xi, xf ,n)

>>ab=linspace(0, 8, 6) à 6 elements, first element 0, last element 8.

ab=
0 1.6000 3.2000 4.8000 6.4000 8.0000

>>cd=linspace( 30, 10, 11)

cd=
30 28 26 24 22 20 18 16 14 12 10

Creating A Two-Dimensional Array (MATRIX)


A two dimensional array is called matrix. A matrix has m number of rows and n columns.
Matrix is created by typing the elements, row by row, inside square brackets [ ]. First type the left
bracket [, then type the elements in the first row separated with spaces or commas. To type the next
row, type the semicolon or press Enter. Type the right bracket ] at the end of the last row.

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] à A semicolon is type before a new line is entered

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

The Transpose Operator


The transpose operator, when applied to a vector, switches a row (column) vector to a column
(row) vector. The transpose operator is applied by typing a single quote ‘ following the variable to be
transposed.

Example:
>>AR=[ 1 2 3] àdefine a row vector AR

AR =
1 2 3

>>GU=AR’ à define a row vector GU as the transpose of AR


GU=
1
2
3
Exercises:

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.

3. Enter the following complex number, z = 2-j3 then


(a) Get the real and the imaginary parts of z
(b) Get the magnitude and the phase angle of z
(c) If y = 3+j5, calculate the following: y+z, y-z, y×z
4. If w = [1+j 5-2*j; 3+2*j 4+3*j]
(a) Get the conjugate transpose
(b) Get the point transpose
nd
(c) Type the elements of 2 row only

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

1. Write a function file (name it chp6one) for the function

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;

4. The difference between plot, semilogy, semilogx, logog commands


5. bar plot command
6. Pie plot

You might also like