Intro to SciPy
(docs.scipy.org)
PHY 688: Numerical Methods for (Astro)Physics
Conventions
Note that some popular functions are available in the main scipy
namespace
Getting Help
Caveats
Integration
Numerical Integration
We want to compute:
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
Gridded Data
Numerical Integration
Numerical Integration
Numerical Integration
Solving:
Compound Integration
Integral over a
single slab
Compound Integration
Compound Trapezoidal
Compound Simpson's
Compound Integration
Gaussian Quadrature
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
...
You may want to enforce some other property on the form of the
interpolant
PHY 688: Numerical Methods for (Astro)Physics
Linear Interpolation
Lagrange Interpolation
Lagrange Interpolation
Lagrange Interpolation
Splines
Cubic Splines
Cubic Splines
Note that the splines can overshoot the original data values
PHY 688: Numerical Methods for (Astro)Physics
Cubic Splines
Root Finding
Root Finding
Bisection
If these are different signs, then the root lies between them
Newton-Raphson
Taylor expansion:
Update
Newton-Raphson
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
Initial guess:
Taylor expansion:
N equations + N
unknowns
Iterate:
PHY 688: Numerical Methods for (Astro)Physics
ODEs
ODEs
GM = 4 2
This is counter-clockwise
orbiting
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 :
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
th
4 -order Runge-Kutta
Adaptive Stepping
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
Adaptive Stepping
Adaptive Stepping
Adaptive Stepping
VODE
Fitting
Fitting Data
(discussion following Garcia)
Define:
We want to fit to
Note that the Ys may be nonlinear but we are still linear in the as
Minimize:
Linear
system
We cannot solve analytically for the parameters, but we can use our
root finding techniques on this.
We want to minimize
FFTs
Fourier Transform
Transform:
Inverse:
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)
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
Another interpretation: Fk / N Fk dk
Linear Algebra
Linear Algebra