16 views

Uploaded by mz_haq

SciPY

- FEM Exam with Solution
- 1ls3 Lectures Week11-1
- ban042-.pdf
- 2006 Finals
- Nested Radicals
- Spc
- Quadrature ICE11
- Cls r 45121215
- Mws Gen Int Txt Gaussquadrature
- skf2133-chapter3na.pdf
- Bessel Stirling Formula Numerical Analysis
- vicha_ms
- Gauss Quadrature Integration
- drs2
- Mathematics Paper 4 Impqs
- An analytical–numerical method for solving a heap leaching problem of one or more solid reactants from porous pellets
- Demidovich Problems in Mathematical Analysis
- Integral Susun
- Jntuk i Year Syllabus-ece_r16
- Tank

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

- FEM Exam with SolutionUploaded byArifian Agusta
- 1ls3 Lectures Week11-1Uploaded byFangcheng Chen
- ban042-.pdfUploaded bySelviana Arifika
- 2006 FinalsUploaded byjack
- Nested RadicalsUploaded byBenny Prasetya
- SpcUploaded byNei Stolberg
- Quadrature ICE11Uploaded byjaswinder singh
- Cls r 45121215Uploaded byFiller123
- Mws Gen Int Txt GaussquadratureUploaded bymanjumv27
- skf2133-chapter3na.pdfUploaded bydaviufc
- Bessel Stirling Formula Numerical AnalysisUploaded byomed
- vicha_msUploaded bydhdhdhd6719
- Gauss Quadrature IntegrationUploaded byBenjamin Flores
- drs2Uploaded bysasikalaindu
- Mathematics Paper 4 ImpqsUploaded byShamsiah Basar
- An analytical–numerical method for solving a heap leaching problem of one or more solid reactants from porous pelletsUploaded byJuan Sebastian Geraldo Escobar
- Demidovich Problems in Mathematical AnalysisUploaded byjwcstar7985
- Integral SusunUploaded byZafrullahAhmadShaddiq
- Jntuk i Year Syllabus-ece_r16Uploaded byratnams
- TankUploaded bySteve Wan
- 01 Scheme of Work ND2Uploaded byOsama Hassan
- Polynomial Interpolation and neville's algorithmUploaded byStephen Rickman
- Tas-302 Computer Based Numerical and Statistical Techniques l t pUploaded byKshitij Kumar
- Paper 24-A Posteriori Error Estimator for Mixed Approximation of the Navier-Stokes Equations With the C a b c Boundary ConditionUploaded byEditor IJACSA
- mtl_che_nle_ppt_newton.pptUploaded byairpavset
- 4 - MTH101 Final Term SolvedUploaded byCraig Freeman
- Csc104 Calculus MicrosyllabusUploaded bykamalshrish
- 01 Scheme of WorkMBD2,(a,B)-HNDUploaded byOsama Hassan
- Course Outline (1)Uploaded byZeeShanShah
- NumericsUploaded bymonu b

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

- Operating a Personal Computer LO 1-2Uploaded byFermo Gadayan Ramos
- Turtle SimulationUploaded bysatheesh_240
- CMPE 108 Midterm Study Questions_2015_2016Uploaded byAhmad Thaher
- Working With Processes LinuxUploaded byruli2010
- Web Operating SystemUploaded bySurya Prakash
- CFD Automatic Optimisation using OpenFOAM in GridUploaded byAbdelhamid Hmama
- lucente_pmacct_swinog19.pdfUploaded byich8dich
- CardioSoft Specifications NetworkingUploaded bygempur_sr
- Answers to Exercise Chapter 1-4 math 4Uploaded byZulhelmie Zaki
- Protection for Multi Owner Data Sharing SchemeUploaded byBONFRING
- Diebold Electronic Voting Machine Security Faults - Princeton UUploaded byPRMurphy
- Design Report SyUploaded byAbhishek Patel
- mikoritkUploaded byChris Jonathan Showip Route
- Abstract on Socket ProgrammingUploaded byyogesh_yadav2012
- Auto Start Enterprise Manager Cloud Control Agent 12c OraclUploaded byPieroFerraz
- Raid Config for Dell Disk ArrayUploaded byShasi Mutum
- Data TypesUploaded byNirali Dutiya
- admemailUploaded bysunilbhoj3
- NET Design GuidelinesUploaded byAndré Cardoso
- 0,295582,siUploaded bylalatendurath5716
- ISO 8583Uploaded bynishantvshah
- V6R2012xUploaded byasep
- Sky X technologyUploaded byKrithika Raj
- Chapter 10Uploaded byminuck12
- 244371864 Computer Science Investigatory Project on Railway Ticket Reservation for Class 12Uploaded byPraveen Kumar
- Copy Table From Remote DBUploaded byjames_alok
- PRO1_02E [읽기 전용] [호환 모드]Uploaded byNguyễn Anh Tú
- NASM - The Netwide AssemblerUploaded byNicholas Capasso
- 9863561_readmeUploaded byzzmaher
- Kalman Filter ExplainedUploaded bymohgawa