# VELOCITY AND ACCELERATION OF CONNECTING ROD CRANK MECHANISM IN PYTHON

Before you start coding i highly recommend watch this video to understand how this code work

import matplotlib.pyplot as plt

from numpy import *

from ipywidgets import interactive

def f(theta,c,cR): # creating a function that give angle to crank c = crank radius  cR = connecting rod length
g = plt.figure(figsize=(20,20))
omega =20 # crank speed in radians per second

xa= 0 # starting point of x cordinate of crank

ya = 0 # starting point of y cordinate of crank

xb = c*cos(radians(theta)) # ending/starting point of x cordinate of crank/connecting rod

yb = c*sin(radians(theta))  # ending/starting point of y cordinate of crank/connecting rod

xc = xb + sqrt(cR**2-yb**2) # ending x cordinate of connecting rod

yc =0 # ending y cordinate of connecting rod

x =([xa,xb,xc])

y =([ya,yb,yc])

plt.xlabel ('distance in meter')

plt.ylabel('distance')

plt.title('connecting rod mechanism in python')

ax.plot(x,y)
print('distance of piston from crankshaft is ',xc)

#plt.axis('equal') #make length  x and y coordinate equal

##VELCOITY GRAPH PLOTTING

t = linspace(0,theta,360)

n = cR/c
velocity = omega*c*(sin(radians(t))+(sin(2*radians(t)))/(2*n)) # formula from page 524 rs khurmi theory of machine
ax2.plot(t,velocity)
plt.title('Velocity graph')
plt.xlabel('theta')
plt.ylabel('velocity')
#plt.axis('equal')
#print("velocity of crank at ",theta," is ", velocity)

##ACCELERATION GRAPH PLOTTING
t = linspace(0,theta,360)
n = cR/c            # ratio of connecting rod length / crank radius
acceleration = omega**2*c*(cos(radians(t))+(cos(2*radians(t)))/(n)) # formula from page 525 rs khurmi theory of machine
ax3.plot(t,acceleration)
plt.title('Acceleration graph')
plt.xlabel('theta')
plt.ylabel('acceleration')
#plt.axis('equal')
#print("acceleration of crank at ",theta," is ", acceleration)

#print('velocity at particular angle is ',velocity)

interactive_plot = interactive(f,theta=(0,360),c = (0,0.7),cR = (0.5,1))

interactive_plot

for better understanding of code watch this video