© 2005 microchip technology incorporated. all rights reserved. slide 1 num 1019 numerical methods...
TRANSCRIPT
© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 1
NUM 1019
Numerical Methods on the PIC Micro
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 2
Class Objective
When you finish this class you will: Know the basics of numerical methods
as applied to embedded systems Know what the PIC micro can really do See a complex mechatronics demo Know where to find additional design
resources
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 3
Agenda
What is numerical methods? The basics Trigonometry Finding the square root Dynamic systems
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 4
What is Numerical Methods?
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 5
Introduction
Numerical methods use numbers to simulate mathematical processes (The world around us)
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 6
Introduction
“The Purpose of Computing Is Insight, Not Numbers” –R.W. Hamming
Computers are for Computing
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 7
The Basics
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 8
Numbers
The three number systems Counting numbers
0,1,2,…,32,767
Fixed point 3.1415, 0.01234, -123.4
Floating point -.12345678 x 104
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 9
Fixed Point Fixed point numbers represent whole
numbers and fractions
10.4 can also be represented as 104 when scaled by 10 (decimal shifted once right)
Computations can be done on whole numbers as long as we remember where to put the decimal in the end (called the radix point)
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
10
4104.10
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 10
Fixed Point
The same applies to binary numbers often referred to as the Qx.x format or Qi.j
i and j are the number of bits to the left and right of the decimal point respectively.
Ex) 11011010.1101 Q8.4
1
0
1
0
2*)(22*)(n
k
n
k
krrk kakaA
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 11
Fixed Point
Signed-magnitude format for negative numbers First bit is used as a sign bit ex) 10010011 = -19
2’s complement is most often used for fixed point negative numbers Complement +1 ex) 9 = 00001001 11110111 = -9
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 12
Floating Point Notation: Floating Point Notation: Sign * (Mantissa) * 2Sign * (Mantissa) * 2exponentexponent = =
Fractional component stored in signed-magnitude format and is in binary form
Floating PointIntroduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
eMS 2
1
0
2*n
k
kkmMbitsignS
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 13
Floating Point
Exponent stored in biased format
This makes number comparisons easier Floating point number stored as:
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
Fxeeb 70
eb M2 M1 M0
IEEE 32bit S e7 … e2 e1 e0 m22 … m17 m16 m15 m14 … m9 m8 m7 m6 … m1 m0
Mchp 32bit e7 e6 … e1 e0 S m22 … m17 m16 m15 m14 … m9 m8 m7 m6 … m1 m0
Mchp 24bit e7 e6 … e1 e0 S m14 … m9 m8 m7 m6 … m1 m0
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 14
Good for large and small numbers (1038,10-38)
Number system is not linear, distance between numbers jumps
Using Floating point numbers for counting and/or logical decision making is generally a bad idea
Floating PointIntroduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
4444333 104103102101109108107
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 15
Fixed Float
Example:
1) Find exponent:
2) Find fractional component:
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
3)int(6780719.2)2ln(
)15625.0ln(15625.02
zez
z
)1bealwayswillx(25.12
15625.03
x
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 16
Fixed Float
3) Find binary number:
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
3
212
221
0
225.115625.02)1(Float)bitsign(0
00000000000000000000010.125.1
0;1yes?,225.025.0;0no?,225.0
25.0125.1;1yes?,225.1
eS
g
MS
bindecM
xmxm
xm
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 17
Fixed Float
4) Convert to Microchip float format eb= Biased Exponent
Microchip Float Format:
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
CxebFxebFxeeb
7070370
000020715625.0012
CMMMExp
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 18
Bases
We typically do calculations in decimal (base 10)
130ten = (1 * 102) + (3 * 101) + (0 * 100)
Machines only understand 1’s and 0’s so they use binary (base 2)
130ten = (1*27)+(0*26)+(0*25)+(0*24)+(0*23)+(0*22)+(1*21)+(0*20)
= 1000 0010two
Hexadecimal (base 16) serves as a shorthand for binary numbers
1000 0010two = (8*161) + (2 * 160) = 82sixteen (0x82)
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 19
Bases
Dec Bin
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
0110000122
102
012
122
152
0112
222
4
3
2
1
0
a
a
a
a
a
11010000.8125.000.1
2
500.0
2
250.1
2
625.1
2
8125.
Fraction Bin
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 20
Bases
twoten 00110.1.0
21
2
61
2
80
2
40
repeating
2
20
2
1.0
Watch out for non-terminating numbers
Never enough precision
Try it:
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
11.010
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 21
Bases
Making the Dec Bin Dec conversion doesn’t always return the same result
Example:
numbersdistinct64
.1001
.1001
.1001
000000.1001
99.9
98.9
01.9
00.9
numbers100
xxxxxx
xxxxxx
xxxxxx
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 22
Bases
Do not confuse the kind of number system used (Counting, Fixed Point, Float) with the form of the number representation (Dec, Bin, Hex)
(they are the same number)
twoten 113 81000000021100.4
2
0
x
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 23
BCD
Binary Coded Decimal (BCD) Counting to ten in binary
Solves 0.1 approximation problem
BCD arithmetic is sometimes required by law for financial applications
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
1300
001100011101
xxD
0001.00001.0
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 24
Four Functions
Addition:
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
ten
ten
ten
380010011012000011002610101000
11
ten
ten
ten
200001010017000100013701010100
20
Subtraction:
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 25
Four FunctionsIntroduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
Division:
1011
10111
10111
1001111101111
1111101010111
101
Multiplication:
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 26
Multiplication A trick that can speed up multiplication in software
Round by inspection to allow shift and add instead of multiply
Use two or three shift and add routines instead of a multiply routine Need to multiply x(n) by 10010101 (dec 149) 10010000 (dec 144) => error = -5 10100000 (dec 160) => error = +11 x x(n)*10010101 x(n)<<7 + x(n)<<4 = x(n)*27+x(n)*24
To multiply y(n) by 00001011(dec 11) 00001100 (dec 12) => error = +1 00001010 (dec 10) => error = -1 In general round down for y(n)
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 27
Evaluating
Horner’s method for a polynomial
Evaluated:
Used to convert thermocouple voltage to temperature (oC) over a wide range of temperatures.
0123455
012
21
1
))))(((()(
5
)(
axaxaxaxaxaxP
n
axaxaxaxaxP nn
nn
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
end
kbckakb
nkfor
nanb
);1(*)()(
0:1:1
);()(
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 28
Evaluating Using unsigned coefficients is sometimes beneficial Subtract values with negative coefficients in algorithm For scaled coefficients between -128 and 127 convert
these to 0 to 255 IIR digital filter example:
Start with difference equation:
)2()1()2()1()()( 21210 nyanyanxbnxbnxbny
)2()128()1()128(
)2()128()1()128()()128()(
21
2101
nyanya
nxbnxbnxbny
128)2(128)1(128)()( nxnxnxnX
)()()( 1 nXnyny
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 29
Error analysis
Things to watch out for: Loss of significance when subtracting two
nearly equal numbers Example with algebraically equivalent
expressions:
1748.111
1500.11)1(500
xx
x
xxxx
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 30
Error analysis
Two kinds of error, Round-Off and Truncation
Round-Off is a result of a fixed precision (machine dependant)
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
999.0333.333.333.
000.131
31
31
46.223.123.14645.223.12345.1
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 31
Error analysis
Round-off example:
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
Decimal Equivalents for a set of binary numbers
-3 -2 -1 0 1 2 3 40.0625000 0.125000 0.25000 0.5000 1.000 2.00 4.0 8.00.0703125 0.140625 0.28125 0.5625 1.125 2.25 4.5 9.00.0781250 0.156250 0.31250 0.6250 1.250 2.50 5.0 10.00.0859375 0.171875 0.34375 0.6875 1.375 2.75 5.5 11.00.0937500 0.187500 0.37500 0.7500 1.500 3.00 6.0 12.00.1015625 0.203125 0.40625 0.8125 1.625 3.25 6.5 13.00.1093750 0.218750 0.43750 0.8750 1.750 3.50 7.0 14.00.1171875 0.234375 0.46875 0.9375 1.875 3.75 7.5 15.0
4-bit mantissa Exponent e0.xxxx1000
110111101111
1001101010111100
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 32
Error analysis
Round-off example: 61
51
101 )(
2103
2251
23101
20011.1
21101.021101.0
201101.021101.0
two
twotwo
twotwo
01157
1261
11103
210000.0211111.0
201011.021011.0
21010.021010.0
twotwo
twotwo
twotwo
%)14.7(033333.010000.0157 twooferror
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 33
Error analysis
What is the difference between Chop-off and Round-off?
Chop-off will introduce unnecessary bias errors
Round-off minimizes bias
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
457.1456.1456852.1RoundorChop
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 34
Error analysis
Truncation error is the result of implementing a finite number of steps of an infinite step sequence (algorithm dependant)
For example, using a fixed number of terms in a Taylor expansion See hand out for further information on
Taylor approximations
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
10
)1(
00
0)(
)()!1(
)()(
)(!
)()()(
NN
N
N
k
kk
N
xxN
cfxE
xxk
xfxPxf
© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 35
Trigonometry
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 36
Trigonometry
Taylor approximation for sin(x) of degree 9, P9(x) about x0 = 0
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
!9!7!5!3!
)0()(
97539
0
)(
9
xxxxxx
k
fxP
k
kk
109 !10
)sin()( x
cxE
-1.5
-1
-0.5
0
0.5
1
1.5
0.E+00
2.E+06
4.E+06
6.E+06
8.E+06
1.E+07
1.E+07
P9(X)
SIN(X)
Error
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 37
CORDIC Transform
Coordinate Rotation Digital Computer An iterative algorithm used for calculating
trig functions The CORDIC Transform only works
for angles between ±90o
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
cossin
sincos'' yxyx
])}[tan(*][][{*])[cos(]1[])}[tan(*][][{*])[cos(]1[iiXiYiiYiiYiXiiX
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 38
Trigonometry
How does it work? Rotates the phase of a complex number,
by multiplying it by a succession of constant values
The multiplier used is in 2, so in binary arithmetic we will use the shift command
See handout for more detail on the derivation
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 39
Trigonometry
Range finders have been around for a long time
They used right triangles to find distances
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
distance)tan( L
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 40
Trigonometry
Lets make a laser range finder!
Our setup:
Use law of Sines to find L:
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
Line up the Line up the laser pointers!laser pointers!
AA BB
cc
LLbb aa
)sin(*)sin(
)sin(*1800 AbL
C
BcbBAC
CC
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 41
Trigonometry
What is the error using CORDIC SIN(X)?
What is the error using Taylor approximation to SIN(X)?
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 42
Finding the square root
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 43
Square Root
Fixed point Newton-Raphson method (AN526)
2)(5.0 0
1
01
Nx
x
xxx
xN
nnn
n
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 44
Square Root
MINIMAX floating point approximation
1) Limit your input range
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 45
Square Root
MINIMAX floating point approximation
2) Find the best linear solution
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
Sqr(2)Sqr(2)
11 22
Same maximum error at mid-point and both end-pointsSame maximum error at mid-point and both end-points
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 46
Square Root
Fast integer square root (TB040)
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2005 Microchip Technology Incorporated. All Rights Reserved. Slide 47
Dynamic Systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 48
Differentiation
Backward difference “three-point formula”
M = maximum value for the 4th derivative Find a step size h to minimize error E3
24
)(
2
43)(
)4(4210
0
cfh
h
fffxf
242
43)( 4
4210
3
Mh
h
eeexE
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 49
Integration
Simpson’s 3/8th rule
)(80
3)33(
8
3)( )4(
5
3210
3
0
cfh
ffffh
dxxfx
x
)(xfy
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 50
Dynamic simulation
World of differential equations
Introduction Introduction The BasicsThe BasicsTrigonometryTrigonometryFinding the square rootFinding the square rootDynamic systemsDynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 52
Summary
What is numerical methods? The basics Trigonometry Finding the square root Dynamic systems
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 53
References
AN670 “Floating Point to ASCII conversion”
TB040 “Fast Integer Square root” AN526 “PIC16 Math Utility Routines” AN544 “Math Utility Routines” AN617 “Fixed Point Routines” AN660 “Floating Point Math
Functions”
© 2006 Microchip Technology Incorporated. All Rights Reserved. Class Slide 54
References “German Optical Rangefinders” by Peter
Lienau “Fixed-Point Trigonometry With CORDIC
Iterations” By Ken Turkowski “A survey of CORDIC algorithms for
FPGA based computers” By Ray Andraka “Math Toolkit for Real-Time
programming” by Jack Crenshaw “Numerical Methods Using Matlab” by
Kurtis D. Fink “Numerical Methods for Scientists and
Engineers” by Richard Hamming