#演習1の解答
import numpy as np
x1 = np.linspace(1,2,21)
y1 = np.sin(x1**3)*np.cos(x1)+1
import matplotlib.pyplot as plt
plt.plot(x1,y1,"g-+")
plt.grid()
plt.show()
$[0,1]$の100分割に利用して、以下の関数の積分の近似値を求めてください。ただし、forループを使用しなく、Numpyの関数sin,sumなどを使用してください。
$$ \int_{0}^1 \sin(x^3) dx $$
#演習2の解答
import numpy as np
n=101
h=1/(n-1)
x = np.linspace(0,1,n)
y = np.sin(x**3)
print(x)
print("積分の近似値=", np.sum(y)*h)
以下の行列Aに関わる連立一次方程式$Ax=b$の解を計算しなさい。
ヒント:
$$ Ax=b, \quad A = \left( \begin{array}{ccc} 2 & 1 & 0 \\ 1 & 2 & 1 \\ 0 & 1 & 2 \\ \end{array} \right) ,\quad b = \left( \begin{array}{c} 1\\ 2\\ 3\\ \end{array} \right) $$
#演習3の解答
import numpy as np
A = [[2,1,0],[1,2,1],[0,1,2]]
b = [1,2,3]
x = np.linalg.inv(A)@b
#np.set_printoptions(suppress=True) #小数表示
print("x =",x)
print("x =",np.linalg.solve(A,b))