它看起来令人生畏,但请忍受我,它并不像看起来那样困难。我这里有关于光束偏转的代码。这只是一些数学和数字。仅最后一部分需要注意。

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


上面的“ beamDeflection”是我输入的简单代码,它仅使用已经给出的公式来计算光束的偏转。本质上,如果将重物放在梁的左侧,它将计算出一个数字,另一侧的数字相同。

    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.
        """


我的理解是,在涉及self.load的同时,我需要一个“ for”循环来遍历每个负载元组。我不确定如何将这两件事结合在一起。如果有人可以帮助我,我将非常感谢。

最佳答案

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

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

10-08 02:53