You are on page 1of 16

1.

00 Lecture 21

Integration, concluded
Matrices

Better Trapezoid Rule

Individual trapezoid approximation:


x2


x1
f ( x ) dx = h ( 0 . 5 f 1 + 0 . 5 f 2 ) + O ( h 3 f ’’)

Use this N-1 times for (x1, x2), (x2, x3), …(xN-1, xN) and
add the results:
xN

∫ f ( x ) dx = h ( 0 .5 f
x1
1 + f 2 + ... + f N −1 + 0 .5 f N ) + O (( b − a ) 3 f ’’/ N 2 )

1
Better Trapezoid Rule

1 9

N=1, requires two function evaluations

Better Trapezoid Rule

1 5 9

N=2, requires only one more function evaluation

2
Better Trapezoid Rule

1 3 5 7 9

N=4, requires only two more function evaluations

Better Trapezoid Rule

1 2 3 4 5 6 7 8 9

N=8, requires only 4 more function evaluations

3
Using Trapezoidal Rule
• Keep cutting intervals in half until desired
accuracy met
– Estimate accuracy by change from previous
estimate
– Each halving requires relatively little work because
past work is retained
• By using a quadratic interpolation (Simpson’s
rule) to function values instead of linear
(trapezoidal rule), we get better error behavior
– By good fortune, errors cancel well with quadratic
approximation used in Simpson’s rule
– Computation same as trapezoid, but uses different
weighting for function values in sum

Extended Trapezoid Method


FODVV7UDSH]RLG^ 1XP5HFS

SXEOLFVWDWLFGRXEOH WUDS]G 0DWK)XQFWLRQIXQFGRXEOHD

GRXEOHE LQW Q ^

LI Q  ^

V  ED IXQFI D IXQFI E 

UHWXUQV`

HOVH^

LQW LW  $GGOLQWHULRUSRLQWV

IRU LQW M MQM

LW 6XEGLYLVLRQV

GRXEOH WQP LW 'RXEOHYDOXHRILW

GRXEOHGHOWD  ED WQP6SDFLQJRISRLQWV

GRXEOH[ D GHOWD3WWRHYDOXDWHI [

GRXEOHVXP  &RQWULERIQHZSWV[

IRU LQW M MLWM ^

VXP IXQFI [ 

[ GHOWD`

V  V ED VXPWQP 9DOXHRILQWHJUDO

UHWXUQV``

SULYDWHVWDWLF GRXEOHV`&XUUHQWYDOXHRILQWHJUDO

4
Extended Simpson Method

Approximate function with quadratic, not linear form

Extended Simpson Method


SXEOLFFODVV6LPSVRQ^ 1XP5HFS

SXEOLFVWDWLFGRXEOH TVLPS 0DWK)XQFWLRQIXQFGRXEOHD

GRXEOHE ^

GRXEOH RVW (

GRXEOH RV (

IRU LQW M M-0$;M ^

GRXEOH VW 7UDSH]RLGWUDS]G IXQFDEM 

V   VW  RVW 6HH 1XP5HFHT

LI M! $YRLGVSXULRXVHDUO\FRQYHUJHQFH

LI 0DWKDEV VRV (36,/21 0DWKDEV RV __

V  RV  ^

6\VWHPRXWSULQWOQ 6LPSVRQLWHUM 

UHWXUQV`

RV V

RVW VW`

6\VWHPRXWSULQWOQ 7RRPDQ\VWHSVLQ TVLPS 

UHWXUQ(55B9$/`

SULYDWHVWDWLFGRXEOHV

