fix project 1 q3
This commit is contained in:
BIN
project1/Project1_A3d_least_squares_approximation.png
Normal file
BIN
project1/Project1_A3d_least_squares_approximation.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 89 KiB |
@@ -135,6 +135,32 @@ def f2(n:int, t: float) -> float:
|
|||||||
"""
|
"""
|
||||||
return sum([t**i for i in range(n + 1)])
|
return sum([t**i for i in range(n + 1)])
|
||||||
|
|
||||||
|
def question_3d():
|
||||||
|
Ns = [25, 50, 100]
|
||||||
|
ns = Ns
|
||||||
|
t = np.linspace(-5, 5, 1000)
|
||||||
|
f_t = f1(t)
|
||||||
|
plt.figure(figsize=(15, 10))
|
||||||
|
for i in range(len(Ns)):
|
||||||
|
N = Ns[i]
|
||||||
|
n = ns[i]
|
||||||
|
A = np.zeros((N + 1, n + 1), dtype=float)
|
||||||
|
A, b = build_A(N, n, f1)
|
||||||
|
x = householder_lstsq(A, b)
|
||||||
|
p_t = sum([x[j] * t**j for j in range(n + 1)])
|
||||||
|
plt.subplot(1, 3, i + 1)
|
||||||
|
plt.plot(t, f_t, label='f(t)', color='blue')
|
||||||
|
plt.plot(t, p_t, label='p(t)', color='red', linestyle='--')
|
||||||
|
plt.title(f'N={N}, n={n}')
|
||||||
|
plt.xlabel('t')
|
||||||
|
plt.ylabel('y')
|
||||||
|
plt.legend()
|
||||||
|
plt.grid(True, alpha=0.3)
|
||||||
|
plt.suptitle('Least Squares Polynomial Approximation using Householder QR')
|
||||||
|
plt.tight_layout(rect=[0, 0.03, 1, 0.95])
|
||||||
|
plt.savefig('Project1_A3d_least_squares_approximation.png')
|
||||||
|
#plt.show()
|
||||||
|
|
||||||
def question_3e():
|
def question_3e():
|
||||||
# n = 4, 6, 8, 10, ..., 20
|
# n = 4, 6, 8, 10, ..., 20
|
||||||
ns = list(range(4, 21, 2))
|
ns = list(range(4, 21, 2))
|
||||||
@@ -161,6 +187,7 @@ def question_3e():
|
|||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
question_3c()
|
question_3c()
|
||||||
|
question_3d()
|
||||||
question_3e()
|
question_3e()
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user