Lagrangian interpolation of sin(x) in Python

In summary, the conversation discusses the use of Lagrange's interpolation formula to interpolate sin x over a specific range using evenly spaced interpolation points. The formula is implemented in a function called Lagrange(x, points) that evaluates pL at a given point x using the interpolation points. The program also includes a plot of the original function sin x and the interpolation function pL(x), as well as the error between the two. The maximum magnitude of the error is printed and it is noted that the maximum error may increase with higher n. The question of how many points are needed to make the error less than 0.01 is also discussed. The code provided for this problem may need to be modified, specifically in the Lagrange function, in
  • #1
xjx
1
0

Homework Statement



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?

Homework Equations





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?
 
Technology news on Phys.org
  • #2
Sorry if this is too much, I'm really new to this and I'm not even sure if I'm asking the right questions.
 

Related to Lagrangian interpolation of sin(x) in Python

1. What is Lagrangian interpolation?

Lagrangian interpolation is a mathematical method for approximating a function using a polynomial. It involves finding a polynomial that passes through a given set of data points and can be used to estimate the value of the function at any point within the range of the data.

2. How does Lagrangian interpolation work?

The Lagrangian interpolation formula involves constructing a polynomial of degree n, where n is the number of data points, and solving for the coefficients using a system of equations. This polynomial is then used to estimate the value of the function at any point by plugging in the desired value for x.

3. How do I implement Lagrangian interpolation of sin(x) in Python?

To implement Lagrangian interpolation of sin(x) in Python, you will need to define a function that takes in a set of x and y values and uses the Lagrangian interpolation formula to calculate the coefficients of the polynomial. You can then use this function to estimate the value of sin(x) at any point within the range of the data.

4. What are the advantages of using Lagrangian interpolation?

One advantage of Lagrangian interpolation is that it provides a simple and efficient way to approximate a function using a polynomial. It also allows for easy interpolation of data points, making it useful for applications such as data analysis and curve fitting.

5. Are there any limitations to using Lagrangian interpolation?

One limitation of Lagrangian interpolation is that it can result in large errors when used with a high degree polynomial or when the data points are unevenly spaced. It also does not work well with data that contains significant outliers. In these cases, other interpolation methods may be more suitable.

Similar threads

  • Programming and Computer Science
Replies
1
Views
10K
  • Programming and Computer Science
Replies
2
Views
1K
Replies
6
Views
1K
  • Programming and Computer Science
Replies
4
Views
4K
Replies
5
Views
550
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Programming and Computer Science
Replies
21
Views
4K
  • Programming and Computer Science
Replies
4
Views
4K
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
946
Back
Top