【发布时间】:2015-12-15 19:52:32
【问题描述】:
from sympy.mpmath import *
我正在构建一个梁模型,但在最后一部分 - getSlope 中遇到了一些问题。否则,其余的应该没问题。
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
上面已经给出,不需要任何调整。
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
上面的函数'beamDeflection'是我做的一些简单的硬编码,如果一个负载放在左侧,那么使用某个公式,如果负载在另一侧,那么使用了不同的公式。
def getTotalDeflection(self, x):
"""A superposition of the deflection.
"""
return sum(self.beamDeflection(loadall, x) for loadall in self.Loads)
'getTotalDeflection' 计算在其上放置多个载荷时的总挠度。
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)
对于这个函数,我应该区分“beamDeflection”或“v”,因为它在多个负载下时定义它,然后输入 x 的值以找到梯度/斜率。
我正在关注这个:“http://docs.sympy.org/dev/modules/mpmath/calculus/differentiation.html”来区分,但它需要第二个参数(看起来是一个整数)才能工作,所以我认为这不是区分它的正确方法。请问有人能解释一下吗?
【问题讨论】:
-
看起来该函数采用了您正在区分的函数以及您要计算导数的点。所以最后一行应该类似于
diff(self.beamDeflection, x)。在您的情况下,lambda可能是不必要的 -
@JCV:谢谢指点! :)
-
@Reti:也谢谢你!我也会试试那个方法:)
-
@Reti43,好点子。我错过了。所以函数调用应该看起来像
diff(lambda y: self.beamDeflection(load, y), x) -
@Reti43:我已将您的方法实现到代码中,如已编辑主帖的最后两行所示。但是,我似乎在导入时遇到了问题,如下所示:imgur.com/a/pC7wM 你知道我该如何解决这个问题吗?
标签: python sympy differentiation