Linear Computacional Final
Linear Computacional Final
Linear Computacional Final
def matrix_rank(matrix):
return np.linalg.matrix_rank(matrix)
def condition_number(matrix):
return np.linalg.cond(matrix)
# Valores de k fornecidos
k_values = [1.05, 1.1, 1.2, 1.3, 1.5, 1.8, 2.0, 2.3, 2.6, 3, 3.5, 4, 5, 6.5, 8, 10, 20, 50, 100, 200]
# Conjunto de pontos P1
t1_values = np.linspace(0, 2 * np.pi, 60)
P1 = np.array([(np.cos(t), np.sin(t)) for t in t1_values])
# Conjunto de pontos P2
t2_values = np.linspace(0, 2 * np.pi, 30)
for k in k_values:
U_matrix = np.zeros((60, 30))
for i in range(60):
for j in range(30):
P2_point = k * np.array([np.cos(t2_values[j]), np.sin(t2_values[j])])
U_matrix[i, j] = matrix_U(P1[i], P2_point)
rank = matrix_rank(U_matrix)
matrix_rank_values.append(rank)
num_cond = condition_number(U_matrix)
num_conditioning.append(num_cond)
# Plotagem do gráfico
plt.figure(figsize=(10, 6))
plt.plot(k_values, matrix_rank_values, marker='o', label='Posto da Matriz U')
plt.plot(k_values, num_conditioning, marker='o', label='Número de Condicionamento')
plt.xlabel('Valores de k')
plt.ylabel('Posto / Número de Condicionamento')
plt.title('Posto e Número de Condicionamento da Matriz U para diferentes valores de k')
plt.legend()
plt.grid(True)
plt.show()
c) Monte um vetor com 60 posições da forma: b = b_i = sen(3t_1)
In [2]: import numpy as np
# Sequência de ângulos t1
t1_values = np.linspace(0, 2 * np.pi, 60)
# Vetor b
b = np.sin(3 * t1_values)
print(b)
[ 0.00000000e+00 3.14076712e-01 5.96367359e-01 8.18302776e-01
9.57422038e-01 9.99645611e-01 9.40700267e-01 7.86551556e-01
5.52800065e-01 2.63102564e-01 -5.32221748e-02 -3.64160575e-01
-6.38244184e-01 -8.47734428e-01 -9.71429893e-01 -9.96812007e-01
-9.21311978e-01 -7.52570770e-01 -5.07665800e-01 -2.11382624e-01
1.06293486e-01 4.13212186e-01 6.78311836e-01 8.74763085e-01
9.82684125e-01 9.91152831e-01 8.99312130e-01 7.16456740e-01
4.61092501e-01 1.59063496e-01 -1.59063496e-01 -4.61092501e-01
-7.16456740e-01 -8.99312130e-01 -9.91152831e-01 -9.82684125e-01
-8.74763085e-01 -6.78311836e-01 -4.13212186e-01 -1.06293486e-01
2.11382624e-01 5.07665800e-01 7.52570770e-01 9.21311978e-01
9.96812007e-01 9.71429893e-01 8.47734428e-01 6.38244184e-01
3.64160575e-01 5.32221748e-02 -2.63102564e-01 -5.52800065e-01
-7.86551556e-01 -9.40700267e-01 -9.99645611e-01 -9.57422038e-01
-8.18302776e-01 -5.96367359e-01 -3.14076712e-01 -7.34788079e-16]
d) Resolva o sistema linear, 60 x 30. Uv = b com a decomposição em valores singulares
In [3]: import numpy as np
# Conjunto de pontos P1
t1_values = np.linspace(0, 2 * np.pi, 60)
P1 = np.array([(np.cos(t), np.sin(t)) for t in t1_values])
# Valores de theta_i para cada ponto P1
theta_values = np.arctan2(P1[:, 1], P1[:, 0])
# Vetor b
b = np.sin(3 * t1_values)
for k in k_values:
# Conjunto de pontos P2 para o valor atual de k
t2_values = np.linspace(0, 2 * np.pi, 30)
P2 = np.array([k * np.array([np.cos(t), np.sin(t)]) for t in t2_values])
for i in range(60):
for j in range(30):
U_matrix[i, j] = matrix_U(P1[i], P2[j])
Resultados para k = 3:
Vetor v: [ 8.38190317e-09 2.12411145e+01 3.38198080e+01 3.26063147e+01
1.80955141e+01 -3.79488815e+00 -2.41376823e+01 -3.46367949e+01
-3.10105422e+01 -1.47377603e+01 7.54528469e+00 2.67512579e+01
3.50476971e+01 2.90511994e+01 1.12072197e+01 -1.12072197e+01
-2.90511994e+01 -3.50476971e+01 -2.67512579e+01 -7.54528468e+00
1.47377602e+01 3.10105422e+01 3.46367949e+01 2.41376824e+01
3.79488813e+00 -1.80955141e+01 -3.26063147e+01 -3.38198080e+01
-2.12411145e+01 7.68341124e-09]
Resultados para k = 4:
Vetor v: [-2.92062759e-06 5.03493142e+01 8.01654653e+01 7.72890473e+01
4.28930658e+01 -8.99528620e+00 -5.72152508e+01 -8.21020290e+01
-7.35064736e+01 -3.49339471e+01 1.78851161e+01 6.34103923e+01
8.30760196e+01 6.88621055e+01 2.65652585e+01 -2.65652589e+01
-6.88621044e+01 -8.30760215e+01 -6.34103896e+01 -1.78851196e+01
3.49339515e+01 7.35064683e+01 8.21020353e+01 5.72152435e+01
8.99529455e+00 -4.28930752e+01 -7.72890370e+01 -8.01654764e+01
-5.03493027e+01 -2.93552876e-06]
Resultados para k = 5:
Vetor v: [-1.37090683e-04 9.83387475e+01 1.56572957e+02 1.50955357e+02
8.37753637e+01 -1.75687935e+01 -1.11748635e+02 -1.60355449e+02
-1.43567387e+02 -6.82303278e+01 3.49318483e+01 1.23848421e+02
1.62257874e+02 1.34496255e+02 5.18853389e+01 -5.18853632e+01
-1.34496180e+02 -1.62257994e+02 -1.23848258e+02 -3.49320490e+01
6.82305642e+01 1.43567118e+02 1.60355755e+02 1.11748291e+02
1.75691793e+01 -8.37757950e+01 -1.50954882e+02 -1.56573471e+02
-9.83382092e+01 -1.37567520e-04]
Resultados para k = 8:
Vetor v: [ 3.37524414e-01 4.02181641e+02 6.41852783e+02 6.17876221e+02
3.43486572e+02 -7.22136230e+01 -4.57554321e+02 -6.56910034e+02
-5.88019409e+02 -2.79452393e+02 1.43019165e+02 5.07380249e+02
6.64480286e+02 5.51054016e+02 2.12334595e+02 -2.12301147e+02
-5.51156189e+02 -6.64301575e+02 -5.07643921e+02 -1.42657471e+02
2.78981201e+02 5.88610962e+02 6.56191040e+02 4.58405762e+02
7.12321777e+01 -3.42384766e+02 -6.19083984e+02 -6.40561523e+02
-4.03526855e+02 3.43872070e-01]
# Conjunto de pontos P1
t1_values = np.linspace(0, 2 * np.pi, 60)
P1 = np.array([(np.cos(t), np.sin(t)) for t in t1_values])
# Vetor b
b = np.sin(3 * t1_values)
for k in k_values:
# Conjunto de pontos P2
t2_values = np.linspace(0, 2 * np.pi, 30)
P2 = np.array([k * np.array([np.cos(t), np.sin(t)]) for t in t2_values])
for i in range(60):
for j in range(30):
U_matrix[i, j] = matrix_U(P1[i], P2[j])
Solução para k = 3:
[-288.5625 21.24111448 33.81980801 32.60631467 18.09551408
-3.79488814 -24.13768236 -34.63679491 -31.01054221 -14.73776022
7.54528465 26.75125795 35.04769709 29.05119946 11.20721963
-11.20721964 -29.05119945 -35.0476971 -26.75125793 -7.54528466
14.73776022 31.01054221 34.63679489 24.13768238 3.7948881
-18.09551402 -32.60631474 -33.81980791 -21.24111459 288.5625 ]
Solução para k = 4:
[ 24.5625 50.34931326 80.16546626 77.28904645 42.89306653
-8.99528687 -57.21525031 -82.1020294 -73.50647322 -34.93394744
17.88511642 63.41039207 83.07601976 68.86210546 26.56525849
-26.56525879 -68.86210449 -83.07602135 -63.41038986 -17.88511931
34.93395112 73.5064687 82.10203476 57.21524404 8.99529397
-42.89307438 -77.28903785 -80.16547543 -50.34930369 -24.5625 ]
Solução para k = 5:
[ -31.71875 98.33873844 156.57296658 150.95534754 83.77537203
-17.56880069 -111.74862933 -160.35545659 -143.56738079 -68.23033321
34.93185329 123.84841859 162.25787508 134.49625462 51.88533843
-51.88536406 -134.49618244 -162.25799131 -123.84826159 -34.93204689
68.23055983 143.56712246 160.35574794 111.74829996 17.56917095
-83.77578712 -150.95489025 -156.57346106 -98.33821774 31.765625 ]
Solução para k = 8:
[ -29.390625 402.20361328 641.83032227 617.8984375 343.46533203
-72.19262695 -457.57458496 -656.89282227 -588.03491211 -279.43933105
143.00823975 507.38861084 664.47442627 551.05731201 212.33398438
-212.30322266 -551.15203857 -664.30877686 -507.63562012 -142.66815186
278.99389648 588.59692383 656.20666504 458.3894043 71.25024414
-342.40405273 -619.06420898 -640.58251953 -403.50488281 30.078125 ]
# Conjunto de pontos P1
t1_values = np.linspace(0, 2 * np.pi, 60)
P1 = np.array([(np.cos(t), np.sin(t)) for t in t1_values])
# Vetor b
b = np.sin(3 * t1_values)
for k in k_values:
# Conjunto de pontos P2
t2_values = np.linspace(0, 2 * np.pi, 30)
P2 = np.array([k * np.array([np.cos(t), np.sin(t)]) for t in t2_values])
for i in range(60):
for j in range(30):
U_matrix[i, j] = matrix_U(P1[i], P2[j])
# Pontos y_i
y_points = np.array([(0, 0), (0.7, 0.4), (0, 0.7), (-0.7, 0.4), (-0.7, -0.4), (0, -0.7), (0.7, -0.4)])
# Conjunto de pontos P1
t1_values = np.linspace(0, 2 * np.pi, 60)
P1 = np.array([(np.cos(t), np.sin(t)) for t in t1_values])
# Vetor b
b = np.sin(3 * t1_values)
for k in k_values:
# Conjunto de pontos P2
t2_values = np.linspace(0, 2 * np.pi, 30)
P2 = np.array([k * np.array([np.cos(t), np.sin(t)]) for t in t2_values])
for i in range(60):
for j in range(30):
U_matrix[i, j] = matrix_U(P1[i], P2[j])
# Pontos y_i
y_points = np.array([(0, 0), (0.7, 0.4), (0, 0.7), (-0.7, 0.4), (-0.7, -0.4), (0, -0.7), (0.7, -0.4)])
# Imprimir os resultados
for i, (phi_calculated, phi_exact, error, r) in enumerate(zip(phi_values_calculated, phi_exata_values, error_values[best_k_index], np.linalg.norm(y_point
print(f"Para o ponto y_{i+1}:")
print(f"Valor calculado de ϕ(y_{i+1}) = {phi_calculated}")
print(f"Valor exato de ϕ(y_{i+1}) = {phi_exact}")
print(f"Erro absoluto = {error}")
print(f"Valor de r = {r}")
print()
# Lista para armazenar os erros para cada valor de k em cada ponto y_i
errors_per_k = []
for k in k_values:
# Conjunto de pontos P2
t2_values = np.linspace(0, 2 * np.pi, 30)
P2 = np.array([k * np.array([np.cos(t), np.sin(t)]) for t in t2_values])
def matrix_rank(matrix):
return np.linalg.matrix_rank(matrix)
def condition_number(matrix):
return np.linalg.cond(matrix)
# Valores de k fornecidos
k_values = [1.05, 1.1, 1.2, 1.3, 1.5, 1.8, 2.0, 2.3, 2.6, 3, 3.5, 4, 5, 6.5, 8, 10, 20, 50, 100, 200]
# Conjunto de pontos P1
t1_values = np.linspace(0, 2 * np.pi, 60)
P1 = np.array([(np.cos(t), np.sin(t)) for t in t1_values])
# Conjunto de pontos P2
t2_values = np.linspace(0, 2 * np.pi, 30)
for k in k_values:
U_matrix = np.zeros((60, 30))
for i in range(60):
for j in range(30):
P2_point = k * np.array([np.cos(t2_values[j]), np.sin(t2_values[j])])
U_matrix[i, j] = matrix_U(P1[i], P2_point)
rank = matrix_rank(U_matrix)
matrix_rank_values.append(rank)
num_cond = condition_number(U_matrix)
num_conditioning.append(num_cond)
# Plotagem do gráfico
plt.figure(figsize=(10, 6))
plt.plot(k_values, matrix_rank_values, marker='o', label='Posto da Matriz U')
plt.plot(k_values, num_conditioning, marker='o', label='Número de Condicionamento')
plt.xlabel('Valores de k')
plt.ylabel('Posto / Número de Condicionamento')
plt.title('Posto e Número de Condicionamento da Matriz U para diferentes valores de k')
plt.legend()
plt.grid(True)
plt.show()
c) Monte um vetor com 60 posições da forma: b = b_i = sen(3t_1)
In [8]: import numpy as np
# Sequência de ângulos t1
t1_values = np.linspace(0, 2 * np.pi, 60)
# Vetor b
b = np.sin(3 * t1_values)
print(b)
[ 0.00000000e+00 3.14076712e-01 5.96367359e-01 8.18302776e-01
9.57422038e-01 9.99645611e-01 9.40700267e-01 7.86551556e-01
5.52800065e-01 2.63102564e-01 -5.32221748e-02 -3.64160575e-01
-6.38244184e-01 -8.47734428e-01 -9.71429893e-01 -9.96812007e-01
-9.21311978e-01 -7.52570770e-01 -5.07665800e-01 -2.11382624e-01
1.06293486e-01 4.13212186e-01 6.78311836e-01 8.74763085e-01
9.82684125e-01 9.91152831e-01 8.99312130e-01 7.16456740e-01
4.61092501e-01 1.59063496e-01 -1.59063496e-01 -4.61092501e-01
-7.16456740e-01 -8.99312130e-01 -9.91152831e-01 -9.82684125e-01
-8.74763085e-01 -6.78311836e-01 -4.13212186e-01 -1.06293486e-01
2.11382624e-01 5.07665800e-01 7.52570770e-01 9.21311978e-01
9.96812007e-01 9.71429893e-01 8.47734428e-01 6.38244184e-01
3.64160575e-01 5.32221748e-02 -2.63102564e-01 -5.52800065e-01
-7.86551556e-01 -9.40700267e-01 -9.99645611e-01 -9.57422038e-01
-8.18302776e-01 -5.96367359e-01 -3.14076712e-01 -7.34788079e-16]
d) Resolva o sistema linear, 60 x 30. Uv = b com a Fatoração QR através de transformações de Householder;
In [9]: import numpy as np
return U_matrix
# Vetor b
t1_values = np.linspace(0, 2 * np.pi, 60)
b = np.sin(3 * t1_values)
# Valores de k fornecidos
k_values = [1.05, 1.1, 1.2, 1.3, 1.5, 1.8, 2.0, 2.3, 2.6, 3, 3.5, 4, 5, 6.5, 8, 10, 20, 50, 100, 200]
for k in k_values:
U_matrix_k = calculate_U(k)
# Fatoração QR de U_matrix_k
Q, R = np.linalg.qr(U_matrix_k)
solutions.append(v_approx)
Resultados para k = 3:
Vetor v: [-2432.07944394 21.24111422 33.8198082 32.60631456
18.0955141 -3.79488809 -24.13768247 -34.63679475
-31.01054241 -14.73775998 7.54528438 26.75125824
35.04769679 29.05119975 11.20721937 -11.20721941
-29.05119965 -35.04769693 -26.75125808 -7.54528451
14.73776007 31.01054238 34.63679471 24.13768258
3.79488788 -18.09551377 -32.60631503 -33.8198076
-21.24111492 2432.07944431]
Resultados para k = 4:
Vetor v: [ 76.06189383 50.34931111 80.16546826 77.28904462 42.89306811
-8.99528823 -57.21524913 -82.10203039 -73.50647241 -34.93394807
17.88511691 63.4103917 83.07602002 68.86210525 26.56525855
-26.56525876 -68.86210463 -83.07602104 -63.41039026 -17.88511878
34.93395043 73.50646952 82.10203382 57.21524519 8.99529261
-42.89307284 -77.28903962 -80.16547344 -50.34930582 -76.0618965 ]
Resultados para k = 5:
Vetor v: [-744.5449948 98.33852192 156.57317982 150.9551416 83.77556899
-17.56898451 -111.74846055 -160.35560551 -143.56725221 -68.23043995
34.93193637 123.8483578 162.2579144 134.49623298 51.88534435
-51.88535429 -134.49620579 -162.25795185 -123.84831919 -34.93196785
68.23045947 143.56724459 160.35560607 111.74845883 17.56899673
-83.77559961 -150.95508833 -156.57325498 -98.33843089 744.54494639]
Resultados para k = 8:
Vetor v: [-1214.43437347 402.2228963 641.8154133 617.91153044
343.44967843 -72.17289463 -457.59975705 -656.85985093
-588.07518 -279.3932681 142.95920507 507.43813679
664.42802543 551.09657738 212.3051049 -212.2867667
-551.15499695 -664.31983067 -507.61063813 -142.70510944
279.03985542 588.54385595 656.26364009 458.33054389
71.30701755 -342.45581932 -619.01785231 -640.62071354
-403.47424164 1215.06981194]
Resultados para k = 10:
Vetor v: [-3928.00303421 757.3640925 1280.5832034 1181.43105984
694.30294583 -162.32793504 -874.64561916 -1299.72264427
-1134.09012259 -557.94433093 289.32158747 983.03823097
1303.7869318 1072.23256742 416.79915846 -414.66777746
-1078.68679752 -1292.84351036 -998.70578752 -268.67734265
532.10556731 1165.2614733 1263.19561486 916.40832931
115.61772893 -643.12452612 -1236.39148791 -1222.73328406
-817.03104254 3958.14675094]
return U_matrix
# Vetor b
t1_values = np.linspace(0, 2 * np.pi, 60)
b = np.sin(3 * t1_values)
# Valores de k fornecidos
k_values = [1.05, 1.1, 1.2, 1.3, 1.5, 1.8, 2.0, 2.3, 2.6, 3, 3.5, 4, 5, 6.5, 8, 10, 20, 50, 100, 200]
for k in k_values:
U_matrix_k = calculate_U(k)
# Fatoração QR de U_matrix_k
Q, R = np.linalg.qr(U_matrix_k)
solutions.append(v_approx)
f) Calcule a solução numérica do problema (1) para os pontos y_1 = (0,0), y_2 = (0.7, 0.4), y_3 = (0, 0.7), y_4 = (-0.7, 0.4), y_5 = (-0.7, -0.4), y_6 = (0, -
0.7) e y_7 = (0.7, -0.4). Basta calcular:ϕ(yi )=∑(j=1)^30 u_ij^ (r)v_j , para i = 1, ..., 7. Os v_j são as componentes do vetor obtido no item (d). Observe que
a conta do u^ agora deve ser feita com os y_i.
In [11]: import numpy as np
return U_matrix
# Vetor b
t1_values = np.linspace(0, 2 * np.pi, 60)
b = np.sin(3 * t1_values)
# Valores de k fornecidos
k_values = [1.05, 1.1, 1.2, 1.3, 1.5, 1.8, 2.0, 2.3, 2.6, 3, 3.5, 4, 5, 6.5, 8, 10, 20, 50, 100, 200]
for k in k_values:
U_matrix_k = calculate_U(k)
# Fatoração QR de U_matrix_k
Q, R = np.linalg.qr(U_matrix_k)
# Calcular ϕ(y_i) para cada ponto y_i usando as soluções aproximadas correspondentes
phi_values = []
for y in y_values:
phi_y_i = calculate_phi(y)
phi_values.append(phi_y_i)
phi_values_for_k[k] = phi_values
return U_matrix
# Vetor b
t1_values = np.linspace(0, 2 * np.pi, 60)
b = np.sin(3 * t1_values)
# Valores de k fornecidos
k_values = [1.05, 1.1, 1.2, 1.3, 1.5, 1.8, 2.0, 2.3, 2.6, 3, 3.5, 4, 5, 6.5, 8, 10, 20, 50, 100, 200]
# Solução exata
def exact_solution(r, theta):
return r**3 * np.sin(3 * theta)
best_r = None
best_phi_values = None
best_errors = None
for k in k_values:
U_matrix_k = calculate_U(k)
# Fatoração QR de U_matrix_k
Q, R = np.linalg.qr(U_matrix_k)
phi_values = []
errors = []
for y in y_values:
phi_y_i = calculate_phi(y)
phi_exact_y_i = exact_solution(np.linalg.norm(y), np.arctan2(y[1], y[0]))
error_i = np.abs(phi_y_i - phi_exact_y_i)
phi_values.append(phi_y_i)
errors.append(error_i)
mean_error = np.mean(errors)
if best_r is None or mean_error < best_error:
best_error = mean_error
best_r = k
best_phi_values = phi_values
best_errors = errors