Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Linear Computacional Final

Fazer download em pdf ou txt
Fazer download em pdf ou txt
Você está na página 1de 26

2º Trabalho avaliativo Algebra Linear Computacional

Eduardo de Castro Luiz - 20190066565

Solução com o decomposição em Valores Singulares (Pseudo-Inversa)


b) Faça um gráfico de uma função que retorna o número de condicionamento e o posto da matriz U, sendo o eixo-x a variação de r_ij e o eixo-y o valor
do número de condicionamento da matriz U. Escolha valores para K no intervalo (1, 200]
In [1]: import numpy as np

def distance(p1, p2):


return np.linalg.norm(p1 - p2)

def matrix_U(p1, p2):


r = distance(p1, p2)
if r == 0:
return 0
return 1 / (2 * np.pi) * np.log(1 / r)

def matrix_rank(matrix):
return np.linalg.matrix_rank(matrix)

def condition_number(matrix):
return np.linalg.cond(matrix)

import matplotlib.pyplot as plt

# 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]

# Matriz retangular U, posto da matriz e número de condicionamento para cada valor de k


matrix_rank_values = []
num_conditioning = []

# 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

def distance(p1, p2):


return np.linalg.norm(p1 - p2)

def matrix_U(p1, p2):


r = distance(p1, p2)
if r == 0:
return 0
return 1 / (2 * np.pi) * np.log(1 / r)

def solve_system(U_matrix, b_vector):


U_pseudo_inv = np.linalg.pinv(U_matrix)
v = U_pseudo_inv @ b_vector
return v

def phi_exata(r, theta):


return r**3 * np.sin(3 * theta)

# 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)

# Valores de k para análise de sensibilidade


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]

# Armazenar as soluções para diferentes valores de k


solutions = {}

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])

# Criar a matriz U para o valor atual de k


U_matrix = np.zeros((60, 30))

for i in range(60):
for j in range(30):
U_matrix[i, j] = matrix_U(P1[i], P2[j])

# Resolver o sistema linear para obter o vetor v


v = solve_system(U_matrix, b)

# Armazenar a solução para o valor atual de k


solutions[k] = v

# Imprimir os resultados para cada valor de k


for k, v in solutions.items():
print(f"Resultados para k = {k}:")
print(f"Vetor v: {v}")
print()
Resultados para k = 1.05:
Vetor v: [-3.33066907e-16 8.98573917e-01 1.43320199e+00 1.38634794e+00
7.75131838e-01 -1.54235490e-01 -1.02465021e+00 -1.48052927e+00
-1.33322781e+00 -6.39795499e-01 3.18355082e-01 1.14946034e+00
1.51202263e+00 1.25587518e+00 4.84934516e-01 -4.84934516e-01
-1.25587518e+00 -1.51202263e+00 -1.14946034e+00 -3.18355082e-01
6.39795499e-01 1.33322781e+00 1.48052927e+00 1.02465021e+00
1.54235490e-01 -7.75131838e-01 -1.38634794e+00 -1.43320199e+00
-8.98573917e-01 -1.33226763e-15]

Resultados para k = 1.1:


Vetor v: [-1.33226763e-15 1.04585912e+00 1.66545804e+00 1.60617856e+00
8.92011582e-01 -1.86102194e-01 -1.18870601e+00 -1.70688895e+00
-1.52906181e+00 -7.27423162e-01 3.71269755e-01 1.31885389e+00
1.72860591e+00 1.43315636e+00 5.52929688e-01 -5.52929688e-01
-1.43315636e+00 -1.72860591e+00 -1.31885389e+00 -3.71269755e-01
7.27423162e-01 1.52906181e+00 1.70688895e+00 1.18870601e+00
1.86102194e-01 -8.92011582e-01 -1.60617856e+00 -1.66545804e+00
-1.04585912e+00 -1.99840144e-15]

Resultados para k = 1.2:


Vetor v: [-9.76996262e-15 1.35941172e+00 2.16444056e+00 2.08678570e+00
1.15811313e+00 -2.42856267e-01 -1.54479108e+00 -2.21674383e+00
-1.98467896e+00 -9.43230957e-01 4.82885605e-01 1.71207898e+00
2.24306266e+00 1.85929004e+00 7.17268050e-01 -7.17268050e-01
-1.85929004e+00 -2.24306266e+00 -1.71207898e+00 -4.82885605e-01
9.43230957e-01 1.98467896e+00 2.21674383e+00 1.54479108e+00
2.42856267e-01 -1.15811313e+00 -2.08678570e+00 -2.16444056e+00
-1.35941172e+00 -1.15463195e-14]

Resultados para k = 1.3:


Vetor v: [-1.77635684e-14 1.72839690e+00 2.75192967e+00 2.65318746e+00
1.47243867e+00 -3.08791075e-01 -1.96409165e+00 -2.81840853e+00
-2.52333934e+00 -1.19921730e+00 6.13962308e-01 2.17675966e+00
2.85184423e+00 2.36390707e+00 9.11935719e-01 -9.11935719e-01
-2.36390707e+00 -2.85184423e+00 -2.17675966e+00 -6.13962308e-01
1.19921730e+00 2.52333934e+00 2.81840853e+00 1.96409165e+00
3.08791075e-01 -1.47243867e+00 -2.65318746e+00 -2.75192967e+00
-1.72839690e+00 -2.48689958e-14]

Resultados para k = 1.5:


Vetor v: [-1.13686838e-13 2.65513932e+00 4.22747600e+00 4.07578934e+00
2.26193926e+00 -4.74361017e-01 -3.01721029e+00 -4.32959937e+00
-3.87631777e+00 -1.84222003e+00 9.43160586e-01 3.34390724e+00
4.38096214e+00 3.63139993e+00 1.40090246e+00 -1.40090246e+00
-3.63139993e+00 -4.38096214e+00 -3.34390724e+00 -9.43160586e-01
1.84222003e+00 3.87631777e+00 4.32959937e+00 3.01721029e+00
4.74361017e-01 -2.26193926e+00 -4.07578934e+00 -4.22747600e+00
-2.65513932e+00 -8.52651283e-14]

Resultados para k = 1.8:


Vetor v: [-1.36424205e-12 4.58808074e+00 7.30507852e+00 7.04296397e+00
3.90863104e+00 -8.19695839e-01 -5.21373939e+00 -7.48154770e+00
-6.69827711e+00 -3.18335622e+00 1.62978149e+00 5.77827171e+00
7.57030258e+00 6.27505907e+00 2.42075945e+00 -2.42075945e+00
-6.27505907e+00 -7.57030258e+00 -5.77827171e+00 -1.62978149e+00
3.18335622e+00 6.69827711e+00 7.48154770e+00 5.21373939e+00
8.19695839e-01 -3.90863104e+00 -7.04296397e+00 -7.30507852e+00
-4.58808074e+00 -4.54747351e-13]

Resultados para k = 2.0:


Vetor v: [ 5.45696821e-12 6.29366356e+00 1.00206838e+01 9.66113028e+00
5.36163380e+00 -1.12441130e+00 -7.15190588e+00 -1.02627541e+01
-9.18830879e+00 -4.36674378e+00 2.23563991e+00 7.92629864e+00
1.03845029e+01 8.60776279e+00 3.32065768e+00 -3.32065768e+00
-8.60776279e+00 -1.03845029e+01 -7.92629864e+00 -2.23563991e+00
4.36674378e+00 9.18830879e+00 1.02627541e+01 7.15190588e+00
1.12441130e+00 -5.36163380e+00 -9.66113028e+00 -1.00206838e+01
-6.29366357e+00 4.54747351e-12]

Resultados para k = 2.3:


Vetor v: [-1.01863407e-10 9.57187557e+00 1.52402075e+01 1.46933715e+01
8.15437480e+00 -1.71008904e+00 -1.08771549e+01 -1.56083661e+01
-1.39742691e+01 -6.64127145e+00 3.40012885e+00 1.20549094e+01
1.57935308e+01 1.30913312e+01 5.05030525e+00 -5.05030525e+00
-1.30913312e+01 -1.57935308e+01 -1.20549094e+01 -3.40012885e+00
6.64127145e+00 1.39742691e+01 1.56083661e+01 1.08771549e+01
1.71008904e+00 -8.15437480e+00 -1.46933715e+01 -1.52402075e+01
-9.57187557e+00 -1.09139364e-10]

Resultados para k = 2.6:


Vetor v: [-6.69388101e-10 1.38271789e+01 2.20154424e+01 2.12255032e+01
1.17795095e+01 -2.47033163e+00 -1.57127372e+01 -2.25472707e+01
-2.01867144e+01 -9.59373608e+00 4.91170088e+00 1.74140781e+01
2.28147528e+01 1.89112548e+01 7.29548493e+00 -7.29548493e+00
-1.89112548e+01 -2.28147528e+01 -1.74140781e+01 -4.91170088e+00
9.59373608e+00 2.01867144e+01 2.25472707e+01 1.57127372e+01
2.47033163e+00 -1.17795095e+01 -2.12255032e+01 -2.20154424e+01
-1.38271789e+01 -6.40284270e-10]

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 = 3.5:


Vetor v: [-8.56816769e-08 3.37301033e+01 5.37046023e+01 5.17776202e+01
2.87350060e+01 -6.02614170e+00 -3.83297457e+01 -5.50019474e+01
-4.92435926e+01 -2.34030173e+01 1.19816325e+01 4.24800070e+01
5.56544447e+01 4.61322290e+01 1.77966495e+01 -1.77966495e+01
-4.61322290e+01 -5.56544447e+01 -4.24800070e+01 -1.19816325e+01
2.34030173e+01 4.92435925e+01 5.50019475e+01 3.83297456e+01
6.02614188e+00 -2.87350062e+01 -5.17776200e+01 -5.37046026e+01
-3.37301030e+01 -8.94069672e-08]

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 = 6.5:


Vetor v: [-1.37329102e-03 2.16053085e+02 3.43987427e+02 3.31652847e+02
1.84049973e+02 -3.85935898e+01 -2.45517311e+02 -3.52294930e+02
-3.15423923e+02 -1.49895355e+02 7.67384567e+01 2.72101875e+02
3.56473709e+02 2.95494972e+02 1.13985550e+02 -1.13985821e+02
-2.95494217e+02 -3.56474922e+02 -2.72100304e+02 -7.67403450e+01
1.49897533e+02 3.15421551e+02 3.52297600e+02 2.45514198e+02
3.85971756e+01 -1.84054092e+02 -3.31648209e+02 -3.43992523e+02
-2.16047607e+02 -1.38092041e-03]

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]

