SciPY

© All Rights Reserved

16 views

SciPY

© All Rights Reserved

- Final Exam Topics
- UT Dallas Syllabus for math6313.501.10s taught by Janos Turi (turi)
- UT Dallas Syllabus for math6313.501.10f taught by Janos Turi (turi)
- gaussian
- Integration Test
- Integration Steps
- AFEM.Ch11
- Polynomial Approximation of Functions of Matrices
- Numerical Methods for Physicist
- Toc
- Document
- Numerical Integration
- chap08
- Computer Science
- Solution Simulation Ict
- lec15
- 04.14102507
- Bulletin of the Brazilian Mathematical Society, New Series Volume 38 Issue 1 2007 [Doi 10.1007_s00574-007-0034-5] Marilena Munteanu -- Quadrature Formulas for the Generalized Riemann-Stieltjes Integ
- For Nberg 1980
- Engenering sylabus 2016

You are on page 1of 79

Intro to SciPy

NumPy

We'll give a brief overview of the algorithms and then look at

the functions.

(docs.scipy.org)

PHY 688: Numerical Methods for (Astro)Physics

Conventions

import numpy as np

import scipy as sp

import matplotlib as mpl

import matplotlib.pyplot as plt

from scipy import linalg, optimize

Note that some popular functions are available in the main scipy

namespace

Getting Help

outputs things without a pager

Python also has a dir() function that lists the names a

module defines

Caveats

and assumptions

doing under the hood

implementations of algorithms that were developed many years

(decades even) ago.

Integration

Numerical Integration

We want to compute:

points

Generally speaking, asking for greater accuracy involves using more of the

discrete points in the approximation for I

We have the freedom to pick our integration points, and this can allow us to

optimize the calculation of I

(weighted) sum at a discrete number of points is called a quadrature rule

Fixed spacing between points: Newton-Cotes quadrature

PHY 688: Numerical Methods for (Astro)Physics

Gridded Data

grid

Numerical Integration

Numerical Integration

of a trapezoid

Numerical Integration

Solving:

Compound Integration

Integral over a

single slab

Compound Integration

Compound Trapezoidal

Compound Simpson's

Compound Integration

PHY 688: Numerical Methods for (Astro)Physics

Gaussian Quadrature

spacings?

We want to express

Gaussian Quadrature

(Garcia, Ch. 10)

We can write

and there will be a set of w's for which the integral is exact if f(x)

is a polynomial of degree < 2N!

Gaussian Quadrature

(Wikipedia)

In practice, the roots and weights are tabulated for these out to

many numbers of points, so there is no need to compute them.

Gaussian Quadrature

(Garcia, Ch. 10)

Example:

erf(1) (exact):

3-point trapezoidal:

3-point Simpson's:

3-point Gauss-Legendre:

0.84270079295

0.825262955597 -0.017437837353

0.843102830043 0.000402037093266

0.842690018485 -1.0774465204e-05

Interpolation

Interpolation

behavior of the functional form of the data

...

interpolant, the higher the accuracy

You may want to enforce some other property on the form of the

interpolant

PHY 688: Numerical Methods for (Astro)Physics

Linear Interpolation

PHY 688: Numerical Methods for (Astro)Physics

Lagrange Interpolation

through all the points (alternate formulations exist)

