2015-12-14 56 views
1

看起来令人望而生畏,但请耐心等待,这并不像看起来那么困难。我在这里有一个关于光束偏转的代码。这只是一些数学和数字。只有最后一部分需要注意。“班级”任务(初学者)需要的指导

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

我的理解是,我需要一个“for”循环都要经过每个负载元组同时累及self.load。我不确定如何将这两件事结合在一起。如果有人能帮助我,我真的很感激。

+1

为什么downvotes?这个问题对于新用户来说并不可怕。问题陈述非常明确,并且包含一些自己的解决方案。 +1 – timgeb

回答

3

你要找的大概是这样的(否则请说明):

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

你在速度和风格上击败了我。 :) +1 – erip

+0

非常感谢您的帮助:)我明白这是一个简单的return语句,但“beamDeflection”没有定义出于某种原因。编辑:其实,我删除了我的“def”左侧的缩进,警告标志似乎已经消失。 – tablue

+0

@tablue应该定义它是否在同一个类中。看着你的代码,'class beam(object):'后面的所有内容似乎缺少一个缩进级别。 – timgeb

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 
+0

非常感谢你的帮助:)你的方法看起来有点熟悉,但对于“总+ = ....”,我得到一个无效的语法。我认为这可能与“load”有关,因为它是一个元组[(P1,a)= Load],但我不确定如何修复它,如果它甚至是错误的。 – tablue

+0

@tablue我忘了在方法前加上'self.'来调用它。现在应该工作。尽管如此,另一个答案却更为pythonic。现在很高兴它的作品! – erip