Resultados para k = 10:


Vetor v: [ 1.33398438 784.46777344 1254.00830078 1207.18164062
669.67431641 -139.11791992 -896.1484375 -1280.20019531
-1151.38085938 -543.10693359 277.11230469 992.48779297
1297.18603516 1075.93481445 416.01293945 -416.79321289
-1073.67211914 -1300.70458984 -988.06542969 -281.99316406
547.95703125 1147.05517578 1283.53759766 894.18261719
139.45556641 -668.27587891 -1210.22949219 -1249.59472656
-789.78125 1.48095703]

Resultados para k = 20:


Vetor v: [-9.93188477e+00 6.30210596e+03 1.00296667e+04 9.64402417e+03
5.37115942e+03 -1.11801440e+03 -7.16834741e+03 -1.02507769e+04
-9.18544263e+03 -4.38183838e+03 2.24977393e+03 7.92544843e+03
1.03712686e+04 8.62360693e+03 3.31606238e+03 -3.33146582e+03
-8.59089697e+03 -1.03926056e+04 -7.93424072e+03 -2.21850415e+03
4.35556445e+03 9.18344873e+03 1.02795461e+04 7.13808350e+03
1.12276392e+03 -5.34577417e+03 -9.67695557e+03 -1.00196358e+04
-6.27784192e+03 -1.02520752e+01]

Resultados para k = 50:


Vetor v: [ 2.88325310e+00 9.83214384e+04 1.56567040e+05 1.50970982e+05
8.37834425e+04 -1.75837608e+04 -1.11758113e+05 -1.60341850e+05
-1.43556218e+05 -6.82427325e+04 3.49194359e+04 1.23859319e+05
1.62271411e+05 1.34486955e+05 5.18707970e+04 -5.18775464e+04
-1.34481082e+05 -1.62263949e+05 -1.23864189e+05 -3.49274449e+04
6.82465557e+04 1.43564582e+05 1.60339084e+05 1.11749559e+05
1.75854718e+04 -8.37748055e+04 -1.50971579e+05 -1.56575693e+05
-9.83228763e+04 2.88267326e+00]

Resultados para k = 100:


Vetor v: [-6.07085419e+00 7.86999410e+05 1.25275024e+06 1.20743947e+06
6.69931936e+05 -1.40496381e+05 -8.93686883e+05 -1.28273797e+06
-1.14878226e+06 -5.46079300e+05 2.79572057e+05 9.91085477e+05
1.29810495e+06 1.07569515e+06 4.14893568e+05 -4.14907731e+05
-1.07568869e+06 -1.29808742e+06 -9.91082193e+05 -2.79587835e+05
5.46067514e+05 1.14879156e+06 1.28275464e+06 8.93686797e+05
1.40479714e+05 -6.69940994e+05 -1.20742800e+06 -1.25273445e+06
-7.87000236e+05 -6.06922913e+00]

Resultados para k = 200:


Vetor v: [-2.26890671e+02 6.29424819e+06 1.00215061e+07 9.66134900e+06
5.36103302e+06 -1.12519013e+06 -7.15203822e+06 -1.02621033e+07
-9.18756779e+06 -4.36669923e+06 2.23494262e+06 7.92560098e+06
1.03845459e+07 8.60850001e+06 3.32130508e+06 -3.32078723e+06
-8.60853106e+06 -1.03850934e+07 -7.92608511e+06 -2.23485030e+06
4.36727105e+06 9.18801534e+06 1.02619538e+07 7.15144826e+06
1.12477815e+06 -5.36083671e+06 -9.66074764e+06 -1.00211059e+07
-6.29440755e+06 -2.26911460e+02]

e) Faça uma análise da sensibilidade do sistema linear do item (d)


In [4]: import numpy as np

def distance(p1, p2):


return np.linalg.norm(p1 - p2)

def matrix_U(p1, p2):


r = distance(p1, p2)
if r == 0:
return 0
return 1 / (2 * np.pi) * np.log(1 / r)

def solve_system(U_matrix, b_vector):


U, s, Vt = np.linalg.svd(U_matrix)
S_inv = np.zeros((U_matrix.shape[1], U_matrix.shape[0]))
S_inv[:U_matrix.shape[1], :U_matrix.shape[1]] = np.linalg.inv(np.diag(s))
v = Vt.T @ S_inv @ U.T @ b_vector
return v

# 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)

# Valores de k com ligeiras variações


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]

# Armazenar as soluções para diferentes valores de k


solutions = {}

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])

# Criar a matriz U para o valor atual de k


U_matrix = np.zeros((60, 30))

for i in range(60):
for j in range(30):
U_matrix[i, j] = matrix_U(P1[i], P2[j])

# Resolver o sistema linear para obter o vetor v


v = solve_system(U_matrix, b)

# Armazenar a solução para o valor atual de k


solutions[k] = v

# Comparar as soluções para diferentes valores de k


for k, v in solutions.items():
print(f"Solução para k = {k}:")
print(v)
print()

# Calcular a norma dos vetores solução


norms = {k: np.linalg.norm(v) for k, v in solutions.items()}

# Encontrar o valor de k mais sensível (maior norma)


most_sensitive_k = max(norms, key=norms.get)
print(f"Valor de k mais sensível: {most_sensitive_k}")
Solução para k = 1.05:
[ 5.09402741e+13 8.37686584e-01 1.43912866e+00 1.39180591e+00
7.83215313e-01 -1.56672386e-01 -1.02089234e+00 -1.47603088e+00
-1.34027069e+00 -6.44108534e-01 3.07173463e-01 1.15457372e+00
1.49353920e+00 1.27348020e+00 4.87718723e-01 -4.94712201e-01
-1.22846716e+00 -1.53300547e+00 -1.14684636e+00 -3.19974096e-01
6.13422835e-01 1.34680025e+00 1.46971644e+00 1.03688352e+00
1.55237659e-01 -7.70732599e-01 -1.39382201e+00 -1.43455220e+00
-8.74481501e-01 -5.09402741e+13]

Solução para k = 1.1:


[-2.81681738e+12 1.05158057e+00 1.66806695e+00 1.60320840e+00
8.93118504e-01 -1.85574868e-01 -1.18868387e+00 -1.70656222e+00
-1.52993998e+00 -7.26322245e-01 3.70914921e-01 1.31707750e+00
1.73150751e+00 1.43224508e+00 5.52410716e-01 -5.53255167e-01
-1.43160753e+00 -1.72950784e+00 -1.31842446e+00 -3.71773804e-01
7.27816404e-01 1.52883598e+00 1.70681756e+00 1.18917319e+00
1.86209393e-01 -8.91739413e-01 -1.60626286e+00 -1.66503845e+00
-1.04592041e+00 2.81681738e+12]

Solução para k = 1.2:


[ 7.39685386e+11 1.36127585e+00 2.16273223e+00 2.08757420e+00
1.15712954e+00 -2.42269981e-01 -1.54460391e+00 -2.21772893e+00
-1.98311198e+00 -9.44632584e-01 4.84066013e-01 1.71074308e+00
2.24427369e+00 1.85835539e+00 7.18003415e-01 -7.18405402e-01
-1.85855522e+00 -2.24387677e+00 -1.71117024e+00 -4.83330982e-01
9.42965300e-01 1.98527698e+00 2.21605254e+00 1.54534016e+00
2.42680402e-01 -1.15834196e+00 -2.08600271e+00 -2.16596716e+00
-1.35636361e+00 -7.39685386e+11]
Solução para k = 1.3:
[-1.57712080e+11 1.72842877e+00 2.75206792e+00 2.65331356e+00
1.47207460e+00 -3.08348203e-01 -1.96446132e+00 -2.81808808e+00
-2.52379549e+00 -1.19862877e+00 6.13418630e-01 2.17712481e+00
2.85177289e+00 2.36392092e+00 9.11741417e-01 -9.11451408e-01
-2.36454634e+00 -2.85119898e+00 -2.17717449e+00 -6.13775714e-01
1.19919412e+00 2.52318095e+00 2.81870942e+00 1.96379415e+00
3.08908169e-01 -1.47234619e+00 -2.65342389e+00 -2.75168394e+00
-1.72846185e+00 1.57712080e+11]

Solução para k = 1.5:


[ 3.38018353e+09 2.65511446e+00 4.22750754e+00 4.07576241e+00
2.26196461e+00 -4.74389441e-01 -3.01718992e+00 -4.32959848e+00
-3.87633531e+00 -1.84220584e+00 9.43161278e-01 3.34389581e+00
4.38098089e+00 3.63137767e+00 1.40091766e+00 -1.40090149e+00
-3.63141887e+00 -4.38093892e+00 -3.34392017e+00 -9.43160220e-01
1.84222805e+00 3.87631250e+00 4.32959469e+00 3.01722014e+00
4.74356543e-01 -2.26194469e+00 -4.07578047e+00 -4.22748035e+00
-2.65513765e+00 -3.38018352e+09]

Solução para k = 1.8:


[-1.86518909e+08 4.58806857e+00 7.30509551e+00 7.04294324e+00
3.90865323e+00 -8.19716106e-01 -5.21372522e+00 -7.48155225e+00
-6.69828362e+00 -3.18334018e+00 1.62976092e+00 5.77829037e+00
7.57028911e+00 6.27506844e+00 2.42075250e+00 -2.42075557e+00
-6.27505802e+00 -7.57030861e+00 -5.77826351e+00 -1.62978833e+00
3.18335953e+00 6.69827855e+00 7.48154092e+00 5.21374946e+00
8.19686738e-01 -3.90862596e+00 -7.04296573e+00 -7.30507669e+00
-4.58808515e+00 1.86518909e+08]

Solução para k = 2.0:


