Runge's Phenomenon
Author's Name: Davi de Souza Barros
Date: 14/11/2023
Assignment
Explain in your words what Runge's Phenomenon is, its implications in polynomial interpolation and, in general, in data over-fitting.
Demonstrate Runge's Phenomenon using Runge's function :
import numpy as np
import matplotlib.pyplot as plt
def demonstrate_runges_phenomenon(n):
#Create the equidistant data points
x=np.linspace(-1,1,n)
#Create the interpolating polynomial
y=[]
for i in x:
y.append(1/(1+(5*i)**2))
coefficients=np.polyfit(x,y,len(x))
#Create the inputs for the graph
x_high_res=np.linspace(-1,1,1000)
y_high_res=1/(1+(5*x_high_res)**2)
y_interpo=np.polyval(coefficients,x_high_res)
#Create the graph
plt.plot(x_high_res,y_high_res,label='Real Function')
plt.plot(x,y,'o',label='Data Points')
plt.plot(x_high_res,y_interpo,label='Interpolating Polynomial')
plt.legend()
plt.show()
demonstrate_runges_phenomenon(11)
Run to view results
What is a possible remedy to avoid spurious oscillations?
Demonstrate the effectiveness of this remedy through some example(s).
# Start writing code here...
import numpy as np
import matplotlib.pyplot as plt
def demonstrate_effectiveness_of_chebyshev_nodes_ex1(n):
#Crate the equidistant data points
x=np.linspace(-1,1,n)
#Crate the interpolating polynomial
y=[]
for i in x:
y.append(1/(1+(5*i)**2))
coefficients=np.polyfit(x,y,len(x))
#Create the inputs for the graph
x_high_res=np.linspace(-1,1,1000)
y_high_res=1/(1+(5*x_high_res)**2)
y_interpo=np.polyval(coefficients,x_high_res)
#Create the Chebyshev nodes
x_cheb=np.cos((2*np.arange(1,n+1)-1)*np.pi/(2*n))
#Create the interpolating polynomial for the Chebyshev nodes
y_cheb=[]
for i in x_cheb:
y_cheb.append(1/(1+(5*i)**2))
coefficients=np.polyfit(x_cheb,y_cheb,len(x))
#Create the inputs for the graph of the polynomial using the Chebyshev nodes
y_interpo_cheb=np.polyval(coefficients,x_high_res)
#Create the graph
plt.plot(x_high_res,y_high_res,label='Real Function')
plt.plot(x_cheb,y_cheb,'o',label='Chebchev Nodes')
plt.plot(x_high_res,y_interpo,label='Interpolating Polynomial')
plt.plot(x_high_res,y_interpo_cheb,label='Interpolating Polynomial Using the Chebchev Nodes')
plt.legend()
plt.show()
demonstrate_effectiveness_of_chebyshev_nodes_ex1(11)
Run to view results
import numpy as np
import matplotlib.pyplot as plt
def demonstrate_effectiveness_of_chebyshev_nodes_ex2(n):
#Crate the equidistant data points
x=np.linspace(-1,1,n)
#Crate the interpolating polynomial
y=[]
for i in x:
y.append(1/(1+(10*i)**-2))
coefficients=np.polyfit(x,y,len(x))
#Create the inputs for the graph
x_high_res=np.linspace(-1,1,1000)
y_high_res=1/(1+(10*x_high_res)**-2)
y_interpo=np.polyval(coefficients,x_high_res)
#Create the Chebyshev nodes
x_cheb=np.cos((2*np.arange(1,n+1)-1)*np.pi/(2*n))
#Create the interpolating polynomial for the Chebyshev nodes
y_cheb=[]
for i in x_cheb:
y_cheb.append(1/(1+(10*i)**-2))
coefficients=np.polyfit(x_cheb,y_cheb,len(x))
#Create the inputs for the graph of the polynomial using the Chebyshev nodes
y_interpo_cheb=np.polyval(coefficients,x_high_res)
#Create the graph
plt.plot(x_high_res,y_high_res,label='Real Function')
plt.plot(x_cheb,y_cheb,'o',label='Chebchev Nodes')
plt.plot(x_high_res,y_interpo,label='Interpolating Polynomial')
plt.plot(x_high_res,y_interpo_cheb,label='Interpolating Polynomial Using the Chebchev Nodes')
plt.legend()
plt.show()
demonstrate_effectiveness_of_chebyshev_nodes_ex2(11)
Run to view results
import numpy as np
import matplotlib.pyplot as plt
def demonstrate_effectiveness_of_chebyshev_nodes_ex3(n):
#Crate the equidistant data points
x=np.linspace(-1,1,n)
#Crate the interpolating polynomial
y=[]
for i in x:
y.append(np.sin(10*i))
coefficients=np.polyfit(x,y,len(x))
#Create the inputs for the graph
x_high_res=np.linspace(-1,1,1000)
y_high_res=np.sin(10*x_high_res)
y_interpo=np.polyval(coefficients,x_high_res)
#Create the Chebyshev nodes
x_cheb=np.cos((2*np.arange(1,n+1)-1)*np.pi/(2*n))
#Create the interpolating polynomial for the Chebyshev nodes
y_cheb=[]
for i in x_cheb:
y_cheb.append(np.sin(10*i))
coefficients=np.polyfit(x_cheb,y_cheb,len(x))
#Create the inputs for the graph of the polynomial using the Chebyshev nodes
y_interpo_cheb=np.polyval(coefficients,x_high_res)
plt.plot(x_high_res,y_high_res,label='Real Function')
plt.plot(x_cheb,y_cheb,'o',label='Chebchev Nodes')
plt.plot(x_high_res,y_interpo,label='Interpolating Polynomial')
plt.plot(x_high_res,y_interpo_cheb,label='Interpolating Polynomial Using the Chebchev Nodes')
plt.legend()
plt.show()
demonstrate_effectiveness_of_chebyshev_nodes_ex3(11)
Run to view results