Runge's Phenomenon
Author's Name: Rea Vickovic
Date: 10/12/2022
Motivation
Problem statement
What is Runge's Phenomenon?
Methods
Demonstrate Runge's Phenomenon using Runge's function
import numpy as np
import matplotlib.pyplot as plt
#Set the interval
a, b = -1, 1
#This is number of points we evaluate the interpolation function on
nplot = 1000
#This code is for the Runge's function
runge = lambda x: 1 / (1 + 25 * x ** 2)
x = np.linspace(a, b, nplot)
y = runge(x)
#Now we plot the corresponding function
plt.plot(x, y)
plt.text(0.2,0.7 , r'$y = \frac{1}{1+(5x)^2}=\frac{1}{1+25x^2}$', fontsize=15)
plt.title ('Runge´s function')
plt.xticks=[]
plt.xlabel("x")
plt.ylabel("y")
plt.figtext(0.5, -0.04, "Figure 1", ha="center", fontsize=10)
from numpy import (zeros_like,prod)
#In the code below we define the Lagrange(x_int,y_int,x) Interpolation function
def lagrange(x_int, y_int, x):
y_new = zeros_like(x)
for xi, yi in zip(x_int, y_int):
y_new += yi*prod([(x - xj)/(xi - xj)
for xj in x_int if xi!=xj], axis=0)
return y_new
#x_int1/2/3 are the isometric nodes
x_int1 = np.linspace(a, b, 6) #x=6
x_int2= np.linspace(a, b,11) #x=11
x_int3= np.linspace(a, b,16) #x=16
#y_new1/2/3 are Lagrange interpolation points
y_new1 = lagrange(x_int1, runge(x_int1), x)
y_new2= lagrange(x_int2,runge(x_int2),x)
y_new3=lagrange(x_int3,runge(x_int3),x)
#Code below is the exact solution for Runge's function
plt.plot(x, runge(x), "k", label="Runge function")
# Code below gives us the green graph for Lagrange Interpolation with nodes x = 6
plt.plot(x, y_new1,'g--',label="Lagrange Interpolation x=6")
#Code below gives us the yellow graph for Lagrange Interpolation with nodes x = 11
plt.plot(x, y_new2,'y--',label="Lagrange Interpolation x=11")
#Code below gives us the blue graph for Lagrange Interpolation with nodes x = 16
plt.plot(x, y_new3,'b--',label="Lagrange Interpolation x=16")
plt.legend(bbox_to_anchor=(1.1,1))
plt.title('Example of Runges Theorem')
plt.xlabel("x")
plt.ylabel("y")
plt.figtext(0.5, -0.04, "Figure 2", ha="center", fontsize=10)
#The figure shows the Runges Phenomenon
plt.show()
Results
What is a possible remedy to avoid spurious oscillations?
Demonstrate the effectiveness of this remedy
Example 1: with 6 Chebyshev nodes
#Here we select Chebyshev nodes with x=6
x_Cheby = np.linspace(-1,1,6)
for i in range(6):
x_Cheby[i] = np.cos((2*(i+1) - 1)/(2*6)*np.pi)
y_Cheby = 1/(1 + 25*x_Cheby**2)
y_newCheby = lagrange(x_Cheby, y_Cheby, x)
#plt under is exact solution for runge
plt.plot(x, runge(x), "k", label="Runge function")
#green graph - Lagrange interpolation
plt.plot(x,y_new1,'g--',label="Lagrange Interpolation x=6")
#Cheby plot
plt.plot(x,y_newCheby,'r--',label="Chebyshev nodes x=6 ")
plt.legend(bbox_to_anchor=(1.6,1))
plt.title('Lagrange Interpolation and Chebyshev nodes')
plt.xlabel("x")
plt.ylabel("y")
plt.figtext(0.5, -0.04, "Figure 3", ha="center", fontsize=10)
plt.show()
Example 2: with 11 Chebyshev Nodes
#Here we select Chebyshev nodes with x=11
x_Cheby = np.linspace(-1,1,11)
for i in range(11):
x_Cheby[i] = np.cos((2*(i+1) - 1)/(2*11)*np.pi)
y_Cheby = 1/(1 + 25*x_Cheby**2)
y_newCheby2 = lagrange(x_Cheby, y_Cheby, x)
#plt under is exact solution for runge
plt.plot(x, runge(x), "k", label="Runge function")
#Yellow graph - Lagrange interpolation
plt.plot(x, y_new2,'y--',label="Lagrange Interpolation x=11")
#Chebyshev
plt.plot(x, y_newCheby2,'r--',label="Chebyshev nodes x=11")
plt.legend(bbox_to_anchor=(1.1,1))
plt.title('Lagrange Interpolation and Chebyshev nodes')
plt.xlabel("x")
plt.ylabel("y")
plt.figtext(0.5, -0.04, "Figure 4", ha="center", fontsize=10)
plt.show()
Example 3: with 16 Chebyshev Nodes
#Here we select Chebyshev nodes with x=16
x_Cheby = np.linspace(-1,1,16)
for i in range(16):
x_Cheby[i] = np.cos((2*(i+1) - 1)/(2*16)*np.pi)
y_Cheby = 1/(1 + 25*x_Cheby**2)
y_newCheby3 = lagrange(x_Cheby, y_Cheby, x)
#plt under is exact solution for rhunge
plt.plot(x, runge(x), "k", label="Runge function")
#Blue graph - Lagrange interpolation
plt.plot(x,y_new3,'b--',label="Lagrange Interpolation x=16")
#cheby plot
plt.plot(x,y_newCheby3,'r--',label="Chebyshev nodes x=16")
plt.legend(bbox_to_anchor=(1.1,1))
plt.title('Lagrange Interpolation and Chebyshev nodes')
plt.xlabel("x")
plt.ylabel("y")
plt.figtext(0.5, -0.04, "Figure 5", ha="center", fontsize=10)
plt.show()
Conclusion
Bibliography
[7] https://www.johndcook.com/blog/2017/11/06/chebyshev-interpolation/