Section 1 Introduction - College of...

48
MAE 4020/5020 – Numerical Methods with MATLAB SECTION 1: INTRODUCTION

Transcript of Section 1 Introduction - College of...

Page 1: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

MAE 4020/5020 – Numerical Methods with MATLAB

SECTION 1: INTRODUCTION

Page 2: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

Mathematical Models2

K. Webb  MAE 4020/5020

Page 3: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

3

Models

K. Webb  MAE 4020/5020

As engineers, we are interested in analyzing and designing physical systems

We represent these systems with models: Abstracted representation of the real system Captures some of the real system’s behavior – the behavior we care about

Simplified in some way Smaller Less complex Linear Lossless, etc. … 

Page 4: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

4

Mathematical Models

K. Webb  MAE 4020/5020

Model of a physical system may be: A physical system itself, simplified in some way e.g., scale model for wind‐tunnel testing

A mathematical model An equation or system of equations that describe the aspects of system behavior that interest us (while ignoring others)

A physical model as an intermediate step in generating a mathematical model An abstraction of the real system, whose behavior we can describe with mathematical expressions 

Page 5: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

Mathematical Models5

K. Webb  MAE 4020/5020

Page 6: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

6

Modeling Example

K. Webb  MAE 4020/5020

Vehicle suspension design – quarter‐car model Simplification of the overall vehicle

Considers only one wheel (one quarter of the car) at a time

Elements may be assumed linear (or not)

Accounts for car body, shock absorbers, wheel, and tire

Road surface provides displacement input

Page 7: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

7

Modeling Example

K. Webb  MAE 4020/5020

By applying Newton’s second law to our simplified physical model, we can generate a mathematical model for our system

The resulting mathematical model is a pair of coupled second‐order differential equations:

0

 

Page 8: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

8

Modeling Example

K. Webb  MAE 4020/5020

Can rearrange into a single fourth‐order ODE Or, express in state‐space form:

In all forms, the model describes the relationship between:  Output (dependent variable) – (e.g. xs(t), in our example) Independent variable – (e.g. t) Input (forcing function) – (e.g. xr(t))

Dependent on parameters – (e.g. k, b, kt, ms, and mus) Model (or it’s solution) has the form:

. . , ,

Page 9: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

9

Modeling Example

K. Webb  MAE 4020/5020

The resulting mathematical model can be solved analytically Tractable, though tedious for this fourth‐order system

Solution is well‐suited to numerical analysis using MATLAB or similar tool

Quickly and easily determine system response to various inputs e.g., how does the suspension respond to driving over a bump

See the effects of system parameters e.g., spring constant, damping coefficient

Page 10: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

10

Numerical Solution to Our Mathematical Model

K. Webb  MAE 4020/5020

Page 11: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

11

Parametric Analysis

K. Webb  MAE 4020/5020

Page 12: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

12

Parametric Analysis – Waterfall Plot

K. Webb  MAE 4020/5020

Page 13: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

13

Parametric Analysis – Surface plot

K. Webb  MAE 4020/5020

Page 14: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

14

Visualization of Numerical Solution ‐ Animation

K. Webb  MAE 4020/5020

Page 15: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

Introduction to MATLAB15

K. Webb  MAE 4020/5020

Page 16: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

16

The MATLAB Desktop

K. Webb  MAE 4020/5020

Workspace

FileBrowser

CommandHistory

Page 17: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

17

The MATLAB Desktop – Command Window

K. Webb  MAE 4020/5020

Command‐line operation Behaves like a calculator

Useful for:  Quick calculations Simple debugging tasks

Page 18: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

18

The MATLAB Desktop – Editor Window

K. Webb  MAE 4020/5020

Editor for m‐files Scripts  Functions

Built‐in debugger Set breakpoints Step through code line‐by‐line or by section

Page 19: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

19

The MATLAB Desktop – Workspace

K. Webb  MAE 4020/5020

Lists all variables currently stored in memory Values for scalars and small arrays

Size and data types for larger arrays

Double‐click a variable to open in a spreadsheet

Page 20: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

20

The MATLAB Desktop – Current Folder

K. Webb  MAE 4020/5020

File browser A built‐in ‘Windows Explorer’

Open, move, copy, rename, delete files from within MATLAB

Page 21: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

21

The MATLAB Desktop – Command History

K. Webb  MAE 4020/5020

Lists previously‐executed commands All commands issued through the command window

Double‐click to re‐execute

Arrow keys cycle through command history in the command window

Page 22: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

22

The MATLAB Desktop – Docking Windows

K. Webb  MAE 4020/5020

Page 23: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

23

The MATLAB Desktop – Docking Windows

K. Webb  MAE 4020/5020

Docked windowsstay on top of the desktopWon’t get hidden below other windows

Can dock figure windows as well

Page 24: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

24

The MATLAB Desktop – Saving Layouts

K. Webb  MAE 4020/5020

Favorite desktop configuration or configurations can be saved

Page 25: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

25

Assignment of Variables

K. Webb  MAE 4020/5020

Can define variables and assign values

Variable and value echoed in command window

Terminating command with semicolon suppresses echo

Variables then  appear in workspace

Page 26: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

26

Scalars, Arrays, Vectors, and Matrices

K. Webb  MAE 4020/5020

MATLAB treats all variables as matrices or arrays The MAT in MATLAB is for MATrix (not MAThematics)

Matrix m x n array of values – m rows, n columns

Scalar 1 x 1 matrix

Vectors 1 x n matrix – one row, n columns – a row vector m x 1 matrix – m rows, 1 column – a column vector

Multi‐dimensional array m x n x p array – an array of p m x n matrices

Page 27: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

27

Vectors and Matrices

K. Webb  MAE 4020/5020

Arrays are enclosed in square brackets

Row vector Commas separate entries on the same rows

Column vector Semicolons separate rows

A 3 x 3 matrix

Page 28: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

28

Vectors and Matrices

K. Webb  MAE 4020/5020

The entries in an array can be arrays themselves

Must conform to size of other entries All entries on a row must have same number of rows

All entries on a column must have same number of columns

Here, A and B are being put on same row, but A has 1 row, while B has 3

Page 29: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

29

Strings

K. Webb  MAE 4020/5020

In addition to numeric data types, MATLAB supports the use of strings Variables whose values are text strings

Enclose strings in single quotes, e.g. ‘a string’

Many MATLAB functions require string input arguments, e.g. filenames, plot annotation, etc.

Page 30: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

30

Mathematical Operations

K. Webb  MAE 4020/5020

All the usual mathematical operations are supported

Operate on numbers or on variables

Again, default variable type is a matrix Operations on matrices are not the same as on scalars

More to come in a few slides, but must be careful

Page 31: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

31

Built‐In Functions and Constants

K. Webb  MAE 4020/5020

Default logarithm is natural log Specify base 2 or 10 if desired

Trigonometric functions assume arguments in radians

Append a ‘d’ to trigonometric functions for input/output in degrees

Page 32: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

32

Built‐In Functions and Constants

K. Webb  MAE 4020/5020

Pi ( ) is a built‐in constant in MATLAB

i and j are both defined as the imaginary unit ( )

Built‐in constants can be redefined Constants will revert to their default values when workspace memory is cleared

Page 33: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

33

Array Generation – Colon Operator

K. Webb  MAE 4020/5020

Here, we define a row vector of values from 1 to 8, incremented by 1

Can also set the increment value to something other than 1

Increment value need not be positive

Page 34: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

34

Array Generation – ones(), zeros()

K. Webb  MAE 4020/5020

Often need to create arrays of all 1’s or all zeros

ones(m,n) creates a vector of all 1’s with m rows and n columns

zeros(m,n) creates an  m x n matrix of all 0’s

ones(N) or zeros(N) generates N x N matrices

Page 35: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

35

Indexing Arrays

K. Webb  MAE 4020/5020

Access individual elements or ranges of elements in an array by using subscript notation, e.g.:

, is the element from the row and the  column In MATLAB:  A(2,3) is the element in the 2nd row, 3rd column of A

Use colon operator for ranges of rows or columns e.g. A(1:3,2:5) represents entries in rows 1 – 3 and columns 2 –5.

A(:,2) represents the elements in all rows, 2nd column of A

Page 36: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

36

Array Size functions

K. Webb  MAE 4020/5020

size(A) returns a 1 x 2 vector containing the number of rows and columns of A

length(A) returns the largest dimension of A length(A)= max(size(A)) Useful for vectors

end is the last possible indexing value Row or column depending on what it is used to index

Page 37: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

The following is a brief review of some very basic linear algebra concepts. We will return to more advanced topics in linear algebra later on in the course.

Linear Algebra Fundamentals37

K. Webb  MAE 4020/5020

Page 38: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

38

Matrix Addition and Subtraction

K. Webb  MAE 4020/5020

As long as matrices have the same dimensions, we can add or subtract them Addition is done element‐by‐element, and the resulting matrix is the same size

We can also add scalars to matrices

Page 39: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

39

Matrix Multiplication

K. Webb  MAE 4020/5020

In order to multiply matrices their inner dimensionsmust agree

We can multiply  only if the number of columns of  is equal to the number of rows of 

Resulting Matrix has same number of rows as  and same number of columns as 

(m x n)  (n x p)  (m x p) ∙

Page 40: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

40

Matrix Multiplication

K. Webb  MAE 4020/5020

The  entry of  is the dot product of the  row of  with the  column of 

Page 41: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

41

Matrix Multiplication

K. Webb  MAE 4020/5020

Remember, matrices don’t need to be the same size, but their inner dimensions must agree

Matrix multiplication is not commutative, i.e., order matters. In general

Page 42: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

42

Vector Multiplication

K. Webb  MAE 4020/5020

Two types of vector multiplication: Inner product (dot product)

Result is a scalar

Outer product  Result for n‐vectors is an n x n matrix

Page 43: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

43

Matrix ‘Division’ – Multiplication by the Inverse

K. Webb  MAE 4020/5020

Scalar division that we are accustomed to can be thought of as multiplication by an inverse:

This is how we ‘divide’ matrices as well

Multiplication of a scalar by its inverse is equal to 1. For a matrix, the result is the identity matrix

Page 44: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

44

Matrix Inverse

K. Webb  MAE 4020/5020

Again, recall that matrix multiplication is not commutative Right‐multiplication is not the same as left‐multiplication

The inverse does not exist for some matrices If not, matrix is singularMust be full rank Non‐squarematrices are rank deficient, so are not invertible

Page 45: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

45

Matrix Inverse in MATLAB

K. Webb  MAE 4020/5020

Use inv(…) to generate a matrix inverse

A matrix times its inverse is the identity matrix

Can also use the left‐divideoperator (backslash) If  exists, then

\ If  does not exist, then left‐division means something different. We’ll come back to this later.

Page 46: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

46

Exponentiation

K. Webb  MAE 4020/5020

As with scalars, raising a matrix to the power, n, is the multiplication of that matrix by itself n times

What must be true of a matrix for exponentiation to be allowable?

Inner matrix dimensions must agree Rows of  must equal columns of  – n x nMatrix must be square

Page 47: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

47

The MATLAB ‘Dot’ Operator 

K. Webb  MAE 4020/5020

Multiply, divide, exponentiate, etc. matrices element‐by‐elementMatrix dimensions (not just inner dimensions) must agree

Use the dot operator:

For example:

Page 48: Section 1 Introduction - College of Engineeringweb.engr.oregonstate.edu/~webbky/MAE4020_5020_files/Section 1... · Vehicle suspension design – quarter‐car model ... Can rearrange

48

Passing Matrices to Functions

K. Webb  MAE 4020/5020

Built‐in MATLAB functions (e.g., cos(…), exp(…), sqrt(…), log(…), etc.) will operate on matrices

The function is applied element‐by‐element Some functions also have special matrix versions, e.g. expm(…), sqrtm(…)