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

Join the discussion
Registration is free. Start your own thread to ask a follow-up.
1 reply · 2K views
Freya
Messages
15
Reaction score
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

print(yi)
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.
 
Physics news on Phys.org
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 )
 
  • Like
Likes   Reactions: jim mcnamara