Lesson Wednesday September 18th
During today’s lesson you’ll work on a complex exercise on the topic of SymPy. Please ask your questions regarding the homework as well!
Exercise SymPy¶
Given the following structure.
- Find the normal force distribution
Given the following structure.
- Find the bending moment distribution
- Find the shear force distribution
- Find the displacement distribution
If you don’t have Python and SymPy installed, click rocket --> Live Code to activate live coding and use the cells below:
import sympy as sym
sym.init_printing()x = sym.symbols('x')
C1, C2, C3, C4 = sym.symbols('C1 C2 C3 C4')q = V = sym.integrate(-q,x)+C1
M = sym.integrate(V,x)+C2
kappa = M / EI
phi = sym.integrate(kappa,x)+C3
w = sym.integrate(-phi,x)+C4Eq1 = sym.Eq(sol = sym.solve((Eq1,Eq2,Eq3,Eq4,Eq5,Eq6,Eq7,Eq8,Eq9,Eq10,Eq11,Eq12), (C1,C2,C3,C4,C5,C6,C7,C8,C9,C10,C11,C12))
display(sol)M.subs(sol)import numpy as np
import matplotlib.pyplot as plt
%config InlineBackend.figure_formats = ['svg']
def plot(w_list, x_range_list,ylabel):
plt.figure()
for i, w in enumerate(w_list):
# check if x is the only symbol in the expression
if len(w.free_symbols) > 1:
raise ValueError('The expression must be a function of x only.')
w_numpy = sym.lambdify(x, w)
x_vals = np.linspace(x_range_list[i][0], x_range_list[i][1], 100)
# if the expression is a constant, we need to make sure that it is broadcasted correctly
if isinstance(w_numpy(x_vals),float) or isinstance(w_numpy(x_vals),int):
w_numpy = np.vectorize(w_numpy)
plt.plot([x_range_list[i][0], x_range_list[i][1]],[w_numpy(x_vals),w_numpy(x_vals)])
else:
plt.plot(x_vals,w_numpy(x_vals))
plt.plot(x_vals,w_numpy(x_vals))
plt.xlabel('$x$')
plt.ylabel(ylabel)
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.spines['bottom'].set_position('zero')
ax.spines['left'].set_position('zero')
ax.invert_yaxis()plot(