/
Lobatto_v2.py
576 lines (384 loc) · 13.4 KB
/
Lobatto_v2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
import numpy as np
from newton import *
# tmp for testing
from quadratures import *
from composite_quadrature import *
# generate the Legendre polynomial function of order n recusrivley
# P(0,X) = 1
# P(1,X) = X
# P(N,X) = ( (2*N-1)*X*P(N-1,X)-(N-1)*P(N-2,X) ) / N
def L(n):
if (n==0):
# P(0,X) = 1
return lambda x: x*0+1.0
elif (n==1):
# P(1,X) = X
return lambda x: x
else:
# P(N,X) = ( (2*N-1)*X*P(N-1,X)-(N-1)*P(N-2,X) ) / N
return lambda x: ( (2.0*n-1.0) * x * L(n-1)(x)-(n-1) * L(n-2)(x) ) / n
# generate derivative function of the Legendre polynomials of
# order n recursivley
# P'(0,X) = 0
# P'(1,X) = 1
# P'(N,X) = ( (2*N-1)*(P(N-1,X)+X*P'(N-1,X)-(N-1)*P'(N-2,X) ) / N
def dL(n):
#[TODO]: allow evaluation at 0
if (n==0):
return lambda x: x*0
elif (n==1):
return lambda x: x*0+1.0
else:
# (1 − x2)pn′(x) = n[−xpn(x) + pn−1(x)]
return lambda x: (n/(x**2-1.0))*(x*L(n)(x)-L(n-1)(x))
#
# get coefficients approximation of polynomial
#
from newton_interp import *
def coef_approximation(p, order):
# maintain parity of order +1
n = 50 + order +1
r = 1
xs = np.linspace(-r, r, num=n)
ys = p(xs)
# [TODO]: fix coeffients method
# replace with 'c = coeffients(xs, ys)'
degree = n #n #order + 1
print(degree)
c = np.polyfit(xs,ys,degree)
#c = coeffients(xs, ys)
return c
def polynomial_derivative(coefficients):
# compute coefficients for first derivative of p with coefficients c
# [TODO]: create own method
c_prime = np.polyder(coefficients)
return c_prime
# generate second derivative of the Lengendre polynomials of
# order n recursivley
# P"(0,X) = 0
# P"(1,X) = 0
# incorrect!!!
# P"(N,X) = ( (2*N-1)*(2*P'(N-1,X)+X*P"(N-1,X)-(N-1)*P'(N-2,X) ) / N
def ddL(n):
if (n==0):
return lambda x: 0.0
elif (n==1):
return lambda x: 0.0
else:
# P"(N,X) = ( (2*N-1)*(2*P'(N-1,X)+X*P"(N-1,X)-(N-1)*P'(N-2,X) ) / N
#return lambda x: ( (2*n-1) * (2 * dL(n-1)(x)) + x * ddL(n-1)(x) - ((n-1) * dL(n-2)(x)) ) / n
# approximate by fitting polynomial and taking derivatives
c_om1 = coef_approximation(L(n), n)
c_prime = polynomial_derivative(c_om1)
c_double_prime = polynomial_derivative(c_prime)
# [TODO]: create own poly eval function
return lambda x: np.polyval(c_double_prime, x)
# find an approximation for roots of the legendre polynomial
# of given order
# on the unit interval [-1, 1]
def unit_lengendre_roots(order, tol=1e-15, output=True):
roots=[]
print("Finding roots of Legendre polynomial of order ", order)
# The polynomials are alternately even and odd functions
# so evaluate only half the number of roots
for i in range(1,int(order/2) +1):
# initial guess, x0, for ith root
# the approximate values of the abscissas.
# these are good initial guesses
x0=np.cos(np.pi*(i-0.25)/(order+0.5))
# call newton to find the roots of the legendre polynomial
Ffun, Jfun = L(order), dL(order)
ri, _ = newton( Ffun, Jfun, x0 )
roots.append(ri)
# use symetric properties to find remmaining roots
# the nodal abscissas are computed by finding the
# nonnegative zeros of the Legendre polynomial pm(x)
# with Newton’s method (the negative zeros are obtained from symmetry).
roots = np.array(roots)
# even. no center
if order % 2==0:
roots = np.concatenate( (-1.0*roots, roots[::-1]) )
# odd. center root is 0.0
else:
roots = np.concatenate( (-1.0*roots, [0.0], roots[::-1]) )
return roots
# find an approximation for roots of the derivative of the legendre polynomial
# of given order
# on the unit interval [-1, 1]
def unit_lobatto_nodes(order, tol=1e-15, output=True):
roots=[]
print("Finding roots of the derivative of the Legendre polynomial of order ", order)
# The polynomials are alternately even and odd functions
# so evaluate only half the number of roots
# order - 1, lobatto polynomial is derivative of legendre polynomial of order n-1
order = order-1
for i in range(1,int(order/2) +1):
# initial guess, x0, for ith root
# the approximate values of the abscissas.
# these are good initial guesses
#x0=np.cos(np.pi*(i-0.25)/(order+0.5))
x0=np.cos(np.pi*(i+0.1)/(order+0.5)) # not sure why this inital guess is better
# call newton to find the roots of the lobatto polynomial
#Ffun, Jfun = dL(order-1), ddL(order-1)
Ffun, Jfun = dL(order), ddL(order)
ri, _ = newton( Ffun, Jfun, x0 )
roots.append(ri)
# remove roots close to zero
cleaned_roots = []
tol = 1e-08
for r in roots:
if abs(r) >= tol:
cleaned_roots += [r]
roots = cleaned_roots
# use symetric properties to find remmaining roots
# the nodal abscissas are computed by finding the
# nonnegative zeros of the Legendre polynomial pm(x)
# with Newton’s method (the negative zeros are obtained from symmetry).
roots = np.array(roots)
# add -1 and 1 to tail ends
# check parity of order + 1
# even. no center
#if order % 2==0:
if (order + 1) % 2==0:
roots = np.concatenate( ([-1.0], -1.0*roots, roots[::-1], [1.0]) )
# odd. center root is 0.0
else:
roots = np.concatenate( ([-1.0], -1.0*roots, [0.0], roots[::-1], [1.0] ) )
return roots
# find weights for the roots of the legendre polynomial
# of given order
# on the unit interval [-1, 1]
def unit_gauss_weights_and_nodes(order):
# find roots of legendre polynomial on unit interval
nodes = unit_lengendre_roots(order)
# calculate weights for unit interval
# Ai = 2 / [ (1 - xi^2)* (p'n+1(xi))^2 ] -- Gauss Legendre Weights
weights = 2.0/( (1.0-nodes**2) * dL(order)(nodes)**2 )
return weights, nodes
# find weights for the lobatto polynomial
# of given order
# on the unit interval [-1, 1]
def unit_lobatto_weights_and_nodes(order):
# find roots of legendre polynomial on unit interval
nodes = unit_lobatto_nodes(order)
# calculate weights for unit interval
# Ai = 2 / [ (1 - xi^2)* (p'n+1(xi))^2 ] -- Gauss Legendre Weights
#weights = 2.0/( (1.0-nodes**2) * dL(order)(nodes)**2 )
# wi = 2/(n(n-1) * Pn-1(xi)^2)
weights = 2.0/( (order*(order-1)) * L(order-1)(nodes)**2 )
return weights, nodes
# given unit weights and nodes on interval [-1,1] map to interval [a,b]
def project_weights_and_nodes(a, b, unit_weights, unit_nodes):
# project onto interval [a,b]
nodes = 0.5*(b-a)*unit_nodes + 0.5*(a+b)
weights = 0.5*(b-a)*unit_weights
return weights, nodes
def gauss_quadrature(f, a, b, order, weights=None, nodes=None):
# if weights and nodes are None, compute them for the interval [a,b]
if weights is None:
assert nodes is None
# find weights for the legendre polynomial on the unit interval
unit_weights, unit_nodes = unit_gauss_weights_and_nodes(order)
# project onto interval [a,b]
weights, nodes = project_weights_and_nodes(a, b, unit_weights, unit_nodes)
# compute integral approximation
Iapprox = sum( weights * f(nodes) )
# record the number of function calls for f as nf
nf = order
return Iapprox, nf
def lobatto_quadrature(f, a, b, order, weights=None, nodes=None):
# if weights and nodes are None, compute them for the interval [a,b]
if weights is None:
assert nodes is None
# find weights for the legendre polynomial on the unit interval
unit_weights, unit_nodes = unit_lobatto_weights_and_nodes(order)
# project onto interval [a,b]
weights, nodes = project_weights_and_nodes(a, b, unit_weights, unit_nodes)
# compute integral approximation
Iapprox = sum( weights * f(nodes) )
# record the number of function calls for f as nf
nf = order
return Iapprox, nf
def composite_gauss_quadrature(f, a, b, order, m):
# m in number of sub intervals
# check inputs
if (b < a):
raise ValueError('composite_gauss_quadrature error: b < a!')
if (m < 1):
raise ValueError('composite_gauss_quadrature error: m < 1!')
# set up subinterval width
h = 1.0*(b-a)/m
# initialize results
Imn = 0.0
nf = 0
# compute the unit weights and nodes for the lobatto quadrature
# of given order
unit_weights, unit_nodes = unit_gauss_weights_and_nodes(order)
# iterate over subintervals
for i in range(m):
# define subintervals start and stop points
ai, bi = a+i*h, a+(i+1)*h
# project onto interval [a,b]
weights, nodes = project_weights_and_nodes(ai, bi, unit_weights, unit_nodes)
# call lobatto quadrature formula on this subinterval
In, nlocal = gauss_quadrature(f, ai, bi*h, order, weights=weights, nodes=nodes)
# increment outputs
Imn += In
nf += nlocal
return Imn, nf
def composite_lobatto_quadrature(f, a, b, order, m):
# m in number of sub intervals
# check inputs
if (b < a):
raise ValueError('composite_gauss_quadrature error: b < a!')
if (m < 1):
raise ValueError('composite_gauss_quadrature error: m < 1!')
# set up subinterval width
h = 1.0*(b-a)/m
# initialize results
Imn = 0.0
nf = 0
# compute the unit weights and nodes for the lobatto quadrature
# of given order
unit_weights, unit_nodes = unit_lobatto_weights_and_nodes(order)
# [TODO]: reuse calculation of end nodes and weights
# iterate over subintervals
for i in range(m):
# define subintervals start and stop points
ai, bi = a+i*h, a+(i+1)*h
# project onto interval [a,b]
weights, nodes = project_weights_and_nodes(ai, bi, unit_weights, unit_nodes)
# call lobatto quadrature formula on this subinterval
In, nlocal = lobatto_quadrature(f, ai, bi*h, order, weights=weights, nodes=nodes)
# increment outputs
Imn += In
nf += nlocal
return Imn, nf
"""
import matplotlib.pyplot as plt
y_approx = lambda coeffs, xs: np.polyval(coeffs, xs)
x = np.linspace(-1.05, 1.05, num=80)
#
# show polynomial interpolation accuracy
#
order = 5
c = coef_approximation(L(order), order)
y = L(order)(x)
y_hats = y_approx(c, x)
L_roots = unit_lengendre_roots(order)
L_root_ys = L(order)(L_roots)
L_root_y_hats = y_approx(c, L_roots)
#plt.scatter(L_roots, L_root_ys, c='g', s=30)
plt.scatter(L_roots, L_root_y_hats, c='b', s=50, marker='X')
plt.plot(x,y, c='g', linewidth=3)
plt.plot(x, y_hats, c='r', linestyle='--', linewidth=2)
plt.grid(color='k', linestyle='-', linewidth=0.5)
plt.show()
#
# show derivative approximation
#
# take derivatice of this representation
#c_om1 = coef_approximation(L(order-1), order-1)
om1 = order-1
c_om1 = coef_approximation(L(om1), om1)
c_prime = polynomial_derivative(c_om1)
dL_y = dL(order-1)(x)
dL_y_hats = np.polyval(c_prime, x) #y_approx(c_prime, x)
dL_roots = unit_lobatto_nodes(order)
dL_roots = dL_roots[1:-1]
dL_root_ys = dL(order)(dL_roots)
dL_root_y_hats = np.polyval(c_prime, dL_roots) #y_approx(c_prime, dL_roots)
#plt.scatter(dL_roots, dL_root_ys, c='g', s=30)
plt.scatter(dL_roots, dL_root_y_hats, c='b', s=50, marker='X')
plt.plot(x, dL_y, c='g', linewidth=3)
plt.plot(x, dL_y_hats, c='r', linestyle='--', linewidth=2)
plt.grid(color='k', linestyle='-', linewidth=0.5)
plt.show()
#
# show 2nd derivative approximation
#
om1 = order-1
c_om1 = coef_approximation(L(om1), om1)
c_prime = polynomial_derivative(c_om1)
c_double_prime = polynomial_derivative(c_prime)
ddL_y_hats = np.polyval(c_double_prime, x) #y_approx(c_prime, x)
plt.plot(x, ddL_y_hats, c='r', linestyle='--', linewidth=2)
plt.grid(color='k', linestyle='-', linewidth=0.5)
plt.show()
"""
"""
order = 5
ws, xs = unit_lobatto_weights_and_nodes(order)
for w,x in zip(ws,xs):
print("w: %s, x: %s" % (w,x))
"""
if __name__ == "__main__":
# set the integration interval, integrand function and parameters
a = -5.0
b = 4.0
c = 0.5
d = 5.0
def f(x):
return np.exp(c*x) + np.sin(d*x)
# set the true integral
Itrue = 1.0/c*(np.exp(c*b) - np.exp(c*a)) - 1.0/d*(np.cos(d*b)-np.cos(d*a))
# gauss quadrature paramters and composite gauss quadrature paramters
order = 6
ms = [2, 20, 200, 2000]
# tmp - test against profs method
pqd = Gauss8
#
# Test gauss quadrature method
#
print("\n\n\nTesting lobatto quadrature method...\n")
# my approx
Iapprox, nf = lobatto_quadrature( f, a, b, order )
# professors approx
pIapprox, pnf = pqd(f, a, b)
# display true integral value
print("\nI = ", Itrue)
# display my approx
print("My Integral Aprroximation : ", Iapprox)
# display professors approx
print("Professors Integral Aprroximation : ", pIapprox)
print("Diffrence : ", abs(Iapprox - pIapprox))
# display my nf
print("\nMy nf : ", nf)
# display professors nf
print("Professors nf : ", pnf)
print("Diffrence : ", abs(nf - pnf))
# check error agains I true from class test function
true_error = Itrue - Iapprox
print("\nMy Error : ", true_error)
p_true_error = Itrue - pIapprox
print("Professors Error : ", p_true_error)
print("Diffrence : ", abs(true_error-p_true_error))
#
# Test composite gauss quadrature method
#
print("\n\n\nTesting composite lobatto quadrature method...")
for m in ms:
print("\nm : ", m)
print("order : ", order)
# my approx
Iapprox, nf = composite_lobatto_quadrature(f, a, b, order, m )
# professors approx
pIapprox, pnf = composite_quadrature(f, a, b, pqd, m)
# display true integral value
print("\nI = ", Itrue)
# display my approx
print("My Integral Aprroximation : ", Iapprox)
# display professors approx
print("Professors Integral Aprroximation : ", pIapprox)
print("Diffrence : ", abs(Iapprox - pIapprox))
# display my nf
print("\nMy nf : ", nf)
# display professors nf
print("Professors nf : ", pnf)
print("Diffrence : ", abs(nf - pnf))
# check error agains I true from class test function
true_error = Itrue - Iapprox
print("\nMy Error : ", true_error)
p_true_error = Itrue - pIapprox
print("Professors Error : ", p_true_error)
print("Diffrence : ", abs(true_error-p_true_error))