[-3.72023131e+06 6.29365937e+00 1.00206870e+01 9.66112815e+00
5.36163484e+00 -1.12441126e+00 -7.15190678e+00 -1.02627527e+01
-9.18831031e+00 -4.36674229e+00 2.23563844e+00 7.92630017e+00
1.03845012e+01 8.60776442e+00 3.32065610e+00 -3.32065613e+00
-8.60776439e+00 -1.03845011e+01 -7.92630068e+00 -2.23563751e+00
4.36674103e+00 9.18831180e+00 1.02627508e+01 7.15190936e+00
1.12440748e+00 -5.36162957e+00 -9.66113498e+00 -1.00206787e+01
-6.29366892e+00 3.72023131e+06]

Solução para k = 2.3:


[ 1.21867312e+05 9.57187621e+00 1.52402070e+01 1.46933719e+01
8.15437465e+00 -1.71008909e+00 -1.08771546e+01 -1.56083664e+01
-1.39742688e+01 -6.64127179e+00 3.40012917e+00 1.20549092e+01
1.57935310e+01 1.30913311e+01 5.05030537e+00 -5.05030532e+00
-1.30913312e+01 -1.57935308e+01 -1.20549095e+01 -3.40012875e+00
6.64127136e+00 1.39742692e+01 1.56083661e+01 1.08771547e+01
1.71008938e+00 -8.15437529e+00 -1.46933709e+01 -1.52402082e+01
-9.57187485e+00 -1.21867375e+05]

Solução para k = 2.6:


[ 1.14015703e+04 1.38271792e+01 2.20154421e+01 2.12255034e+01
1.17795093e+01 -2.47033153e+00 -1.57127373e+01 -2.25472707e+01
-2.01867144e+01 -9.59373613e+00 4.91170093e+00 1.74140781e+01
2.28147528e+01 1.89112548e+01 7.29548496e+00 -7.29548498e+00
-1.89112548e+01 -2.28147529e+01 -1.74140780e+01 -4.91170101e+00
9.59373622e+00 2.01867143e+01 2.25472708e+01 1.57127371e+01
2.47033180e+00 -1.17795097e+01 -2.12255030e+01 -2.20154427e+01
-1.38271785e+01 -1.14015938e+04]

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 = 3.5:


[ 2.0625 33.73010334 53.70460231 51.77762023 28.73500602
-6.0261417 -38.32974569 -55.00194738 -49.24359258 -23.40301728
11.98163245 42.48000698 55.65444474 46.13222896 17.79664948
-17.79664948 -46.13222897 -55.65444474 -42.48000697 -11.98163247
23.40301733 49.24359251 55.00194748 38.32974556 6.02614188
-28.73500624 -51.77761997 -53.70460262 -33.730103 -2.09375 ]

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 = 6.5:


[ 13.46875 216.05303955 343.9874115 331.65283203 184.05003357
-38.59366608 -245.51726532 -352.29496765 -315.42390442 -149.89539719
76.7384758 272.10181808 356.47376633 295.49493408 113.98562241
-113.98590088 -295.49414444 -356.4749794 -272.10021591 -76.74041367
149.89759064 315.42145157 352.29764557 245.51413727 38.597229
-184.0541687 -331.6481781 -343.99255371 -216.0475769 -13.40625 ]

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 ]

Solução para k = 10:


[ -43.890625 120.3125 1907.25 572.5625 1278.875
-715.5625 -358.875 -1771.625 -711.5625 -925.5625
597.75 737.90625 1482.34375 962.390625 456.375
-383.4921875 -1180.21875 -1122.34375 -1236.0625 32.53125
171.0625 1581.875 796.25 1428.0625 -434.25
-61.1875 -1843.4375 -597.5 -1453.625 714.4375 ]

Solução para k = 20:


[ 12867.375 4377.40625 11480.15625 8755.3671875
5685.5625 -903.671875 -7811.0234375 -9341.359375
-10144.4375 -3606.640625 1838.15625 7919.875
10689.5625 8219.09375 3556.1640625 -3247.4375
-8989.875 -9854.1875 -8350.1875 -2150.65625
4719.28515625 8491.40625 11041.109375 6633.640625
1082.109375 -4597.65625 -11126.7421875 -8032.390625
-8532.3125 -10667.1875 ]

Solução para k = 50:


[ 344744.75 -6430.234375 175826.6796875 190424.921875
52762.2578125 -31942.734375 -74123.9375 -178228.2890625
-156763.921875 -50273.09375 37445.3125 106876.9375
169154.5 147264.09375 34992.515625 -48587.5625
-127957.76953125 -159720.36328125 -142574. -16355.671875
69944.890625 123442.65625 177630.640625 108847.40625
20350.75 -103075.4609375 -132195.78125 -127223.4375
-202864. -201391. ]

Solução para k = 100:


[ 540627.875 657480.15625 1150224.375 1355473.703125
610008.453125 -103954.1796875 -994934.4453125 -1166946.640625
-1199616.2265625 -525062.65429688 222909.1640625 1046698.07421875
1303996.6171875 1061139.88867188 346344.7421875 -296155.8984375
-1128087.359375 -1331836.671875 -965890.8046875 -244696.74023438
502894.0703125 1163174.25 1246030.921875 989444.203125
66603.6484375 -722853.4375 -1083211.46875 -1243166.3515625
-1034153.875 -222483.625 ]

Solução para k = 200:


[ 2200976.625 4430915.9375 10288094.34765625
9846689.9609375 5757759.47265625 -1537985.859375
-7653692.2734375 -9392267.79296875 -9006524.234375
-5727481.1484375 3276593.4765625 8258653.21484375
9514223.33398438 8486205.453125 4631116.44921875
-4662280.1328125 -7977561.796875 -10764243.37890625
-7325691.265625 -2600458.1875 4016179.9609375
9765650.8984375 10155401.859375 6949611.890625
1198068.4296875 -5531213.765625 -9218805.671875
-9831525.5703125 -8109538.4375 563128.625 ]

Valor de k mais sensível: 1.05


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 [5]: import numpy as np
def distance(p1, p2):
return np.linalg.norm(p1 - p2)

def matrix_U(p1, p2):


r = distance(p1, p2)
if r == 0:
return 0
return 1 / (2 * np.pi) * np.log(1 / r)

def solve_system(U_matrix, b_vector):


U, s, Vt = np.linalg.svd(U_matrix)
S_inv = np.zeros((U_matrix.shape[1], U_matrix.shape[0]))
S_inv[:U_matrix.shape[1], :U_matrix.shape[1]] = np.linalg.inv(np.diag(s))
v = Vt.T @ S_inv @ U.T @ b_vector
return v

# 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)

# Valores de k para análise de sensibilidade


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]

# Armazenar as soluções para diferentes valores de k


solutions_list = []

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])

# Criar a matriz U para o valor atual de k


U_matrix = np.zeros((60, 30))

for i in range(60):
for j in range(30):
U_matrix[i, j] = matrix_U(P1[i], P2[j])

# Resolver o sistema linear para obter o vetor v


v = solve_system(U_matrix, b)

# Armazenar a solução para o valor atual de k


solutions_list.append((k, v))

# 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)])

# Calcular ϕ(y_i) para cada ponto y_i usando a matriz U e o vetor v


