xjx
xjx

Reputation: 11

Interpolation of sin(x) using Python

I am working on a homework problem for which I am supposed to make a function that interpolates sin(x) for n+1 interpolation points and compares the interpolation to the actual values of sin at those points. The problem statement asks for a function Lagrangian(x,points) that accomplishes this, although my current attempt at executing it does not use 'x' and 'points' in the loops, so I think I will have to try again (especially since my code doesn't work as is!) However, why I can't I access the items in the x_n array with an index, like x_n[k]? Additionally, is there a way to only access the 'x' values in the points array and loop over those for L_x? Finally, I think my 'error' definition is wrong, since it should also be an array of values. Is it necessary to make another for loop to compare each value in the 'error' array to 'max_error'? This is my code right now (we are executing in a GUI our professor made, so I think some of the commands are unique to that such as messages.write()):

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[i] - x_n[k])
            return Lagrange = y_n[i] * 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)

Edited:

Yes, the different things ThiefMaster mentioned are part of my (non CS) professor's environment; and yes, voithos, I'm using numpy and at this point have definitely had more practice with Matlab than Python (I guess that's obvious!). n and m are values entered by the user in the GUI; n+1 is the number of interpolation points and m is the number of points you plot against later.

Pseudocode: Given n and m

Generate x_n a list of n evenly spaced points from 0 to 2*pi Generate y_n a corresponding list of points for sin(x_n)

Define points, a 2D array consisting of these ordered pairs

Define Lagrange, a function of x and points

for each value in the range n+1 (this is where I would like to use points but don't know how to access those values appropriately)

evaluate y_n * (x - x_n[later index] / x_n[earlier index] - x_n[later index])

Calculate max error Calculate error interpolation Lagrange - sin(x)

plot sin(x); plot Lagrange; plot error

Does that make sense?

Upvotes: 1

Views: 2850

Answers (1)

mathematical.coffee
mathematical.coffee

Reputation: 56915

Some suggestions:

  • You can access items in x_n via x_n[k] (to answer your question).
  • Your loops for i in n+1: and for k in n+1: only execute once each, one with i=n+1 and one with k=n+1. You need to use for i in range(n+1) (or xrange) to get the whole list of values [0,1,2,...,n].
  • in error = np.sin(x) - Lagrange: You haven't defined x anywhere, so this will probably result in an error. Did you mean for this to be within the Lagrange function? Also, you're subtracting a function (Lagrange) from a number np.sin(x), which isn't going to end well.
  • When you use the return statement in your def Lagrange you are exiting your function. So your loop will never loop more than once because you're returning out of the function. I think you might actually want to store those values instead of returning them.

Can you write some pseudocode to show what you'd like to do? e.g.:

Given a set of points `xs` and "interpolated" points `ys`:
For each point (x,y) in (xs,ys):
    Calculate `sin(x)`
    Calculate `sin(x)-y` being the difference between the function and y
.... etc etc

This will make the actual code easier for you to write, and easier for us to help you with (especially if you intellectually understand what you're trying to do, and the only problem is with converting that into python).

So : try fix up some of these points in your code, and try write some pseudocode to say what you want to do, and we'll keep helping you :)

Upvotes: 1

Related Questions