Given n points: x0, x1, ... , xn{1 , with associated function values:

f0, f1, ... , fn{1

Lagrange Interpolation

Lagrange Interpolation

sampled uniformly from tanh(). The error is shown below.

PHY 688: Numerical Methods for (Astro)Physics

Splines

points

Large number of points two distinct options:

match all derivatives at each pointthis is a spline

PHY 688: Numerical Methods for (Astro)Physics

Cubic Splines

Cubic Splines

Note that the splines can overshoot the original data values

PHY 688: Numerical Methods for (Astro)Physics

Cubic Splines

interpolation scheme (and sometimes far from...)

But, for plotting/graphics applications, they look right

Root Finding

Root Finding

graphically

Bisection

If these are different signs, then the root lies between them

PHY 688: Numerical Methods for (Astro)Physics

Newton-Raphson

Taylor expansion:

Update

the root <

PHY 688: Numerical Methods for (Astro)Physics

Newton-Raphson

root

Multiple roots?

not a guarantee

PHY 688: Numerical Methods for (Astro)Physics

Newton-Raphson

(Wikipedia)

Consider f(x) = x3 { 2x +2

Cycle

(wikipedia)

Secant Method

expensive to compute (e.g. EOS)

Brent's method combines bisection, secant, and other methods

to provide a very reliable method

Initial guess:

Taylor expansion:

N equations + N

unknowns

Iterate:

PHY 688: Numerical Methods for (Astro)Physics

ODEs

ODEs

explore the properties of ODE

integrators

mass):

years

GM = 4 2

Need to specify a semi-major

axis and eccentricity

Initial conditions:

This is counter-clockwise

orbiting

energy is not conserved here!

Things get better with a smaller timestep, but this is still first-order

Let's look at the code and see how small we need to get a closed circle

PHY 688: Numerical Methods for (Astro)Physics

Higher-order Methods

nd

2 -order Runge-Kutta

nd

2 -order Runge-Kutta

nd

2 -order Runge-Kutta

nd

2 -order Runge-Kutta

th

4 -order Runge-Kutta

Consider system:

Update through :

PHY 688: Numerical Methods for (Astro)Physics

th

4 -order Runge-Kutta

th

4 -order Runge-Kutta

th

4 -order Runge-Kutta

th

4 -order Runge-Kutta

th

4 -order Runge-Kutta

th

4 -order Runge-Kutta

PHY 688: Numerical Methods for (Astro)Physics

th

4 -order Runge-Kutta

PHY 688: Numerical Methods for (Astro)Physics

Adaptive Stepping

rapidly

evolution

timestep to reach some desired accuracy

Lot's of different techniques for this in the literature

Sun-grazing comet

Just to get a

reasonable-looking

solution, we needed

to use = 0.0005

This takes 2001

steps

At perihelion, conservation

is the worse

velocity is greatest, and

therefore the solution

changes the fastest

timestep at aphelion than

perihelion

Adaptive Stepping

same as the non-adaptive case ( = 0.005)

215 steps

Adaptive Stepping

Adaptive Stepping

Specifying absolute and relative errors in the state variables

ensures you know about the quality of the solution

The SciPy ODE packages can control all of this for you

timescales for change, 1 = 1 and 2 = 10{3

called stiff

PHY 688: Numerical Methods for (Astro)Physics

VODE

(alternately, a 15th order explicit method)

numerically

influence on your solution

Fitting

Fitting Data

(discussion following Garcia)

times (or positions) and associate values

values: i for each yi

find a simple functional form that best represents the data

this is the fitting problem

Define:

M

consider

But watch out, 1 may also mean our errors were too large to

begin with, we used too many parameters, ...

PHY 688: Numerical Methods for (Astro)Physics

We want to fit to

Note that the Ys may be nonlinear but we are still linear in the as

general polynomial

Minimize:

Linear

system

variable

minimizing:

We cannot solve analytically for the parameters, but we can use our

root finding techniques on this.

PHY 688: Numerical Methods for (Astro)Physics

(Yakowitz and Szidarovszky)

nonlinearly:

We want to minimize

root find

PHY 688: Numerical Methods for (Astro)Physics

FFTs

Fourier Transform

representation of a function into frequency-space

represented by a frequency k

For discrete data, the discrete analog of the Fourier transform gives:

PHY 688: Numerical Methods for (Astro)Physics

points

Transform:

Inverse:

PHY 688: Numerical Methods for (Astro)Physics

If fn is Real, then:

Python/NumPy's FFT

numpy.fft:

http://docs.scipy.org/doc/numpy/reference/routines.fft.html

fft/ifft: 1-d data

By design, the k=0, ... N/2 data is first, followed by the negative

frequencies. These later are not relevant for a real-valued f(x)

as fft/ifft, but doesn't return the negative frequencies

2-d and n-d routines analogously defined

points

point corresponds to 2 real numbers.

duplicate, i.e. they don't add to the information content

F({k) = F(k) if f(x) is real

where the function is real (e.g. rfft)

1/ (2 x) is called the Nyquist frequency

PHY 688: Numerical Methods for (Astro)Physics

Normalization

When plotting, we want to put the physical scale back in, as well as make

the correspondence to the continuous representation

Wavenumber to frequency:

In index space, the smallest wavelength is from one cell to the next, and

the smallest frequency is 1/N

Adding points to our data means we open up higher and higher frequencies

Amplitudes: rewrite inverse expression:

Another interpretation: Fk / N Fk dk

continuous form

Linear Algebra

Linear Algebra

- Final Exam TopicsUploaded byvn_1234
- UT Dallas Syllabus for math6313.501.10s taught by Janos Turi (turi)Uploaded byUT Dallas Provost's Technology Group
- UT Dallas Syllabus for math6313.501.10f taught by Janos Turi (turi)Uploaded byUT Dallas Provost's Technology Group
- gaussianUploaded byamargg
- Integration TestUploaded byBHAAJI0001
- Integration StepsUploaded bystochos
- AFEM.Ch11Uploaded byArun Kumar Kumar
- Polynomial Approximation of Functions of MatricesUploaded byHillel Tal-Ezer
- Numerical Methods for PhysicistUploaded bydanlad13
- TocUploaded byAryan Kumar
- DocumentUploaded byVenkadesh Kannan
- Numerical IntegrationUploaded byMuhammad Izzat Fakhri
- chap08Uploaded byAshoka Vanjare
- Computer ScienceUploaded byAnusha Vedanabhatla
- Solution Simulation IctUploaded byfrandi93
- lec15Uploaded bySubhendu Maity
- 04.14102507Uploaded bySandip Maity
- Bulletin of the Brazilian Mathematical Society, New Series Volume 38 Issue 1 2007 [Doi 10.1007_s00574-007-0034-5] Marilena Munteanu -- Quadrature Formulas for the Generalized Riemann-Stieltjes IntegUploaded byXtrayasasasazzzzz
- For Nberg 1980Uploaded byAshnek
- Engenering sylabus 2016Uploaded byMuhammad Awais
- f4-3Uploaded byTebsu Tebsuko
- Gradually Varied FlowUploaded byjose
- Integral_Calculus.pdfUploaded byZachary
- An Efficient Method for the Computation of Mixed pUploaded byEda Vujević
- Laboratorio de Intregracion AN2 (1)Uploaded byMarlon Alejdro
- Numerical IntegrationUploaded byAishwary Mishra
- Nguyen 2011Uploaded byAnonymous WkbmWCa8M
- ExtrapolationUploaded byendoparasite
- MA6459 Numerical MethodsUploaded byP S HARSHITA
- Final Research Thesis(2).pdfUploaded byYosef Gemessa Dadi

- The Concept of Exergy and Energy Quality - Truls GundersenUploaded byuser_account
- IJPGC2003-40143Uploaded bymz_haq
- HT2003-47484Uploaded bymz_haq
- Icope03 DraftUploaded bymz_haq
- Fundamental studies of premixed combustion [PhD Thesis]Uploaded bymz_haq
- IMECE2011-62423Uploaded bymz_haq
- ME 201 SecondLawUploaded byBisonno Rudro
- ME6101_FirstLawUploaded bymz_haq
- Hybrid VehiclesUploaded bymz_haq
- 11_hv_ev_fcevUploaded bymz_haq
- ME6101 BasicsUploaded bymz_haq
- ME 6101 OutlineUploaded bymz_haq
- hybrid_4Uploaded bymz_haq
- Power2013-98279Uploaded bymz_haq
- 01-matplotlib.pdfUploaded bymz_haq
- DOE_2015.pdfUploaded bymz_haq
- ProcICGET13 HaqUploaded bymz_haq
- Tikz TutorialUploaded bymz_haq
- 1_5302_711_MO_LGW523_GBUploaded bymz_haq
- ME 201 OutlineUploaded bymz_haq
- ME 201 Processes IUploaded bymz_haq
- ME 201 WorkHeatUploaded bymz_haq
- ME 201 FirstLawUploaded bymz_haq
- ME 201 BasicsUploaded bymz_haq
- ME 201 BasicsUploaded bymz_haq
- Solar Energy 2012Uploaded bymz_haq

- IntegrationUploaded byNasra Djabir
- monteCarloMethodsForSecurityPricing.pdfUploaded byMario Claudio Amaro Carreño
- Numerical Methods for Electrical EngineersUploaded byvignanaraj
- ME - SE (1)Uploaded byRavi Ayyavu
- FEM.pdfUploaded byJaved
- M. Tech. PE - SyllabusUploaded bymohitreddy
- qUploaded byAneuk Agam Jinoe
- Supersonic Flutter Analysis of Functionally Graded Material Plates with CracksUploaded bySEP-Publisher
- DIFFERENTIAL QUADRATURE SOLUTIONS OF THE GENERALIZED BURGERS–FISHER EQUATION WITH A STRONG STABILITY PRESERVING HIGH-ORDER TIME INTEGRATIONUploaded byHamid Mojiry
- Deriving Simpsons Rule Using Newton Interpolation (1)Uploaded byVinjk
- EMM 3514 - Numerical MethodUploaded byAdhar Good
- 8. Pengintegralan Numerik (lanjutan)Uploaded bycantik02m89
- 0471624896 NumericalUploaded byJingzhou Wang
- Chap08_8up- Numerical Integration and DifferentiationUploaded byagcore
- newton InterpolationsUploaded byJennifer Jenkins
- Carnell-MATLAB-Applications-in-Chemical-Engineering (1).pdfUploaded byErick Santiago Cubillos
- Critical Values of TUploaded byRam Singh
- Fluidization Engineering - Practice (Second Expanded Edition).pdfUploaded byभूषण लोहार
- Implemenration of an Eulerian Multi Phase Model in OpenFOAM and Its Application to Polydisperse Two Phase FlowsUploaded byAghajani
- Trapezoidal Rule NotesUploaded byAmbreen Khan
- Detailed TOCUploaded byDiego Tume
- part6.pdfUploaded byTebsu Tebsuko
- Finite Element Analysis - Content Lecture Module 1 - 7Uploaded bySmr Only
- Monte Carlo With Applications to Finance in RUploaded byMadMinarch
- 0471648078_2Uploaded byHamed Memarian
- 2 Hand-out on Gaussian Quadratures and Romberg Integration (1)Uploaded byuchiha_rhenzaki
- Afternotes on Numerical AnalysisUploaded bypizandola
- A Differential Quadrature as a Numerical Method to Solve Differential EquationsUploaded byHamid Mojiry
- MA6459 Numerical MethodsUploaded byP S HARSHITA
- Mtech Mde Syllabus v5 FinalUploaded byyathish

## Much more than documents.

Discover everything Scribd has to offer, including books and audiobooks from major publishers.

Cancel anytime.