for k, v in solutions_list:
print(f"--- Resultados para k = {k} ---")
for i, y in enumerate(y_points):
u_star = np.zeros(30)
for j in range(30):
u_star[j] = matrix_U(y, k * np.array([np.cos(t2_values[j]), np.sin(t2_values[j])]))
phi_yi = np.sum(u_star * v)
print(f"ϕ(y_{i+1}) = {phi_yi:.15e}")
--- Resultados para k = 1.05 ---
ϕ(y_1) = 3.234863281250000e-03
ϕ(y_2) = 4.814453125000000e-01
ϕ(y_3) = -3.281250000000000e-01
ϕ(y_4) = 5.634765625000000e-01
ϕ(y_5) = -4.912109375000000e-01
ϕ(y_6) = 3.562011718750000e-01
ϕ(y_7) = -5.576171875000000e-01
--- Resultados para k = 1.1 ---
ϕ(y_1) = -3.913879394531250e-03
ϕ(y_2) = 5.469665527343750e-01
ϕ(y_3) = -3.537597656250000e-01
ϕ(y_4) = 4.992980957031250e-01
ϕ(y_5) = -5.493469238281250e-01
ϕ(y_6) = 3.320159912109375e-01
ϕ(y_7) = -5.003051757812500e-01
--- Resultados para k = 1.2 ---
ϕ(y_1) = 7.190704345703125e-03
ϕ(y_2) = 5.064468383789062e-01
ϕ(y_3) = -3.300552368164062e-01
ϕ(y_4) = 5.500946044921875e-01
ϕ(y_5) = -4.978942871093750e-01
ϕ(y_6) = 3.559799194335938e-01
ϕ(y_7) = -5.414810180664062e-01
--- Resultados para k = 1.3 ---
ϕ(y_1) = -1.049041748046875e-05
ϕ(y_2) = 5.239982604980469e-01
ϕ(y_3) = -3.430175781250000e-01
ϕ(y_4) = 5.239906311035156e-01
ϕ(y_5) = -5.240211486816406e-01
ϕ(y_6) = 3.429813385009766e-01
ϕ(y_7) = -5.239925384521484e-01
--- Resultados para k = 1.5 ---
ϕ(y_1) = -1.613238453865051e-02
ϕ(y_2) = 5.284391790628433e-01
ϕ(y_3) = -3.630522489547729e-01
ϕ(y_4) = 4.919821619987488e-01
ϕ(y_5) = -5.560178160667419e-01
ϕ(y_6) = 3.229479193687439e-01
ϕ(y_7) = -5.195608958601952e-01
--- Resultados para k = 1.8 ---
ϕ(y_1) = 5.847558379173279e-03
ϕ(y_2) = 5.255659380927682e-01
ϕ(y_3) = -3.364519067108631e-01
ϕ(y_4) = 5.332413949072361e-01
ϕ(y_5) = -5.147586092352867e-01
ϕ(y_6) = 3.495481088757515e-01
ϕ(y_7) = -5.224340567365289e-01
--- Resultados para k = 2.0 ---
ϕ(y_1) = 5.553010851144791e-07
ϕ(y_2) = 5.240002461359836e-01
ϕ(y_3) = -3.429993983008899e-01
ϕ(y_4) = 5.240008046384901e-01
ϕ(y_5) = -5.239991954294965e-01
ϕ(y_6) = 3.430006015114486e-01
ϕ(y_7) = -5.239997537864838e-01
--- Resultados para k = 2.3 ---
ϕ(y_1) = 8.285006681035156e-03
ϕ(y_2) = 5.289766784753738e-01
ϕ(y_3) = -3.342744104738813e-01
ϕ(y_4) = 5.350156167660316e-01
ϕ(y_5) = -5.129843832291954e-01
ϕ(y_6) = 3.517255895203562e-01
ϕ(y_7) = -5.190233215289481e-01
--- Resultados para k = 2.6 ---
ϕ(y_1) = 3.564190937140665e-03
ϕ(y_2) = 5.264750873982393e-01
ϕ(y_3) = -3.393052941282804e-01
ϕ(y_4) = 5.284807002735761e-01
ϕ(y_5) = -5.195192997289269e-01
ϕ(y_6) = 3.466947058623191e-01
ϕ(y_7) = -5.215249126090384e-01
--- Resultados para k = 3 ---
ϕ(y_1) = 9.462617356348346e-09
ϕ(y_2) = 5.240000072561131e-01
ϕ(y_3) = -3.429999902650920e-01
ϕ(y_4) = 5.240000113672494e-01
ϕ(y_5) = -5.239999886575362e-01
ϕ(y_6) = 3.430000096871524e-01
ϕ(y_7) = -5.239999927767158e-01
--- Resultados para k = 3.5 ---
ϕ(y_1) = 6.230694333023923e-03
ϕ(y_2) = 5.291711149481683e-01
ϕ(y_3) = -3.366717724130708e-01
ϕ(y_4) = 5.311599364245789e-01
ϕ(y_5) = -5.168400633266343e-01
ϕ(y_6) = 3.493282280095920e-01
ϕ(y_7) = -5.188288848024669e-01
--- Resultados para k = 4 ---
ϕ(y_1) = -1.082484012648877e-06
ϕ(y_2) = 5.239990609441687e-01
ϕ(y_3) = -3.430010935679508e-01
ϕ(y_4) = 5.239987907744057e-01
ϕ(y_5) = -5.240012103816865e-01
ϕ(y_6) = 3.429989050547029e-01
ϕ(y_7) = -5.240009397326162e-01
--- Resultados para k = 5 ---
ϕ(y_1) = -1.207431610504983e-02
ϕ(y_2) = 5.130249184806095e-01
ϕ(y_3) = -3.551471391292313e-01
ϕ(y_4) = 5.109242009666701e-01
ϕ(y_5) = -5.370758033682641e-01
ϕ(y_6) = 3.308528786156337e-01
ϕ(y_7) = -5.349750580207173e-01
--- Resultados para k = 6.5 ---
ϕ(y_1) = -1.941011122727287e-02
ϕ(y_2) = 5.057492771426952e-01
ϕ(y_3) = -3.624690319000203e-01
ϕ(y_4) = 5.035124058487908e-01
ϕ(y_5) = -5.444887724942236e-01
ϕ(y_6) = 3.235288118412560e-01
ϕ(y_7) = -5.422520087255864e-01
--- Resultados para k = 8 ---
ϕ(y_1) = -8.988907659862377e-03
ϕ(y_2) = 5.153986308654144e-01
ϕ(y_3) = -3.520120008411673e-01
ϕ(y_4) = 5.146387273201505e-01
ϕ(y_5) = -5.333522784630098e-01
ϕ(y_6) = 3.340010468623724e-01
ϕ(y_7) = -5.325961258578538e-01
--- Resultados para k = 10 ---
ϕ(y_1) = -2.891659979951555e-01
ϕ(y_2) = 2.390439047047721e-01
ϕ(y_3) = -6.294845196009078e-01
ϕ(y_4) = 2.330286102668424e-01
ϕ(y_5) = -8.174378571349052e-01
ϕ(y_6) = 5.169537172020000e-02
ϕ(y_7) = -8.120297334488100e-01
--- Resultados para k = 20 ---
ϕ(y_1) = -2.402552435132748e-01
ϕ(y_2) = 2.823262049732875e-01
ϕ(y_3) = -5.828742217927356e-01
ϕ(y_4) = 2.856300267576444e-01
ϕ(y_5) = -7.628160915946864e-01
ϕ(y_6) = 1.023376144112262e-01
ϕ(y_7) = -7.661259371761844e-01
--- Resultados para k = 50 ---
ϕ(y_1) = -6.566672095796093e-01
ϕ(y_2) = -1.302825181046501e-01
ϕ(y_3) = -9.995511201850604e-01
ϕ(y_4) = -1.348904522892553e-01
ϕ(y_5) = -1.183030038169818e+00
ϕ(y_6) = -3.138064799713902e-01
ϕ(y_7) = -1.178434607660165e+00
--- Resultados para k = 100 ---
ϕ(y_1) = 1.732132959878072e-01
ϕ(y_2) = 6.971218862163369e-01
ϕ(y_3) = -1.697594805445988e-01
ϕ(y_4) = 6.973351784108672e-01
ϕ(y_5) = -3.506971432070713e-01
ϕ(y_6) = 5.161865929549094e-01
ϕ(y_7) = -3.509074382891413e-01
--- Resultados para k = 200 ---
ϕ(y_1) = -3.541005430743098e-01
ϕ(y_2) = 1.700044833123684e-01
ϕ(y_3) = -6.970835900865495e-01
ϕ(y_4) = 1.698138338397257e-01
ϕ(y_5) = -8.782056414638646e-01
ϕ(y_6) = -1.111752865836024e-02
ϕ(y_7) = -8.780148327350616e-01
(g) Compare a resposta nos pontos do item (f) com a solução exata.

(h) Para qual r a solução foi a melhor dentro da sua comparação?


In [6]: import numpy as np
import matplotlib.pyplot as plt

def distance(p1, p2):


return np.linalg.norm(p1 - p2)

# Solução exata para ϕ(r, θ) = r^3 * sen(3θ)


def phi_exata(r, theta):
return r**3 * np.sin(3 * theta)

def matrix_U(p1, p2):


r = distance(p1, p2)
if r == 0:
return 0
return 1 / (2 * np.pi) * np.log(1 / r)

def solve_system(U_matrix, b_vector):


U, s, Vt = np.linalg.svd(U_matrix)
S_inv = np.zeros((U_matrix.shape[1], U_matrix.shape[0]))
S_inv[:U_matrix.shape[1], :U_matrix.shape[1]] = np.linalg.inv(np.diag(s))
v = Vt.T @ S_inv @ U.T @ b_vector
return v

# 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)

# Valores de k para análise de sensibilidade


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]

# Armazenar as soluções para diferentes valores de k


solutions_list = []

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])

# Criar a matriz U para o valor atual de k


U_matrix = np.zeros((60, 30))

for i in range(60):
for j in range(30):
U_matrix[i, j] = matrix_U(P1[i], P2[j])

# Resolver o sistema linear para obter o vetor v


v = solve_system(U_matrix, b)

# Armazenar a solução para o valor atual de k


solutions_list.append({k: v})

# 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)])

# Valores de theta_i para cada ponto y_i


theta_values = np.arctan2(y_points[:, 1], y_points[:, 0])

# Cálculo da solução exata para cada ponto y_i


phi_exata_values = phi_exata(np.linalg.norm(y_points, axis=1), theta_values)

# Calcular o erro para cada valor de k


error_values = []

for solutions_dict in solutions_list:


k, v = next(iter(solutions_dict.items()))
phi_values_calculated = []
for y in y_points:
u_star = np.zeros(30)
for j in range(30):
u_star[j] = matrix_U(y, k * np.array([np.cos(t2_values[j]), np.sin(t2_values[j])]))
phi_yi = np.sum(u_star * v)
phi_values_calculated.append(phi_yi)
error_values.append(np.abs(phi_values_calculated - phi_exata_values))

# Encontrar o valor de k que resulta no menor erro absoluto médio


mean_error_values = np.mean(error_values, axis=1)
best_k_index = np.argmin(mean_error_values)
best_k = k_values[best_k_index]

# 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()

print(f"O valor de k que resulta no menor erro absoluto médio é: {best_k}")

# Valores de k com ligeiras variações


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]

# 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])

# Criar a matriz U para o valor atual de k


U_matrix = np.zeros((60, 30))
for i in range(60):
for j in range(30):
U_matrix[i, j] = matrix_U(P1[i], P2[j])

# Resolver o sistema linear para obter o vetor v


v = solve_system(U_matrix, b)

# Calcular ϕ(y_i) para cada ponto y_i usando a matriz U e o vetor v


phi_values = []
for y in y_points:
u_star = np.zeros(30)
for j in range(30):
u_star[j] = matrix_U(y, k * np.array([np.cos(t2_values[j]), np.sin(t2_values[j])]))
phi_yi = np.sum(u_star * v)
phi_values.append(phi_yi)

# Cálculo do erro absoluto para cada ponto y_i


errors = np.abs(phi_values - phi_exata_values)
errors_per_k.append(errors)

# Encontrar o valor de k que resulta no menor erro absoluto médio


mean_errors_per_k = np.mean(errors_per_k, axis=1)
best_k_index = np.argmin(mean_errors_per_k)
best_k = k_values[best_k_index]
Para o ponto y_1:
Valor calculado de ϕ(y_1) = -0.3541005430743098
Valor exato de ϕ(y_1) = 0.0
Erro absoluto = 9.462617356348346e-09
Valor de r = 0.0

Para o ponto y_2:


Valor calculado de ϕ(y_2) = 0.1700044833123684
Valor exato de ϕ(y_2) = 0.5239999999999999
Erro absoluto = 7.2561131547743685e-09
Valor de r = 0.8062257748298549

Para o ponto y_3:


Valor calculado de ϕ(y_3) = -0.6970835900865495
Valor exato de ϕ(y_3) = -0.3429999999999999
Erro absoluto = 9.734907935321502e-09
Valor de r = 0.7

Para o ponto y_4:


Valor calculado de ϕ(y_4) = 0.1698138338397257
Valor exato de ϕ(y_4) = 0.5239999999999999
Erro absoluto = 1.136724947503609e-08
Valor de r = 0.8062257748298549

Para o ponto y_5:


Valor calculado de ϕ(y_5) = -0.8782056414638646
Valor exato de ϕ(y_5) = -0.5239999999999999
Erro absoluto = 1.1342463746011333e-08
Valor de r = 0.8062257748298549

Para o ponto y_6:


Valor calculado de ϕ(y_6) = -0.011117528658360243
Valor exato de ϕ(y_6) = 0.3429999999999999
Erro absoluto = 9.687152469073368e-09
Valor de r = 0.7

Para o ponto y_7:


Valor calculado de ϕ(y_7) = -0.8780148327350616
Valor exato de ϕ(y_7) = -0.5239999999999999
Erro absoluto = 7.2232840819808075e-09
Valor de r = 0.8062257748298549

O valor de k que resulta no menor erro absoluto médio é: 3


===============================================================================================================

Solução com a Fatoração QR através de transformações de Householder


b) Faça um gráfico de uma função que retorna o número de condicionamento e o posto da matriz U, sendo o eixo-x a variação de r_ij e o eixo-y o valor
do número de condicionamento da matriz U. Escolha valores para K no intervalo (1, 200]
In [7]: import numpy as np

def distance(p1, p2):


return np.linalg.norm(p1 - p2)

def matrix_U(p1, p2):


r = distance(p1, p2)
if r == 0:
return 0
return 1 / (2 * np.pi) * np.log(1 / r)

def matrix_rank(matrix):
return np.linalg.matrix_rank(matrix)

def condition_number(matrix):
return np.linalg.cond(matrix)

import matplotlib.pyplot as plt

# 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]

# Matriz retangular U, posto da matriz e número de condicionamento para cada valor de k


matrix_rank_values = []
num_conditioning = []

# 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

# Função para calcular a matriz U para um determinado valor de k


def calculate_U(k):
t1_values = np.linspace(0, 2 * np.pi, 60)
t2_values = np.linspace(0, 2 * np.pi, 30)

P1 = np.array([(np.cos(t), np.sin(t)) for t in t1_values])


P2 = np.array([k * np.array([np.cos(t), np.sin(t)]) for t in t2_values])

U_matrix = np.zeros((60, 30))


for i in range(60):
for j in range(30):
U_matrix[i, j] = 1 / (2 * np.pi) * np.log(1 / np.linalg.norm(P1[i] - P2[j]))

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 aproximada para cada valor de k


solutions = []

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)

# Resolvendo o sistema triangular superior Rv = QTb usando substituição para frente


Qt_b = np.dot(Q.T, b)
v_approx = np.linalg.solve(R, Qt_b)

solutions.append(v_approx)

# Exibindo as soluções aproximadas para cada valor de k


for i, k in enumerate(k_values):
print(f"Resultados para k = {k}:")
print(f"Vetor v:",solutions[i])
print()
Resultados para k = 1.05:
Vetor v: [ 5.09402741e+13 8.51684812e-01 1.44189672e+00 1.38296125e+00
7.76412743e-01 -1.58322266e-01 -1.02357184e+00 -1.48127104e+00
-1.33449144e+00 -6.43343729e-01 3.20094792e-01 1.15010798e+00
1.51094108e+00 1.25547147e+00 4.84712011e-01 -4.89216521e-01
-1.25058624e+00 -1.51554869e+00 -1.14921087e+00 -3.19887411e-01
6.36617518e-01 1.33468981e+00 1.47985557e+00 1.02357325e+00
1.52978475e-01 -7.72966174e-01 -1.39036813e+00 -1.43383064e+00
-8.74097610e-01 -5.09402741e+13]

Resultados para k = 1.1:


Vetor v: [-2.81681738e+12 1.04968495e+00 1.66492410e+00 1.60611298e+00
8.91886421e-01 -1.86110590e-01 -1.18824615e+00 -1.70757319e+00
-1.52822592e+00 -7.27859499e-01 3.71417274e-01 1.31866802e+00
1.72891472e+00 1.43312379e+00 5.52809964e-01 -5.52882108e-01
-1.43304552e+00 -1.72872974e+00 -1.31884598e+00 -3.71296278e-01
7.27465235e-01 1.52927618e+00 1.70692245e+00 1.18859969e+00
1.86118498e-01 -8.91774541e-01 -1.60659439e+00 -1.66496194e+00
-1.04667299e+00 2.81681738e+12]

Resultados para k = 1.2:


Vetor v: [ 7.39685386e+11 1.36040178e+00 2.16407675e+00 2.08687085e+00
1.15806510e+00 -2.42901698e-01 -1.54470182e+00 -2.21680006e+00
-1.98461276e+00 -9.43296363e-01 4.83117262e-01 1.71178018e+00
2.24321070e+00 1.85930158e+00 7.17243544e-01 -7.17262810e-01
-1.85932857e+00 -2.24296251e+00 -1.71227019e+00 -4.82544059e-01
9.42834595e-01 1.98499700e+00 2.21647124e+00 1.54496760e+00
2.43047086e-01 -1.15870834e+00 -2.08583521e+00 -2.16588660e+00
-1.35664344e+00 -7.39685386e+11]

Resultados para k = 1.3:


Vetor v: [-1.57712080e+11 1.72879293e+00 2.75178291e+00 2.65329648e+00
1.47234076e+00 -3.08743727e-01 -1.96407442e+00 -2.81844235e+00
-2.52331818e+00 -1.19924068e+00 6.14014172e-01 2.17662052e+00
2.85208387e+00 2.36367690e+00 9.12075415e-01 -9.11982430e-01
-2.36391317e+00 -2.85181559e+00 -2.17681131e+00 -6.13876722e-01
1.19918091e+00 2.52327859e+00 2.81853485e+00 1.96395875e+00
3.08869812e-01 -1.47241608e+00 -2.65333151e+00 -2.75176469e+00
-1.72840899e+00 1.57712080e+11]

Resultados para k = 1.5:


Vetor v: [ 1.13863857e+10 2.65508411e+00 4.22754218e+00 4.07571017e+00
2.26203789e+00 -4.74468197e-01 -3.01713242e+00 -4.32961805e+00
-3.87634704e+00 -1.84217436e+00 9.43129005e-01 3.34390006e+00
4.38101092e+00 3.63133708e+00 1.40094228e+00 -1.40089847e+00
-3.63145045e+00 -4.38088950e+00 -3.34396327e+00 -9.43141328e-01
1.84222968e+00 3.87631196e+00 4.32957548e+00 3.01725225e+00
4.74336357e-01 -2.26195029e+00 -4.07575985e+00 -4.22749963e+00
-2.65512692e+00 -1.13863857e+10]

Resultados para k = 1.8:


Vetor v: [-1.40758365e+09 4.58802946e+00 7.30514231e+00 7.04287957e+00
3.90873683e+00 -8.19810131e-01 -5.21363424e+00 -7.48163131e+00
-6.69821847e+00 -3.18339250e+00 1.62980180e+00 5.77826019e+00
7.57030468e+00 6.27507225e+00 2.42073440e+00 -2.42073865e+00
-6.27505771e+00 -7.57032948e+00 -5.77823672e+00 -1.62980151e+00
3.18334906e+00 6.69831668e+00 7.48147303e+00 5.21383480e+00
8.19611869e-01 -3.90858069e+00 -7.04298675e+00 -7.30506028e+00
-4.58811210e+00 1.40758365e+09]
Resultados para k = 2.0:
Vetor v: [-1.75889094e+07 6.29364423e+00 1.00206976e+01 9.66112167e+00
5.36163763e+00 -1.12441060e+00 -7.15191025e+00 -1.02627476e+01
-9.18831592e+00 -4.36673667e+00 2.23563282e+00 7.92630585e+00
1.03844956e+01 8.60776987e+00 3.32065092e+00 -3.32065106e+00
-8.60776981e+00 -1.03844947e+01 -7.92630858e+00 -2.23562797e+00
4.36673017e+00 9.18832349e+00 1.02627385e+01 7.15192259e+00
1.12439306e+00 -5.36161370e+00 -9.66115251e+00 -1.00206595e+01
-6.29368891e+00 1.75889094e+07]

Resultados para k = 2.3:


Vetor v: [ 8.96295970e+05 9.57188016e+00 1.52402039e+01 1.46933739e+01
8.15437372e+00 -1.71008919e+00 -1.08771537e+01 -1.56083679e+01
-1.39742670e+01 -6.64127358e+00 3.40013079e+00 1.20549078e+01
1.57935320e+01 1.30913304e+01 5.05030584e+00 -5.05030567e+00
-1.30913310e+01 -1.57935307e+01 -1.20549098e+01 -3.40012824e+00
6.64127084e+00 1.39742694e+01 1.56083665e+01 1.08771534e+01
1.71009157e+00 -8.15437840e+00 -1.46933671e+01 -1.52402126e+01
-9.57187025e+00 -8.96295970e+05]

Resultados para k = 2.6:


Vetor v: [ 5.96924021e+04 1.38271805e+01 2.20154410e+01 2.12255044e+01
1.17795087e+01 -2.47033115e+00 -1.57127374e+01 -2.25472707e+01
-2.01867142e+01 -9.59373639e+00 4.91170117e+00 1.74140779e+01
2.28147529e+01 1.89112547e+01 7.29548511e+00 -7.29548519e+00
-1.89112545e+01 -2.28147533e+01 -1.74140775e+01 -4.91170155e+00
9.59373680e+00 2.01867137e+01 2.25472714e+01 1.57127364e+01
2.47033252e+00 -1.17795105e+01 -2.12255020e+01 -2.20154439e+01
-1.38271772e+01 -5.96924021e+04]

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 = 3.5:


Vetor v: [ 10.12795706 33.73010329 53.70460236 51.7776202 28.73500604
-6.02614172 -38.32974567 -55.0019474 -49.24359257 -23.40301729
11.98163245 42.48000698 55.65444473 46.13222898 17.79664947
-17.79664946 -46.13222899 -55.65444472 -42.48000699 -11.98163244
23.4030173 49.24359254 55.00194745 38.32974558 6.02614184
-28.7350062 -51.77762001 -53.70460258 -33.73010305 -10.12795718]

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 = 6.5:


Vetor v: [ 306.43778249 216.05340862 343.98712676 331.65307722 184.04980283
-38.59345636 -245.5174064 -352.29487325 -315.42388194 -149.89555163
76.73879331 272.10131567 356.47448265 295.49397416 113.98680268
-113.98728428 -295.49256309 -356.47672979 -272.09836407 -76.74230909
149.89951209 315.4195547 352.29953675 245.51240099 38.59882203
-184.05553397 -331.64700668 -343.9934734 -216.0468818 -306.44107719]

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]

Resultados para k = 20:


Vetor v: [ 10949.53949015 9965.22338565 4375.73770137 16358.10441322
-1390.45833106 4849.11597682 -11824.12490814 -7067.86422699
-11026.00056514 -3559.95172602 2017.67562341 8037.15710563
9927.73542266 9763.23987239 1286.30842939 -451.17826139
-12047.54730064 -6780.4451516 -11295.78554688 676.14320354
1845.50510656 11671.39654165 7324.56871983 10941.05565278
-3579.15786208 -138.00421737 -14602.54888366 -6353.82027105
-7810.54135493 -12061.07803811]

Resultados para k = 50:


Vetor v: [ 84632.5364527 62756.36513771 119911.86335453 222972.72443061
75261.81127623 -133980.82572547 75012.1639139 -288096.61070468
-153528.30194208 34653.69265622 -51900.50251902 141050.81829563
169834.34922224 171059.32886203 -28504.54707006 5184.59571784
-125126.05646937 -194457.71439951 -152060.52208958 72942.36211717
-38420.85092719 145027.02808804 287152.06489592 -68627.61472053
158250.18089256 -150197.48182684 -138410.24909811 -135998.62036204
-151014.73343475 -15377.25402412]

Resultados para k = 100:


Vetor v: [-1047642.42433127 1198754.37723802 584879.73364253 1618727.09219034
460409.75418747 268745.27850576 -1567576.68607678 -675560.38491809
-1515458.26928068 -243610.86961855 -71699.90648465 1230886.09280524
1245992.81483573 1185378.53731372 26306.47981981 86092.45185094
-1328198.27924379 -1437144.40733872 -603020.15179506 -668215.85645359
661944.69573037 1527405.38040133 495568.31539767 1574010.38353347
107288.69042517 -1235536.41627195 -712076.32468896 -1118337.35989568
-1337567.16472755 1289254.42324774]

Resultados para k = 200:


Vetor v: [ -1530667.34465275 2626698.29492198 18065487.27119772
4888817.47732183 6665768.29796466 -3664328.57649286
-6208588.15002503 -4407422.76560693 -15837619.7088785
-6379547.2631297 8744281.72893899 7545645.93484305
7173775.14258692 3980355.90206967 16416481.10826211
-15011409.58571111 -2694177.41155057 -13531190.65030401
-6829804.58961376 -172209.03779668 1858486.28000782
9592732.94746244 10010712.17837579 7997067.67427783
2858205.0394027 -9584690.1866314 -7323408.77124175
-10520038.65885304 -3641040.83419864 -1088371.7429468 ]

e) Faça uma análise da sensibilidade do sistema linear do item (d)


In [10]: import numpy as np

# Função para calcular a matriz U para um determinado valor de k


def calculate_U(k):
t1_values = np.linspace(0, 2 * np.pi, 60)
t2_values = np.linspace(0, 2 * np.pi, 30)

P1 = np.array([(np.cos(t), np.sin(t)) for t in t1_values])


P2 = np.array([k * np.array([np.cos(t), np.sin(t)]) for t in t2_values])

U_matrix = np.zeros((60, 30))


for i in range(60):
for j in range(30):
U_matrix[i, j] = 1 / (2 * np.pi) * np.log(1 / np.linalg.norm(P1[i] - P2[j]))

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ções aproximadas e seus desvios para cada valor de k


solutions = []
sensitivity = []

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)

# Resolvendo o sistema triangular superior Rv = QTb usando substituição para frente


Qt_b = np.dot(Q.T, b)
v_approx = np.linalg.solve(R, Qt_b)

# Calculando o desvio da solução aproximada


deviation = np.linalg.norm(U_matrix_k @ v_approx - b)
sensitivity.append(deviation)

solutions.append(v_approx)

# Exibindo as soluções aproximadas e seus desvios para cada valor de k


for i, k in enumerate(k_values):
print(f"--- Resultados para k = {k} ---")
print(f"Solução aproximada: {solutions[i]}")
print(f"Desvio da solução: {sensitivity[i]}")
print()
--- Resultados para k = 1.05 ---
Solução aproximada: [ 5.09402741e+13 8.51684812e-01 1.44189672e+00 1.38296125e+00
7.76412743e-01 -1.58322266e-01 -1.02357184e+00 -1.48127104e+00
-1.33449144e+00 -6.43343729e-01 3.20094792e-01 1.15010798e+00
1.51094108e+00 1.25547147e+00 4.84712011e-01 -4.89216521e-01
-1.25058624e+00 -1.51554869e+00 -1.14921087e+00 -3.19887411e-01
6.36617518e-01 1.33468981e+00 1.47985557e+00 1.02357325e+00
1.52978475e-01 -7.72966174e-01 -1.39036813e+00 -1.43383064e+00
-8.74097610e-01 -5.09402741e+13]
Desvio da solução: 0.23524621462685683

--- Resultados para k = 1.1 ---


Solução aproximada: [-2.81681738e+12 1.04968495e+00 1.66492410e+00 1.60611298e+00
8.91886421e-01 -1.86110590e-01 -1.18824615e+00 -1.70757319e+00
-1.52822592e+00 -7.27859499e-01 3.71417274e-01 1.31866802e+00
1.72891472e+00 1.43312379e+00 5.52809964e-01 -5.52882108e-01
-1.43304552e+00 -1.72872974e+00 -1.31884598e+00 -3.71296278e-01
7.27465235e-01 1.52927618e+00 1.70692245e+00 1.18859969e+00
1.86118498e-01 -8.91774541e-01 -1.60659439e+00 -1.66496194e+00
-1.04667299e+00 2.81681738e+12]
Desvio da solução: 0.07698865637605294

--- Resultados para k = 1.2 ---


Solução aproximada: [ 7.39685386e+11 1.36040178e+00 2.16407675e+00 2.08687085e+00
1.15806510e+00 -2.42901698e-01 -1.54470182e+00 -2.21680006e+00
-1.98461276e+00 -9.43296363e-01 4.83117262e-01 1.71178018e+00
2.24321070e+00 1.85930158e+00 7.17243544e-01 -7.17262810e-01
-1.85932857e+00 -2.24296251e+00 -1.71227019e+00 -4.82544059e-01
9.42834595e-01 1.98499700e+00 2.21647124e+00 1.54496760e+00
2.43047086e-01 -1.15870834e+00 -2.08583521e+00 -2.16588660e+00
-1.35664344e+00 -7.39685386e+11]
Desvio da solução: 0.009788654225244255

--- Resultados para k = 1.3 ---


Solução aproximada: [-1.57712080e+11 1.72879293e+00 2.75178291e+00 2.65329648e+00
1.47234076e+00 -3.08743727e-01 -1.96407442e+00 -2.81844235e+00
-2.52331818e+00 -1.19924068e+00 6.14014172e-01 2.17662052e+00
2.85208387e+00 2.36367690e+00 9.12075415e-01 -9.11982430e-01
-2.36391317e+00 -2.85181559e+00 -2.17681131e+00 -6.13876722e-01
1.19918091e+00 2.52327859e+00 2.81853485e+00 1.96395875e+00
3.08869812e-01 -1.47241608e+00 -2.65333151e+00 -2.75176469e+00
-1.72840899e+00 1.57712080e+11]
Desvio da solução: 0.0015231075573957826

--- Resultados para k = 1.5 ---


Solução aproximada: [ 1.13863857e+10 2.65508411e+00 4.22754218e+00 4.07571017e+00
2.26203789e+00 -4.74468197e-01 -3.01713242e+00 -4.32961805e+00
-3.87634704e+00 -1.84217436e+00 9.43129005e-01 3.34390006e+00
4.38101092e+00 3.63133708e+00 1.40094228e+00 -1.40089847e+00
-3.63145045e+00 -4.38088950e+00 -3.34396327e+00 -9.43141328e-01
1.84222968e+00 3.87631196e+00 4.32957548e+00 3.01725225e+00
4.74336357e-01 -2.26195029e+00 -4.07575985e+00 -4.22749963e+00
-2.65512692e+00 -1.13863857e+10]
Desvio da solução: 5.613995625619894e-05

--- Resultados para k = 1.8 ---


Solução aproximada: [-1.40758365e+09 4.58802946e+00 7.30514231e+00 7.04287957e+00
3.90873683e+00 -8.19810131e-01 -5.21363424e+00 -7.48163131e+00
-6.69821847e+00 -3.18339250e+00 1.62980180e+00 5.77826019e+00
7.57030468e+00 6.27507225e+00 2.42073440e+00 -2.42073865e+00
-6.27505771e+00 -7.57032948e+00 -5.77823672e+00 -1.62980151e+00
3.18334906e+00 6.69831668e+00 7.48147303e+00 5.21383480e+00
8.19611869e-01 -3.90858069e+00 -7.04298675e+00 -7.30506028e+00
-4.58811210e+00 1.40758365e+09]
Desvio da solução: 9.283233838012651e-07

--- Resultados para k = 2.0 ---


Solução aproximada: [-1.75889094e+07 6.29364423e+00 1.00206976e+01 9.66112167e+00
5.36163763e+00 -1.12441060e+00 -7.15191025e+00 -1.02627476e+01
-9.18831592e+00 -4.36673667e+00 2.23563282e+00 7.92630585e+00
1.03844956e+01 8.60776987e+00 3.32065092e+00 -3.32065106e+00
-8.60776981e+00 -1.03844947e+01 -7.92630858e+00 -2.23562797e+00
4.36673017e+00 9.18832349e+00 1.02627385e+01 7.15192259e+00
1.12439306e+00 -5.36161370e+00 -9.66115251e+00 -1.00206595e+01
-6.29368891e+00 1.75889094e+07]
Desvio da solução: 7.473162764803892e-08

--- Resultados para k = 2.3 ---


Solução aproximada: [ 8.96295970e+05 9.57188016e+00 1.52402039e+01 1.46933739e+01
8.15437372e+00 -1.71008919e+00 -1.08771537e+01 -1.56083679e+01
-1.39742670e+01 -6.64127358e+00 3.40013079e+00 1.20549078e+01
1.57935320e+01 1.30913304e+01 5.05030584e+00 -5.05030567e+00
-1.30913310e+01 -1.57935307e+01 -1.20549098e+01 -3.40012824e+00
6.64127084e+00 1.39742694e+01 1.56083665e+01 1.08771534e+01
1.71009157e+00 -8.15437840e+00 -1.46933671e+01 -1.52402126e+01
-9.57187025e+00 -8.96295970e+05]
Desvio da solução: 2.981640188754405e-09

--- Resultados para k = 2.6 ---


Solução aproximada: [ 5.96924021e+04 1.38271805e+01 2.20154410e+01 2.12255044e+01
1.17795087e+01 -2.47033115e+00 -1.57127374e+01 -2.25472707e+01
-2.01867142e+01 -9.59373639e+00 4.91170117e+00 1.74140779e+01
2.28147529e+01 1.89112547e+01 7.29548511e+00 -7.29548519e+00
-1.89112545e+01 -2.28147533e+01 -1.74140775e+01 -4.91170155e+00
9.59373680e+00 2.01867137e+01 2.25472714e+01 1.57127364e+01
2.47033252e+00 -1.17795105e+01 -2.12255020e+01 -2.20154439e+01
-1.38271772e+01 -5.96924021e+04]
Desvio da solução: 1.8229304074645285e-10

--- Resultados para k = 3 ---


Solução aproximada: [-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]
Desvio da solução: 6.733207399029602e-12

--- Resultados para k = 3.5 ---


Solução aproximada: [ 10.12795706 33.73010329 53.70460236 51.7776202 28.73500604
-6.02614172 -38.32974567 -55.0019474 -49.24359257 -23.40301729
11.98163245 42.48000698 55.65444473 46.13222898 17.79664947
-17.79664946 -46.13222899 -55.65444472 -42.48000699 -11.98163244
23.4030173 49.24359254 55.00194745 38.32974558 6.02614184
-28.7350062 -51.77762001 -53.70460258 -33.73010305 -10.12795718]
Desvio da solução: 1.9268254801781e-13

--- Resultados para k = 4 ---


Solução aproximada: [ 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 ]
Desvio da solução: 1.5169432972663809e-13

--- Resultados para k = 5 ---


Solução aproximada: [-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]
Desvio da solução: 4.5361408323041287e-13

--- Resultados para k = 6.5 ---


Solução aproximada: [ 306.43778249 216.05340862 343.98712676 331.65307722 184.04980283
-38.59345636 -245.5174064 -352.29487325 -315.42388194 -149.89555163
76.73879331 272.10131567 356.47448265 295.49397416 113.98680268
-113.98728428 -295.49256309 -356.47672979 -272.09836407 -76.74230909
149.89951209 315.4195547 352.29953675 245.51240099 38.59882203
-184.05553397 -331.64700668 -343.9934734 -216.0468818 -306.44107719]
Desvio da solução: 3.049521916353213e-13

--- Resultados para k = 8 ---


Solução aproximada: [-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]
Desvio da solução: 1.5825023009666053e-12

--- Resultados para k = 10 ---


Solução aproximada: [-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]
Desvio da solução: 1.929452497327449e-12

--- Resultados para k = 20 ---


Solução aproximada: [ 10949.53949015 9965.22338565 4375.73770137 16358.10441322
-1390.45833106 4849.11597682 -11824.12490814 -7067.86422699
-11026.00056514 -3559.95172602 2017.67562341 8037.15710563
9927.73542266 9763.23987239 1286.30842939 -451.17826139
-12047.54730064 -6780.4451516 -11295.78554688 676.14320354
1845.50510656 11671.39654165 7324.56871983 10941.05565278
-3579.15786208 -138.00421737 -14602.54888366 -6353.82027105
-7810.54135493 -12061.07803811]
Desvio da solução: 1.521942954329661e-11

--- Resultados para k = 50 ---


Solução aproximada: [ 84632.5364527 62756.36513771 119911.86335453 222972.72443061
75261.81127623 -133980.82572547 75012.1639139 -288096.61070468
-153528.30194208 34653.69265622 -51900.50251902 141050.81829563
169834.34922224 171059.32886203 -28504.54707006 5184.59571784
-125126.05646937 -194457.71439951 -152060.52208958 72942.36211717
-38420.85092719 145027.02808804 287152.06489592 -68627.61472053
158250.18089256 -150197.48182684 -138410.24909811 -135998.62036204
-151014.73343475 -15377.25402412]
Desvio da solução: 2.572689065351729e-10

--- Resultados para k = 100 ---


Solução aproximada: [-1047642.42433127 1198754.37723802 584879.73364253 1618727.09219034
460409.75418747 268745.27850576 -1567576.68607678 -675560.38491809
-1515458.26928068 -243610.86961855 -71699.90648465 1230886.09280524
1245992.81483573 1185378.53731372 26306.47981981 86092.45185094
-1328198.27924379 -1437144.40733872 -603020.15179506 -668215.85645359
661944.69573037 1527405.38040133 495568.31539767 1574010.38353347
107288.69042517 -1235536.41627195 -712076.32468896 -1118337.35989568
-1337567.16472755 1289254.42324774]
Desvio da solução: 4.248925335606488e-09

--- Resultados para k = 200 ---


Solução aproximada: [ -1530667.34465275 2626698.29492198 18065487.27119772
4888817.47732183 6665768.29796466 -3664328.57649286
-6208588.15002503 -4407422.76560693 -15837619.7088785
-6379547.2631297 8744281.72893899 7545645.93484305
7173775.14258692 3980355.90206967 16416481.10826211
-15011409.58571111 -2694177.41155057 -13531190.65030401
-6829804.58961376 -172209.03779668 1858486.28000782
9592732.94746244 10010712.17837579 7997067.67427783
2858205.0394027 -9584690.1866314 -7323408.77124175
-10520038.65885304 -3641040.83419864 -1088371.7429468 ]
Desvio da solução: 3.386067577288218e-08

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

# Função para calcular a matriz U para um determinado valor de k


def calculate_U(k):
t1_values = np.linspace(0, 2 * np.pi, 60)
t2_values = np.linspace(0, 2 * np.pi, 30)

P1 = np.array([(np.cos(t), np.sin(t)) for t in t1_values])


P2 = np.array([k * np.array([np.cos(t), np.sin(t)]) for t in t2_values])

U_matrix = np.zeros((60, 30))


for i in range(60):
for j in range(30):
r = np.linalg.norm(P1[i] - P2[j])
if r < 1e-10:
u_star = 0
else:
u_star = 1 / (2 * np.pi) * np.log(1 / r)
U_matrix[i, j] = u_star

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]

# Pontos y_i fornecidos


y_values = [(0, 0), (0.7, 0.4), (0, 0.7), (-0.7, 0.4), (-0.7, -0.4), (0, -0.7), (0.7, -0.4)]

# Conjuntos de valores de ϕ(y_i) para cada valor de k


phi_values_for_k = {}

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)

# Resolvendo o sistema triangular superior Rv = QTb usando substituição para frente


Qt_b = np.dot(Q.T, b)
v_approx = np.linalg.solve(R, Qt_b)

# Função para calcular a contribuição de cada ponto y_i


def calculate_phi(y):
contributions = []
for j in range(30):
t2 = np.arctan2(y[1], y[0]) # Cálculo de t2 para o ponto y
t2 = (t2 + 2 * np.pi) % (2 * np.pi) # Ajuste para que t2 esteja no intervalo [0, 2π]
r = np.linalg.norm(y - k * np.array([np.cos(t2), np.sin(t2)]))
if r < 1e-10:
u_star = 0
else:
u_star = 1 / (2 * np.pi) * np.log(1 / r)
contributions.append(u_star * v_approx[j])
return np.sum(contributions)

# 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

# Exibindo os conjuntos de valores de ϕ(y_i) para cada valor de k


for k, phi_values in phi_values_for_k.items():
print(f"--- Resultados para k = {k} ---")
for i, y in enumerate(y_values):
print(f"ϕ(y_{i+1}) = {phi_values[i]}")
print()
--- Resultados para k = 1.05 ---
ϕ(y_1) = 6.103515625e-05
ϕ(y_2) = -0.00390625
ϕ(y_3) = -0.00390625
ϕ(y_4) = -0.00390625
ϕ(y_5) = -0.00390625
ϕ(y_6) = -0.00390625
ϕ(y_7) = -0.00390625

--- Resultados para k = 1.1 ---


ϕ(y_1) = 0.0
ϕ(y_2) = -6.103515625e-05
ϕ(y_3) = -0.00018310546875
ϕ(y_4) = -6.103515625e-05
ϕ(y_5) = -6.103515625e-05
ϕ(y_6) = -0.00018310546875
ϕ(y_7) = -6.103515625e-05
--- Resultados para k = 1.2 ---
ϕ(y_1) = 3.814697265625e-06
ϕ(y_2) = 0.0
ϕ(y_3) = -3.0517578125e-05
ϕ(y_4) = 0.0
ϕ(y_5) = -1.52587890625e-05
ϕ(y_6) = -3.0517578125e-05
ϕ(y_7) = 0.0

--- Resultados para k = 1.3 ---


ϕ(y_1) = 0.0
ϕ(y_2) = 3.814697265625e-06
ϕ(y_3) = 1.9073486328125e-06
ϕ(y_4) = 3.814697265625e-06
ϕ(y_5) = 3.814697265625e-06
ϕ(y_6) = 1.9073486328125e-06
ϕ(y_7) = 0.0

--- Resultados para k = 1.5 ---


ϕ(y_1) = 5.960464477539062e-07
ϕ(y_2) = -2.384185791015625e-07
ϕ(y_3) = -2.384185791015625e-07
ϕ(y_4) = -1.1920928955078125e-07
ϕ(y_5) = -1.1920928955078125e-07
ϕ(y_6) = -2.384185791015625e-07
ϕ(y_7) = -2.384185791015625e-07

--- Resultados para k = 1.8 ---


ϕ(y_1) = -5.960464477539063e-08
ϕ(y_2) = 0.0
ϕ(y_3) = 0.0
ϕ(y_4) = 0.0
ϕ(y_5) = 0.0
ϕ(y_6) = 0.0
ϕ(y_7) = 0.0

--- Resultados para k = 2.0 ---


ϕ(y_1) = -2.3283064365386963e-10
ϕ(y_2) = -1.1641532182693481e-10
ϕ(y_3) = -2.3283064365386963e-10
ϕ(y_4) = -1.1641532182693481e-10
ϕ(y_5) = -1.1641532182693481e-10
ϕ(y_6) = -2.3283064365386963e-10
ϕ(y_7) = -1.1641532182693481e-10

--- Resultados para k = 2.3 ---


ϕ(y_1) = -1.4551915228366852e-11
ϕ(y_2) = -2.1827872842550278e-11
ϕ(y_3) = -1.4551915228366852e-11
ϕ(y_4) = -2.1827872842550278e-11
ϕ(y_5) = -2.1827872842550278e-11
ϕ(y_6) = -1.4551915228366852e-11
ϕ(y_7) = -2.1827872842550278e-11

--- Resultados para k = 2.6 ---


ϕ(y_1) = -7.275957614183426e-12
ϕ(y_2) = -1.8189894035458565e-12
ϕ(y_3) = -3.637978807091713e-12
ϕ(y_4) = -1.8189894035458565e-12
ϕ(y_5) = -1.8189894035458565e-12
ϕ(y_6) = -3.637978807091713e-12
ϕ(y_7) = -1.8189894035458565e-12

--- Resultados para k = 3 ---


ϕ(y_1) = 1.1368683772161603e-13
ϕ(y_2) = 1.1368683772161603e-13
ϕ(y_3) = 5.684341886080802e-14
ϕ(y_4) = 1.1368683772161603e-13
ϕ(y_5) = 1.1368683772161603e-13
ϕ(y_6) = 5.684341886080802e-14
ϕ(y_7) = 1.1368683772161603e-13

--- Resultados para k = 3.5 ---


ϕ(y_1) = 1.3322676295501878e-15
ϕ(y_2) = 8.43769498715119e-15
ϕ(y_3) = -7.327471962526033e-15
ϕ(y_4) = 8.43769498715119e-15
ϕ(y_5) = 0.0
ϕ(y_6) = -7.327471962526033e-15
ϕ(y_7) = 6.661338147750939e-16
--- Resultados para k = 4 ---
ϕ(y_1) = 3.197442310920451e-14
ϕ(y_2) = 3.552713678800501e-15
ϕ(y_3) = 2.6645352591003757e-14
ϕ(y_4) = 3.552713678800501e-15
ϕ(y_5) = 3.552713678800501e-15
ϕ(y_6) = 2.6645352591003757e-14
ϕ(y_7) = 5.329070518200751e-15

--- Resultados para k = 5 ---


ϕ(y_1) = 0.0
ϕ(y_2) = -2.842170943040401e-14
ϕ(y_3) = 0.0
ϕ(y_4) = -2.842170943040401e-14
ϕ(y_5) = -2.842170943040401e-14
ϕ(y_6) = 0.0
ϕ(y_7) = -2.842170943040401e-14

--- Resultados para k = 6.5 ---


ϕ(y_1) = -2.842170943040401e-14
ϕ(y_2) = 1.4210854715202004e-14
ϕ(y_3) = -9.947598300641403e-14
ϕ(y_4) = 1.4210854715202004e-14
ϕ(y_5) = 1.4210854715202004e-14
ϕ(y_6) = -9.947598300641403e-14
ϕ(y_7) = 1.4210854715202004e-14

--- Resultados para k = 8 ---


ϕ(y_1) = 2.2737367544323206e-13
ϕ(y_2) = 1.1368683772161603e-13
ϕ(y_3) = 5.684341886080802e-14
ϕ(y_4) = 5.684341886080802e-14
ϕ(y_5) = 5.684341886080802e-14
ϕ(y_6) = 5.684341886080802e-14
ϕ(y_7) = 1.1368683772161603e-13

--- Resultados para k = 10 ---


ϕ(y_1) = 0.0
ϕ(y_2) = -2.2737367544323206e-13
ϕ(y_3) = 2.2737367544323206e-13
ϕ(y_4) = -9.094947017729282e-13
ϕ(y_5) = -2.2737367544323206e-13
ϕ(y_6) = 2.2737367544323206e-13
ϕ(y_7) = -9.094947017729282e-13

--- Resultados para k = 20 ---


ϕ(y_1) = 3.637978807091713e-12
ϕ(y_2) = 0.0
ϕ(y_3) = 9.094947017729282e-13
ϕ(y_4) = 0.0
ϕ(y_5) = 0.0
ϕ(y_6) = 9.094947017729282e-13
ϕ(y_7) = 0.0

--- Resultados para k = 50 ---


ϕ(y_1) = 5.093170329928398e-11
ϕ(y_2) = -2.546585164964199e-11
ϕ(y_3) = 1.4551915228366852e-11
ϕ(y_4) = -2.546585164964199e-11
ϕ(y_5) = -2.546585164964199e-11
ϕ(y_6) = 1.4551915228366852e-11
ϕ(y_7) = -2.546585164964199e-11

--- Resultados para k = 100 ---


ϕ(y_1) = -1.1641532182693481e-10
ϕ(y_2) = 9.313225746154785e-10
ϕ(y_3) = 6.984919309616089e-10
ϕ(y_4) = 9.313225746154785e-10
ϕ(y_5) = 9.313225746154785e-10
ϕ(y_6) = 6.984919309616089e-10
ϕ(y_7) = 9.313225746154785e-10

--- Resultados para k = 200 ---


ϕ(y_1) = -6.51925802230835e-09
ϕ(y_2) = 1.280568540096283e-09
ϕ(y_3) = 9.313225746154785e-10
ϕ(y_4) = 1.280568540096283e-09
ϕ(y_5) = 1.280568540096283e-09
ϕ(y_6) = 9.313225746154785e-10
ϕ(y_7) = 1.280568540096283e-09
(g) Compare a resposta nos pontos do item (f) com a solução exata.

(h) Para qual r a solução foi a melhor dentro da sua comparação?


In [12]: import numpy as np

# Função para calcular a matriz U para um determinado valor de k


def calculate_U(k):
t1_values = np.linspace(0, 2 * np.pi, 60)
t2_values = np.linspace(0, 2 * np.pi, 30)

P1 = np.array([(np.cos(t), np.sin(t)) for t in t1_values])


P2 = np.array([k * np.array([np.cos(t), np.sin(t)]) for t in t2_values])

U_matrix = np.zeros((60, 30))


for i in range(60):
for j in range(30):
r = np.linalg.norm(P1[i] - P2[j])
if r < 1e-10:
u_star = 0
else:
u_star = 1 / (2 * np.pi) * np.log(1 / r)
U_matrix[i, j] = u_star

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]

# Pontos y_i fornecidos


y_values = [(0, 0), (0.7, 0.4), (0, 0.7), (-0.7, 0.4), (-0.7, -0.4), (0, -0.7), (0.7, -0.4)]

# Conjuntos de valores de ϕ(y_i) para cada valor de k


phi_values_for_k = {}

# 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)

# Resolvendo o sistema triangular superior Rv = QTb usando substituição para frente


Qt_b = np.dot(Q.T, b)
v = np.linalg.solve(R, Qt_b)

# Calculando os valores de phi para cada ponto y_i


def calculate_phi(y):
contributions = []
for j in range(30):
t2 = np.arctan2(y[1], y[0]) # Cálculo de t2 para o ponto y
t2 = (t2 + 2 * np.pi) % (2 * np.pi) # Ajuste para que t2 esteja no intervalo [0, 2π]
r = np.linalg.norm(y - k * np.array([np.cos(t2), np.sin(t2)]))
if r < 1e-10:
u_star = 0
else:
u_star = 1 / (2 * np.pi) * np.log(1 / r)
contributions.append(u_star * v[j])
return np.sum(contributions)

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

# Exibindo os resultados apenas para o melhor valor de k


for i, y in enumerate(y_values):
print(f"Para o ponto y_{i+1}:")
print(f"Valor calculado de ϕ(y_{i+1}) = {best_phi_values[i]}")
print(f"Valor exato de ϕ(y_{i+1}) = {exact_solution(np.linalg.norm(y), np.arctan2(y[1], y[0]))}")
print(f"Erro absoluto = {best_errors[i]}")
print(f"Valor de r = {np.linalg.norm(y)}")
print()

print(f"O valor de k que resulta no menor erro absoluto médio é: {best_r}")


Para o ponto y_1:
Valor calculado de ϕ(y_1) = 3.814697265625e-06
Valor exato de ϕ(y_1) = 0.0
Erro absoluto = 3.814697265625e-06
Valor de r = 0.0

Para o ponto y_2:


Valor calculado de ϕ(y_2) = 0.0
Valor exato de ϕ(y_2) = 0.5239999999999999
Erro absoluto = 0.5239999999999999
Valor de r = 0.8062257748298549

Para o ponto y_3:


Valor calculado de ϕ(y_3) = -3.0517578125e-05
Valor exato de ϕ(y_3) = -0.3429999999999999
Erro absoluto = 0.3429694824218749
Valor de r = 0.7

Para o ponto y_4:


Valor calculado de ϕ(y_4) = 0.0
Valor exato de ϕ(y_4) = 0.5239999999999999
Erro absoluto = 0.5239999999999999
Valor de r = 0.8062257748298549

Para o ponto y_5:


Valor calculado de ϕ(y_5) = -1.52587890625e-05
Valor exato de ϕ(y_5) = -0.5239999999999999
Erro absoluto = 0.5239847412109374
Valor de r = 0.8062257748298549

Para o ponto y_6:


Valor calculado de ϕ(y_6) = -3.0517578125e-05
Valor exato de ϕ(y_6) = 0.3429999999999999
Erro absoluto = 0.3430305175781249
Valor de r = 0.7

Para o ponto y_7:


Valor calculado de ϕ(y_7) = 0.0
Valor exato de ϕ(y_7) = -0.5239999999999999
Erro absoluto = 0.5239999999999999
Valor de r = 0.8062257748298549

O valor de k que resulta no menor erro absoluto médio é: 1.2


In [ ]:

Você também pode gostar