# Computing $\pi$ with Interpolation¶

In [9]:
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt


Here's the half-circle function:

In [14]:
def f(x):
return np.sqrt(1-x**2)


We'll only look at it on $[0,1]$:

In [15]:
x_plot = np.linspace(0, 1, 1000)
plt.plot(x_plot, f(x_plot))

Out[15]:
[<matplotlib.lines.Line2D at 0x7fe57c7b6be0>]
In [84]:
degree = 4

nodes = np.linspace(0, 1, degree+1)
nodes

Out[84]:
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])

Now build the Vandermonde matrix:

In [85]:
V = np.array([
nodes**i
for i in range(degree+1)
]).T


And find the coefficients as coeffs:

In [86]:
coeffs = la.solve(V, f(nodes))


Now evaluate the interpolant at x_plot

In [87]:
interp_plot = 0*x_plot
for i in range(degree+1):
interp_plot += coeffs[i] * x_plot**i

plt.plot(x_plot, interp_plot)
plt.plot(x_plot, f(x_plot))
plt.plot(nodes, f(nodes), "o")

Out[87]:
[<matplotlib.lines.Line2D at 0x7fe5770c2c88>]

Now integrate the interpolant:

In [88]:
# clear
integral = 0
for i in range(degree+1):
integral += coeffs[i] * 1/(i+1) * (1**(i+1) - 0**(i+1))

In [89]:
my_pi = 4*integral
my_pi

Out[89]:
3.0907636490484132
In [ ]:
The text passage describes a Python code implementation for approximating the value of $\pi$ using polynomial interpolation of a half-circle function. It includes code for defining the function, creating an interpolant, and calculating the integral of the interpolant to estimate $\pi$.
no_question

the passage does not contain any explicit mathematical question or request for a solution. it is a procedural description and implementation of a method to approximate $\pi$.
Given a text passage, determine:
    1. If it contains a mathematical question (including word problems or calculations)
    2. If an answer to that mathematical question is provided in the text

First, describe the content structure you found in 1-2 sentences.

Then provide your analysis in the following format:
TYPE: <one of the following values>
- NO_QUESTION
- SINGLE_QUESTION
- SINGLE_QUESTION_WITH_ANSWER
- MULTIPLE_QUESTIONS
- MULTIPLE_QUESTIONS_WITH_ANSWERS
system
# Computing $\pi$ with Interpolation¶

In [9]:
import numpy as np
import numpy.linalg as la
import matplotlib.pyplot as plt


Here's the half-circle function:

In [14]:
def f(x):
return np.sqrt(1-x**2)


We'll only look at it on $[0,1]$:

In [15]:
x_plot = np.linspace(0, 1, 1000)
plt.plot(x_plot, f(x_plot))

Out[15]:
[<matplotlib.lines.Line2D at 0x7fe57c7b6be0>]
In [84]:
degree = 4

nodes = np.linspace(0, 1, degree+1)
nodes

Out[84]:
array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ])

Now build the Vandermonde matrix:

In [85]:
V = np.array([
nodes**i
for i in range(degree+1)
]).T


And find the coefficients as coeffs:

In [86]:
coeffs = la.solve(V, f(nodes))


Now evaluate the interpolant at x_plot

In [87]:
interp_plot = 0*x_plot
for i in range(degree+1):
interp_plot += coeffs[i] * x_plot**i

plt.plot(x_plot, interp_plot)
plt.plot(x_plot, f(x_plot))
plt.plot(nodes, f(nodes), "o")

Out[87]:
[<matplotlib.lines.Line2D at 0x7fe5770c2c88>]

Now integrate the interpolant:

In [88]:
# clear
integral = 0
for i in range(degree+1):
integral += coeffs[i] * 1/(i+1) * (1**(i+1) - 0**(i+1))

In [89]:
my_pi = 4*integral
my_pi

Out[89]:
3.0907636490484132
In [ ]:
user
The text passage describes a Python code implementation for approximating the value of $\pi$ using polynomial interpolation of a half-circle function. It includes code for defining the function, creating an interpolant, and calculating the integral of the interpolant to estimate $\pi$.

TYPE: NO_QUESTION

The passage does not contain any explicit mathematical question or request for a solution. It is a procedural description and implementation of a method to approximate $\pi$.