KEMBAR78
S01 Numerical Methods 2024 Fall | PDF | Matrix (Mathematics) | Matlab
0% found this document useful (0 votes)
28 views42 pages

S01 Numerical Methods 2024 Fall

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)
28 views42 pages

S01 Numerical Methods 2024 Fall

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/ 42

Applied Numerical Methods

with MATLAB®
for Engineers and Scientists
4th Edition
Steven C. Chapra

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and


Prof. Steve Chapra, Tufts University

©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.
Part 1
Chapter 1
Mathematical Modeling,
Numerical Methods, and
Problem Solving

©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.
Chapter Objectives
• Learning how mathematical models can be formulated on
the basis of scientific principles to simulate the behavior of a
simple physical system.
• Understanding how numerical methods afford a means to
generalize solutions in a manner that can be implemented
on a digital computer.
• Understanding the different types of conservation laws that
lie beneath the models used in the various engineering
disciplines and appreciating the difference between steady-
state and dynamic solutions of these models.
• Learning about the different types of numerical methods we
will cover in this book.
3
A Simple Mathematical Model
• A mathematical model can be broadly defined as a
formulation or equation that expresses the essential
features of a physical system or process in mathematical
terms.
• Models can be represented by a functional relationship
between dependent variables, independent variables,
parameters, and forcing functions.

4
Model Function
Dependent independent forcing 
 f  , parameters, 
variable  variables functions

• Dependent variable - a characteristic that usually


reflects the behavior or state of the system
• Independent variables - dimensions, such as time and
space, along which the system’s behavior is being
determined
• Parameters - constants reflective of the system’s
properties or composition
• Forcing functions - external influences acting upon the
system
5
Model Function Example
• Assuming a bungee jumper is in mid-flight,
an analytical model for the jumper’s
velocity, accounting for drag, is

dv cd 2 gm  gc 
 g v vt  tanh d
t 
dt m cd  m 

• Dependent variable - velocity v


• Independent variables - time t
• Parameters - mass m, drag coefficient cd
• Forcing function - gravitational
acceleration g
6
Model Results
• Using a computer (or a calculator), the model can be used
to generate a graphical representation of the system. For
example, the graph below represents the velocity of a 68.1
kg jumper, assuming a drag coefficient of 0.25 kg/m

7
Numerical Modeling
• Some system models will be given as implicit functions or as
differential equations - these can be solved either using
analytical methods or numerical methods.

• Example - the bungee jumper velocity equation from before


is the analytical solution to the differential equation
dv cd 2
 g v
dt m
where the change in velocity is determined by the
gravitational forces acting on the jumper versus the drag
force.

8
Numerical Methods
• To solve the problem using a numerical method, note that
the time rate of change of velocity can be approximated as:

dv v vti1  vti 
 
dt t ti1  ti

dv cd 2
 g v
dt m

v(ti 1 )
 cd 
 v(ti )   g  v(ti )2  (ti1  ti )
 m  9
Numerical Results
• As shown in later chapters, the efficiency and accuracy of
numerical methods will depend upon how the method is
applied.
• Applying the previous method in 2 s intervals yields:

 c 
v(ti 1 )  v(ti )   g  d v(ti ) 2  (ti 1  ti )
 m 

10
11
Bases for Numerical Models
• Conservation laws provide the foundation for many model
functions.
• Different fields of engineering and science apply these laws
to different paradigms within the field.
• Among these laws are:
– Conservation of mass
– Conservation of momentum
– Conservation of charge
– Conservation of energy

12
Table 1.1

Copyright © McGraw-Hill Education. All rights reserved. No reproduction or distribution without the prior written consent of McGraw-Hill Education. 13
Summary of Numerical Methods
• The book is divided into five categories of numerical
methods:

14
Part 1
Chapter 2
MATLAB Fundamentals

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University


