数值计算-习题2.2

发布时间 2023-09-06 21:25:06作者: 小奔奔
 1 # -*- coding: utf-8 -*-
 2 """
 3 Created on Wed Sep  6 19:35:03 2023
 4 
 5 @author: shixi
 6 """
 7 # 2.2(1)
 8 x0,x1,x2 = 0.46,0.47,0.48
 9 y0,y1,y2 = 0.4846555,0.4937452,0.5027498
10 
11 def p2(x):
12     return y0*(x-x1)*(x-x2)/((x0-x1)*(x0-x2)) \
13          + y1*(x-x0)*(x-x2)/((x1-x0)*(x1-x2)) \
14          + y2*(x-x0)*(x-x1)/((x2-x0)*(x2-x1))
15          
16 print(p2(0.472))
17 
18 # 2.2(2)
19 x0,x1,x2 = 0.4937452,0.5027498,0.5116683
20 y0,y1,y2 = 0.47,0.48,0.49
21 
22 def p2(x):
23     return y0*(x-x1)*(x-x2)/((x0-x1)*(x0-x2)) \
24          + y1*(x-x0)*(x-x2)/((x1-x0)*(x1-x2)) \
25          + y2*(x-x0)*(x-x1)/((x2-x0)*(x2-x1))
26          
27 print(p2(0.5))