Challenge

Week 3 - Solving second order ODEs

from tkinter import W, Y
import numpy as np
import math
import matplotlib.pyplot as plt 
from scipy.integrate import odeint


def model(theta,t,b,g,l,m):
    theta1=theta[0]
    theta2=theta[1]
    dtheta1_dt=theta2
    dtheta2_dt=((b/m)*theta2)-((g/l)*math.sin(theta1))
    dtheta_dt=[dtheta1_dt,dtheta2_dt]
    return dtheta_dt
  

b=0.05
g=9.81
l=1
m=1

# initial condition
theta_0=[math.pi/2,3]

t=np.linspace(0,10,150)

#ODE
theta=odeint(model,theta_0,t,args=(b,g,l,m))


plt.plot(t,theta[:,0])
plt.plot(t,theta[:,1])
plt.xlabel('time')
plt.ylabel('plot')
plt.savefig('ode_plot')
plt.show()


# ploting on graph
x=theta[0,1]
y=theta[:,0]
ct=1

for k in y:
    x1=0 
    y1=0
    x2=l*math.sin(k)
    y2=-l*math.cos(k)
    plt.figure()
    plt.plot([-2,2],[0,0],'y',linewidth=5)
    plt.plot([x1,x2],[y1,y2],linewidth=2)
    plt.plot(x1,y1,'o',markersize=5,markerfacecolor='b')
    plt.plot(x2,y2,'o',markersize=15,markerfacecolor='w')
    plt.xlim(-2,2)
    plt.ylim(-2,0.5)
    filename='test%5d.png'%ct 
    plt.savefig(filename)
    ct=ct+1

    



# model function program
import numpy as np
import math
import matplotlib.pyplot as plt 
from scipy.integrate import odeint


def model(theta,t,b,g,l,m):
    theta1=theta[0]
    theta2=theta[1]
    dtheta1_dt=theta2
    dtheta2_dt=((b/m)*theta2)-((g/l)*math.sin(theta1))
    dtheta_dt=[dtheta1_dt,dtheta2_dt]
    return dtheta_dt

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.