All images copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter Objectives
• Learning how real and complex numbers are assigned to
variables.
• Learning how vectors and matrices are assigned values
using simple assignment, the colon operator, and the
linspace and logspace functions.
• Understanding the priority rules for constructing
mathematical expressions.
• Gaining a general understanding of built-in functions and
how you can learn more about them with MATLAB’s Help
facilities.
• Learning how to use vectors to create a simple line plot
based on an equation.
16
The MATLAB Environment
• MATLAB uses three primary windows-
– Command window - used to enter commands and data
– Graphics window(s) - used to display plots and graphics
– Edit window - used to create and edit M-files (programs)
• Depending on your computer platform and the version of
MATLAB used, these windows may have different looks and
feels.

17
Calculator Mode
• The MATLAB command widow can be used as a calculator
where you can type in commands line by line. Whenever a
calculation is performed, MATLAB will assign the result to
the built-in variable ans

• Example:
>> 55 - 16
ans =
39

18
MATLAB Variables
• While using the ans variable may be useful for performing
quick calculations, its transient nature makes it less useful
for programming.
• MATLAB allows you to assign values to variable names.
This results in the storage of values to memory locations
corresponding to the variable name.
• MATLAB can store individual values as well as arrays; it can
store numerical data and text (which is actually stored
numerically as well).
• MATLAB does not require that you pre-initialize a variable; if
it does not exist, MATLAB will create it for you.

19
Scalars
• To assign a single value to a variable, simply type the
variable name, the = sign, and the value:
>> a = 4
a =
4

• Note that variable names must start with a letter, though


they can contain letters, numbers, and the underscore (_)
symbol. Variable names are case sensitive.

20
Scalars (cont)
• You can tell MATLAB not to report the result of a calculation
by appending the semi-colon (;) to the end of a line. The
calculation is still performed.

• You can ask MATLAB to report the value stored in a


variable by typing its name:
>> a
a =
4

21
Scalars (cont)
• You can use the complex variable i (or j) to represent the
unit imaginary number.
• You can tell MATLAB to report the values back using
several different formats using the format command. Note
that the values are still stored the same way, they are just
displayed on the screen differently. Some examples are:
– short - scaled fixed-point format with 5 digits
– long - scaled fixed-point format with 15 digits for double
and 7 digits for single
– short eng - engineering format with at least 5 digits
and a power that is a multiple of 3 (useful for SI prefixes)

22
Format Examples
• >> format short; pi
ans =
3.1416
>> format long; pi
ans =
3.14159265358979
>> format short eng; pi
ans =
3.1416e+000
>> pi*10000
ans =
31.4159e+003
• Note - the format remains the same unless another format
command is issued.
23
Arrays, Vectors, and Matrices
• MATLAB can automatically handle rectangular arrays of
data - one-dimensional arrays are called vectors and two-
dimensional arrays are called matrices.

• Arrays are set off using square brackets [ and ] in MATLAB

• Entries within a row are separated by spaces or commas

• Rows are separated by semicolons

24
Array Examples
• >> a = [1 2 3 4 5 ]
a =
1 2 3 4 5
>> b = [2;4;6;8;10]
b =
2
4
6
8
10
• Note 1 - MATLAB does not display the brackets
• Note 2 - if you are using a monospaced font, such as
Courier, the displayed values should line up properly
25
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

26
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.

• 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.

27
Accessing Array Entries
• 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

28
Accessing Array Entries (cont)
• 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! 29
Array Creation - Built In
• There are several built-in functions to create arrays:
– zeros(r,c) will create an r row by c column matrix of
zeros
– zeros(n) will create an n by n matrix of zeros
– ones(r,c) will create an r row by c column matrix of
ones
– ones(n) will create an n by n matrix one ones
• help elmat has, among other things, a list of the
elementary matrices

30
Array Creation - Colon Operator
• The colon operator : is useful in several contexts. It can be
used to create a linearly spaced array of points using the
notation
start:diffval:limit
where start is the first value in the array, diffval is the
difference between successive values in the array, and
limit is the boundary for the last value (though not
necessarily the last value).
>>1:0.6:3
ans =
1.0000 1.6000 2.2000 2.8000

31
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
32
operator, not the limit value; that is, (3:6)’ not 3:6’
Array Creation - linspace

• To create a row vector with a specific number of linearly


spaced points between two numbers, use the linspace
command.
• linspace(x1, x2, n) will create a linearly spaced array
of n points between x1 and x2
>>linspace(0, 1, 6)
ans =
0 0.2000 0.4000 0.6000 0.8000 1.0000
• If n is omitted, 100 points are created.

• To generate a column, transpose the output of the


linspace command.
33
Array Creation - logspace

• To create a row vector with a specific number of


logarithmically spaced points between two numbers, use the
logspace command.
• logspace(x1, x2, n) will create a logarithmically
spaced array of n points between 10x1 and 10x2
>>logspace(-1, 2, 4)
ans =
0.1000 1.0000 10.0000 100.0000
• If n is omitted, 100 points are created.

• To generate a column, transpose the output of the


logspace command.
34
Character Strings & Ellipsis
Alphanumeric constants are enclosed by apostrophes (')
>> f = 'Miles ';
>> s = 'Davis'
Concatenation: pasting together of strings
>> x = [f s]
x =
Miles Davis
Ellipsis (...): Used to continue long lines
>> a = [1 2 3 4 5 ...
6 7 8]
a =
1 2 3 4 5 6 7 8
You cannot use an ellipsis within single quotes to continue a string. But you can
piece together shorter strings with ellipsis
>> quote = ['Any fool can make a rule,' ...
' and any fool will mind it']
quote =
Any fool can make a rule, and any fool will mind it
Some Useful Character Functions
Function Description
Number of characters, n, in a string, s.

Converts a string, s, to a number, n.


Converts a number, n, to a string, s.

Replaces characters in a string with


different characters
Returns the starting indices of any
occurrences of the string s2 in the string s1
Converts a string to upper case
Converts a string to lower case
• % Example 1: Replace text in a character vector.

claim = 'This is a good example';


new_claim = strrep(claim, 'good', 'great')

new_claim =
This is a great example.
Mathematical Operations
• Mathematical operations in MATLAB can be performed on
both scalars and arrays.
• The common operators, in order of priority, are:
^ Exponentiation 4^2 = 8
- Negation -8 = -8
(unary operation)
* Multiplication and 2*pi = 6.2832
/ Division pi/4 = 0.7854
\ Left Division 6\2 = 0.3333
+ Addition and 3+5 = 8
- Subtraction 3-5 = -2

38
Order of Operations
• The order of operations is set first by parentheses, then by
the default order given above:
– y = -4 ^ 2 gives y = -16
since the exponentiation happens first due to its higher
default priority, but
– y = (-4) ^ 2 gives y = 16
since the negation operation on the 4 takes place first

39
Complex Numbers
• All the operations above can be used with complex
quantities (i.e. values containing an imaginary part entered
using i or j and displayed using i)
x = 2+i*4; (or 2+4i, or 2+j*4, or 2+4j)
y = 16;
3 * x
ans =
6.0000 +12.0000i
x+y
ans =
18.0000 + 4.0000i
x'
ans = 40
2.0000 - 4.0000i
Vector-Matrix Calculations
• MATLAB can also perform operations on vectors and
matrices.
• The * operator for matrices is defined as the outer product
or what is commonly called “matrix multiplication.”
– The number of columns of the first matrix must match the number of
rows in the second matrix.
– The size of the result will have as many rows as the first matrix and
as many columns as the second matrix.
– The exception to this is multiplication by a 1x1 matrix, which is
actually an array operation.

• The ^ operator for matrices results in the matrix being


matrix-multiplied by itself a specified number of times.
– Note - in this case, the matrix must be square!
41
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 1x1

• Array exponentiation (raising each element to a


corresponding power in another matrix) is performed with .^
– Again, for array operations, both matrices must be the same size or
one of the matrices must be 1x1
42

You might also like