Dernière activité 1 month ago

Paras aulas de física experimental.

generate.py Brut
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3from __future__ import unicode_literals
4
5import sys
6import numpy as np
7import matplotlib.mlab as mlab
8import matplotlib.pyplot as plt
9
10width = 20 # cm
11height = 15 # cm
12
13if len(sys.argv) < 4:
14 print "Usage: generate.py data.txt title output"
15 exit(1)
16
17output = sys.argv[3]
18title = sys.argv[2].decode(encoding='UTF-8',errors='strict')
19
20data = np.fromfile(sys.argv[1], sep='\n')
21
22num_bins = 28
23avg = np.average(data)
24std = np.std(data)
25
26fig, ax = plt.subplots()
27n, bins, patches = ax.hist(data, num_bins, color='burlywood', histtype='stepfilled')
28
29print "Média: %s Desvio Padrão: %s" %(avg, std)
30
31ax.axvline(avg, color='red', label="Média = %.2f s" %avg)
32ax.axvline(avg-std, color='lightblue', label="Média - Desvio Padrão = %.2f s" %(avg-std))
33ax.axvline(avg+std, color='darkblue', label="Média + Desvio Padrão = %.2f s" % (avg+std))
34
35ax.set_xlabel('Tempo (s)')
36ax.set_ylabel('Contagem (n)')
37ax.set_title(title)
38ax.set_ylim( None, n.max() * 1.2)
39
40legend = ax.legend(loc='upper left', shadow=True, prop={'size':10})
41fig.tight_layout()
42
43fig.set_size_inches(width / 2.54, height / 2.54)
44plt.savefig('%s.png' %output, dpi=100)
grafa.py Brut
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3from __future__ import unicode_literals
4
5import sys
6import numpy as np
7import matplotlib.mlab as mlab
8import matplotlib.pyplot as plt
9
10width = 20 # cm
11height = 15 # cm
12
13deltaS = 34 # metros
14
15
16if len(sys.argv) < 4:
17 print "Usage: generate.py data.txt type output"
18 exit(1)
19
20type = sys.argv[2].decode(encoding='UTF-8',errors='strict')
21output = sys.argv[3]
22
23data = np.fromfile(sys.argv[1], sep='\n')
24
25data = (2 * 34) / (data ** 2)
26
27num_bins = 12
28avg = np.average(data)
29std = np.std(data)
30
31fig, ax = plt.subplots()
32n, bins, patches = ax.hist(data, num_bins, color='burlywood', histtype='stepfilled')
33
34print "Média: %s Desvio Padrão: %s" %(avg, std)
35
36ax.axvline(avg, color='red', label="Média = %.2f m/s²" %avg)
37ax.axvline(avg-std, color='lightblue', label="Média - Desvio Padrão = %.2f m/s²" %(avg-std))
38ax.axvline(avg+std, color='darkblue', label="Média + Desvio Padrão = %.2f m/s²" % (avg+std))
39
40ax.set_xlabel('Aceleração (m/s²)')
41ax.set_ylabel('Contagem (n)')
42ax.set_title('Histograma das Acelerações Médias (%s)' %type)
43ax.set_ylim( None, n.max() * 1.2)
44
45legend = ax.legend(loc='upper left', shadow=True, prop={'size':10})
46fig.tight_layout()
47
48fig.set_size_inches(width / 2.54, height / 2.54)
49plt.savefig('%s.png' %output, dpi=100)
grafv.py Brut
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3from __future__ import unicode_literals
4
5import sys
6import numpy as np
7import matplotlib.mlab as mlab
8import matplotlib.pyplot as plt
9
10width = 20 # cm
11height = 15 # cm
12
13deltaS = 34 # metros
14
15if len(sys.argv) < 4:
16 print "Usage: generate.py data.txt type output"
17 exit(1)
18
19type = sys.argv[2].decode(encoding='UTF-8',errors='strict')
20output = sys.argv[3]
21
22data = np.fromfile(sys.argv[1], sep='\n')
23
24data = deltaS / data
25
26num_bins = 12
27avg = np.average(data)
28std = np.std(data)
29
30fig, ax = plt.subplots()
31n, bins, patches = ax.hist(data, num_bins, color='burlywood', histtype='stepfilled')
32
33print "Média: %s Desvio Padrão: %s" %(avg, std)
34
35ax.axvline(avg, color='red', label="Média = %.2f m/s" %avg)
36ax.axvline(avg-std, color='lightblue', label="Média - Desvio Padrão = %.2f m/s" %(avg-std))
37ax.axvline(avg+std, color='darkblue', label="Média + Desvio Padrão = %.2f m/s" % (avg+std))
38
39ax.set_xlabel('Velocidade (m/s)')
40ax.set_ylabel('Contagem (n)')
41ax.set_title('Histograma das Velocidades Médias (%s)' %type)
42ax.set_ylim( None, n.max() * 1.2)
43
44legend = ax.legend(loc='upper left', shadow=True, prop={'size':10})
45fig.tight_layout()
46
47fig.set_size_inches(width / 2.54, height / 2.54)
48plt.savefig('%s.png' %output, dpi=100)
molahisto.py Brut
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3from __future__ import unicode_literals
4
5import sys
6import numpy as np
7import matplotlib.mlab as mlab
8import matplotlib.pyplot as plt
9
10width = 20 # cm
11height = 15 # cm
12
13if len(sys.argv) < 4:
14 print "Usage: molahisto.py data.txt title output"
15 exit(1)
16
17type = sys.argv[2].decode(encoding='UTF-8',errors='strict')
18output = sys.argv[3]
19
20data = np.fromfile(sys.argv[1], sep='\n')
21
22num_bins = 8
23
24fig, ax = plt.subplots()
25n, bins, patches = ax.hist(data, num_bins, color='burlywood', histtype='stepfilled')
26
27ax.set_xlabel('Constante Elástica (N/m)')
28ax.set_ylabel('Contagem')
29ax.set_title('%s' %type)
30ax.set_ylim( None, n.max() * 1.2)
31
32fig.tight_layout()
33
34fig.set_size_inches(width / 2.54, height / 2.54)
35plt.savefig('%s.png' %output, dpi=100)
super.py Brut
1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3from __future__ import unicode_literals
4
5import sys
6import numpy as np
7import matplotlib.mlab as mlab
8import matplotlib.pyplot as plt
9
10
11if len(sys.argv) < 4:
12 print "Usage: generate.py data.txt type output"
13 exit(1)
14
15def GenHistogram(data, output, type, unit, title):
16 width = 20 # cm
17 height = 15 # cm
18 num_bins = min(max(len(data) / 3, 2), 12)
19 avg = np.average(data)
20 std = np.std(data)
21
22 fig, ax = plt.subplots()
23 n, bins, patches = ax.hist(data, num_bins, color='burlywood', histtype='stepfilled')
24
25 ax.axvline(avg, color='red', label="Média = %.2f %s" % (avg, unit))
26 ax.axvline(avg-std, color='lightblue', label="Média - Desvio Padrão = %.2f %s" %(avg-std, unit))
27 ax.axvline(avg+std, color='darkblue', label="Média + Desvio Padrão = %.2f %s" % (avg+std, unit))
28
29 plt.text(0.025, 0.8, r'$\sigma = %.2f$' % std, transform = ax.transAxes)
30 ax.set_xlabel('%s (%s)' %(type, unit))
31 ax.set_ylabel('Contagem (n)')
32 ax.set_title('%s' %title)
33 ax.set_ylim( None, n.max() * 1.2)
34
35 legend = ax.legend(loc='upper left', shadow=True, prop={'size':10})
36 fig.tight_layout()
37
38 fig.set_size_inches(width / 2.54, height / 2.54)
39 plt.savefig('%s.png' %output, dpi=100)
40
41def GenSigmaChart(data, output, type, std):
42 #Tstd, "%s-sigma" %(output), 'Tempo de Queda'
43 width = 20 # cm
44 height = 15 # cm
45 x = [2**i for i in range(len(data))]
46 s = std / np.sqrt(x)
47 fig, ax = plt.subplots()
48 ax.plot(x, data, label="Calculado")
49 ax.plot(x, s, label="Teórico")
50
51 ax.set_xlabel('Número de Amostras usadas na média')
52 ax.set_ylabel('Desvio Padrão')
53 ax.set_title('Desvio Padrão da Média de %s' %type)
54
55 legend = ax.legend(loc='upper right', shadow=True, prop={'size':10})
56 fig.tight_layout()
57
58 fig.set_size_inches(width / 2.54, height / 2.54)
59 plt.savefig('%s.png' %output, dpi=100)
60
61deltaS = 34 # metros
62
63type = sys.argv[2].decode(encoding='UTF-8',errors='strict')
64output = sys.argv[3]
65data = np.fromfile(sys.argv[1], sep='\n')
66dataV = deltaS / data
67dataA = (2 * deltaS) / (data ** 2)
68
69Tstd = []
70Vstd = []
71Astd = []
72
73Tavg = []
74Vavg = []
75Aavg = []
76
77for i in range(0,6):
78 n = 2**i
79 arrT = []
80 arrV = []
81 arrA = []
82
83 print "Calculando para %s" %n
84 filenameT = "o/%s-%s-t.txt" %(output, n)
85 filenameV = "o/%s-%s-v.txt" %(output, n)
86 filenameA = "o/%s-%s-a.txt" %(output, n)
87
88 print "Salvando em %s %s %s" % (filenameT, filenameV, filenameA)
89 fT = open(filenameT, "w")
90 fV = open(filenameV, "w")
91 fA = open(filenameA, "w")
92
93 for z in range(0, len(data), n):
94 avgT = 0
95 avgV = 0
96 avgA = 0
97 if z+n >= len(data):
98 break;
99 for k in range(0, n):
100 avgT += data[z+k]
101 avgV += dataV[z+k]
102 avgA += dataA[z+k]
103
104 avgT /= n
105 avgV /= n
106 avgA /= n
107
108 arrT.append(avgT)
109 arrV.append(avgV)
110 arrA.append(avgA)
111
112 fT.write("%.2f\n" %avgT)
113 fV.write("%.2f\n" %avgV)
114 fA.write("%.2f\n" %avgA)
115
116 fT.close()
117 fV.close()
118 fA.close()
119
120 npT = np.array(arrT)
121 npV = np.array(arrV)
122 npA = np.array(arrA)
123
124 Tstd.append(np.std(npT))
125 Vstd.append(np.std(npV))
126 Astd.append(np.std(npA))
127
128 print "Salvando Gráfico de Histograma do Tempo de Queda"
129 GenHistogram(npT, "%s-%s-hist" %(output, n), 'Tempo de Queda', 's', 'Histograma de Tempo de Queda Média N=%s (%s)' %(n, type))
130
131 Tavg.append(np.average(npT))
132 Vavg.append(np.average(npV))
133 Aavg.append(np.average(npA))
134
135
136print "Desenhando gráfico sigma"
137
138GenSigmaChart(Tstd, "%s-sigma" %(output), 'Tempo de Queda (%s)' %type, np.std(data))
139
140
141