Lecture 2 - ch2 - Fundamentals
Lecture 2 - ch2 - Fundamentals
with MATLAB®
for Engineers and Scientists
4th Edition
Steven C. Chapra
©McGraw‐Hill Education. All rights reserved. Authorized only for instructor use in the classroom. No reproduction or further distribution permitted without the prior written consent of McGraw‐Hill Education.
Expressing Functions in MATLAB
Syntax
• Function:
ax x c
2
b dx 2
©McGraw‐Hill Education.
Colon Operator - Notes
If diffval is omitted, the default value is 1:
>>3:6
ans =
3 4 5 6
To create a decreasing series, diffval must be negative:
>> 5:-1.2:2
ans =
5.0000 3.8000 2.6000
If start+diffval>limit for an increasing series or
start+diffval<limit for a decreasing series, an empty
matrix is returned:
>>5:2
ans =
Empty matrix: 1-by-0
To create a column, transpose the output of the colon operator,
not the limit value; that is, (3:6)’ not 3:6’
©McGraw‐Hill Education.
Matrices
A 2-D array, or matrix, of data is entered row
by row, with spaces (or commas) separating
entries within the row and semicolons
separating the rows:
>> A = [1 2 3; 4 5 6; 7 8 9]
A =
1 2 3
4 5 6
7 8 9
©McGraw‐Hill Education.
Useful Array Commands
The transpose operator (apostrophe) can be used
to flip an array over its own diagonal. For example,
if b is a row vector, b’ is a column vector
containing the complex conjugate of b.
The command window will allow you to separate
rows by hitting the Enter key - script files and
functions will allow you to put rows on new lines as
well.
The who command will report back used variable
names; whos will also give you the size, memory,
and data types for the arrays.
©McGraw‐Hill Education.
Accessing Array Entries, 1
Individual entries within a array can be both read
and set using either the index of the location in the
array or the row and column.
The index value starts with 1 for the entry in the
top left corner of an array and increases down a
column - the following shows the indices for a 4
row, 3 column matrix:
1 5 9
2 6 10
3 7 11
4 8 12
©McGraw‐Hill Education.
Accessing Array Entries, 2
Assuming some matrix C:
C =
2 4 9
3 3 16
3 0 8
10 13 17
C(2) would report 3
C(4) would report 10
C(13) would report an error!
Entries can also be access using the row and column:
C(2,1) would report 3
C(3,2) would report 0
C(5,1) would report an error!
©McGraw‐Hill Education.
Transpose of a Matrix
1 3 5 1 2 0
A 2 4 6
A 3 4 3
T
0 3 7 5 6 7
MATLAB code:
A [1, 2,3; 4,5,6; 7,8,9]
AT A '
Creating a Vector in MATLAB
• Enter the elements directly
– v=[1 2 3]
– v=[1,2,3]
• Using the colon operator
– v=[1:3]=[1 2 3]
– v=[0:3:9]=[0 3 6 9]
– v=[9:-3:0]=[9 6 3 0]
• Using Function linspace
– v=linspace(0,9,4)= [0 3 6 9]
– v=linspace(9,0,4)= [9 6 3 0]
Indexing Vectors
• Accessing the value of the third element of
vector v: x=v(3)
• Max and Min element from vector v:
– [value,index]=max(v)
– [value,index]=min(v)
• Length of vector v:
– n_elements=length(v)
• Apply the find function:
– Find(v>2)
– Answers are the indexes that satisfy the
condition.
Generating Matrices
• Directly: M=[1,2,3;4,5,6]
• From a collection of row vectors:
M=[r1;r2;r3]
• From a collection of column vectors:
M=[c1,c2,c3] or M=[c1 c2 c3]
• Using a sequence: M=[1:3;4:6;7:9]
• A matrix with all zero or unity elements:
M=zeros(3,4) M=ones(3,4)
• Identity matrix: M=eye(3)
Indexing Matrices
• Retriving an element from a matrix:
– x=M(3,2) i.e., third row, second column
• Retriving a row or column vector:
– r=M(2,:)
– c=M(:,3)
• Determining the size of a matrix:
– ans=Size(M); ans given as number of
rows and number of columns, e.g., ans= 4 3
Matrix Equality and Summation
• Matrix equality (i.e., A=B) means that A
and B have the same number of rows and
columns and that aij=bij for all values of i
and j.
• Matrix addition (i.e., A+B=C) means that
A, B and C each have the same number of
rows and columns and that cij=aij+bij for all
values of i and j.
• MATLAB code: C=A+B
Matrix Multiplication
• For matrix multiplication (i.e., C=AB), the
number of rows of A must equal the number
of columns of B.
• The element of cij is equal to the scalar
product of ith row of A and the jth column of
B.
• For example, for
a11 a12 b11 b12 c11 c12
A B C
21
a a22 21 22
b b 21 22
c c
b11
c21 a21 a22 a21b11 a22b21
b21
Matrix Multiplication
• MATLAB code:
C=A*B
Inverse of a Matrix
• The inverse of a matrix A (i.e., A-1) is
defined by
AA-1=A-1A=I
©McGraw‐Hill Education.
Element-by-Element Calculations
At times, you will want to carry out calculations item by item
in a matrix or vector. The MATLAB manual calls these array
operations. They are also often referred to as element-by-
element operations.
MATLAB defines .* and ./ (note the dots) as the array
multiplication and array division operators.
• For array operations, both matrices must be the same size or one of
the matrices must be 1 by 1
©McGraw‐Hill Education.
Built-In Functions
There are several built-in functions you can use to create
and manipulate data.
The built-in help function can give you information about
both what exists and how those functions are used:
• help elmat will list the elementary matrix creation and
manipulation functions, including functions to get information about
matrices.
• help elfun will list the elementary math functions, including trig,
exponential, complex, rounding, and remainder functions.
©McGraw‐Hill Education.
Graphics
MATLAB has a powerful suite of built-in graphics
functions.
©McGraw‐Hill Education.
Plotting Example
t = [0:2:20]’;
g = 9.81; m = 68.1; cd = 0.25;
v = sqrt(g*m/cd) * tanh(sqrt(g*cd/m)*t);
plot(t, v)
©McGraw‐Hill Education.
Plotting Annotation Example
title('Plot of v versus t')
xlabel('Values of t')
ylabel('Values of v')
grid
©McGraw‐Hill Education.
Plotting Options
When plotting data, MATLAB can use several
different colors, point styles, and line styles. These
are specified at the end of the plot command
using plot specifiers as found in Table 2.2.
The default case for a single data set is to create a
blue line with no points. If a line style is specified
with no point style, no point will be drawn at the
individual points; similarly, if a point style is
specified with no point style, no line will be drawn.
Examples of plot specifiers:
• ‘ro:’ - red dotted line with circles at the points
• ‘gd’ - green diamonds at the points with no line
• ‘m--’ - magenta dashed line with no point symbols
©McGraw‐Hill Education.
Other Plotting Commands
hold on and hold off
• hold on tells MATLAB to keep the current data plotted
and add the results of any further plot commands to the
graph. This continues until the hold off command,
which tells MATLAB to clear the graph and start over if
another plotting command is given. hold on should be
used after the first plot in a series is made.
subplot(m, n, p)
• subplot splits the figure window into an mxn array of
small axes and makes the pth one active. Note - the first
subplot is at the top left, then the numbering continues
across the row. This is different from how elements are
numbered within a matrix!
©McGraw‐Hill Education.