【问题标题】:How to differentiate within a class? python beginner如何区分一个类?蟒蛇初学者
【发布时间】: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


【解决方案1】:

进口

首先,进入good habit of not importing things with a star

这是 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() 这样的函数,您将不得不使用 encapsulate it in a function 或仅 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)

【讨论】:

    猜你喜欢
    • 2019-03-04
    • 1970-01-01
    • 1970-01-01
    • 2018-03-17
    • 2017-08-18
    • 1970-01-01
    • 2018-01-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多