forked from fvasc/calculo_numerico
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknown_solution_test
79 lines (59 loc) · 1.5 KB
/
known_solution_test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Jan 13 21:18:14 2018
@author: mac
"""
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import matplotlib.pyplot as plt
import math
def gerarGrafico(X, Y, XE, YE):
plt.plot(X,Y,':',label='Primeira linha')
plt.plot(XE,YE,label='Solução exata')
plt.xlabel('Eixo X')
plt.ylabel('Eixo Y')
plt.title('Titulo do gráfico')
plt.legend()
plt.show()
def metodoDePassoUnico(X, Y, n, h):
#y_k1 = y_k + h * f(x_k, y_k)
for k in range(0, n):
t_k1 = X[k] + h
y_k1 = Y[k] + h * eulerExplicito(X[k], Y[k])
Y.append(y_k1)
X.append(t_k1)
def eulerExplicito(x_k, y_k):
# return y_k
return (math.e ** (2 * x_k)) * y_k
def gerarPontosDaSolucaoExata(XE, YE, n, h):
for k in range(0, n):
xe_k1 = XE[k] + h
ye_k1 = calcularSolucaoExata(xe_k1, h)
XE.append(xe_k1)
YE.append(ye_k1)
def calcularSolucaoExata(x, h):
#return x * (math.e ** h)
return math.e ** ((1/2) * ( (math.e ** (2*x)) - 1 ))
def main():
#Dados iniciais
t0 = 0
tf = 1
n = 32
y0 = 1
h = (tf - t0)/n
#Lista dos resultados com C.I. adicionada
X = [t0]
Y = [y0]
#metodo numerico
metodoDePassoUnico(X, Y, n, h)
#Lista com solucao exata
XE = [t0]
YE = [y0]
gerarPontosDaSolucaoExata(XE, YE, n, h)
#saida
gerarGrafico(X, Y, XE, YE)
main()