+ All Categories
Home > Documents > September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

Date post: 17-Jan-2018
Category:
Upload: theodora-welch
View: 219 times
Download: 0 times
Share this document with a friend
Description:
September 15, 2005 Lecture 5 - By Paul Lin More MATLAB Functions size(this_array) returns the two values specifying the number of rows and columns in this_arrayreturns the two values specifying the number of rows and columns in this_arraylength(this_array) returns the length of a vector, or the longest dimension of a 2-D arrayreturns the length of a vector, or the longest dimension of a 2-D arrayzeros(n) built-in MATLAB function for creating an n- by-n array with all elements are initialized to zerobuilt-in MATLAB function for creating an n- by-n array with all elements are initialized to zero zeros(n, m) for creating an n-by-m all-zero arrayfor creating an n-by-m all-zero array
23
September 15, 2005 September 15, 2005 Lecture 5 - By Paul Lin Lecture 5 - By Paul Lin 1 CPET 190 CPET 190 Lecture 5 Lecture 5 Problem Solving with Problem Solving with MATLAB MATLAB http://www.etcs.ipfw.edu/~lin http://www.etcs.ipfw.edu/~lin
Transcript
Page 1: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 11

CPET 190 CPET 190

Lecture 5Lecture 5Problem Solving with MATLABProblem Solving with MATLAB

http://www.etcs.ipfw.edu/~linhttp://www.etcs.ipfw.edu/~lin

Page 2: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 22

Lecture 5: More On MATLAB Lecture 5: More On MATLAB ArraysArrays

5.1 More MATLAB Functions5.1 More MATLAB Functions5.2 Array Operators5.2 Array Operators5.3 Array Arithmetic Operations5.3 Array Arithmetic Operations5.4 Sub-arrays5.4 Sub-arrays

Page 3: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 33

5.1 More MATLAB Functions5.1 More MATLAB Functions

size(this_array) size(this_array) • returns the two values specifying the returns the two values specifying the

number of rows and columns in this_arraynumber of rows and columns in this_arraylength(this_array) length(this_array)

• returns the length of a vector, or the returns the length of a vector, or the longest dimension of a 2-D arraylongest dimension of a 2-D array

zeros(n) zeros(n) • built-in MATLAB function for creating an n-built-in MATLAB function for creating an n-

by-n array with all elements are initialized by-n array with all elements are initialized to zeroto zero

zeros(n, m) zeros(n, m) • for creating an n-by-m all-zero array for creating an n-by-m all-zero array

Page 4: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 44

5.1 More MATLAB Functions5.1 More MATLAB Functions (continue)(continue)

ones(n) ones(n) • built-in MATLAB function for creating an n-built-in MATLAB function for creating an n-

by-n array with all elements are initialized to by-n array with all elements are initialized to oneone

ones(n, m) ones(n, m) • for creating an n-by-m all-one arrayfor creating an n-by-m all-one array

eye(n)eye(n)• for creating an n-by-n identity matrix in which for creating an n-by-n identity matrix in which

all elements in the main diagonal are onesall elements in the main diagonal are oneseye(n, m) eye(n, m)

• for creating an n-by-m identity matrixfor creating an n-by-m identity matrix

Page 5: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 55

5.1 More MATLAB Functions5.1 More MATLAB Functions (continue)(continue)

>> help rand>> help randRAND Uniformly distributed random numbers.RAND Uniformly distributed random numbers. RAND(N) is an N-by-N matrix with random entries, chosen RAND(N) is an N-by-N matrix with random entries, chosen

from a uniform distribution on the interval (0.0,1.0).from a uniform distribution on the interval (0.0,1.0).

RAND(M,N) and RAND([M,N]) are M-by-N matrices with RAND(M,N) and RAND([M,N]) are M-by-N matrices with random entries.random entries.

RAND(M,N,P,...) or RAND([M,N,P,...]) generate random RAND(M,N,P,...) or RAND([M,N,P,...]) generate random arrays.arrays.

RAND with no arguments is a scalar whose value RAND with no arguments is a scalar whose value changes each time it is referenced. changes each time it is referenced.

RAND(SIZE(A)) is the same size as A.RAND(SIZE(A)) is the same size as A.

Page 6: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 66

5.1 More MATLAB Functions5.1 More MATLAB Functions (continue)(continue)

>> help fix>> help fix

FIX Round towards zero.FIX Round towards zero.FIX(X) rounds the elements of X to the nearest FIX(X) rounds the elements of X to the nearest integers towards zero.integers towards zero.

See also FLOOR, ROUND, CEIL.See also FLOOR, ROUND, CEIL.

Page 7: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 77

5.1 More MATLAB Functions5.1 More MATLAB Functions (continue)(continue)

Example 5.1: Creating Example 5.1: Creating arrays using array arrays using array functionsfunctions

n = 3; m = 2;n = 3; m = 2; % an 3 x 3 all-zero array% an 3 x 3 all-zero arrayA0 = zeros(3)A0 = zeros(3) % an 3 x 2 all zero array% an 3 x 2 all zero arrayB0 = zeros(3, 2)B0 = zeros(3, 2)% an 3 x 3 all-one array% an 3 x 3 all-one arrayA1 = ones(3)A1 = ones(3)% an 3 x 2 all-zero array% an 3 x 2 all-zero arrayB1 = ones(3,2)B1 = ones(3,2)%An identify array%An identify arrayA_eye = eye(n)A_eye = eye(n)

A1 =

1 1 1 1 1 1 1 1 1

B1 = 1 1 1 1 1 1

A_eye = 1 0 0 0 1 0 0 0 1

A0 = 0 0 0 0 0 0 0 0 0

B0 = 0 0 0 0 0 0

Page 8: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 88

5.2 Array Operators5.2 Array Operators

Transpose operator ( Transpose operator ( '' ) )• swap the rows and columns of an swap the rows and columns of an

arrayarrayColon operator (: )Colon operator (: )

• one of the most useful operator in one of the most useful operator in MATLAB. We can use it to create MATLAB. We can use it to create regularly spaced vectors, subscript regularly spaced vectors, subscript matrices, and specify for iterationsmatrices, and specify for iterations

Page 9: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 99

5.2 Array Operators5.2 Array OperatorsExample 5.2: Vector Example 5.2: Vector

Transpose Example Transpose Example rand() – random number rand() – random number

generation functiongeneration function>> % Generate an array >> % Generate an array

using rand() functionusing rand() functionarray_r1 = rand(1, 4)array_r1 = rand(1, 4)array_r2 = rand(1, 4)array_r2 = rand(1, 4)% Convert row vector to % Convert row vector to

column vectorcolumn vectorarray_c1 = array_r1'array_c1 = array_r1'array_c2 = array_r2'array_c2 = array_r2'

array_r1array_r1 = 0.2722 0.1988 = 0.2722 0.1988 0.0153 0.74680.0153 0.7468

array_r2array_r2 = 0.4451 0.9318 = 0.4451 0.9318 0.4660 0.41860.4660 0.4186

array_c1array_c1 = = 0.27220.2722 0.19880.1988 0.01530.0153 0.74680.7468

array_c2array_c2 = = 0.44510.4451 0.93180.9318 0.46600.4660 0.41860.4186

Page 10: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 1010

5.2 Array Operators 5.2 Array Operators (continue)(continue)

Example 5.3: Using rand(), Example 5.3: Using rand(), fix() functions and array fix() functions and array transpose operatortranspose operator

>> A = rand(1,4) *10>> A = rand(1,4) *10A =A =

0.5789 3.5287 8.1317 0.09860.5789 3.5287 8.1317 0.0986

>> A = fix(A)>> A = fix(A)A =A =

0 3 8 00 3 8 0

>> B = fix(rand(1,4)*10)>> B = fix(rand(1,4)*10)

B =B =

1 2 1 61 2 1 6

>> C = [A' B']>> C = [A' B']

C =C =

0 10 1 3 23 2 8 18 1 0 60 6

Page 11: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 1111

5.2 Array Operators5.2 Array Operators (continue)(continue)

Example 5.4: Example 5.4: Using colon Using colon operator to pick-up selected operator to pick-up selected rows or columnsrows or columns

A(: , j) – extracts j-th column of AA(: , j) – extracts j-th column of A A(i, :) – extracts the i-th row of AA(i, :) – extracts the i-th row of AC =C = 0 10 1 3 23 2 8 18 1 0 60 6C_1 = C(:,1)C_1 = C(:,1)C_2 = C(:, 2)C_2 = C(:, 2)R_1 = C(1,:)R_1 = C(1,:)R_2 = C(2, :)R_2 = C(2, :)R_3 = C(3, :)R_3 = C(3, :)R_4 = C(4, :)R_4 = C(4, :)

R_1 =R_1 =

0 10 1

R_2 =R_2 =

3 23 2

R_3 =R_3 =

8 18 1

R_4 =R_4 =

0 60 6

C_1 =C_1 =

00 33 88 00

C_2 =C_2 =

11 22 11 66

Page 12: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 1212

5.2 Array Operators5.2 Array Operators (continue)(continue)

Example 5.5: This problem solving example Example 5.5: This problem solving example uuses the colon operator with integers to ses the colon operator with integers to generate a regularly spaced temperature vector generate a regularly spaced temperature vector from 0 to 100 in degree C.from 0 to 100 in degree C. We will also print all We will also print all data to show that C to F Temperature data to show that C to F Temperature Conversion holding a linear relationship. Conversion holding a linear relationship.

Analysis (identified equations and MATLAB Analysis (identified equations and MATLAB equations, and using plot function to show the equations, and using plot function to show the linear relationship) linear relationship) F = 9/5 C + 32F = 9/5 C + 32C = 0:10:100;C = 0:10:100;F = 9/5 * C + 32;F = 9/5 * C + 32;plot(C, F)plot(C, F)

Page 13: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 1313

5.2 Array Operators 5.2 Array Operators (continue)(continue)

Example 5.5: SolutionExample 5.5: Solution

% CtoF_plot.m% CtoF_plot.m% Author: M. Lin% Author: M. Lin% Date: 9/6/04% Date: 9/6/04% Description:% Description:C = 0:10:100;C = 0:10:100;F = (9/5)* C + 32;F = (9/5)* C + 32;plot(C, F), grid onplot(C, F), grid onxlabel('Degree C'), xlabel('Degree C'),

ylabel('Degree F')ylabel('Degree F')title(' C vs F')title(' C vs F')

0 10 20 30 40 50 60 70 80 90 10020

40

60

80

100

120

140

160

180

200

220

Degree C

Deg

ree

F

C vs F

Page 14: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 1414

5.2 Array Operators5.2 Array Operators (continue)(continue)

Example 5.6: Example 5.6: Reconstruct the sine Reconstruct the sine 60Hz signal and its time 60Hz signal and its time vector as a 2-D array vector as a 2-D array and save it as and save it as sine60hz.mat file. Verify sine60hz.mat file. Verify result by reloading the result by reloading the program and plot the program and plot the sine wave using sine wave using colon colon operator.operator.

Solution:Solution: f = 60 Hz, T = 1/ff = 60 Hz, T = 1/f

Vm = 10 volts, Vm = 10 volts, dt = 0.001*T,dt = 0.001*T, t = 0:dt: 5*Tt = 0:dt: 5*T e = Vm*sin(2*pi*f*t) e = Vm*sin(2*pi*f*t)

% sine60hz_2d.m% sine60hz_2d.m% Author: M. Lin% Author: M. Lin% Date: 9/6/04% Date: 9/6/04% Description:% Description:f = 60; T = 1/f; Vm = 10; f = 60; T = 1/f; Vm = 10; dt = 0.001*T;dt = 0.001*T;t = 0:dt: 5*T;t = 0:dt: 5*T;e = Vm*sin(2*pi*f*t);e = Vm*sin(2*pi*f*t);sine60 = [t' e'];sine60 = [t' e'];save sine60.mat sine60save sine60.mat sine60clearclearload sine60.matload sine60.matplot(sine60(:,1), plot(sine60(:,1), sine60(:,2))sine60(:,2))

Page 15: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 1515

5.2 Array Operators 5.2 Array Operators (continue)(continue)

Example 5.6: MATLAB SolutionExample 5.6: MATLAB Solution

0 0.01 0.02 0.03 0.04 0.05 0.06 0.07 0.08 0.09-10

-8

-6

-4

-2

0

2

4

6

8

10

Page 16: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 1616

5.3 Array Arithmetic Operations5.3 Array Arithmetic Operations

+ Addition+ Addition• A + B adds A and B arrays of the same A + B adds A and B arrays of the same

dimension, unless one is a scalar. A scalar dimension, unless one is a scalar. A scalar can be added to a matrix of any dimension.can be added to a matrix of any dimension.

• An Example: both A and B are 2-by-2 array, An Example: both A and B are 2-by-2 array, A + B is A + B is

| (a11 + b11) (a12 + b12) || (a11 + b11) (a12 + b12) || (a21 + b21) (a22 + b22) || (a21 + b21) (a22 + b22) |

Page 17: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 1717

5.3 Array Arithmetic Operations5.3 Array Arithmetic Operations(continue)(continue)

- Subtraction. - Subtraction. • A - B subtracts B from A. A and B arrays A - B subtracts B from A. A and B arrays

must have the same dimension, unless one must have the same dimension, unless one is a scalar. A scalar can be subtracted from is a scalar. A scalar can be subtracted from a matrix of any dimension.a matrix of any dimension.

• An Example: both A and B are 2-by-2 array, An Example: both A and B are 2-by-2 array, A - B isA - B is

| (a11 - b11) (a12 - b12) || (a11 - b11) (a12 - b12) || (a21 - b21) (a22 - b22) || (a21 - b21) (a22 - b22) |

Page 18: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 1818

5.3 Array Arithmetic Operations 5.3 Array Arithmetic Operations (continue)(continue)

.* Array Multiplication.* Array Multiplication• A .* B is the element-by-element product of A .* B is the element-by-element product of

the arrays A and B. A and B must have the the arrays A and B. A and B must have the same dimension, unless one is a scalar. A same dimension, unless one is a scalar. A scalar can be multiplied to a matrix of any scalar can be multiplied to a matrix of any dimension.dimension.

• An Example: both A and B are 2-by-2 array, An Example: both A and B are 2-by-2 array, A .* B isA .* B is

| (a11 * b11) (a12 * b12) || (a11 * b11) (a12 * b12) || (a21 * b21) (a22 * b22) | | (a21 * b21) (a22 * b22) |

Page 19: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 1919

5.3 Array Arithmetic Operations 5.3 Array Arithmetic Operations (continue)(continue)

./ Array Right Division ./ Array Right Division • A ./B is the element-by-element division of A ./B is the element-by-element division of

the arrays A and B. A and B must have the the arrays A and B. A and B must have the same dimension, unless one is a scalar. A same dimension, unless one is a scalar. A scalar can be divided by a matrix of any scalar can be divided by a matrix of any dimension.dimension.

• An Example: both A and B are 2-by-2 An Example: both A and B are 2-by-2 array, A ./ B isarray, A ./ B is

| (a11 / b11) (a12 / b12) || (a11 / b11) (a12 / b12) || (a21 / b21) (a22 / b22) | | (a21 / b21) (a22 / b22) |

Page 20: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 2020

5.3 Array Arithmetic Operations 5.3 Array Arithmetic Operations (continue)(continue)

Example 5.7: Element-by-Example 5.7: Element-by-element array element array arithmetic operationsarithmetic operations

A = fix(rand(2)*10)A = fix(rand(2)*10)B = fix(rand(2)*10)B = fix(rand(2)*10)W = A - BW = A - BX = A + BX = A + BY = A .* BY = A .* BZ = A./ BZ = A./ B

>> A = fix(rand(2)*10)B = fix(rand(2)*10)W = A - BX = A + BY = A .* BZ = A./ B

A =

9 6 2 4

B =

8 4 7 0

Page 21: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 2121

5.3 Array Arithmetic Operations 5.3 Array Arithmetic Operations (continue)(continue)

Example 5.7: continueExample 5.7: continueW = A - BW = A - BX = A + BX = A + BY = A .* BY = A .* BZ = A./ BZ = A./ B

A =A = 9 69 6 2 42 4B =B = 8 48 4 7 07 0>> >> A - BA - BW =W = 1 21 2 -5 4-5 4>> >> A + BA + BX =X = 17 1017 10 9 49 4

>> >> A .* BA .* BY =Y = 72 2472 24 14 14 00>> >> A./BA./BWarning: Divide by zero.Warning: Divide by zero.(Type "warning off (Type "warning off MATLAB: divideByZero" to MATLAB: divideByZero" to suppress this warning.)suppress this warning.)Z =Z = 1.1250 1.50001.1250 1.5000 0.2857 0.2857 InfInf

Page 22: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 2222

5.4 SubArrays5.4 SubArraysSubarrays can be formed by using colon (: ) Subarrays can be formed by using colon (: )

operator to select a portion of an arrayoperator to select a portion of an array

Example 5.9: Subarray ExamplesExample 5.9: Subarray Examplesarray4 = [10 20 30; -20 -30 -40; 30 40 50]array4 = [10 20 30; -20 -30 -40; 30 40 50]array5 = array4(1, : ) % [10 20 30]array5 = array4(1, : ) % [10 20 30]array6 = array4(: ,1: 2: 3) % [first column third column]array6 = array4(: ,1: 2: 3) % [first column third column]array6 =array6 = 10 3010 30 -20 -40-20 -40 30 5030 50

array1 = [1 2 3 4 5];array1 = [1 2 3 4 5];array7 = array1(3: end) array7 = array1(3: end) array 7 = 3 4 5array 7 = 3 4 5array1(end) array1(end) ans = 5ans = 5

Page 23: September 15, 2005 Lecture 5 - By Paul Lin 1 CPET 190 Lecture 5 Problem Solving with MATLAB

September 15, 2005September 15, 2005 Lecture 5 - By Paul LinLecture 5 - By Paul Lin 2323

SummarySummary

5.1 More MATLAB Functions5.1 More MATLAB Functions5.2 Array Operators5.2 Array Operators5.3 Array Arithmetic Operations5.3 Array Arithmetic Operations5.4 Subarrays5.4 Subarrays


Recommended