FEEG1001 Design & Computing Numerical Methods Dr Stephen...

105
FEEG1001 Design & Computing Numerical Methods Dr Stephen Boyd All lecture slides delivered to date

Transcript of FEEG1001 Design & Computing Numerical Methods Dr Stephen...

Page 1: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Design & Computing

Numerical Methods

Dr Stephen Boyd

All lecture slides delivered to date

Presenter
Presentation Notes
Title Contents – Motivation - Aims and objectives - Planned method - Research challenges - Initial DIC results - Next steps
Page 2: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Design & Computing

Numerical Methods

Dr Stephen Boyd

Lecture 1

Presenter
Presentation Notes
Title Contents – Motivation - Aims and objectives - Planned method - Research challenges - Initial DIC results - Next steps
Page 3: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Introduction

What is the point of these lectures?

Presenter
Presentation Notes
The world is full of problems - not war, crime corruption – but engineering and science problems that we can solve through our understanding of the way things work and our knowledge of mathematical tools. The aim of the FEEG1001 module is to provide the grounding in design and computing that you will need to help solve problems in your time here at university but also beyond The lectures that I will deliver over the next 6 weeks are not aimed going to teach you how to use Python, Dr Hovorka’s lectures and the Lab sessions are there to do that. I am also not going to teach you the mathematics that sits behind functions in Python, MATH1054 will provide that. What I am going to try and demonstrate is that although the Mathematics that you see on the board or in text books is relatively simple, problems are usually more complex and therefore using the power of computing and the knowledge of some simple mathematical tools can help to solve problems efficiently. Lets call it context!
Page 4: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Content

• Week 1 – Problems, data, matrices

Page 5: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
One must always remember that the decision to use black boxes should be taken carefully. Does the required input and the resulting output actually give you what you are looking for? Sometimes, the only way is to test what the answer should be for a simple problem before embarking on the solution to a large and complex problem.
Page 6: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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.

Presenter
Presentation Notes
So…… lets look at the following problem. Draw the diagram on the board!
Page 7: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Directed graph

Page 8: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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= 𝑃

Page 9: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
After third click – do the Maths on the board! – Sheet 2
Page 10: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Where are the students?

Page 11: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Python

Presenter
Presentation Notes
Lets look at student_loc.py
Page 12: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 13: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Directed graph

Presenter
Presentation Notes
Imagine that this is a miniature version of the WWW, with each node representing a web page each arrow representing the existence of a link from one page to another One possible criterion for importance of a page is the number of inbound links — an indication of popularity By this measure, m and j are the most important pages, with 5 inbound links each However, what if the pages linking to m, say, are not themselves important? Thinking this way, it seems appropriate to weight the inbound nodes by relative importance The PageRank algorithm does precisely this Lets look at the code for doing this – go to WWW.py This is a somewhat simplified example of what actually happens - click
Page 14: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Video

• Google video

Presenter
Presentation Notes
This video explains how the web search ranking actually works. Can you spot where the Markov Chain Matrix is created??
Page 15: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Design & Computing

Numerical Methods

Dr Stephen Boyd

Lecture 2

Presenter
Presentation Notes
Title Contents – Motivation - Aims and objectives - Planned method - Research challenges - Initial DIC results - Next steps
Page 16: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Content

• Week 1 – Problems, data, matrices

• Week 2 – Linear algebra

Page 17: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Linear Algebra

• Solving simultaneous equations

• 3 equations, 3 unknowns

3𝐵 = 4C

4𝐴 = 3B + 4C

4𝐴 + 3𝐵 + 4𝐶 = 48

Presenter
Presentation Notes
Again, there are lots of application where we can define what is physically represented with a system of equations, i.e. use mathematics. Take for example this simple balance problem, what are the weights of A, B and C? Draw on the board time Sheets 1, 2 and 3
Page 18: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 19: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 20: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 21: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Linear Algebra

Presenter
Presentation Notes
Lets take a larger problem
Page 22: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
I don’t like the idea of doing the elimination for this matrix as there is a lot to do. Luckily, Python has an inbuilt solver for doing this is conducted within the routine allowing for row interchange, which we have used, partial pivoting, which makes the elimination method more stable and reduces rounding errors, (we have not discussed here but is a useful tool) and LU decomposition. First lets go back to the problem from Last week!
Page 23: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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.

