0
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 ”来区分,但它需要第二个参数(看起来是一个整数)才能工作,所以我不认为这是区分它的正确方法。任何人都可以对此有所了解吗?

4

1 回答 1

0

进口

首先,要养成不带星进口东西的好习惯

这是 sympy 包中的一个具体示例。

from sympy import *              # imports the symbol pi
>>> type(pi)                     # can be used to construct analytical expressions
<class 'sympy.core.numbers.Pi'>
>>> 2*pi
2*pi

>>> from sympy.mpmath import *   # imports a different pi and shadows the previous one
>>> type(pi)                     # floating point precision of the constant pi
<class 'sympy.mpmath.ctx_mp_python.constant'>
>>> 2*pi
mpf('6.2831853071795862')

总体而言,我建议您使用from sympy import mpmath as mp,然后您可以使用该软件包中的任何内容,例如:mp.diff()、、mp.pi等。

差异化

sympy.mpmath.diff()(或mp.diff()从现在开始)计算函数在某个点 x 的导数。您至少需要提供两个强制性参数;x 和 x 的函数,即兴趣点。

如果您的函数是类似getTotalDeflection()的,只有一个x输入,您可以按原样传递它。例如,

def getSlope(self, x):
    return mp.diff(self.getTotalDeflection, x)

但是,如果您想使用类似 的函数beamDeflection(),则必须将其封装在only 的函数中x,同时以某种方式传递另一个参数。例如,

def getSlope(self, x, load):
    f_of_x = lambda x: self.beamDeflection(load, x)
    return mp.diff(f_of_x, x)

根据您设置方法的方式beamDeflection(),参数Load是两个值的元组,即负载和位置。一个示例用途是

b.getSlope(1.0, (900, 3.1))

如果您想获得负载列表的导数,则必须为其提供列表(或元组)列表。

def getSlope(self, x, loads):
    f_of_x = lambda x: sum(self.beamDeflection(load, x) for load in loads)
    return mp.diff(f_of_x, x)

b.getSlope(1.0, [(900, 3.1), (700, 3.8)])

当然,如果您要使用的负载是存储在 中的负载self.Loads,那么您可以简单地使用

def getSlope(self, x):
    return mp.diff(self.getTotalDeflection, x)
于 2015-12-16T02:54:11.793 回答