# def make_func(letters,result): # global func_x # # print(letters,result) # draw_axis(-100, 100, 20) # letter = str(letters).split(',') # n = len(letter)-1 # decimals = [round(random.uniform(-10, 10), 1) for _ in range(n)] # target_result = functional_formula(letter[1:],result,decimals) # # output.configure(state="normal") # # output.delete("1.0", tk.END) # 删除当前输出框中的所有文本 # # output.insert(tk.END, target_result) # # output.configure(state="disabled") # code_str = fitting('x', target_result) # # print(code_str) # target_func = create_func(code_str) # 获取当前函数func # # # print(target_result) # # # 生成带噪声的待拟合数据 # # np.random.seed(0) # # x_data = np.linspace(-100, 100, 50) # # y_data = target_func(x_data) + np.random.normal(0, 0.5, 50) # # # print(y_data) # # code_str = fitting(letters, result) # # print(code_str) # # func_x = create_func(code_str) # 获取当前函数func # # # # # 初始参数的估计值 # # initial_coeffs = [1.0 for _ in range(n)] # # # 使用最小二乘法拟合函数 # # result = leastsq(residuals, initial_coeffs, args=(y_data, x_data)) # # best_coeffs = result[0] # # # 打印拟合得到的系数 # # print("拟合得到的系数:", best_coeffs) # # # 绘制拟合结果 # x_fit = np.linspace(-100, 100, 50) # y_fit = target_func(x_fit) # # # plt.plot(x_data, y_data, 'bo', label='原始数据') # plt.plot(x_fit, y_fit, 'r-', label='拟合曲线') # plt.legend() # plt.xlabel('x') # plt.ylabel('y') # # plt.show() # plt.savefig('new_line.png') # # 清除当前图形状态 # plt.clf() # show_image() # # # 定义误差函数,即最小二乘法的目标函数 # def residuals(coeffs, y, x): # return y - func_x(x, *coeffs)