' class'所需的指导任务(初学者)

时间:2015-12-14 21:29:08

标签: python python-2.7 class

它看起来令人生畏,但请耐心等待,它并不像它看起来那么困难。我这里有一个关于光束偏转的代码。在这一点上,它只是一些数学和数字。只有最后一部分需要注意。

class beam(object):
"""This class is models the deflection of a simply supported beam under
multiple point loads, following Euler-Bernoulli theory and the principle of
superposition.
"""

    def __init__(self, E, I, L):
        """The class costructor.
        """
        self.E = 8.0E9  # Young's modulus of the beam in N/m^2
        self.I = 1.333E-4  # Second moment of area of the beam in m^4
        self.L = 5.0  # Length of the beam in m
        self.Loads = [(0.0, 0.0)]  # the list of loads applied to the beam
        self.name = "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 __ init __"和" def setLoads"给出了,所以上面不需要改变。我为self.E,I和L输入了值,因为我认为我需要它们,但这些数字可以替换回它们以前的字母。

    def beamDeflection(self, Load, x):
        """Just a simple calculation, really.
        """
        E = 8.09 * (10 ** 9)
        I = 1.333 * (10 ** -4)
        L = 5
        a = 2.5
        b = a + (x - a)
        (P1, a) = Load
        if 0 <= x <= 2.5:
            beamDeflection = ((P1*b*x)/(6*L*E*I))*((L**2)-(x**2)-(b**2))
        else:
            if 2.5 < x <= 5:
                beamDeflection = ((P1*b)/(6*L*E*I)) / (((L/b)*((x-a)**3)) -
                                                       (x**3) + (x*((L**2) -
                                                                 (b**2))))
        return beamDeflection

以上&#34; beamDeflection&#34;是我输入的简单代码,它只使用已经给出的公式计算梁中的偏差。基本上,如果在梁的左侧放置一个重量,它将计算出一个数字,而另一侧则相同。

    def getTotalDeflection(self, x):
        """The function getTotalDeflection(self, x) should go through each load tuple in the
        list.Loads and calculate the beam deflection at point x (Hint: the function you just 
        created could be handy here!). getTotalDeflection should return the total deflection at x, 
        which is the sum over each of the individual deflections.
        """

我的理解是我需要一个&#34; for&#34;循环遍历每个加载元组,同时涉及self.load。我不确定如何将这两种东西结合在一起。如果有人能帮助我,我真的非常感激。

2 个答案:

答案 0 :(得分:3)

您正在寻找的可能是这个(否则请澄清):

def getTotalDeflection(self, x):
    return sum(self.beamDeflection(loadval, x) for loadval in self.Loads)

答案 1 :(得分:3)

我认为这就是你想要的:

def getTotalDeflection(self, x):
  total = 0
  # For every load in `Loads`
  for load in self.Loads:
    # Compute the deflection and add it to the total
    total += self.beamDeflection(load, x):
  return total