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

Click For Summary
SUMMARY

The discussion centers on creating a polynomial interpolation program in Python that can handle an arbitrary number of data points. The user has successfully implemented linear interpolation but seeks guidance on extending this to n points without using external libraries like SciPy. Key insights include the importance of dynamically allocating arrays in Python and correcting the misconception about infinite polynomial terms, which should instead be defined as having a variable number of terms.

PREREQUISITES
  • Understanding of polynomial interpolation concepts
  • Familiarity with Python programming language
  • Knowledge of dynamic array handling in Python
  • Basic mathematical principles of summation and series
NEXT STEPS
  • Research dynamic array implementation in Python
  • Learn about polynomial interpolation techniques, specifically Lagrange interpolation
  • Explore Python's built-in data structures for handling variable-sized collections
  • Study the mathematical foundations of polynomial equations and their applications
USEFUL FOR

Python developers, students learning numerical methods, and anyone interested in implementing polynomial interpolation algorithms from scratch.

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   Reactions: jim mcnamara

Similar threads

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