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 :
5
2
"""
6
3
Calculate the Lagrange interpolation of a function based on provided data points.
7
4
8
5
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).
11
8
value (float): The value at which to interpolate the function.
12
9
13
10
Returns:
14
11
float: The interpolated value of the function at the specified 'value'.
15
12
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)
21
17
14.666666666666666
22
-
23
18
"""
24
19
ans = 0
25
20
n = len (x )
26
21
27
- for i in range (n ):
22
+ for i in range (n ):
28
23
term = y [i ]
29
24
for j in range (n ):
30
25
if i != j :
@@ -36,17 +31,22 @@ def lagrange_interpolation(x: List[float], y: List[float], value: float) -> floa
36
31
def main ():
37
32
"""
38
33
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.
40
36
"""
41
37
n = int (input ("Enter the number of values: " ))
42
38
x = []
43
39
y = []
44
40
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 )
47
45
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 )
50
50
51
51
value_to_interpolate = float (input ("Enter the value to interpolate:" ))
52
52
0 commit comments