Professional Documents
Culture Documents
00 Lecture 21
Integration, concluded
Matrices
∫
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
1 5 9
2
Better Trapezoid Rule
1 3 5 7 9
1 2 3 4 5 6 7 8 9
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
GRXEOHE LQW Q^
LIQ ^
UHWXUQV`
HOVH^
LW 6XEGLYLVLRQV
VXP IXQFI[
[ GHOWD`
UHWXUQV``
SULYDWHVWDWLF GRXEOHV`&XUUHQWYDOXHRILQWHJUDO
4
Extended Simpson Method
GRXEOHE^
GRXEOH RV (
V RV ^
6\VWHPRXWSULQWOQ6LPSVRQLWHUM
UHWXUQV`
RV V
RVW VW`
UHWXUQ(55B9$/`
SULYDWHVWDWLFGRXEOHV
SXEOLFVWDWLFILQDOGRXEOH(36,/21 (
SXEOLFVWDWLFILQDOGRXEOH(55B9$/ (`
5
Using the Methods
SXEOLFVWDWLFYRLGPDLQ6WULQJ>@ DUJV^
6\VWHPRXWSULQWOQ6LPSOHWUDSH]RLGXVH
LQW P :DQWAPVWHSV
LQW M P
([DPSOHXVLQJH[WHQGHG6LPSVRQPHWKRG
6\VWHPRXWSULQWOQ6LPSVRQXVH
SXEOLFGRXEOHIGRXEOH[^
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
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
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
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[
GDWD QHZGRXEOH>P@>Q@`
0HWKRGWRVHWLGHQWLW\0DWUL[
SXEOLFYRLG VHW,GHQWLW\^
LQW LM
LQWQURZV GDWDOHQJWK
LQWQFROV GDWD>@OHQJWK
IRUL LQURZVL
IRUM MQFROVM
LIL M
GDWD>L@>M@
HOVH
GDWD>L@>M@ `
UHWXUQGDWDOHQJWK`
UHWXUQGDWD>@OHQJWK`
UHWXUQGDWD>L@>M@`
GDWD>L@>M@ YDO`
0DWUL[UHVXOW QXOO
LQWQURZV GDWDOHQJWK
LQWQFROV GDWD>@OHQJWK
UHVXOWGDWD>L@>M@ GDWD>L@>M@EGDWD>L@>M@`
UHWXUQUHVXOW`
10
Matrix class, p.3
SXEOLF0DWUL[ PXOW0DWULFHV0DWUL[E^
0DWUL[UHVXOW QXOO
LQWQURZV GDWDOHQJWK
LQW S GDWD>@OHQJWK
LIS EGDWDOHQJWK^
GRXEOHW
UHVXOWGDWD>L@>M@ W``
UHWXUQUHVXOW`
SXEOLFYRLGSULQW^
6\VWHPRXWSULQWGDWD>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
SULYDWHGRXEOH>@GDWD5HSODFHVGDWDUHSUHVHQWDWLRQ
GDWD QHZGRXEOH>QU@`
SXEOLFLQWJHW1XP5RZV^
UHWXUQGDWDOHQJWK`
SXEOLF LQWJHW1XP&ROV^
UHWXUQGDWDOHQJWK`
LIL M
UHWXUQGDWD>L@
HOVH
UHWXUQ`
LIL M
GDWD>L@ YDO`
12
DiagonalMatrix class, p.2
SXEOLF0DWUL[ PXOW0DWULFHV0DWUL[E^
LQW S GDWDOHQJWK
LIS EJHW1XP5RZV^
UHWXUQUHVXOW`
SXEOLFYRLGSULQW^
LIL M
6\VWHPRXWSULQWGDWD>L@
HOVH
6\VWHPRXWSULQW
6\VWHPRXWSULQWOQ`
6\VWHPRXWSULQWOQ``
MatrixMain, p.1
SXEOLFFODVV 0DWUL[0DLQ ^
SXEOLFVWDWLFYRLGPDLQ6WULQJ>@DUJV^
PDWVHW,GHQWLW\ PDWVHW,GHQWLW\
0DWUL[UHV 6HWYDOXHVEHORZ
PDWVHW(OHPHQWLML M
13
MatrixMain, p.2
6\VWHPRXWSULQWOQPDW 2XWSXWUHJXODU
PDWSULQW
6\VWHPRXWSULQWOQPDW
PDWSULQW
UHVSULQW
'LDJRQDOPDWUL[
0DWUL[UHV
6\VWHPRXWSULQWOQPDW
PDWSULQW 2XWSXWGLDJRQDO
6\VWHPRXWSULQWOQUHV
UHVSULQW 2XWSXWUHJXODU
UHVSULQW
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”
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