Ask Your Question
1

Polynomial interpolation given a mathematic function f(x), an interval [a,b] and a natural number n

asked 2018-10-12 13:39:25 +0200

Hell_Boy gravatar image

Hi, i'm a computer science bachelor 's student and i'm having some trouble with this exercice, i'd like to get some help since it's my first course using sagemath, thanks, the exercice:

Write a function that takes as input a mathematical function f, an interval [a, b] and a natural n and return the interpolative polynomial of the function in the n + 1 points resulting from dividing the interval into subinterval n of equal length. That is to say, a polynomial p (x) of degree ≤ n such p (xi) = f (xi), where xi = a + i * ((b-a)/n) for all i = 0, ... n. I'm stuck in the way of transforming a list into a matrix. This is what i've tried so far:

def list1(a,b,n):
    r=(b-a)/n
    return [a+i*r for i in range(n+1)]
def list2(a,b,n):
    r=(b-a)/n
    l=[]
    for i in range(n+1):
        l.append(a+i*r)
    return l

def functionlist(f,llista):
    return[f(i) for i in llista]

px=matrix(7, 1,list2(1, 5, 6))
show(px)
py=matrix(7, 1, functionlist(sin, list1(1, 5, 6)))
show(py)

points= zip(px,py); points

A=matrix(RDF,[[1,px[0],px[0]^2,px[0]^3],[1,px[1],px[1]^2,px[1]^3],[1,px[2],px[2]^2,px[2]^3],[1,px[3],px[3]^2,px[3]^3]]); A

And gives me this error:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-112-a1670df9c827> in <module>()
     19 points= zip(px,py); points
     20 
---> 21 A=matrix(RDF,[[Integer(1),px[Integer(0)],px[Integer(0)]**Integer(2),px[Integer(0)]**Integer(3)],[Integer(1),px[Integer(1)],px[Integer(1)]**Integer(2),px[Integer(1)]**Integer(3)],[Integer(1),px[Integer(2)],px[Integer(2)]**Integer(2),px[Integer(2)]**Integer(3)],[Integer(1),px[Integer(3)],px[Integer(3)]**Integer(2),px[Integer(3)]**Integer(3)]]); A
     22 
     23 

/opt/sagemath-8.3/local/lib/python2.7/site-packages/sage/modules/free_module_element.pyx in sage.modules.free_module_element.FreeModuleElement.__pow__ (build/cythonized/sage/modules/free_module_element.c:15842)()
   2019             NotImplementedError
   2020         """
-> 2021         raise NotImplementedError
   2022 
   2023     def _repr_(self):

NotImplementedError:

Pls help, thanks.

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2018-10-12 15:14:09 +0200

FrédéricC gravatar image

your px and py should be vectors, not matrices. Use vector(list2(2,5,6))

edit flag offensive delete link more

Comments

Thanks you saved my life.

Hell_Boy gravatar imageHell_Boy ( 2018-10-12 18:09:57 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2018-10-12 13:36:28 +0200

Seen: 448 times

Last updated: Oct 12 '18