from sympy.mpmath import *
I'm constructing a beam model, but I've encountered some trouble with the last part - getSlope. Otherwise though, the rest should be fine.
class beam(object):
"""Model of a beam.
"""
def __init__(self, E, I, L):
"""The class costructor.
"""
self.E = E # Young's modulus of the beam in N/m^2
self.I = I # Second moment of area of the beam in m^4
self.L = L # Length of the beam in m
self.Loads = [(0.0, 0.0)] # the list of loads applied to the beam
def setLoads(self, Loads):
'''This function allows multiple point loads to be applied to the beam
using a list of tuples of the form (load, position)
'''
self.Loads = Loads
The above doesn't need any adjustment since it was given.
def beamDeflection(self, Load, x):
"""A measure of how much the beam bends.
"""
a = 2.5
b = a + (x - a)
(P1, A) = Load
if 0 <= x <= a:
v = ((P1*b*x)/(6*self.L*self.E*self.I))*((self.L**2)-(x**2)-(b**2))
else:
if a < x <= 5:
v = ((P1*b)/(6*self.L*self.E*self.I)) * (((self.L/b)*((x-a)**3)) - (x**3) + (x*((self.L**2) - (b**2))))
return v
The above function 'beamDeflection' is some simple hardcoding that I've done, where if a single load is placed on the left hand side, then a certain formula is used and if the load is on the other side, then a different formula is used.
def getTotalDeflection(self, x):
"""A superposition of the deflection.
"""
return sum(self.beamDeflection(loadall, x) for loadall in self.Loads)
'getTotalDeflection' calculates the total deflection at a point when multiple loads are placed on it.
def getSlope(self, x):
"""Differentiate 'v' then input a value for x to obtain a result.
"""
mp.dps = 15
mp.pretty = True
theta = sympy.diff(lambda x: self.beamDeflection(self.Loads, x), x)
return theta
b = beam(8.0E9, 1.333E-4, 5.0)
b.setLoads([(900, 3.1), (700, 3.8), (1000, 4.2)])
print b.getSlope(1.0)
For this function, I'm supposed to differentiate 'beamDeflection' or 'v' as I defined it while it's under more than one load then input a value for x to find the gradient/slope.
I'm following this: "http://docs.sympy.org/dev/modules/mpmath/calculus/differentiation.html" to differentiate, but it needs a second argument (an integer it seems) for it to work, so I don't think this is the correct method of differentiating it. Could anyone shed some light on this please?