Last active
September 21, 2018 08:54
-
-
Save kmahyyg/539eac7166e47166282ea819ef84d646 to your computer and use it in GitHub Desktop.
YNU-Phyexp1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- encoding: utf-8 -*- | |
import math | |
import subprocess | |
import os | |
import time | |
def clipcopy(text): | |
with open(os.devnull, 'wb') as devnull: | |
pipe = subprocess.Popen('clip', stdin=subprocess.PIPE, stderr=devnull) | |
pipe.communicate(text) | |
if pipe.returncode: | |
return False | |
else: | |
return True | |
def main(): | |
print("1. Average Calculate " + "2. Variance Calculate " + "3. Calculate V0 in theory") | |
print("This Program will calculate all of the above datas") | |
allv0intheory = [] | |
caldata = input("Input all X0 data, Seperate with space: ") | |
caldata = caldata.split(' ') | |
caldata2 = [] # save all x0 original data | |
for i in range(0,len(caldata)): | |
caldata2.append(float(caldata[i])) | |
caldata3 = input("Input the corresponding angles in degrees, seperate with space: ") | |
caldata3 = caldata3.split(' ') | |
if len(caldata) == len(caldata3): | |
pass | |
else: | |
raise BaseException("Input Data Error!") | |
caldata4 = [] # save all angle original data | |
for i in range(0,len(caldata)): | |
caldata4.append(float(caldata3[i])) | |
if len(caldata2) == len(caldata4): | |
for i in range(0,len(caldata2)): | |
tempv0 = cal_v0theory(caldata2[i],caldata4[i]) | |
allv0intheory.append(tempv0) | |
allv0_ppr = [] | |
for i in range(0,len(allv0intheory)): | |
tempvar1 = round(allv0intheory[i],3) | |
clipcopy(str(tempvar1).encode()) | |
print("This v0 data is copied to clipboard. All data will auto-copy. You have 3s to paste it.") | |
time.sleep(5) | |
allv0_ppr.append(tempvar1) | |
print("All V0 in theory: ", end='') | |
print(allv0_ppr) | |
avgshere = average_calc(allv0intheory) | |
varihere = variance_calc(allv0intheory, avgshere) | |
clipcopy(str(round(avgshere,3)).encode()) # use round() to cut off and copy result in %.3f | |
print("Average Number is: " + str(avgshere) + " (Already copied to your clipboard)") | |
input("Press any key to copy next data") | |
clipcopy(str(round(varihere,4)).encode()) | |
print("Variance is: " + str(varihere) + " (Already copied to your clipboard)") | |
time.sleep(5) | |
else: | |
raise BaseException("Angle data is NOT correspoding to X0 data!") | |
# use x0= square(v0) * sin2A / G | |
def cal_v0theory(x0value,angleX): | |
gravity_const = 9.8 | |
v0val = 0.0 | |
try: | |
angleX_rad = math.radians(angleX) | |
tempvar = (gravity_const * x0value) / (math.sin(2*angleX_rad)) | |
v0val = math.sqrt(tempvar) | |
except: | |
print("The data you entered may result in 0 or a negative number, which could not be sqrted. Will return 65535 as an error.") | |
v0val = 65535 | |
return v0val | |
# calculate average number | |
def average_calc(datas): | |
if isinstance(datas,list): | |
sums = 0.00 | |
avergs = 0.00 | |
length = len(datas) | |
for i in range(0,length): | |
sums += datas[i] | |
avergs = sums / length | |
return avergs | |
else: | |
raise TypeError | |
# single variable variance, not cooperated one | |
def variance_calc(datas,aveg): | |
if isinstance(datas,list): | |
length = len(datas) | |
sum1 = 0.0 | |
sum2 = 0.0 | |
for i in range(0,length): | |
tempvar1 = (datas[i] - aveg) ** 2 | |
sum1 += tempvar1 | |
sum2 = sum1 / length | |
return sum2 | |
else: | |
raise TypeError | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- encoding:utf-8 -*- | |
# tutorial here: | |
# https://wizardforcel.gitbooks.io/matplotlib-user-guide/3.1.html | |
import matplotlib.pyplot as plt | |
import random,string | |
spdA=[99.857, 100.029, 100.007, 100.021, 99.96, 100.038, 99.97, 100.029, 99.97, 100.007] | |
baseA=100 | |
spdB=[200.215, 200.107, 199.976, 200.025, 200.006, 200.014, 200.009, 200.033, 200.059, 200.066] | |
baseB=200 | |
spdC=[300.048, 300.071, 300.225, 300.019, 300.24, 299.933, 300.528, 300.103, 299.931, 300.038] | |
baseC=300 | |
spdD=[399.964, 400.312, 400.143, 400.33, 400.082, 400.105, 399.957, 400.104, 400.143, 400.105] | |
baseD=400 | |
spdE=[499.885, 500.537, 500.265, 500.742, 499.632, 500.138, 500.272, 499.998, 499.974, 500.086] | |
baseE=500 | |
def exp1dtaimg(spd0,basedata): | |
plt.figure(1) | |
plt.title('Speed v0') | |
plt.xlabel('Calculate times') | |
plt.ylabel('Speed (m/s)') | |
xdata = [kt for kt in range(1,len(spd0)+1)] | |
if isinstance(spd0,list) and isinstance(basedata,int): | |
plt.axis([0.5,10.5,basedata-0.3,basedata+0.4]) | |
plt.plot(xdata,spd0,'bo-',label='Speed V0') # plot(x,y,format_str) | |
for a,b in zip(xdata,spd0): | |
plt.text(a,b,(a,b), ha='left',va='bottom', fontsize=8) | |
randfilename = ''.join(random.choices(string.ascii_uppercase + string.digits, k=4)) | |
randfilename += '.png' | |
plt.legend() | |
plt.savefig(randfilename,dpi=350) | |
plt.show() | |
else: | |
raise TypeError("I need a list!") | |
exp1dtaimg(spdA,baseA) | |
exp1dtaimg(spdB,baseB) | |
exp1dtaimg(spdC,baseC) | |
exp1dtaimg(spdD,baseD) | |
exp1dtaimg(spdE,baseE) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment