實現拉格朗日計算,可以直接調用scipy.interpolate
由於要求要有界面,使用tkinter,整了個醜醜的界面
from tkinter import *
from scipy.interpolate import lagrange
def run1():
a = list(inp1.get().split(","))
b = list(inp2.get().split(","))
c = float(inp3.get())
x = [float(i) for i in a]
y = [float(i) for i in b]
lagrange(x, y)
p = lagrange(x, y)(c)
txt.insert(END, p) # 追加顯示運算結果
def run2():
txt.delete(0.0, END)
inp1.delete(0, END) # 清空輸入
inp2.delete(0, END) # 清空輸入
inp3.delete(0, END) # 清空輸入
root = Tk()
root.geometry('500x400')
root.title('lagrange')
lb1 = Label(root, text='請輸入數值')
lb1.place(relx=0.1, rely=0.1, relwidth=0.8, relheight=0.1)
inp1 = Entry(root)
inp1.place(relx=0.01, rely=0.2, relwidth=0.2, relheight=0.1)
inp2 = Entry(root)
inp2.place(relx=0.4, rely=0.2, relwidth=0.2, relheight=0.1)
inp3 = Entry(root)
inp3.place(relx=0.7, rely=0.2, relwidth=0.2, relheight=0.1)
# 方法-直接調用 run1()
btn1 = Button(root, text='計算', command=run1)
btn1.place(relx=0.1, rely=0.4, relwidth=0.3, relheight=0.1)
btn1 = Button(root, text='清除', command=run2)
btn1.place(relx=0.6, rely=0.4, relwidth=0.3, relheight=0.1)
# 在窗體垂直自上而下位置60%處起,佈局相對窗體高度40%高的文本框
txt = Text(root)
txt.place(rely=0.6, relheight=0.4)
if __name__ == "__main__":
root.mainloop()
#第一個輸入框是x值,第二個輸入框是y值,第三個輸入框是待求x值
#x,y輸入值用中文","分割