Small updates

master
Per.Andreas.Brodtkorb 12 years ago
parent 70146d8ef2
commit 869e088793

@ -18,11 +18,11 @@ def magic(n):
''' '''
if np.mod(n,1)==1: # odd order if np.mod(n,1)==1: # odd order
ix = np.arange(n)+1 ix = np.arange(n) + 1
J, I = np.meshgrid(ix,ix) J, I = np.meshgrid(ix, ix)
A = np.mod(I+J-(n+3)/2,n) A = np.mod(I + J - (n + 3) / 2, n)
B = np.mod(I+2*J-2,n) B = np.mod(I + 2 * J - 2, n)
M = n*A + B + 1 M = n * A + B + 1
return M return M
@ -33,7 +33,7 @@ def peaks(x=None, y=None, n=51):
Example Example
------- -------
>>> import pylab as plt >>> import matplotlib.pyplot as plt
>>> x,y,z = peaks() >>> x,y,z = peaks()
>>> h = plt.contourf(x,y,z) >>> h = plt.contourf(x,y,z)
@ -54,6 +54,13 @@ def peaks(x=None, y=None, n=51):
def humps(x=None): def humps(x=None):
''' '''
Computes a function that has three roots, and some humps. Computes a function that has three roots, and some humps.
Example
-------
>>> import matplotlib.pyplot as plt
>>> x = np.linspace(0,1)
>>> y = humps(x)
>>> h = plt.plot(x,y)
''' '''
if x is None: if x is None:
y = np.linspace(0, 1) y = np.linspace(0, 1)
@ -63,4 +70,4 @@ def humps(x=None):
return 1.0 / ((y - 0.3) ** 2 + 0.01) + 1.0 / ((y - 0.9) ** 2 + 0.04) + 2 * y - 5.2 return 1.0 / ((y - 0.3) ** 2 + 0.01) + 1.0 / ((y - 0.9) ** 2 + 0.04) + 2 * y - 5.2
if __name__ == '__main__': if __name__ == '__main__':
pass pass

@ -1136,7 +1136,7 @@ def richardson(Q, k):
R = Q[k] + (Q[k] - Q[k - 1]) / c R = Q[k] + (Q[k] - Q[k - 1]) / c
return R return R
def quadgr(fun, a, b, abseps=1e-5): def quadgr(fun, a, b, abseps=1e-5, maxiter=17):
''' '''
Gauss-Legendre quadrature with Richardson extrapolation. Gauss-Legendre quadrature with Richardson extrapolation.
@ -1218,7 +1218,7 @@ def quadgr(fun, a, b, abseps=1e-5):
Q = -Q Q = -Q
return Q, err return Q, err
#% Gauss-Legendre quadrature (12-point) # Gauss-Legendre quadrature (12-point)
xq = np.asarray([0.12523340851146894, 0.36783149899818018, 0.58731795428661748, xq = np.asarray([0.12523340851146894, 0.36783149899818018, 0.58731795428661748,
0.76990267419430469, 0.9041172563704748, 0.98156063424671924]) 0.76990267419430469, 0.9041172563704748, 0.98156063424671924])
wq = np.asarray([0.24914704581340288, 0.23349253653835478, 0.20316742672306584, wq = np.asarray([0.24914704581340288, 0.23349253653835478, 0.20316742672306584,
@ -1232,8 +1232,8 @@ def quadgr(fun, a, b, abseps=1e-5):
# else: # else:
dtype = np.float64 dtype = np.float64
#% Initiate vectors # Initiate vectors
max_iter = 17 # Max number of iterations # max_iter = 17 # Max number of iterations
Q0 = zeros(max_iter, dtype=dtype) # Quadrature Q0 = zeros(max_iter, dtype=dtype) # Quadrature
Q1 = zeros(max_iter, dtype=dtype) # First Richardson extrapolation Q1 = zeros(max_iter, dtype=dtype) # First Richardson extrapolation
Q2 = zeros(max_iter, dtype=dtype) # Second Richardson extrapolation Q2 = zeros(max_iter, dtype=dtype) # Second Richardson extrapolation
@ -1480,10 +1480,12 @@ def main():
q0 = np.trapz(humps(x), x) q0 = np.trapz(humps(x), x)
[q, err] = romberg(humps, 0, np.pi / 2, 1e-4) [q, err] = romberg(humps, 0, np.pi / 2, 1e-4)
print q, err print q, err
def test_docstrings(): def test_docstrings():
np.set_printoptions(precision=7) np.set_printoptions(precision=7)
import doctest import doctest
doctest.testmod() doctest.testmod()
if __name__ == '__main__': if __name__ == '__main__':
test_docstrings() test_docstrings()
#main() #main()

Loading…
Cancel
Save