Presenter
Presentation Notes
So…… lets return to the problem of students and the pub
Page 24: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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𝑅

Page 25: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Where are the students?

Presenter
Presentation Notes
So here we can see that the result is tending towards a solution. But what is the stationary state?
Page 26: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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𝑅

Presenter
Presentation Notes
Lets look at student_loc.py
Page 27: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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 ])

Presenter
Presentation Notes
Go to Linalg.py And show the station_matrix function
Page 28: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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.])

Page 29: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
This video explains how systems of linear equations are used in the Finite Element Analysis
Page 30: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Design & Computing

Numerical Methods

Dr Stephen Boyd

Lecture 3

Presenter
Presentation Notes
Title Contents – Motivation - Aims and objectives - Planned method - Research challenges - Initial DIC results - Next steps
Page 31: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Content

• Week 1 – Problems, data, matrices

• Week 2 – Linear algebra

• Week 3 - Interpolation

Page 32: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
In my research here at the University of Southampton I do a lot of mechanical testing of structures and materials – click I have conducted my measurement between strains of 0.0 and 2.0 – click If the value of strain at which I want to evaluate the stress is bounded by the data, this is interpolation – click If the value is outside of the range of measurement, e.g. 3.2% strain, this is extrapolation – same principle but more dangerous and risky procedure! In accordance with the ISO standards I have to do a large number of tests in order to do the statistics, so will be repeating this again and again for each tests, this is why we should consider writing some simple programmes to help - click
Page 33: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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!

𝑦 = 𝑓 𝑥

Presenter
Presentation Notes
If we can represent the data with a continuous function then we can use the function to find the value of stress at any value of strain – click This is what microsoft excel does if we put the data in there and try and fit a function to it!
Page 34: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
Curve fitting is as the name suggests, an attempt to fit a curve to the data, but we don’t expect the curve to pass through all the data points - click One way of judging is to minimise the sum of the squares of the discrepancies (or residuals) between the actual data and the values predicted by the chosen function. We can then adjust the parameters of the function to minimise this sum. This is known as least squares fitting of data. - click Lets look at this in python, check out interpolation.py Check two things, fit a 2nd order polynomial to the data Fit a 6th order polynomial to the data Examine the 6th order polynomial values and compare with excel
Page 35: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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])

Page 36: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Curve fitting

Presenter
Presentation Notes
Fitting a second order polynomial to the data provides a relatively poor fit to the original data! - click Increasing the order of the polynomial to 6 provides a better fit and the residuals are generally going down There are a lot of dangers to this approach and it requires a judgement of the closeness of the fit.
Page 37: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
So we have seen that representing the data with a continuous function is possible but difficult, so lets look at other ways of getting the data we want.
Page 38: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Linear interpolation

Presenter
Presentation Notes
Interpolation is the same as curve fitting just that we want function to pass through all the data points. So if we deal with each of the points as pairs, we can fit a straight line between them, this is linear interpolation – click Linear interpolation is easy to programme - click – sheet 1 Then show this little routine in inmterpolation.py
Page 39: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Polynomial interpolation

Presenter
Presentation Notes
There is a way to make a polynomial curve fit pass through all the data points. If the data has n points, a polynomial with order n-1 will pass through all data points! Try it in Python - click
Page 40: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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.

Presenter
Presentation Notes
Luckily there is a rigorous way to draw a smooth curve between a set of points, this is the spline. Cars, boats and planes are often drawn with splines to ensure ‘smoothness’ or ‘fairness’ of the object. Lets look at interpolation.py
Page 41: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Linear interpolation

Exact Solution x=0.5, y=19.88679245

Presenter
Presentation Notes
4 interpolated data points using a linear spline between points, I cheated I have the function that describes this data, the exact solution is 19.88. So this shows that linear interpolation is not perfect but it’s a good first approximation. However, the interp1D function allows us to go a little further. Try a cubic spline between points!
Page 42: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Cubic Spline Interpolation

Exact Solution x=0.5, y=19.88679245

Presenter
Presentation Notes
Of course there is something wrong here that cannot be seen immediately. If we analyse this function and plot the exact solution you can see that there are still some errors, but overall the behaviour is good -click
Page 43: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Cubic Spline Interpolation

Exact Solution x=0.5, y=19.88679245

Presenter
Presentation Notes
This is all very simple when done in two-dimensions. What happens when we get ever larger data sets and expand into 3 dimensions? - click
Page 44: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
plot_3D_scatter - click
Page 45: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Interpolation in 3D

Presenter
Presentation Notes
3D plot of the data as a scatter plot is not very intuitive
Page 46: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Interpolation in 3D

Presenter
Presentation Notes
This is slightly more informative and you can now see the pressure distribution, but really if we wanted to interpolate this data either to know the pressures at specific points or just to create a more refined image of the data we can use the SCIPY interpolate function interp2d
Page 47: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Interpolation in 3D

Page 48: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Interpolation

• So why is this important???

Presenter
Presentation Notes
Computational fluid dynamics is a powerful and data rich numerical method for solving fluid problems, my PhD student is examining the effect of hydrodynamic pressure on the elastic response of a structure, she has modelled a foil in the CFD and provided the imput velocity and angle of attack and the properties of the fluid. In order to do this she has divided the structure into small elements and solved the set of simultaneous equations to calculate the pressures. She must now import this into her ,finite element model where the mesh topology is not necessarily the same. An interpolation routine is used within the co-simulation environment to transfer the pressures from the element centres to the nodes of the finite element model. As there are many millions of elements in both models this is only possible using a numerical method.
Page 49: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Video

• Frame interpolation

• Motion interpolation

Presenter
Presentation Notes
This video explains how systems of linear equations are used in the Finite Element Analysis
Page 50: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Design & Computing

Numerical Methods

Dr Stephen Boyd

Lecture 4

Presenter
Presentation Notes
Title Contents – Motivation - Aims and objectives - Planned method - Research challenges - Initial DIC results - Next steps
Page 51: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Content

• Week 1 – Problems, data, matrices

• Week 2 – Linear algebra

• Week 3 – Interpolation

• Week 4 - Integration

Page 52: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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 !!

Presenter
Presentation Notes
Lets start with something simple – do maths on the board!
Page 53: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 54: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Integration

• What if we don’t have the function but some data points and want to integrate?

Presenter
Presentation Notes
We could use the curve fitting again, but the dangers of do so are the same as for interpolation
Page 55: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

𝑏

𝑎

Presenter
Presentation Notes
This equation is easily written into a python function see Trap.py
Page 56: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
We can also do this with numpy and scipy
Page 57: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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𝑏

𝑎

Page 58: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
We can also do this with inbuilt function from numpy and scipy
Page 59: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

𝑃(𝑠𝑠𝑠𝑠𝑑𝑖) =𝑁𝑠𝑁 𝑠𝑠𝑠𝑠𝑑𝑖 𝐶𝑠𝐴𝑐𝐶𝑖𝑇𝑐𝑡𝐴𝐶 𝑠𝑠𝑁𝑛𝑖𝐴

𝐴𝐴𝑖𝐴 𝑐𝑓 𝑡ℎ𝑖 𝑐𝑠𝐴𝑐𝐶𝑖𝐴𝐴𝑖𝐴 𝑐𝑓 𝑡ℎ𝑖 𝑠𝑠𝑠𝐴𝐴𝑖

=𝑁𝑠𝑁 𝑠𝑠𝑠𝑠𝑑𝑖 𝐶𝑠𝐴𝑐𝐶𝑖𝑇𝑐𝑡𝐴𝐶 𝑠𝑠𝑁𝑛𝑖𝐴

𝐴𝐴𝑖𝐴 𝑐𝑓 𝑡ℎ𝑖 𝑐𝑠𝐴𝑐𝐶𝑖 =𝑁𝑠𝑁 𝑠𝑠𝑠𝑠𝑑𝑖 𝐶𝑠𝐴𝑐𝐶𝑖𝑇𝑐𝑡𝐴𝐶 𝑠𝑠𝑁𝑛𝑖𝐴

× 𝐴𝐴𝑖𝐴 𝑐𝑓 𝑡ℎ𝑖 𝑠𝑠𝑠𝐴𝐴𝑖

