Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python How can I input a polynomial equation of infinite terms in P

  1. Oct 18, 2015 #1
    I have been given a task to create an interpolating/extrapolating programme. I have completed the programme for linear interpolation (2 points) but now must make it usable for 3 or more points, ie a polynomial of n points. I think I have the equation in general for a polynomial as it is an infinite series up to nth degree and have tweaked the general formula of summation. So far I have;

    n= int(input("How many data points do you have for the polynomial="))for d in range (1, n+1):
    x = float(input ("Enter 1st x value x"))
    y = float(input ("Enter 1st y value y"))

    yi =0e0for i in range (1, n+1):
    p=1e0for j in range (1, n+1):if(j != i):
    p *=(xi-x[j])/(x-x[j])
    yi += p*y

    but I need a way of having the user input the number of coordinates they have (n) and then using this information, the points they input take up the role of x[1] y[1]... up to x[n+1] and y[n+1] but I don't have a clue how to do this. I should probably mention we are not aloud to use .scipy or such add ons, we must do it all from scratch.
  2. jcsd
  3. Oct 18, 2015 #2


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    P means Python? The answer of how to allocate space for arrays of size n is specific to the language, so you should be more clear about the language (hopefully in the title). Did 'Python' get truncated in the title? Also, you don't want polynomials of 'infinite' terms, which is impossible. You want polynomials with variable number of terms.

    Arrays of variable size are easy in Python. You can declare it without giving a size and keep appending to it. (see http://stackoverflow.com/questions/2910864/in-python-how-can-i-declare-a-dynamic-array )
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook