MMQRETA

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

MMQRETA

November 10, 2022

[1]: import math


import numpy as np
import matplotlib.pyplot as plt

[69]: yx=[20,40,60,80,100]+np.random.standard_normal(5)*5
xx=[1,2,3,4,5]
plt.plot(xx,yx,'*')

[69]: [<matplotlib.lines.Line2D at 0x7f998df43940>]

[70]: # Definindo os termos da matriz:


M11=np.zeros(5)
M12=[0]*5
M22=[0]*5
# o termo M21=M12
V1=[0]*5

1
V2=[0]*5

[71]: # Montando a matriz M:


for i in range(5):
M11[i]=(xx[i])**2
M12[i]=xx[i]
M22[i]=1
# definindo os termos do lado esquerdo do sistema
V1[i]=yx[i]*xx[i]
V2[i]=yx[i]

[72]: MM=[[0,0],[0,0]]

MM=[[math.fsum(M11),math.fsum(M12)],[math.fsum(M12),math.fsum(M22)]]
print(MM)

[[55.0, 15.0], [15.0, 5.0]]

[73]: # Calculando o determinante

detMM=np.linalg.det(MM) # função do numpy que calcula o determinante de uma␣


,→matriz

MA=[[math.fsum(V1),math.fsum(M12)],[math.fsum(V2),math.fsum(M22)]] #␣
,→susbtituino o lado esquerdo do sistem na matriz

MB=[[math.fsum(M11),math.fsum(V1)],[math.fsum(M12),math.fsum(V2)]]

detMA=np.linalg.det(MA)
detMB=np.linalg.det(MB)

[74]: #Calculando os coeficientes A e B


AA=detMA/detMM
BB=detMB/detMM
print(AA,BB)

22.703610530281985 -11.809331062947805

[78]: #Recuperando o ajuste


xx2=np.arange(0.5,5.5,0.1) #função para gerar a série np.arange(start,stop,step)
#xx2?
print(xx2)
Yx2=[0]*50
print(Yx2)
for j in range(50):
Yx2[j]=AA*xx2[j]+BB
print(Yx2)

[0.5 0.6 0.7 0.8 0.9 1. 1.1 1.2 1.3 1.4 1.5 1.6 1.7 1.8 1.9 2. 2.1 2.2

2
2.3 2.4 2.5 2.6 2.7 2.8 2.9 3. 3.1 3.2 3.3 3.4 3.5 3.6 3.7 3.8 3.9 4.
4.1 4.2 4.3 4.4 4.5 4.6 4.7 4.8 4.9 5. 5.1 5.2 5.3 5.4]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
[-0.457525797806813, 1.8128352552213851, 4.083196308249583, 6.35355736127778,
8.62391841430598, 10.894279467334176, 13.164640520362376, 15.435001573390569,
17.705362626418772, 19.975723679446972, 22.24608473247517, 24.516445785503365,
26.78680683853156, 29.057167891559764, 31.32752894458796, 33.59788999761616,
35.86825105064435, 38.13861210367256, 40.40897315670075, 42.67933420972894,
44.949695262757146, 47.22005631578535, 49.49041736881354, 51.760778421841735,
54.03113947486993, 56.30150052789814, 58.57186158092634, 60.842222633954535,
63.11258368698273, 65.38294474001093, 67.65330579303912, 69.92366684606732,
72.19402789909552, 74.46438895212373, 76.73475000515192, 79.00511105818012,
81.27547211120833, 83.54583316423651, 85.81619421726471, 88.08655527029289,
90.3569163233211, 92.6272773763493, 94.89763842937751, 97.16799948240569,
99.43836053543389, 101.7087215884621, 103.9790826414903, 106.2494436945185,
108.51980474754669, 110.79016580057488]

[79]: #Plotting the results


plt.ylabel('YX')
plt.xlabel('XX')
plt.plot(xx,yx, 'k*')
plt.plot(xx2,Yx2, '-')

[79]: [<matplotlib.lines.Line2D at 0x7f998dd19280>]

3
[ ]:

You might also like