SXEOLFVWDWLFILQDOGRXEOH(36,/21 (

SXEOLFVWDWLFILQDO LQW -0$; 

SXEOLFVWDWLFILQDOGRXEOH(55B9$/ (`

5
Using the Methods
SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@ DUJV ^

6LPSOHH[DPSOHZLWKMXVW WUDS]G VHH 1XP5HF S

6\VWHPRXWSULQWOQ 6LPSOHWUDSH]RLGXVH 

LQW P :DQWAPVWHSV

LQW M P

GRXEOH DQV 

IRU M M PM ^0XVWXVH7UDS]GLQORRS

DQV 7UDSH]RLGWUDS]G QHZ )XQF& M 

6\VWHPRXWSULQWOQ ,WHUDWLRQ M ´

,QWHJUDO DQV `

6\VWHPRXWSULQWOQ ,QWHJUDO DQV 

([DPSOHXVLQJH[WHQGHG6LPSVRQPHWKRG

6\VWHPRXWSULQWOQ 6LPSVRQXVH 

DQV TVLPS QHZ )XQF&  

6\VWHPRXWSULQWOQ ,QWHJUDO DQV 

6\VWHPH[LW  `` (QG6LPSVRQFODVV

FODVV )XQF& LPSOHPHQWV 0DWK)XQFWLRQ ^

SXEOLFGRXEOHI GRXEOH[ ^

UHWXUQ[ [ [ [``

Romberg Integration
• Generalization of Simpson (NumRec p. 140)
– Based on numerical analysis to remove more
terms in error series associated with the
numerical integral
• Uses trapezoid as building block as does Simpson
– Romberg is adequate for smooth (analytic)
integrands, over intervals with no singularities,
where endpoints are not singular
– Romberg is much faster than Simpson or the
elementary routines. For a sample integral:
• Romberg: 32 iterations
• Simpson: 256 iterations
• Trapezoid: 8192 iterations

6
Improper Integrals
• Improper integral defined as having
integrable singularity or approaching
infinity at limit of integration
– Use extended midpoint rule instead of
trapezoid rule to avoid function evaluations at
singularities or infinities
• Must know where singularities or infinities are
– Use change of variables: often replace x with
1/t to convert an infinity to a zero
• Done implicitly in many routines
• Last improvement: Gaussian quadrature
– In Simpson, Romberg, etc. the x values are
evenly spaced. By relaxing this, we can get
better efficiency and often better accuracy

Midpoint Rule

See Numerical Recipes for discussion, code

7
Matrices
• Matrix is 2-D array of m rows by n columns
a00 a01 a02 a03 … a0n
a10 a11 a12 a13 … a1n
a20 a21 a22 a23 … a02n
… … … … … …
am0 am1 am2 am3 … amn

– In math notation, we use index 1, … m and 1, … n.


– In Java, we usually use index 0, … m-1 and 0, …n-1
• They often represent a set of linear equations:
a00x0 + a01x1 + a02x2 + … + a0,n-1xn-1 = b0
a10x0 + a11x1 + a12x2 + … + a1,n-1xn-1 = b1

am-1,0x0 + am-1,1x1 + am-1,2x2 + … + am-1,n-1xn-1 = bm-1
– n unknowns x are related by m equations
– Coefficients a are known, as are right hand side b

Matrices, p.2
• If n=m, we will try to solve for unique set of
x. Obstacles:
– If any row (equation) or column (variables) is
linear combination of others, matrix is
degenerate or not of full rank. No solution.
– If rows or columns are nearly linear
combinations, roundoff errors can make them
linearly dependent during computations. We’ll
fail to find a solution, even though one may exist.
– Roundoff errors can accumulate rapidly. While
you may get a solution, when you substitute it
into your equation system, you’ll find it’s not a
solution.
• Linear systems tend to be close to singular
(degenerate). Beware!
– We’ll do solutions for linear systems next lecture

8
Matrices, p.3
• In this lecture we cover basic matrix
representation and manipulation
– Used most often to prepare matrices for use in
solving linear systems
• Java has 2-D arrays, declared as, for
example
double[ ][ ] squareMatrix= new double[5][5];
– But there are no built-in methods for them
• So, it’s helpful to create a Matrix class:
– Create methods to add, subtract, multiply,
form identity matrix, etc.
• Sparse matrices are handled differently:
– Almost all large matrices are extremely sparse
(99%+ of entries are zeros)
– Store (i, j, value) in a list or 1-D array

2-D Arrays
data[0][0]
No of columns=
data data[0] 8.0 14.0 2.0 3.0
data[0].length
data[1] 5.1 4.2 6.6 0.8

data[2] 2.4 6.4 0.4 4.3

data[2] 3.3 3.2 5.5 6.6

data[3] 12.3 3.7 9.3 3.0

No. of rows=
data.length
A 2-D array is:
a reference to a 1-D array of references to 1-D arrays of data.
This is how we’ll store the matrix data in class Matrix

9
Matrix class, p.1
SXEOLFFODVV0DWUL[^

SULYDWHGRXEOH>@>@GDWD 5HIHUHQFHWRDUUD\

&RQVWUXFWRUIRU0DWUL[

SXEOLF0DWUL[ LQW P LQW Q ^

GDWD QHZGRXEOH>P@>Q@`

0HWKRGWRVHWLGHQWLW\0DWUL[

SXEOLFYRLG VHW,GHQWLW\ ^

LQW LM

LQWQURZV GDWDOHQJWK

LQWQFROV GDWD>@OHQJWK

IRU L LQURZVL

IRU M MQFROVM

LI L M

GDWD>L@>M@ 

HOVH

GDWD>L@>M@ `

Matrix class, p.2


SXEOLFLQWJHW1XP5RZV ^5HWXUQQRURZVLQ0DWUL[

UHWXUQGDWDOHQJWK`

SXEOLF LQWJHW1XP&ROV ^5HWXUQQRFROXPQVLQ0DWUL[

UHWXUQGDWD>@OHQJWK`

SXEOLFGRXEOH JHW(OHPHQW LQW L LQW M ^*HWHOHPHQWLM

UHWXUQGDWD>L@>M@`

SXEOLFYRLG VHW(OHPHQW LQW L LQW MGRXEOH YDO ^

GDWD>L@>M@ YDO`

SXEOLF0DWUL[ DGG0DWULFHV 0DWUL[E ^

0DWUL[UHVXOW QXOO

LQWQURZV GDWDOHQJWK

LQWQFROV GDWD>@OHQJWK

LI QURZV EGDWDOHQJWK QFROV EGDWD>@OHQJWK ^

UHVXOW QHZ0DWUL[ QURZV QFROV 

IRU LQW L LQURZVL

IRU LQW M MQFROVM

UHVXOWGDWD>L@>M@ GDWD>L@>M@EGDWD>L@>M@`

UHWXUQUHVXOW`

10
Matrix class, p.3
SXEOLF0DWUL[ PXOW0DWULFHV 0DWUL[E ^

0DWUL[UHVXOW QXOO

LQWQURZV GDWDOHQJWK

LQW S GDWD>@OHQJWK

LI S EGDWDOHQJWK ^

UHVXOW QHZ0DWUL[ QURZVEGDWD>@OHQJWK 

IRU LQW L LQURZVL

IRU LQW M MUHVXOWGDWD>@OHQJWKM ^

GRXEOHW 

IRU LQW N NSN ^

W GDWD>L@>N@ EGDWD>N@>M@`

UHVXOWGDWD>L@>M@ W``

UHWXUQUHVXOW`

SXEOLFYRLGSULQW ^

IRU LQW L LGDWDOHQJWKL ^

IRU LQW M MGDWD>@OHQJWKM

6\VWHPRXWSULQW GDWD>L@>M@ 

6\VWHPRXWSULQWOQ `

6\VWHPRXWSULQWOQ ``

Exercise
• Write a method to multiply a matrix by a scalar
_________________________________
_________________________________
_________________________________
_________________________________
_________________________________
_________________________________
_________________________________
_________________________________
_________________________________
_________________________________
_________________________________
_________________________________
_________________________________
_________________________________
_________________________________
_________________________________
_________________________________

11
Diagonal Matrix Class
• Only diagonal elements are nonzero:
a00 0 0 0 … 0
0 a11 0 0 … 0
0 0 a22 0 … 0
… … … … …
0 0 0 0… amm

– We can store this as a 1-D matrix of m


elements
– We can implement the same matrix methods
as our main Matrix class

DiagonalMatrix class, p.1


SXEOLFFODVV 'LDJRQDO0DWUL[ ^'RHVQRWH[WHQG0DWUL[

SULYDWHGRXEOH>@GDWD5HSODFHVGDWDUHSUHVHQWDWLRQ

SXEOLF 'LDJRQDO0DWUL[ LQW QU ^ &RQVWUXFWRU

GDWD QHZGRXEOH>QU@`

SXEOLFLQWJHW1XP5RZV ^

UHWXUQGDWDOHQJWK`

SXEOLF LQWJHW1XP&ROV ^

UHWXUQGDWDOHQJWK`

SXEOLFGRXEOH JHW(OHPHQW LQW L LQW M ^

LI L M

UHWXUQGDWD>L@

HOVH

UHWXUQ`

SXEOLFYRLG VHW(OHPHQW LQW L LQW MGRXEOH YDO ^

LI L M

GDWD>L@ YDO`

12
DiagonalMatrix class, p.2
SXEOLF0DWUL[ PXOW0DWULFHV 0DWUL[E ^

0DWUL[UHVXOW QXOO 5HWXUQUHJXODU0DWUL[

LQWQURZV GDWDOHQJWK 0XOWLSO\'LDJ E\UHJ0DWUL[

LQW S GDWDOHQJWK

LI S EJHW1XP5RZV ^

UHVXOW QHZ0DWUL[ QURZVEJHW1XP&ROV 

IRU LQW L LQURZVL

IRU LQW M MUHVXOWJHW1XP&ROV M

UHVXOWVHW(OHPHQW LMEJHW(OHPHQW LM GDWD>L@ `

UHWXUQUHVXOW`

SXEOLFYRLGSULQW ^

IRU LQW L LGDWDOHQJWKL ^

IRU LQW M MGDWDOHQJWKM

LI L M

6\VWHPRXWSULQW GDWD>L@ 

HOVH

6\VWHPRXWSULQW  

6\VWHPRXWSULQWOQ `

6\VWHPRXWSULQWOQ ``

MatrixMain, p.1
SXEOLFFODVV 0DWUL[0DLQ ^

SXEOLFVWDWLFYRLGPDLQ 6WULQJ>@DUJV ^

0DWUL[PDW QHZ0DWUL[   5HJXODU0DWULFHV

0DWUL[PDW QHZ0DWUL[  

0DWUL[ UHV 5HVXOW

PDWVHW,GHQWLW\  PDWVHW,GHQWLW\ 

UHV PDWDGG0DWULFHV PDW  $GGUHJXODU

6\VWHPRXWSULQWOQ PDW PDWSULQW 

6\VWHPRXWSULQWOQ PDW PDWSULQW 

6\VWHPRXWSULQWOQ PDWPDW UHVSULQW 

0DWUL[PDW QHZ0DWUL[   5HJXODU0DWULFHV

0DWUL[PDW QHZ0DWUL[  

0DWUL[UHV 6HWYDOXHVEHORZ

IRU LQW L LPDWJHW1XP5RZV L

IRU LQW M MPDWJHW1XP&ROV M

PDWVHW(OHPHQW LML M 

IRU LQW L LPDWJHW1XP5RZV L

IRU LQW M MPDWJHW1XP&ROV M

PDWVHW(OHPHQW LM LM 

UHV PDWPXOW0DWULFHV PDW  0XOWLSO\UHJXODU

13
MatrixMain, p.2
6\VWHPRXWSULQWOQ PDW  2XWSXWUHJXODU

PDWSULQW 

6\VWHPRXWSULQWOQ PDW 

PDWSULQW 

6\VWHPRXWSULQWOQ PDW PDW 

UHVSULQW 

'LDJRQDOPDWUL[

'LDJRQDO0DWUL[ PDW QHZ 'LDJRQDO0DWUL[  

IRU LQW L LPDWJHW1XP5RZV L

PDWVHW(OHPHQW LLL 

0DWUL[UHV

UHV PDWPXOW0DWULFHV UHV  0XOWE\UHJXODU

6\VWHPRXWSULQWOQ PDW 

PDWSULQW  2XWSXWGLDJRQDO

6\VWHPRXWSULQWOQ UHV 

UHVSULQW  2XWSXWUHJXODU

6\VWHPRXWSULQWOQ PDW UHV 

UHVSULQW 

Matrix Program Design Issues


• Option 1: The approach used in these notes, based
on multiple matrix classes (Matrix, DiagonalMatrix,
etc.):
– Disadvantage: We must write methods for each class to take
arguments of the others, if we want maximum flexibility. In
our brief example:
• DiagonalMatrix has method to multiply by Matrix
• Matrix does not have method to multiply by DiagonalMatrix
• We would have to write these (and other methods) to have a
complete class
• Also we would need methods to multiply vectors by matrices,
matrices by vectors, etc.
– Advantages of this approach:
• Efficiency: We access the private data of each class directly,
without accessor methods in our class methods (e.g.,
data.length instead of getNumRows())
• Pattern: Typical of how C++ would implement this, which is the
principal language and style used for numerical methods

14
Matrix Design Issues, p.2
• Option 2: Inheritance could be used here:
– DiagonalMatrix could extend Matrix
– Matrix would have to use accessor methods in its class
methods; it couldn’t access its private data directly
– Every Matrix method would have to be overridden in
DiagonalMatrix or it would not work properly
– Advantage:
• Fewer methods must be written. E.g., Matrix needs just one
multMatrix method, since DiagonalMatrix is a Matrix
– Disadvantages:
• With each subclass having a different internal
representation of the matrix data (1-D arrays, 2-D arrays,
Vector) from the superclass, the superclass private data
would be hidden but present in each subclass.
• Efficiency: Much more method call overhead, and unused
superclass data in subclasses
– Dr. J Harward (Java): “Elegant, a little sly”
– Dr. G Kocur (C++): “Awkward, a misuse of inheritance”

Matrix Design Issues, p.3


• Option 3: A Matrix interface could be defined:
– Each class would implement these methods in the best
way for its private data (like option 1)
– Fewer methods would be required than in the approach
in these notes (option 1), since all arguments would be
Matrix types
– Method overhead would be large (like option 2), since
class methods could not access an argument’s private
data directly (they wouldn’t know whether they were
operating on a Matrix, DiagonalMatrix, etc.)
– Each class could access its own private data directly,
though
– There wouldn’t be spurious superclass private data in
subclasses (like option 2)
– Probably a cleaner implementation than option 2, but it
doesn’t seem totally natural for Matrix to be an interface

15
What Do You Think?
• Jot down questions you still have,
and discuss them with your
neighbors
• Jot down your preference, and why
• We’ll take a brief poll at the end

16

You might also like