0% found this document useful (0 votes)
6 views6 pages

CP1 - MechComp - JupyterLab

Uploaded by

Sheen Bendon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views6 pages

CP1 - MechComp - JupyterLab

Uploaded by

Sheen Bendon
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

28/04/2024, 16:17 CP1_MechComputationSheet

1. Large Amplitude Pendulum

In [24]: import numpy as np


import math
from scipy.integrate import solve_ivp
w = 9.8995
pi = 3.14159265359

def system1(t,y): return [y[1], -(w**2)*np.sin(y[0])]

sol = solve_ivp(system1,[0,86400] ,[pi/6, 0],dense_output=True)

In [25]: t = np.linspace(0, 25, 300)


z1 = sol.sol(t)
z2 = (pi/6)*np.cos(w*t)
import matplotlib.pyplot as plt
plt.plot(t, z1[0])
plt.plot(t, z2)
plt.xlabel('t')
plt.legend(['Numerical Solution','SHO Approx'])
plt.title('Large Amplitude Pendulum')
plt.show()

localhost:8888/doc/tree/Documents/Oxford/Jupyter Notebooks/CP1_MechComputationSheet.ipynb 1/6


28/04/2024, 16:17 CP1_MechComputationSheet

In [26]: zsum = z1[0]+z2


plt.plot(t,zsum)
plt.plot(19.5,0,'ro')
plt.show()

In [ ]:

In [ ]:

In [27]: t = np.linspace(0, 86400, 1000)


z1 = sol.sol(t)
z2 = (pi/6)*np.cos(w*t)
import matplotlib.pyplot as plt
zsum = z1[0]+z2
plt.plot(t,zsum)
plt.show()

localhost:8888/doc/tree/Documents/Oxford/Jupyter Notebooks/CP1_MechComputationSheet.ipynb 2/6


28/04/2024, 16:17 CP1_MechComputationSheet

In [98]: import sympy as sym


from sympy import integrate
x = sym.Symbol('x')
k = sym.Symbol('k')
a = sym.Symbol('a')
l = sym.Symbol('l')

f = sym.symbols('f', cls = sym.Function)

equ = sym.Eq((f(x) - k)*((1 + (f(x).diff(x))**2)**-0.5), a)

#sym.pprint((f(x) - k)/((1 + (f(x).diff(x))**2)**0.5) - a)


sym.pprint(equ)
#sym.dsolve((f(x) - k)/((1 + (f(x).diff(x)**2))**0.5) - a,f(x))
#sym.dsolve((f(x) - k)/((1 + (f(x).diff(x)**2))**0.5) - a,f(x), ics = {f(

an_sol = sym.dsolve(equ,ics = {f(-l/2):0})

sym.pprint(an_sol)

-0.5
⎛ 2 ⎞
⎜⎛d ⎞ ⎟
(-k + f(x))⋅⎜⎜──(f(x))⎟ + 1⎟ = a
⎝⎝dx ⎠ ⎠
[]

localhost:8888/doc/tree/Documents/Oxford/Jupyter Notebooks/CP1_MechComputationSheet.ipynb 3/6


28/04/2024, 16:17 CP1_MechComputationSheet

In [99]: from scipy.optimize import fsolve, root_scalar

BigL = 4
l = 2

def equations(vars):
a, k = vars
eq1 = BigL - 2*a*math.sinh(l/(2*a))
eq2 = k + (a**2 + 0.25*(BigL**2))**0.5

return [eq1, eq2]

initial_guess = [5,5]

[a,k] = fsolve(equations, initial_guess)


print("a:", a, "k:", k)

a: 0.4592804301695728 k: -2.052057141879458

In [100… t = np.linspace(-1, 1, 300)


y = k + a*np.cosh(t/a)
import matplotlib.pyplot as plt
plt.plot(t, y)
plt.xlabel('t')
plt.title('catenary')
plt.show()

ymin = k + a*np.cosh(0/a)
print('lowest point of string =',ymin)

localhost:8888/doc/tree/Documents/Oxford/Jupyter Notebooks/CP1_MechComputationSheet.ipynb 4/6


28/04/2024, 16:17 CP1_MechComputationSheet

lowest point of string = -1.5927767117098852

In [127… H = sym.Symbol('H')
p = sym.Symbol('p')
u = sym.Symbol('u')

intF = sym.Symbol('intF')

intF = ((p-u)*(u**2 - 1))**-0.5


intG = u*(((p-u)*(u**2 - 1))**-0.5)

H = sym.integrate(intF, (u,1,p))/sym.integrate(intG, (u,1,p)) - 1.5

print(H)

lam_H = sym.lambdify(p, H)

sol = root_scalar(lam_H, x0 = 0)

print(sol)

pval = sol.root

print('lambda/a =', pval)

roota = 1.5*(sym.integrate(intF.subs({p:pval}), (u,1,pval))**-1)

aval = sym.N(roota**2)
lambdaval = pval*aval

localhost:8888/doc/tree/Documents/Oxford/Jupyter Notebooks/CP1_MechComputationSheet.ipynb 5/6


28/04/2024, 16:17 CP1_MechComputationSheet

print("a =", aval)


print("lambda =", lambdaval)

-1.5 + Integral(((p - u)*(u**2 - 1))**(-0.5), (u, 1, p))/Integral(u/((p -


u)*(u - 1)*(u + 1))**0.5, (u, 1, p))
converged: True
flag: converged
function_calls: 12
iterations: 6
root: 0.36373064743062805
lambda/a = 0.36373064743062805
a = 0.378224968704148 + 3.44109025401972e-9*I
lambda = 0.137572012741189 + 1.25162998596182e-9*I

localhost:8888/doc/tree/Documents/Oxford/Jupyter Notebooks/CP1_MechComputationSheet.ipynb 6/6

You might also like