Escolar Documentos
Profissional Documentos
Cultura Documentos
Trabalho Prático 2 Cálculo Numérico-1
Trabalho Prático 2 Cálculo Numérico-1
TRABALHO PRÁTICO 2:
RELATÓRIO FINAL
Professor:
Eduardo Camponogara
Alunos:
Bruno Forte de Lunardi Pinto - 22102525
Marcus Vinicius Novais Ferrari - 22104098
Paulo Librelotto Ferroli - 22100768
Victória De Carvalho Taveira - 21202235
Florianópolis
2023
SUMÁRIO
1 QUESTÃO 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 ITEM 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 ITEM 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.3 ITEM 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 ITEM 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4.1 Para a variável x . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.4.2 Para a variável y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.4.3 Para a variável z . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.4.4 Para fe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2 QUESTÃO 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.1 ITEM 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
2.2 ITEM 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3 QUESTÃO 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.1 ITENS 1, 2 E 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.2 ITENS 4, 5 E 6 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
4 QUESTÃO 4 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.1 ITEM 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
4.2 ITEM 2 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.2.1 Considerando N = 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4.2.2 Considerando N = 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.2.3 Considerando N = 20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.3 ITEM 3 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.3.1 Considerando N = 1 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
4.3.2 Considerando N = 10 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
4.3.3 Considerando N = 20 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
1
1 QUESTÃO 1
1.1 ITEM 1
# Carregando os dados
data = pd.read_csv(’lorenz_data.txt’, sep=", ")
1.2 ITEM 2
def Xls(A,estados):
Erro=0
b = []
k=0
while k < len(estados):
bi = estados[k][3]
b.append(bi)
k+=1
b =np.array(b)
A = np.array(A)
At = A.T
AtA = np.dot(At, A)
inversa = np.linalg.inv(AtA)
Xls(matriz_A(estados), estados)
Utilizando o método dos mínimos quadrados mostrado acima , os modelos identifi-
2
1.3 ITEM 3
Os coeficientes identificados para cada uma das variáveis foram apresentados ante-
riormente no item 2.
O método aplicado a seguir para achar o erro de x, foi também aplicado para en-
contrar o de y e z.
# Calculando o erro
for k in range(len(dxdt)):
Erro = Erro + ((1/len(dxdt))*(dxdt[k]-b[k])**2)
3
print(Erro)
É possível analisar que, ao usar um modelo polinomial com ordem maior que as
equações que definem o atrator de lorenz, foi possível alcançar todos os pontos, logo, o erro
apresentado se dá apenas por arredondamento numérico do computador e é considerado
desprezível. Diante disso, há uma grande qualidade em cada um dos modelos.
1.4 ITEM 4
xt = [x0]
k=0
i=0
t = 0
x= x0
while t < T:
x = x + dxdt[1][i]*dt
t+= dt
i+=1
xt.append(x)
xt.pop(-1)
tempo = np.arange(len(xt))*dt
# Plotagem do gráfico
plt.plot(tempo, Xr , ’r’, label=’x’)
4
EulerODEX(Xls(matriz_A(estados),estados),-8, 0.0025, 5 )
yt = [y0]
k=0
i=0
t = 0
5
y= y0
while t < T:
y = y + dydt[1][i]*dt
t+= dt
i+=1
yt.append(y)
yt.pop(-1)
tempo = np.arange(len(yt))*dt
# Plotagem do gráfico
plt.plot(tempo, Yr , ’r’, label=’y’)
plt.plot(tempo, yt, label=’ŷ’)
plt.xlabel(’t’)
plt.ylabel(’y(t)’)
plt.title(’Trajetória do sistema em y’)
plt.legend()
plt.show()
for k in range(len(Yr)):
Erro = Erro + ((1/len(Yr))*(Yr[k]-yt[k])**2)
print(Erro)
EulerODEY(Yls(matriz_A(estados),estados),8, 0.0025, 5 )
zt = [z0]
k=0
i=0
t = 0
z= z0
while t < T:
z = z + dzdt[1][i]*dt
t+= dt
i+=1
zt.append(z)
zt.pop(-1)
tempo = np.arange(len(zt))*dt
# Plotagem do gráfico
plt.plot(tempo, Zr , ’r’, label=’z’)
plt.plot(tempo, zt, label=’ẑ’)
plt.xlabel(’t’)
plt.ylabel(’z(t)’)
plt.title(’Trajetória do sistema em z’)
plt.legend()
plt.show()
for k in range(len(Zr)):
Erro = Erro + ((1/len(Zr))*(Zr[k]-zt[k])**2)
print(Erro)
EulerODEZ(Zls(matriz_A(estados),estados),27, 0.0025, 5 )
7
1.4.4 Para fe
# Plotagem do gráfico 3D
ax = fig.add_subplot(111, projection=’3d’)
ax.set_xlabel(’X’)
ax.set_ylabel(’Y’)
ax.set_zlabel(’Z’)
plt.show()
8
Figura 1 – Trajetória (indo do roxo para o amarelo) obtida por simulação da respectiva
aproximação fe.
9
2 QUESTÃO 2
2.1 ITEM 1
def V(x):
return x**4 - 8*x**3 + 17*x**2
# Plotagem do gráfico
plt.figure(figsize=(10,6))
plt.plot(x, y)
plt.xlabel(’x’)
plt.ylabel(’V(x)’)
plt.grid(True)
plt.show()
10
def ddV(x):
return 12*x**2 - 48*x + 34
A expressão analítica de
d(V (x))
dx
é 4x3 − 24x2 + 34x.
A expressão analítica de
d2 (V (x))
dx2
é 12x2 − 48x + 34.
2.2 ITEM 2
for _ in range(max_iter):
if abs(dV(x)) <= tol:
break
if ddV(x) > 0:
dx = -dV(x)/ddV(x)
else:
dx = -dV(x)
while V(x + dx) > V(x) + alpha*dV(x)*dx:
dx = dx/2
x += alpha * dx
x_values.append(x)
V_values.append(V)
return x_values
return V_values
plt.subplot(2, 2, 1)
plt.plot(x_values_2, label=’x inicial = 2’)
plt.plot(x_values_4_5, label=’x inicial = 4.5’)
plt.xlabel(’Iteração’)
plt.ylabel(’x_k’)
plt.title(’Iterações de x_k’)
plt.grid(True)
plt.subplot(2, 2, 2)
plt.plot(V_values_2, label=’V(x) para x inicial = 2’)
plt.plot(V_values_4_5, label=’V(x) para x inicial = 4.5’)
plt.xlabel(’Iteração’)
plt.ylabel(’V(x)’)
plt.title(’Iterações V(x_k)’)
plt.grid(True)
plt.show()
Figura 2 – Plotagem dos gráficos para x com valor inicial 2 (linha azul) e 4.5 (linha
laranja).
3 QUESTÃO 3
3.1 ITENS 1, 2 E 3
ax1 = fig.add_subplot(212)
ax2 = fig.add_subplot(221)
ax3 = fig.add_subplot(222)
ax1.plot(array_x, array_y)
ax1.set_title(’Plano de fase’)
ax1.set_xlabel(’x’)
ax1.set_ylabel(’y’)
ax2.plot(array_t, array_x)
ax2.set_title(’Trajetória de x(t)’)
ax2.set_xlabel(’t’)
ax2.set_ylabel(’x(t)’)
ax3.plot(array_t, array_y)
ax3.set_title(’Trajetória de y(t)’)
ax3.set_xlabel(’t’)
ax3.set_ylabel(’y(t)’)
plt.tight_layout()
plt.show()
14
3.2 ITENS 4, 5 E 6
f2k2 = (1 - x**2)*(y+k1y) - x
k2y = h*f2k2
y= y + (k1y + k2y)/2
array_x.append(x)
array_y.append(y)
array_t.append(t)
ax1 = fig.add_subplot(212)
ax2 = fig.add_subplot(221)
ax3 = fig.add_subplot(222)
ax1.plot(array_x, array_y)
ax1.set_title(’Plano de fase’)
ax1.set_xlabel(’x’)
ax1.set_ylabel(’y’)
ax2.plot(array_t, array_x)
ax2.set_title(’Trajetória de x(t)’)
ax2.set_xlabel(’t’)
ax2.set_ylabel(’x(t)’)
ax3.plot(array_t, array_y)
ax3.set_title(’Trajetória de y(t)’)
ax3.set_xlabel(’t’)
ax3.set_ylabel(’y(t)’)
plt.tight_layout()
plt.show()
4 QUESTÃO 4
4.1 ITEM 1
pi = math.pi
def f(x0,pontos):
x =x0
h = 2*pi/pontos
array_x = []
array_f = []
while x < pi:
if -pi<= x < -(pi/2):
f =0
elif -pi/2 <= x < pi/2:
f = 1
else:
f = 0
x+= h
array_x.append(x)
array_f.append(f)
plt.figure()
plt.plot( array_x, array_f)
plt.xlabel("x")
plt.ylabel("Amplitude")
plt.show()
f(-pi, 300)
18
4.2 ITEM 2
4.2.1 Considerando N = 1
Somatorio =0
I0 =0
Iak =0
Ibk =0
ak =[]
bk =[]
19
x =-T/2
h= 2*pi/pontos
array_fn =[]
array_x = []
while -T/2 <= x < T/2:
if -pi<= x < -(pi/2):
f =0
elif -pi/2 <= x < pi/2:
f = 1
else:
f = 0
I0 = I0 + f*h
x+= h
a0 = (1/T)*I0
x =-pi/2
for k in range(1,N+1):
while -pi/2 <= x < pi/2:
Iak = Iak + math.cos((2*pi*x*k)/T)*h
Ibk = Ibk + math.sin((2*pi*x*k)/T)*h
x+= h
ak.append(Iak/pi)
bk.append(Ibk/pi)
Iak = 0
Ibk = 0
x = -pi/2
x =-pi
while x<pi:
for i in range(len(ak)):
Somatorio += ak[i]*math.cos((2*pi*x*(i+1))/T)+ bk[i]*math.sin((2*pi*x*(i+1)
x+=h
fn = a0 + Somatorio
Somatorio=0
array_fn.append(fn)
array_x.append(x)
plt.figure()
plt.plot(array_x, array_fn, color=’blue’)
plt.plot(array_xf, array_f, color=’red’)
plt.xlabel("Amplitude")
plt.ylabel("x")
plt.show()
return a0, ak, bk
retangulo(300,1, 2*pi)
20
Figura 6 – Função f(x) (linha vermelha) e sua aproximação fN (x) pela série de Fourier
truncada com N = 1 (linha azul).
k a0 ak bk
1 0.5000000000000006 0.6365965011407555 -0.006666666666665967
4.2.2 Considerando N = 10
Alterando apenas a última linha do código anterior, mudando o valor de N para 10:
retangulo(300,10, 2*pi)
21
Figura 7 – Função f(x) (linha vermelha) e sua aproximação fN (x) pela série de Fourier
truncada com N = 10 (linha azul).
k a0 ak bk
1 0.5000000000000006 0.6365965011407555 -0.006666666666665967
2 -2.904464865364253e-16 -1.6579193456760779e-16
3 -0.21213677302515904 0.00666666666666591
4 9.90610260164158e-16 1.3114266264715021e-17
5 0.127207577918188 -0.006666666666665882
6 -8.282694482977909e-16 2.8989430690422677e-17
7 -0.09078272598531673 0.006666666666665867
8 5.146314172090274e-16 -4.955812198981782e-17
9 0.07052596662270395 -0.006666666666665863
10 -9.839841045777755e-16 1.7366049432643682e-16
4.2.3 Considerando N = 20
retangulo(300,20, 2*pi)
22
Figura 8 – Função f(x) (linha vermelha) e sua aproximação fN (x) pela série de Fourier
truncada com N = 20 (linha azul).
k a0 ak bk
1 0.5000000000000006 0.6365965011407555 -0.006666666666665967
2 -2.904464865364253e-16 -1.6579193456760779e-16
3 -0.21213677302515904 0.00666666666666591
4 9.90610260164158e-16 1.3114266264715021e-17
5 0.127207577918188 -0.006666666666665882
6 -8.282694482977909e-16 2.8989430690422677e-17
7 -0.09078272598531673 0.006666666666665867
8 5.146314172090274e-16 -4.955812198981782e-17
9 0.07052596662270395 -0.006666666666665863
10 -9.839841045777755e-16 1.7366049432643682e-16
11 -0.057618316406875515 0.0066666666666658665
12 6.504676067298651e-16 -8.917701060006214e-17
13 0.04866785354599036 -0.00666666666666591
14 -6.68137354960218e-16 1.8691280549920146e-16
15 -0.042091676764500166 0.006666666666665899
16 8.238520112402026e-16 -2.1866313435061677e-16
17 0.03705177555834107 -0.0066666666666658336
18 -5.963540027744094e-16 7.288771145020559e-17
19 -0.03306298238967387 0.006666666666665867
20 7.123117255361001e-16 -3.285468811581237e-16
4.3 ITEM 3
4.3.1 Considerando N = 1
ak.append(Iak/pi)
bk.append(Ibk/pi)
Iak = 0
Ibk = 0
x = -T/4
x =-T/2
while x<T/2:
for i in range(len(ak)):
Somatorio += ak[i]*math.cos((2*pi*x*(i+1))/T)+ bk[i]*math.sin((2*pi*x*(i+1))/T)
x+=h
fn = a0 + Somatorio
Somatorio=0
array_fn.append(fn)
array_x.append(x)
plt.figure()
plt.plot(array_x, array_fn, color=’blue’)
plt.plot(array_xf, array_f, color=’red’)
plt.xlabel("x")
plt.ylabel("Amplitude")
plt.show()
trapezio(300, 1, 2*pi)
25
Figura 9 – Função f(x) (linha vermelha) e sua aproximação fN (x) pela série de Fourier
truncada com N = 1 (linha azul).
k a0 ak bk
1 0.5000000000000006 0.6365965011407553 6.891201809837619e-16
4.3.2 Considerando N = 10
Alterando apenas a última linha do código anterior, mudando o valor de N para 10:
Figura 10 – Função f(x) (linha vermelha) e sua aproximação fN (x) pela série de Fourier
truncada com N = 1 (linha azul).
k a0 ak bk
1 0.5000000000000006 0.6365965011407553 6.891201809837619e-16
2 -2.5510699007571957e-16 -7.922052160698245e-17
3 -0.21213677302515907 -8.823830522532465e-16
4 9.596882007610403e-16 -2.543477430814466e-17
5 0.127207577918188 7.90721233308291e-16
6 -7.874081555150998e-16 4.0516180512566934e-17
7 -0.09078272598531668 -7.796776406643204e-16
8 4.947529504498804e-16 3.257859829971311e-17
9 0.07052596662270388 8.172258556538203e-16
10 -1.0049669306013195e-15 6.819418457651811e-17
4.3.3 Considerando N = 20
Figura 11 – Função f(x) (linha vermelha) e sua aproximação fN (x) pela série de Fourier
truncada com N = 1 (linha azul).
k a0 ak bk
1 0.5000000000000006 0.6365965011407553 6.891201809837619e-16
2 -2.5510699007571957e-16 -7.922052160698245e-17
3 -0.21213677302515907 -8.823830522532465e-16
4 9.596882007610403e-16 -2.543477430814466e-17
5 0.127207577918188 7.90721233308291e-16
6 -7.874081555150998e-16 4.0516180512566934e-17
7 -0.09078272598531668 -7.796776406643204e-16
8 4.947529504498804e-16 3.257859829971311e-17
9 0.07052596662270388 8.172258556538203e-16
10 -1.0049669306013195e-15 6.819418457651811e-17
11 -0.05761831640687539 -8.072866222742468e-16
12 6.70346073489012e-16 3.064596958701826e-17
13 0.04866785354599028 7.719471258135411e-16
14 -6.593024808450415e-16 7.523447488704933e-17
15 -0.042091676764500076 -7.597991739051735e-16
16 8.050779037454527e-16 -5.204293033471119e-17
17 0.037051775558340945 8.426261187349525e-16
18 -6.173368287979534e-16 -5.190488542666156e-17
19 -0.03306298238967372 -8.028691852166586e-16
20 7.145204440648942e-16 -1.327992015437458e-16