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

Click For Summary
The discussion revolves around creating a polynomial interpolation program that can handle multiple data points, moving beyond linear interpolation. The user has successfully implemented linear interpolation but seeks guidance on extending this to n points, using a polynomial approach. They have formulated a general equation for polynomial interpolation but require assistance with user input for the number of coordinates and the corresponding x and y values. The conversation highlights the need for clarity regarding the programming language, which is Python, and emphasizes that the polynomial should have a variable number of terms rather than an infinite series. Additionally, it notes that Python allows for dynamic array creation without predefined sizes, which can facilitate user input handling.
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.
 
Technology 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 jim mcnamara
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 15 ·
Replies
15
Views
2K
Replies
55
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
6
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K