# Homework Help: Lagrangian interpolation of sin(x) in Python

1. Jan 19, 2012

### xjx

1. The problem statement, all variables and given/known data

The polynomial pL(x) is known as Lagranges interpolation formula, and the points (x0; y0),
. . . , (xn; yn) are called interpolation points. You will use Lagrange's interpolation formula to
interpolate sin x over the range [0; 2pi]. Begin with n + 1 interpolation points evenly spaced
in [0; 2pi]. Make a function Lagrange(x, points) that evaluates pL at the point x, given
n+1 interpolation points as a two-dimensional array points, such that points[i,0] is the x
coordinate of point number i and points[i,1] is the corresponding y coordinate. To verify
the program, we observe that Lk(xk) = 1 and that Lk(xi) = 0 for i 6= k, implying that
pL(xk) = yk. That is, the interpolation function should exactly match the original function
at the interpolation points. Plot the original function sin x at the n+1 interpolation points,
and connect those points with lines. Plot the interpolation function pL(x), and the error
pL(x) - sin(x) with points on the same plot with di fferent colors and/or symbols for m
evenly spaced points along x with x 2 [0; 2pi] and m >> n. Print the maximum magnitude
of the error to messages. Describe how many points (n + 1) are needed to make the error
less than 0.01 over the entire range. Does the maximum error keep decreasing for increasing
n? At what n does the error seem to be minimum, and what is the minimum error? Why
might the maximum error increase with higher n?

2. Relevant equations

3. The attempt at a solution

This is my current code in Python:

def problem_6_run(problem_6_n, problem_6_m, plot, messages, **kwargs):
n = problem_6_n.value
m = problem_6_m.value
messages.write('\n=== PROBLEM 6 ==========================\n')
x_n = np.linspace(0,2*math.pi,n+1)
y_n = np.sin(x_n)
points = np.column_stack((x_n,y_n))
i = 0
k = 1
L_x = 1.0

def Lagrange(x, points):
for i in n+1:
for k in n+1:
return L_x = (x- x_n[k] / x_n - x_n[k])
return Lagrange = y_n * L_x

error = np.sin(x) - Lagrange
max_error = 0
if error > max_error
max_error = error
print.messages('Maximum error = &g' % max_error)
plot.draw_lines(n+1,np.sin(x))
plot.draw_points(m,Lagrange)
plots.draw_points(m,error)

We execute the code in a GUI he designed, so things like "print.messages()" might be unique to that but they are correct. This is a computational physics class that assumes we have no prior programming experience. Since he was very specific in his instructions, I know I need to change the Lagrangian definition so that it is looping through the points array, but I'm not really sure how I can isolate only the x values in that array for the loop. The way I did it above doesn't seem to work, at least in part because I can't access the elements in the array by their index (which is what I wanted to do with things like x_n[k]).

I guess my main questions right now are: am I doing that indexing wrong or why isn't that working? Also, is there a way I can access only the vertical values in the array? I think my 'error' definition is also wrong, since that is probably also an array of points; do I need to make another loop to compare each element with the value of max_error?