Skip to content

Commit f99335f

Browse files
authored
Merge pull request #2 from hacquees/hacquees-patch-1
Update lagrange_interpolation.py
2 parents db33c28 + 58a987a commit f99335f

File tree

1 file changed

+18
-18
lines changed

1 file changed

+18
-18
lines changed

maths/numerical_analysis/lagrange_interpolation.py

+18-18
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,25 @@
1-
import math
2-
from typing import List
3-
4-
def lagrange_interpolation(x: List[float], y: List[float], value: float) -> float:
1+
def lagrange_interpolation(x: list[float], y: list[float], value: float) -> float:
52
"""
63
Calculate the Lagrange interpolation of a function based on provided data points.
74
85
Parameters:
9-
x (List[float]): List of x-values (independent variable).
10-
y (List[float]): List of corresponding y-values (dependent variable).
6+
x (list[float]): List of x-values (independent variable).
7+
y (list[float]): List of corresponding y-values (dependent variable).
118
value (float): The value at which to interpolate the function.
129
1310
Returns:
1411
float: The interpolated value of the function at the specified 'value'.
1512
16-
for eg.:
17-
18-
>>> x = [5, 6, 9, 11]
19-
>>> y = [12, 13, 14, 16]
20-
>>> lagrange_interpolation(x, y, 10)
13+
Examples:
14+
>>> x = [5.0, 6.0, 9.0, 11.0]
15+
>>> y = [12.0, 13.0, 14.0, 16.0]
16+
>>> lagrange_interpolation(x, y, 10.0)
2117
14.666666666666666
22-
2318
"""
2419
ans = 0
2520
n = len(x)
2621

27-
for i in range(n):
22+
for i in range(n):
2823
term = y[i]
2924
for j in range(n):
3025
if i != j:
@@ -36,17 +31,22 @@ def lagrange_interpolation(x: List[float], y: List[float], value: float) -> floa
3631
def main():
3732
"""
3833
Main function for performing Lagrange interpolation.
39-
Takes user input for data points and the value to interpolate, then displays the interpolated result.
34+
Takes user input for data points and the value to interpolate,
35+
then displays the interpolated result.
4036
"""
4137
n = int(input("Enter the number of values: "))
4238
x = []
4339
y = []
4440

45-
print("Enter the values of x in a list: ")
46-
x = list(map(float, input().split()))
41+
print("Enter the values of x in a list:")
42+
for i in range(n):
43+
value = float(input())
44+
x.append(value)
4745

48-
print("Enter the values of corresponding y: ")
49-
y = list(map(float, input().split()))
46+
print("Enter the values of corresponding y:")
47+
for j in range(n):
48+
value = float(input())
49+
y.append(value)
5050

5151
value_to_interpolate = float(input("Enter the value to interpolate:"))
5252

0 commit comments

Comments
 (0)