EGR 277 Digital Logic - PowerPoint PPT Presentation

1 / 21
About This Presentation
Title:

EGR 277 Digital Logic

Description:

A program has the advantage of being reusable. ... Matrix multiplication (only allowed if A has dimensions (m x r) and B has dimensions (r x n) ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 22
Provided by: tcgo
Category:
Tags: egr | digital | logic | rb

less

Transcript and Presenter's Notes

Title: EGR 277 Digital Logic


1
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Reading Assignment MATLAB handout MATLAB
Assignment MATLAB Assignment 2
MATLAB Script Files (.m files) We have seen how
to execute MATLAB commands in the Command Window
and how to re-execute commands using the Command
History Window. We will now see how to write a
program in MATLAB. The program (called a script
file or .m file) is a file containing a series of
statements much like those used previously in the
Command Window. A program has the advantage of
being reusable. You can easily try a sequence of
commands and then modify them if the output isnt
correct. The program can be executed by typing
its name in the Command Window, thus executing
the sequence of statements in the program.
The Current Directory When the name of a program
is entered into the Command Window, MATLAB looks
for the program in the Current Directory and
executes it if it is located there.
2
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Changing the Current Directory Select the Current
Directory window in MATLAB. Pick the New Folder
icon and create a new folder named EGR110
Files. Next double-click on the new EGR110 Files
folder to open the folder. It is now the Current
Directory.
3
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Creating a script (.m) file
4
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Enter the sample program below into the MATLAB
editor. Save the file as Example1 (do not use
spaces). The file will automatically be given a
.m extension. Note that comments begin with .
5
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Running the program (script file)
Run a program by typing its name in the Command
Window
Program output
6
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Specifying input values from the command line Try
modifying the previous program to remove the
statement rad 4 (or change it to a comment).
Save the program as Example 1b. Try out the
program by entering a value for rad and then
running the program.
So running a script file is just like executing a
sequence of commands in the Command Window. You
can execute any sequence of commands and script
files.
7
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Using MATLABs INPUT function The following
MATLAB function is useful for prompting the user
to enter inputs input (message) returns a
value entered from the keyboard. Example
Height input(Enter the height of the
triangle)
Example Write a MATLAB program to calculate the
area of a right triangle after prompting the user
to enter values for the base and height of the
triangle (in feet). Display the input values and
the output (with units). Solution next page
8
MATLAB Lecture 2 EGR 110 Engineering
Graphics
9
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Matrix Operations MATLAB is designed around using
matrices for calculations. Even when an
expression like x 2.5 is used in MATLAB,
the scalar variable x is stored as a (1 x 1)
matrix. It is often important to understand how
matrix operations work in order to understand
calculations in MATLAB.
Matrix Operations - Review A matrix (or an array)
is a collection of elements arranged in rows and
columns. A matrix in general has m rows and r
columns. The dimension of a matrix is the number
of rows by the number of columns (m x r).
Matrix A above has dimension (2 x 3).
10
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Several examples of matrices (and their
dimensions) are shown below. A matrix with only
one column or only one row is often called a
vector. Matrix C below is a column vector and
matrix D below is a row vector.
Matrix A has dimension (2 x 3)
Matrix B has dimension (3 x 2)
Matrix C has dimension (3 x 1)
Matrix D has dimension (1 x 3)
  • Specifying a matrix in MATLAB
  • Matrices are specified in MATLAB as follows
  • use brackets around the elements of the
    matrix
  • commas or spaces are used to separate the
    elements in a row
  • a semicolon () is used separate each row

Matrices A, B, C, and D would be specified as
follows in MATLAB A 1 0 74 15 2
or A 1,0,74,15,2 B 1 37 46
5 C 246 D 11 9 7
11
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Elements in a matrix The elements in a matrix can
be referred to by row and column number.
Specifying elements in a matrix in MATLAB A
specific element in a matrix in MATLAB can be
referred to as follows MatrixName(RowNumber,
RolumnNumber) For example, referring to matrix A
defined above A(1,2) 0, A(2,1) 4, and
A(2,3) 2 See the MATLAB example on the
following page
12
MATLAB Lecture 2 EGR 110 Engineering
Graphics
13
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Matrix Addition and Subtraction Matrix addition
and subtraction are only defined for matrices
with the same dimensions. If A and B are (m x r)
matrices, then C A B implies that
C(i,j) A(i,j) B(i,j) and D A - B
implies that D(i,j) A(i,j) - B(i,j)
14
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Matrix Multiplication The matrix multiplication
AB is only defined if matrix A has dimension (m
x r) and matrix B has dimension (r x n). The
result will be a matrix with dimension (m x n).
So the column dimension of the first matrix must
equal the row dimension of the second matrix.
15
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Exercise Determine the dimensions of the
resulting matrix for each case shown. Enter NA
(not applicable) if the matrix multiplication is
not allowed.
How does matrix multiplication work?
Verification using MATLAB
16
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Transpose of a Matrix The transpose of a matrix
is found by swapping its rows and columns. If a
matrix has dimension (m x n) then its transpose
has dimension (n x m). AT transpose of matrix
A and if B AT, then B(i,j) A(j,i)
17
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Matrix Functions Operations in MATLAB The
following functions are operations are useful for
working with matrices in MATLAB.
18
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Sample MATLAB program demonstrating matrix
operations (The program output is shown on the
next page.)
19
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Output of the program on the previous page.
20
MATLAB Lecture 2 EGR 110 Engineering
Graphics
21
MATLAB Lecture 2 EGR 110 Engineering
Graphics
Write a Comment
User Comments (0)
About PowerShow.com