Presenter
Presentation Notes
An example is the use of Monte Carlo integration to find the value of pi Show working from sheet 2
Page 60: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
An example is the use of Monte Carlo integration to find the value of pi
Page 61: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
An example is the use of Monte Carlo integration to find the value of pi
Page 62: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
An example is the use of Monte Carlo integration to find the value of pi
Page 63: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 64: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 65: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 66: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Video

• Gambling

Page 67: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Design & Computing

Numerical Methods

Dr Stephen Boyd

Lecture 5

Presenter
Presentation Notes
Title Contents – Motivation - Aims and objectives - Planned method - Research challenges - Initial DIC results - Next steps
Page 68: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 69: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
Lets start with something simple – do maths on the board!
Page 70: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Root finding

• However, this approach only works for quadratic equations

• What about this function:

𝑦 = 8𝑠𝑠𝑠 𝑥 3 + 2𝑥 − 4

Page 71: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
There are lots of methods of doing this
Page 72: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Root finding

• Using this simple function:

𝑦 = 𝑥2 − 3

Page 73: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
We are going to take this concept of the function changing sign and use it to hunt for roots of functions Incremental search is inefficient but the concept used here is the same for the more “sophisticated” and “respected” methods
Page 74: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
This can be easily written into a function in Python – alternatively you can use the Scipy function scipy.optimize.bisect(f,a,b)
Page 75: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 76: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

False position (‘regula falsi’)

Page 77: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Presenter
Presentation Notes
This method does not use bracketing and therefore should converge more quickly
Page 78: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Secant method

Page 79: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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 = 𝑥𝑛 −𝑓 𝑥𝑛𝑓𝑓 𝑥𝑛

Page 80: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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])

Presenter
Presentation Notes
Lets go back to this example, we have been finding single roots with all of these numerical root finding methods Use fsolve – based on an algorithm called powells method to search for roots of n function in n dimensions – i.e. it is very powerful.
Page 81: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 82: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 83: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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,]

Page 84: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 85: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 86: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Refinery Example

Page 87: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Video

• UPS

Presenter
Presentation Notes
Draw the diagram
Page 88: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Design & Computing

Numerical Methods

Dr Stephen Boyd

Lecture 6

Presenter
Presentation Notes
Title Contents – Motivation - Aims and objectives - Planned method - Research challenges - Initial DIC results - Next steps
Page 89: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 90: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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?

Presenter
Presentation Notes
A set of tuning forks, they are specifically designed to resonate at a specific frequency. Everything has a specific natural frequency at which it is excited. The bridge was excited to destruction due to the wind exciting it at its resonate frequency.
Page 91: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Signal processing

• High speed testing of materials

Presenter
Presentation Notes
Lets go back to the fact that I do a lot of experimental testing of materals. Unlike the smooth stress strain data I shoed two weeks ago our Very High Speed (VHS) test machine tests materials at up to 20m/s. The machine has a lot of components. On the right is the slack adaptor that allows the test machine to accelerate to its required velocity before pulling the specimen. Once the specimen is loaded the material is put under stress and ultimately fails. The loading of the specimen and the failure of the specimen result in excitation of the load train and this is picked up by the load cell.
Page 92: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Signal Processing

Page 93: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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 𝜔𝜔

Page 94: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Middle C

Page 95: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Middle C with Noise

Page 96: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 97: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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

Page 98: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

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∆𝜔

=𝑁𝑇𝑑

= 𝑠𝑠𝑠𝑠𝑠𝑠𝑠𝑠 𝑓𝑟𝑒𝑓𝑓𝑒𝑠𝑓𝑦 = 𝑓𝑠 𝐻𝐻

Page 99: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Application of FFT

• FFT of music

Page 100: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Example

• Create a noise signal to examine

Page 101: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Example

• Create a noise signal to examine

Page 102: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Example

• Run the FFT to see what frequencies are present

Page 103: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Example

• Design a filter which will filter out all but the frequencies we want

Page 104: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Example

• Result of filtering

Page 105: FEEG1001 Design & Computing Numerical Methods Dr Stephen …feeg1001/pdfs/Steve_Boyd_Lecture2.pdf · 2015. 12. 7. · FEEG1001 Numerical Methods Introduction What is the point of

FEEG1001 Numerical Methods

Video

• Signal processing

Presenter
Presentation Notes
Draw the diagram