习题2.13代码
import numpy as np
import pandas as pd
import sympy as sp
sp.init_printing(use_unicode=True)
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Times New Roman + SimSun + WFM Sans SC']
plt.rcParams['mathtext.fontset']='cm'
Times New Roman + SimSun + WFM Sans SC
simsum宋体, times new roman -*, simhei黑体, kaiti楷体,
dengxian等线, fangsong仿宋, Microsoft Yahei微软雅黑
plt.rcParams['axes.unicode_minus']=False
plt.rcParams['figure.dpi'] = 200
plt.rcParams['figure.figsize'] = [4, 3]
plt.rcParams['font.size'] = 12
plt.rcParams['xtick.direction'] = 'in'
plt.rcParams['ytick.direction'] = 'in'
f = lambda x: (np.abs(x+1) - np.abs(x-1))/2 + np.sin(x)
g = lambda x: (np.abs(x+3) - np.abs(x-3))/2 + np.cos(x)
from scipy.optimize import least_squares
eqs = lambda z: [
3f(z[2])+4g(z[3])-1-2z[0],
2f(z[2])+6g(z[3])-2-3z[1],
f(z[0])+3g(z[1])-3-z[2],
4f(z[0])+6g(z[1])-1-5z[3],
f(z[3])+g(z[1])-2-z[0]-z[2],
2f(z[0])-10g(z[2])-5-z[1]-3*z[3]
]
s = least_squares(eqs, np.random.randn(4))
print(s)