FEEG1001 Design & Computing Numerical Methods Dr Stephen...
Transcript of FEEG1001 Design & Computing Numerical Methods Dr Stephen...
FEEG1001 Design & Computing
Numerical Methods
Dr Stephen Boyd
All lecture slides delivered to date
FEEG1001 Design & Computing
Numerical Methods
Dr Stephen Boyd
Lecture 1
FEEG1001 Numerical Methods
Introduction
What is the point of these lectures?
FEEG1001 Numerical Methods
Content
• Week 1 – Problems, data, matrices
FEEG1001 Numerical Methods
Matrices
• Many of the worlds problems can be devolved into matrices
• The many matrix manipulation tools you will learn in MATH1054 have been coded and are available through the callable module NUMPY.
Input
Output
FEEG1001 Numerical Methods
Problem • Detailed research by university management has shown that
engineering students are either in lectures, in the gym, or at the pub.
• They found that:
– If student is in a lecture in the next lecture slot they have 60% chance of still being in lecture, a 20% chance of being in the gym, and a 20% chance of being in the pub.
– If the student is in the gym then in the next hour they have 50% chance of being in a lecture, a 20% chance of remaining in the gym, and a 30% chance of being in the pub.
– If the student is in the pub, then in the next hour there is a 5% chance that he or she will be in a lecture, a 5% chance of being in the gym, and an 90% chance of staying in the pub.
FEEG1001 Numerical Methods
Directed graph
FEEG1001 Numerical Methods
Matrix mathematics
• Can transfer this into matrix form
• If 70% of students are in a lecture at 9am and 30% are in the Gym,
L
P
G
P G
L
0.7 0.3 0.0 = 𝑆0 L P G
0.6 0.2 0.20.5 0.2 0.3
0.05 0.05 0.9= 𝑃
FEEG1001 Numerical Methods
Question
• What proportion of students are in the pub after a long time?
• Start with ratio of students after 1 hour
• After 2 hours
𝑆1 = 𝑆0𝑃
𝑆1 = 0.7 0.3 0.00.6 0.2 0.20.5 0.2 0.3
0.05 0.05 0.9
𝑆1 = 0.57 0.20 0.23
𝑆2 = 𝑆1𝑃 𝑆2 = 0.4535 0.1655 0.381
FEEG1001 Numerical Methods
Where are the students?
FEEG1001 Numerical Methods
Python
FEEG1001 Numerical Methods
Problem
• To understand the issue, consider the set of results returned by a query to a web search engine
• For the user, it is desirable to:
– receive a large set of accurate matches
– have the matches returned in order, where the order corresponds to some measure of “importance”
• Ranking according to a measure of importance is the problem we now consider
• The methodology developed to solve this problem by Google founders Larry Page and Sergey Brin is known as PageRank
FEEG1001 Numerical Methods
Directed graph
FEEG1001 Numerical Methods
Video
• Google video
FEEG1001 Design & Computing
Numerical Methods
Dr Stephen Boyd
Lecture 2
FEEG1001 Numerical Methods
Content
• Week 1 – Problems, data, matrices
• Week 2 – Linear algebra
FEEG1001 Numerical Methods
Linear Algebra
• Solving simultaneous equations
• 3 equations, 3 unknowns
3𝐵 = 4C
4𝐴 = 3B + 4C
4𝐴 + 3𝐵 + 4𝐶 = 48
FEEG1001 Numerical Methods
Linear Algebra
• Solve by substitution 3𝐵 = 4C
4𝐴 = 3B + 4C
4𝐴 + 3𝐵 + 4𝐶 = 48
(1)
(2)
(3) 𝐶 =34𝐵 Use (1) (4)
Put (4) into (2) 4𝐴 = 3B + 434𝐵 𝐴 =
34
B +34𝐵 𝐴 =
64
B
Put (4) and (5) into (3) 464
B + 3𝐵 + 434𝐵 = 48 6𝐵 + 3𝐵 + 3𝐵 = 48
𝐵 = 4
(5)
(6)
Put (6) into (5) 𝐴 =64
4 𝐴 = 6
Put (6) into (4) 𝐴 =34
4 𝐶 = 3
𝐴 = 6
𝐵 = 4
𝐶 = 3
FEEG1001 Numerical Methods
Linear Algebra
• Solve by elimination
0𝐴 + 3𝐵 − 4𝐶 = 0
4𝐴 − 3B − 4C = 0
4𝐴 + 3𝐵 + 4𝐶 = 48
Rewrite into matrix form
0 3 −44 −3 −44 3 4
00
48
4 3 44 −3 −40 3 −4
4800
Do some row rearrangement of
rows 1 and 3
Get this to zero
𝑅𝑅 → 𝑅𝑅 − 𝑅𝑅
4 3 40 −6 −80 3 −4
48−48
0
4 3 40 −6 −80 3 −4
48−48
0 𝑅3 → 𝑅3 +
𝑅𝑅𝑅𝑅
4 3 40 −6 −80 0 −8
48−48−𝑅4
𝐴 = 6 𝐵 = 4 𝐶 = 3
4 3 44 −3 −40 3 −4
4800
Get this to zero
FEEG1001 Numerical Methods
Linear Algebra
• Other techniques can be used to solve large problems
• LU decomposition is one such method where the Matrix is decomposed into an Upper triangular matrix and a Lower triangular matrix which can both be solved using substitution.
• This can be a particularly powerful tool when dealing with Large problems
FEEG1001 Numerical Methods
Linear Algebra
FEEG1001 Numerical Methods
Linear Algebra
• Requires 8 equations to find the 8 unknowns
0 7 −4 0 0 0 0 00 0 0 9 −3 0 0 00 0 0 0 0 0 8 −𝑅0 0 0 9 3 −5 0 0−6 7 4 0 0 0 0 00 0 0 −9 −3 −5 −8 𝑅6 7 4 −9 −3 −5 −8 −𝑅6 7 4 9 3 5 8 𝑅
0000000
𝑅0𝑅4
FEEG1001 Numerical Methods
Problem • Detailed research by university management has shown that
engineering students are either in lectures, in the gym, or at the pub.
• They found that:
– If student is in a lecture in the next lecture slot they have 60% chance of still being in lecture, a 20% chance of being in the gym, and a 20% chance of being in the pub.
– If the student is in the gym then in the next hour they have 50% chance of being in a lecture, a 20% chance of remaining in the gym, and a 30% chance of being in the pub.
– If the student is in the pub, then in the next hour there is a 5% chance that he or she will be in a lecture, a 5% chance of being in the gym, and an 90% chance of staying in the pub.
FEEG1001 Numerical Methods
Question
• What proportion of students are in the pub after a long time?
• Start with ratio of students after 1 hour
• After 2 hours
𝑆1 = 𝑆0𝑃
𝑆1 = 0.7 0.3 0.00.6 0.𝑅 0.𝑅0.5 0.𝑅 0.3
0.05 0.05 0.9
𝑆1 = 0.57 0.𝑅0 0.𝑅3
𝑆2 = 𝑆1𝑃 𝑆2 = 0.4535 0.𝑅655 0.38𝑅
FEEG1001 Numerical Methods
Where are the students?
FEEG1001 Numerical Methods
What is the stationary state?
𝑆1 𝑆2 𝑆3𝑃11 𝑃12 𝑃13𝑃21 𝑃22 𝑃23𝑃31 𝑃32 𝑃33
= 𝑆1 𝑆2 𝑆3
𝑆𝑃 = 𝑆
𝑆1𝑃11 + 𝑆2𝑃21 + 𝑆3𝑃31 = 𝑆1
𝑆1𝑃13 + 𝑆2𝑃23 + 𝑆3𝑃33 = 𝑆3 𝑆1𝑃12 + 𝑆2𝑃22 + 𝑆3𝑃32 = 𝑆2
𝑆1 + 𝑆2 + 𝑆3 = 𝑅
𝑃11 𝑃21 𝑃31𝑃12 𝑃22 𝑃32𝑃13 𝑃23 𝑃33𝑅 𝑅 𝑅
𝑆1𝑆2𝑆3
=
𝑆1𝑆2𝑆3𝑅
FEEG1001 Numerical Methods
• After some manipulation to get S on only left side!
• So we have a system of linear equations!
𝑃11 𝑃21 𝑃31𝑃12 𝑃22 𝑃32𝑃13 𝑃23 𝑃33𝑅 𝑅 𝑅
𝑆1𝑆2𝑆3
=
𝑆1𝑆2𝑆3𝑅
𝑃11 𝑃21 𝑃31𝑃12 𝑃22 𝑃32𝑅 𝑅 𝑅
𝑆1𝑆2𝑆3
=𝑆1𝑆2𝑅
𝑃11 − 𝑅 𝑃21 𝑃31𝑃12 𝑃22 − 𝑅 𝑃32𝑅 𝑅 𝑅
𝑆1𝑆2𝑆3
=00𝑅
𝐴𝐴 = 𝑏
In [125]: station_matrix(P) Out[125]: array([ 0.20634921, 0.0952381 , 0.6984127 ])
FEEG1001 Numerical Methods
Solutions to problems
In [127]: solve_lin_eq(F, u) Out[127]: array([ 42.66666667, 18.28571429, 32. , 7.11111111, 21.33333333, 25.6 , 16. , 128. ])
In [128]: solve_lin_eq(A, b) Out[128]: array([ 6., 4., 3.])
FEEG1001 Numerical Methods
Video
• FEA video
It’s a system of simultaneous equations
If we know stiffness and we
know the force being applied - we can solve for the deflections
FEEG1001 Design & Computing
Numerical Methods
Dr Stephen Boyd
Lecture 3
FEEG1001 Numerical Methods
Content
• Week 1 – Problems, data, matrices
• Week 2 – Linear algebra
• Week 3 - Interpolation
FEEG1001 Numerical Methods
Interpolation
• So, I have some points from an experiment or by sampling a function by a computational experiment.
• What next?
• I would like to find the stress at 0.5% strain
Interpolation
FEEG1001 Numerical Methods
Curve fitting
• More generally we may want to try and represent this set of data by a continuous function
• This would allow us to evaluate the function at whatever value of x we choose!
𝑦 = 𝑓 𝑥
FEEG1001 Numerical Methods
Curve fitting
• Naturally there are lots of functions that could be used so which do we choose?
• How do we judge the best possible fit?
• Use a least squares fit?
• The NUMPY function polyfit(x, y, a) uses the least squares method to fit a polynomial of order a to the data in x and y.
• The NUMPY function polyval(p, x) will evaluate the polynomial defined by p at the set of points x
FEEG1001 Numerical Methods
Curve fitting
In [8]: fit = np.polyfit(x,y,6) In [9]: fit Out[9]: array([ -9.38771034e+01, 7.93732880e+02, -2.51703886e+03, 3.74662099e+03, -2.62558297e+03, 7.05734471e+02, 9.99970020e-01])
FEEG1001 Numerical Methods
Curve fitting
FEEG1001 Numerical Methods
Interpolation
• So, I have some points from an experiment or by sampling a function by a computational experiment.
• What next?
• I would like to find the stress at 0.5% strain
Interpolation
FEEG1001 Numerical Methods
Linear interpolation
FEEG1001 Numerical Methods
Polynomial interpolation
FEEG1001 Numerical Methods
Spline interpolation
• SCIPY has a number of interpolation functions
• interp1d(x,y, kind=‘ ‘) returns a function which can be evaluated for a target value
• I’ve used this to write a function that takes in my experimental data and the number of intermediate interpolation points, I can also set what kind of spline I use.
FEEG1001 Numerical Methods
Linear interpolation
Exact Solution x=0.5, y=19.88679245
FEEG1001 Numerical Methods
Cubic Spline Interpolation
Exact Solution x=0.5, y=19.88679245
FEEG1001 Numerical Methods
Cubic Spline Interpolation
Exact Solution x=0.5, y=19.88679245
FEEG1001 Numerical Methods
Interpolation in 3D
• Lets say we have measured, using pitot tubes, the pressures on a plate subject to a jet blast
• Limited number of results
FEEG1001 Numerical Methods
Interpolation in 3D
FEEG1001 Numerical Methods
Interpolation in 3D
FEEG1001 Numerical Methods
Interpolation in 3D
FEEG1001 Numerical Methods
Interpolation
• So why is this important???
FEEG1001 Numerical Methods
Video
• Frame interpolation
• Motion interpolation
FEEG1001 Design & Computing
Numerical Methods
Dr Stephen Boyd
Lecture 4
FEEG1001 Numerical Methods
Content
• Week 1 – Problems, data, matrices
• Week 2 – Linear algebra
• Week 3 – Interpolation
• Week 4 - Integration
FEEG1001 Numerical Methods
Integration
• Say we want to evaluate the following integral:
� 𝑠𝑠𝑠 𝑥 + 28
0
𝐹 𝑥 = −𝑐𝑐𝑠 𝑥 + 2 𝑥
−𝑐𝑐𝑠 8 + 2 8 − −𝑐𝑐𝑠 0 + 2 0
17.1455 Exact solution !!
FEEG1001 Numerical Methods
Integration
• Again, SCIPY has some integration functionality scipy.integrate.quad(f,a,b)where f is the function and a and b are the bounds.
• This integration will give us the area under the function
In [85]: import scipy.integrate as integ In [86]: ans,err = integ.quad(sinx,0,8) In [87]: print ans 17.1455000338
FEEG1001 Numerical Methods
Integration
• What if we don’t have the function but some data points and want to integrate?
FEEG1001 Numerical Methods
Trapezium Rule
• Assume that the data represents a series of trapezia
• The sum of the area of all trapezia give the area
� 𝑓 𝑥 𝑑𝑥 ≈ ℎ12 𝑦0 + 𝑦𝑛 + 𝑦1 + 𝑦2 + 𝑦3 … . .𝑦𝑛−1
𝑏
𝑎
FEEG1001 Numerical Methods
Trapezium Rule
• What drives this accuracy?
• Data density
x y weight y * weight
0 2.0 0.5 1.0
1 2.8414 1 2.8414
2 2.9093 1 2.9093
3 2.1411 1 2.1411
4 1.2432 1 1.2432
5 1.0411 1 1.0411
6 1.7206 1 1.7206
7 2.6570 1 2.6570
8 2.9894 0.5 1.4947
Sum 17.0484
h 1.0
h*sum 17.0484
� 𝑠𝑠𝑠 𝑥 + 28
0
In [100]: y = sinx(x) In [101]: trap(x,y) Out[101]: 17.048411873553935
No. data points Area 9 17.04841 18 17.12428 27 17.13645 36 17.14051 45 17.14234 54 17.14332 63 17.14391 72 17.14429 81 17.14455 90 17.14473 99 17.14486
108 17.14497 117 17.14505 126 17.14511 135 17.14516 144 17.1452 153 17.14524 162 17.14526 171 17.14529
FEEG1001 Numerical Methods
Simpson’s rule
• In trapezium rule we perform linear interpolation between sample points. Simpson’s rule fits a parabola through sets of three consecutive data points
� 𝑓 𝑥 𝑑𝑥 ≈ℎ3
𝑦0 + 𝑦𝑛 + 4 𝑦1 + 𝑦3 + 𝑦5 … . .𝑦𝑛−1 + 2 𝑦2 + 𝑦4 + 𝑦6 … . .𝑦𝑛−2𝑏
𝑎
FEEG1001 Numerical Methods
Simpson’s Rule
No. data points Area 9 17.1527
18 16.6714 27 17.1456 36 16.9165 45 17.1455 54 16.9945 63 17.1455 72 17.0329 81 17.1455 90 17.0557 99 17.1455
108 17.0709 117 17.1455 126 17.0816 135 17.1455 144 17.0897 153 17.1455 162 17.0959 171 17.1455
x y weight y * weight
0 2.0 1 2.0
1 2.8414 4 11.3656
2 2.9092 2 5.8186
3 2.1411 4 8.5644
4 1.2431 2 2.4864
5 1.0410 4 4.1644
6 1.7206 2 3.4412
7 2.6570 4 10.628
8 2.9894 1 2.9894
Sum 51.458
h 1.0
h*sum/3 17.1527
� 𝑠𝑠𝑠 𝑥 + 28
0
In [159]: y = sinx(x) In [160]: simp(x,y) Out[160]: 17.1527101269849
• Again density drives accuracy
FEEG1001 Numerical Methods
Alternative Methods
• Monte Carlo methods can be used for integration
• The technique uses random numbers
If I throw a dart at this picture what is the probability that it will land inside the circle?
𝑃 𝑠𝑠𝑠𝑠𝑑𝑖 =𝐴𝐴𝑖𝐴 𝑐𝑓 𝑡ℎ𝑖 𝐶𝑠𝐴𝑐𝐶𝑖𝐴𝐴𝑖𝐴 𝑐𝑓 𝑡ℎ𝑖 𝑠𝑠𝑠𝐴𝐴𝑖
Throw a lot of darts – say 20
𝑃(𝑠𝑠𝑠𝑠𝑑𝑖) =𝑁𝑠𝑁 𝑠𝑠𝑠𝑠𝑑𝑖 𝐶𝑠𝐴𝑐𝐶𝑖𝑇𝑐𝑡𝐴𝐶 𝑠𝑠𝑁𝑛𝑖𝐴
𝐴𝐴𝑖𝐴 𝑐𝑓 𝑡ℎ𝑖 𝑐𝑠𝐴𝑐𝐶𝑖𝐴𝐴𝑖𝐴 𝑐𝑓 𝑡ℎ𝑖 𝑠𝑠𝑠𝐴𝐴𝑖
=𝑁𝑠𝑁 𝑠𝑠𝑠𝑠𝑑𝑖 𝐶𝑠𝐴𝑐𝐶𝑖𝑇𝑐𝑡𝐴𝐶 𝑠𝑠𝑁𝑛𝑖𝐴
𝐴𝐴𝑖𝐴 𝑐𝑓 𝑡ℎ𝑖 𝑐𝑠𝐴𝑐𝐶𝑖 =𝑁𝑠𝑁 𝑠𝑠𝑠𝑠𝑑𝑖 𝐶𝑠𝐴𝑐𝐶𝑖𝑇𝑐𝑡𝐴𝐶 𝑠𝑠𝑁𝑛𝑖𝐴
× 𝐴𝐴𝑖𝐴 𝑐𝑓 𝑡ℎ𝑖 𝑠𝑠𝑠𝐴𝐴𝑖
FEEG1001 Numerical Methods
Alternative Methods
• Monte Carlo methods can be used for integration
• The technique uses random numbers
In [182]: ans = [findPi(20) for i in range(20)] In [183]: sum(ans)/len(ans) Out[183]: 2.97
𝜋𝑅2
4𝑅2=𝑁𝑠𝑁 𝑠𝑠𝑠𝑠𝑑𝑖 𝐶𝑠𝐴𝑐𝐶𝑖𝑇𝑐𝑡𝐴𝐶 𝑠𝑠𝑁𝑛𝑖𝐴
𝜋 = 4 ×𝑁𝑠𝑁 𝑠𝑠𝑠𝑠𝑑𝑖 𝐶𝑠𝐴𝑐𝐶𝑖𝑇𝑐𝑡𝐴𝐶 𝑠𝑠𝑁𝑛𝑖𝐴
Throw 20 darts - 16 inside, 4 outside
Therefore π = 3.2
FEEG1001 Numerical Methods
Alternative Methods
• Monte Carlo methods can be used for integration
• The technique uses random numbers Create N random pairs of numbers with
values between -1.0 and 1.0
Say N = 200
Count how many of these points fall inside the circle
The area inside the circle (pi) is then
given by:
𝐴𝐴𝑖𝐴 𝑐𝑓 𝑠𝑠𝑠𝐴𝐴𝑖 ∗ 𝑝𝑐𝑠𝑠𝑡𝑠 𝑠𝑠𝑠𝑠𝑑𝑖 𝑐𝑠𝐴𝑐𝐶𝑖𝑁
In [189]: ans = [findPi(200) for i in range(20)] In [190]: sum(ans)/len(ans) Out[190]: 3.1529999999999996
FEEG1001 Numerical Methods
Alternative Methods
• Monte Carlo methods can be used for integration
• The technique uses random numbers Create N random pairs of numbers with
values between -1.0 and 1.0
Say N = 2000
Count how many of these points fall inside the circle
The area inside the circle (pi) is then
given by:
𝐴𝐴𝑖𝐴 𝑐𝑓 𝑠𝑠𝑠𝐴𝐴𝑖 ∗ 𝑝𝑐𝑠𝑠𝑡𝑠 𝑠𝑠𝑠𝑠𝑑𝑖 𝑐𝑠𝐴𝑐𝐶𝑖𝑁
In [210]: ans = [findPi(2000) for i in range(20)] In [211]: sum(ans)/len(ans) Out[211]: 3.1370999999999993
FEEG1001 Numerical Methods
Monte Carlo
In [12]: ans = [MC_integ(sinx,0,8,2000) for i in range(20)] In [13]: sum(ans)/len(ans) Out[13]: 17.198991218374964
FEEG1001 Numerical Methods
Integration in 3D
• Power of Monte Carlo integration increases as the number of dimensions increases
In [2]: dint(peaks,-3,3) Out[2]: 247.93482117912382
FEEG1001 Numerical Methods
Integration in 3D
In [4]: ans = [MC_integ_3D(peaks,-3,3,400) for i in range(50)]
In [5]: sum(ans)/len(ans) Out[5]: 247.39350467063898
In [2]: dint(peaks,-3,3) Out[2]: 247.93482117912382
FEEG1001 Design & Computing
Numerical Methods
Dr Stephen Boyd
Lecture 5
FEEG1001 Numerical Methods
Content
• Week 1 – Problems, data, matrices
• Week 2 – Linear algebra
• Week 3 – Interpolation
• Week 4 – Integration
• Week 5 – Root finding and optimisation
FEEG1001 Numerical Methods
Root finding
• Lets start with a simple function
• We can find the roots of this function analytically using:
𝑦 = 𝑥2 − 2𝑥 − 2
𝑥 =−𝑏 ± 𝑏2 − 4𝑎𝑎
2𝑎
𝑥 = 2.732051
𝑥 = −0.732051
FEEG1001 Numerical Methods
Root finding
• However, this approach only works for quadratic equations
• What about this function:
𝑦 = 8𝑠𝑠𝑠 𝑥 3 + 2𝑥 − 4
FEEG1001 Numerical Methods
Bracketing a root
• Suppose we have a continuous function f(x)
• We have two values a and b such that f(a).f(b)<0.0
• By intermediate value theorem (from calculus) there is at least one root between in the interval [a,b]
• i.e. function changes sign
FEEG1001 Numerical Methods
Root finding
• Using this simple function:
𝑦 = 𝑥2 − 3
FEEG1001 Numerical Methods
Incremental search
• Primitive (but effective)
1. Identify a region of the function containing a root by proceeding from left to right checking the sign of the function
2. Bracket this root and repeat with smaller increments
3. Root is found when convergence has been obtained
• Very inefficient
• Robust
FEEG1001 Numerical Methods
Bisection
1. Bracket a root between xlower and xupper
2. Construct a mid-point 𝑥𝑛𝑛𝑛 = 𝑥𝑙𝑙𝑙𝑙𝑙+𝑥𝑢𝑢𝑢𝑙𝑙2
3. Check the relative signs of xlower, xupper and 𝑥𝑛𝑛𝑛
4. Choose the two with opposite signs as they bracket the root
5. Repeat until convergence
FEEG1001 Numerical Methods
False position (‘regula falsi’)
• Bracket a root between xlower and xupper
• Evaluate the function f(xlower) and f(xupper)
• Interpolate linearly between these two points
• 𝑥𝑛𝑛𝑛 is determined by when the straight line cuts x axis
• Check the relative signs of f(xlower), f(xupper) and 𝑓(𝑥𝑛𝑛𝑛)
• Choose the two with opposite signs as they bracket the root
• Repeat until convergence
FEEG1001 Numerical Methods
False position (‘regula falsi’)
FEEG1001 Numerical Methods
Secant method
• This method uses interpolation between the last two points found
• Danger is that the root might not remain bracketed and therefore tend to infinity
• Given two guesses for the root xn and xn-1 then a better guess is given by:
𝑥𝑛+1 =𝑥𝑛−1𝑓 𝑥𝑛 − 𝑥𝑛𝑓 𝑥𝑛−1
𝑓 𝑥𝑛 − 𝑓 𝑥𝑛−1
FEEG1001 Numerical Methods
Secant method
FEEG1001 Numerical Methods
Newton-Raphson Method
• You have already done a code for this in Lab 7
• Next guess is given by the following equation:
𝑥𝑛+1 = 𝑥𝑛 −𝑓 𝑥𝑛𝑓𝑓 𝑥𝑛
FEEG1001 Numerical Methods
Root finding
• Lets start with a simple function
• We can find the roots of this function analytically using:
𝑦 = 𝑥2 − 2𝑥 − 2
𝑥 =−𝑏 ± 𝑏2 − 4𝑎𝑎
2𝑎
𝑥 = 2.732051
𝑥 = −0.732051 In [36]: xs = np.array([-1,2]) In [40]: opti.fsolve(cup,xs) Out[40]: array([-0.73205081, 2.73205081])
FEEG1001 Numerical Methods
Root finding
• What about this function:
𝑦 = 8𝑠𝑠𝑠 𝑥 3 + 2𝑥 − 4
In [41]: xs = np.array([1,3,5]) In [42]: opti.fsolve(wobble,xs) Out[42]: array([ 0.74667876, 4.07467387, 5.11503398])
In [43]: opti.newton(wobble,1) Out[43]: 0.74667875718448773
In [44]: opti.newton(wobble,3) Out[44]: 4.0746738725079457 In [45]: opti.newton(wobble,5)
Out[45]: 5.1150339802341476
FEEG1001 Numerical Methods
Minimum of a function
• Need to be careful here!
• There are a number of approaches:
– Examine the function at intervals and choose the minimum of the results
– Use scipy.optimize.minimum which will look for a local minimum of a function
– Use a global minimisation function like scipy.optimize.basinhopping which uses the minimum function from above but hops around the function to try and identify the global minimum
FEEG1001 Numerical Methods
In [116]: minfunc_x(wobble,-2,6,100) Out[116]: -15.21965613370468 In [117]: minfunc_x(wobble,-2,6,200) Out[117]: -15.222021699222097 In [118]: minfunc_x(wobble,-2,6,2000) Out[118]: -15.225256558272278 In [119]: minfunc_x(wobble,-2,6,20000) Out[119]: -15.225268440479127
In [115]: minfunc_scipy(wobble,-2,6,100) Out[115]: -15.225268456380793
[-1.6548195235885699, 2.8417840735091988, 4.6283657017930144,]
FEEG1001 Numerical Methods
Refinery Example • An oil refinery produces both petrol and diesel
• It can deal with a maximum of 12500 barrels of oil with a conversion rate of 80%
• It has a private contract to deliver 1000 barrels of petrol to a garage
• It has a government contract to deliver 2000 barrels of diesel to the tank training ground
• It transport via truck with a maximum of 180,000 barrel miles
• Petrol is delivered 10 miles, diesel is delivered 30 miles
• Profit is made at 10% and 20% per barrel of petrol and diesel respectively
FEEG1001 Numerical Methods
Refinery Example
• Maximise this profit equation
• Constraints
𝑃 𝑥,𝑦 = 0.1𝑥 + 0.2𝑦
0.1𝑥 + 0.3𝑦 ≤ 180000 Transport constraint
𝑥 + 𝑦 ≤ 10000 Process constraint
𝑥 ≥ 1000 Minimum Petrol
Minimum Diesel 𝑦 ≥ 2000
FEEG1001 Numerical Methods
Refinery Example
FEEG1001 Numerical Methods
Video
• UPS
FEEG1001 Design & Computing
Numerical Methods
Dr Stephen Boyd
Lecture 6
FEEG1001 Numerical Methods
Content
• Week 1 – Problems, data, matrices
• Week 2 – Linear algebra
• Week 3 – Interpolation
• Week 4 – Integration
• Week 5 – Root finding and optimisation
• Week 6 – Signal processing
FEEG1001 Numerical Methods
Signal Processing
• Signals are everywhere
• Tacoma Bridge
• Lightening
• What do we do with signals and what is the influence of their quality?
FEEG1001 Numerical Methods
Signal processing
• High speed testing of materials
FEEG1001 Numerical Methods
Signal Processing
FEEG1001 Numerical Methods
Simple concept
• A sine wave can be interpreted as sound
• A is the amplitude of the sound (volume)
• ω is the frequency
• In music middle C has a frequency of 261.63Hz or 1643.87rad/s
𝑦 = 𝐴 ∗ sin 𝜔𝜔
FEEG1001 Numerical Methods
Middle C
FEEG1001 Numerical Methods
Middle C with Noise
FEEG1001 Numerical Methods
What is the noise?
• We quite often want to filter out the noise
• First step is to identify what the noise is
• Numerical method called Fast Fourier Transform (FFT)
• Converts data acquired in the time domain in to the frequency domain
FEEG1001 Numerical Methods
FFT - Basics
• Definition of Fourier transform
• If we sample data, say N samples
• We can create a discrete function
• This is called a Discrete Fourier Transform (DFT)
• FFT is an algorithm that computes a DFT efficiently
𝑓 𝜔 =1
2𝜋� 𝑓 𝜔 𝑒𝑖𝜔𝜔𝑑𝜔∞
−∞
𝑓 𝜔𝑥 =1𝑁� 𝑓 𝜔𝛾 𝑒𝑖𝜔𝛾𝜔𝑥𝑁−1
𝛾=0
FEEG1001 Numerical Methods
FFT - Basics
Td (seconds)
N Samples
n: 0 1 2 3 4 ………………..N-1
t: 0 Δt ……………… ……… 𝑁−1𝑁𝑇𝑑
𝑇𝑑𝑁
= ∆𝜔 = 𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠 𝑟𝑠𝜔𝑒
Bb (Hz)
N Samples
k: 0 1 2 3 4 ………………..N-1
f: 0 Δf ……………… ……… 𝑁−1𝑁𝑓𝑠
𝐵𝑏𝑁
=𝑓𝑠𝑁
= ∆𝑓
1∆𝜔
=𝑁𝑇𝑑
= 𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠 𝑓𝑟𝑒𝑓𝑓𝑒𝑠𝑓𝑦 = 𝑓𝑠 𝐻𝐻
FEEG1001 Numerical Methods
Application of FFT
• FFT of music
FEEG1001 Numerical Methods
Example
• Create a noise signal to examine
FEEG1001 Numerical Methods
Example
• Create a noise signal to examine
FEEG1001 Numerical Methods
Example
• Run the FFT to see what frequencies are present
FEEG1001 Numerical Methods
Example
• Design a filter which will filter out all but the frequencies we want
FEEG1001 Numerical Methods
Example
• Result of filtering
FEEG1001 Numerical Methods
Video
• Signal processing