Skip to content

Instantly share code, notes, and snippets.

@pozitron57
Last active Jan 14, 2022
Embed
What would you like to do?
import numpy as np
def cos(x):
return round( np.cos( np.radians(x) ), 10 )
def sin(x):
return round( np.sin( np.radians(x) ), 10 )
def asin(x):
return np.degrees( np.arcsin(x) )
# Plot setup {{{
from matplotlib import pyplot as plt
from matplotlib import rc, rcParams
rcParams['font.size'] = 16.
rcParams['text.usetex'] = True
rcParams['font.sans-serif'] = ['Computer Modern']
rc('text.latex', preamble=r'\usepackage[T2A]{fontenc} \usepackage[utf8]{inputenc}')
rc('axes', linewidth=1.2)
rc('xtick.major', size=4, width=1.1)
rc('ytick.major', size=4, width=1.1)
fig, ax = plt.subplots()
fig.subplots_adjust(left=.15, bottom=.15, right=.95, top=.95)
#}}}
def sqrt(x): return x**(1./2.)
v = 20
g = 10
t = np.linspace(0,5,100)
angles = [0,20,40,50,60,asin(sqrt(8/9)),78,83,90]
for a in angles:
x = v * cos(a) * t
y = v * sin(a) * t - g*t**2 / 2
r = sqrt(x**2 + y**2)
ax.plot(t,r)
ax.grid()
ax.set_xlabel('$t$ [\\textrm{с}]')
ax.set_ylabel('$r$ [\\textrm{м}]')
ax.set_ylim([0,80])
#plt.savefig('r_t.png', bbox_inches='tight')
plt.show()
import numpy as np
# Triginometry {{{
def cos(x):
return round( np.cos( np.radians(x) ), 10 )
def sin(x):
return round( np.sin( np.radians(x) ), 10 )
def sins(x):
return round( np.sin( np.radians(x) )**2, 10 )
def asin(x):
return np.degrees( np.arcsin(x) )
#}}}
def sqrt(x): return x**(1./2.)
# Plot setup {{{
from matplotlib import pyplot as plt
from matplotlib import rc, rcParams
rcParams['font.size'] = 16.
rcParams['text.usetex'] = True
rcParams['font.sans-serif'] = ['Computer Modern']
rc('text.latex', preamble=r'\usepackage[T2A]{fontenc} \usepackage[utf8]{inputenc} \usepackage{bm}')
rc('axes', linewidth=1.2)
rc('xtick.major', size=4, width=1.1)
rc('ytick.major', size=4, width=1.1)
fig, ax = plt.subplots()
fig.subplots_adjust(left=.15, bottom=.15, right=.95, top=.95)
#}}}
# Initial parameters (SI units)
x0 = 0
y0 = 0
v0 = 20
g = 10
#a = asin(sqrt(8/9)) # 70.53 deg
t = np.linspace(0,5,100)
angles = np.linspace(90, 55, 300)
for a in angles:
# Kinematics equations
x = v0*cos(a)*t
y = v0*sin(a)*t - g*t**2/2
vx = v0*cos(a)
vy = v0*sin(a) - g*t
if a >= asin(sqrt(8/9)) and a < 88.3:
# First time when r is perpendicular to v
t1 = (3/2*v0*g*sin(a) - sqrt(9/4*v0**2*g**2*sins(a) - 2*g**2*v0**2)) / g**2
x1 = v0*cos(a)*t1
y1 = v0*sin(a)*t1 - g*t1**2/2
v1y = v0*sin(a) - g*t1
# Second time when r is perpendicular to v
t2 = (3/2*v0*g*sin(a) + sqrt(9/4*v0**2*g**2*sins(a) - 2*g**2*v0**2)) / g**2
x2 = v0*cos(a)*t2
y2 = v0*sin(a)*t2 - g*t2**2/2
v2y = v0*sin(a) - g*t2
# v1
ax.arrow(x1, y1, vx, v1y,
width=0.3,
head_length=1,
ec='None',
fc='#FF5555',
zorder=4,
length_includes_head=True)
ax.text((x1+vx)*1.06, y1+v1y, '$\\bm{v_1}$', c='#FF5555', ha='left')
# v2
ax.arrow(x2, y2, vx, v2y,
width=0.3,
head_length=1,
ec='None',
fc='#FF5555',
zorder=5,
length_includes_head=True)
ax.text((x2+vx)*1.05, y2+v2y, '$\\bm{v_2}$', c='#FF5555', ha='left')
# r1
ax.arrow(x0, y0, x1, y1,
width=0.3,
head_length=1.5,
ec='None',
fc='#3333BB',
length_includes_head=True)
ax.text((x0+x1)/2.2 , (y0+y1)/2, '$\\bm{r_1}$', c='#3333BB', ha='right')
# r2
ax.arrow(x0, y0, x2, y2,
width=0.3,
head_length=1.5,
ec='None',
fc='#3333BB',
length_includes_head=True)
ax.text((x0+x2)/1.95 , (y0+y2)/2, '$\\bm{r_2}$', c='#3333BB', ha='left', va='top')
# Final plot setup and trajectory
#ax.text(30.9, 16, '$\\alpha={}^\\circ$'.format(int(a)))
ax.plot([], [], ' ', label='$v_0={}~$'.format(v0)+'\\textrm{м/с}')
ax.plot([], [], ' ', label='$\\alpha={}^\circ$'.format( "{:.1f}".format(a)) )
ax.legend(markerscale=0, handletextpad=-2.0)
ax.grid()
ax.plot(x, y, ls='-', c='#888888', lw=1.4)
plt.axis('equal')
ax.set_axisbelow(True)
ax.set_xlim([-0,45])
ax.set_ylim([-20,25])
ax.set_xlabel('$x$ [\\textrm{м}]')
ax.set_ylabel('$y$ [\\textrm{м}]')
# Save the figure
plt.savefig('rpvt_a/' + str(a)+'.png', bbox_inches='tight')
plt.cla()
# Use imagemagick to convert *.png to *.gif:
# convert -delay 0.01 -loop 0 -reverse *.png r.gif
import numpy as np
# Triginometry {{{
def cos(x):
return round( np.cos( np.radians(x) ), 10 )
def sin(x):
return round( np.sin( np.radians(x) ), 10 )
def sins(x):
return round( np.sin( np.radians(x) )**2, 10 )
def asin(x):
return np.degrees( np.arcsin(x) )
#}}}
def sqrt(x): return x**(1./2.)
# Plot setup {{{
from matplotlib import pyplot as plt
from matplotlib import rc, rcParams
rcParams['font.size'] = 16.
rcParams['text.usetex'] = True
rcParams['font.sans-serif'] = ['Computer Modern']
rc('text.latex', preamble=r'\usepackage[T2A]{fontenc} \usepackage[utf8]{inputenc} \usepackage{bm}')
rc('axes', linewidth=1.2)
rc('xtick.major', size=4, width=1.1)
rc('ytick.major', size=4, width=1.1)
fig, ax = plt.subplots()
fig.subplots_adjust(left=.15, bottom=.15, right=.95, top=.95)
#}}}
# Initial parameters (SI units)
x0 = 0
y0 = 0
v0 = 20
g = 10
a = 75
times = np.linspace(0.001, 4.5, 200)
rs=[]
drs=[]
cmap = plt.get_cmap('coolwarm')
cmapb = plt.get_cmap('Blues')
cmapr = plt.get_cmap('Oranges')
i=0
for t in times:
x = v0*cos(a)*t
y = v0*sin(a)*t - g*t**2/2
r = sqrt(x**2+y**2)
rs = np.append(rs, r)
drs = np.append(drs, rs[i]-rs[i-1])
i+=1
drs /= np.amax(drs)
i=0
for t in times:
# Kinematics equations
x = v0*cos(a)*t
y = v0*sin(a)*t - g*t**2/2
vx = v0*cos(a)
vy = v0*sin(a) - g*t
# v
vc = '#333333'
ax.arrow(x, y, vx, vy,
width=0.3,
head_length=1,
ec='None',
fc=vc,
zorder=4,
length_includes_head=True)
ax.text((x+vx)*1.03, y+vy, '$\\bm{v}$', c=vc, ha='left')
# r
if drs[i]>=0:
fcr=cmapb(10+int(drs[i]*400))
else:
fcr=cmapr(10+int(abs(drs[i])*600))
print (int(drs[i]*256))
ax.arrow(x0, y0, x, y,
width=0.3,
head_length=1.5,
ec='None',
fc=fcr,
length_includes_head=True)
ax.text((x0+x)/2 , (y0+y)/2 - 1, '$\\bm{r}$', c=fcr, ha='center', va='top')
# Final plot setup and trajectory
times = np.linspace(0,5,100)
x = v0*cos(a)*times
y = v0*sin(a)*times - g*times**2/2
vx = v0*cos(a)
vy = v0*sin(a) - g*times
ax.plot(x, y, ls='-', c='#888888', lw=1.4)
ax.plot([], [], ' ', label='$v_0={}~$'.format(v0)+'\\textrm{м/с}')
ax.plot([], [], ' ', label='$\\alpha={}^\circ$'.format(a))
ax.plot([], [], ' ', label='$t={}~$'.format( "{:.1f}".format(t) )+'\\textrm{с}')
ax.legend(markerscale=0, handletextpad=-2.0)
ax.grid()
plt.axis('equal')
ax.set_axisbelow(True)
ax.set_xlim([-10,80])
ax.set_ylim([-55,40])
ax.set_xlabel('$x$ [\\textrm{м}]')
ax.set_ylabel('$y$ [\\textrm{м}]')
# Save the figure
plt.savefig('rv_t/' + str(a) + '_' + str(t)+'.png', bbox_inches='tight')
plt.cla()
i+=1
# Use imagemagick to convert *.png to *.gif:
# convert -delay 0.01 -loop 0 -reverse *.png r.gif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment