import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
set(style="ticks", font_scale=1.5)
sns.from sympy import symbols, diff, solve
from sympy.abc import x
= 122.0 # mm
L = 3.31 # mm
R = 14 # g
Mcan = 1.026 # g/mL
rho_soda = Mcan / (rho_soda * np.pi * R**2)
C = np.linspace(0, L, 1001)
h
def RCM(H):
return (C*L/2 + H**2/2) / (C+H)
= RCM(h)
Rcm
# Convert the function to a symbolic expression
= RCM(x)
expr # Take the derivative
= diff(expr, x)
f_prime # Solve for x where the derivative equals zero
= solve(f_prime, x)
solutions # Print the solutions
print(solutions)
# choose the physically relevant solution
= solutions[1] h_min
[-7.36225033226790, 6.56937517541552]
= plt.subplots(1)
fig, ax =2)
ax.plot(h, Rcm, lwf"can is less likely to topple when\nlevel = {h_min:.1f} mm",
ax.annotate(=(h_min, RCM(h_min)), xycoords='data',
xy=(15, 50), textcoords='data',
xytext=16,
size=dict(arrowstyle="->",
arrowprops="arc3",
connectionstyle="black"),
color
)set(
ax.="soda level (mm)",
xlabel="center of mass (mm)",
ylabel=[0,L/2+5],
ylim="center of mass of a can of soda partially full")
title"can_graph.png", bbox_inches='tight', dpi=300) fig.savefig(