2D数组的Python函数返回

2024-09-28 05:18:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在将一个旧的Matlab代码转换成Python脚本,以免我无法访问我的Matlab许可证

Python代码的目的是编写一个脚本,在给定指定的力要求和与力加载点的距离的情况下,计算最佳的工字梁几何体

当函数不返回任何内容,只打印“vals”(即print(vals))时,输出会显示所有可能的组合,这是需要的。但是,当我返回VAL数组(即return vals)时,输出仅为0.0。我不太清楚为什么我不能返回VAL

理想情况下,我希望实现所有可能的VAL组合的2D数组(它应该是980x980数组),然后使用不同的函数调用提取VAL的最大值

这是我的代码:

## Defining the system inputs
sigma = 100 # Max stress not allowed to exceed (N/mm^2)
F = 100 * 10**3 # Force applied ( N)
dist = [100, 1000, 2000, 3000, 4000] # Distance from force applied (mm)

t = 10 # Thickness of I-beam (mm)
y = np.zeros(1000 - 2*t) # Pre-allocating vector with zeroes
h = np.arange(1, 1001 - 2*t, 1)  # Height of I-beam
w = np.arange(1, 1001 - 2*t, 1) # width of I-beam

tol = 2.00 # tolerance for the system (how close we want our desired shear modulus to be to the limit)


# Writing the IBeam function
def IBeam(sigma, F, t, h, w, dist, tol):
    M = F * dist # calculating moment
    zShear = M / sigma # calculating shear modulus based on given conditions

    # iterating through all combinations of height and width
    for i in range(0, len(h)): # iterating through the height vector
        for j in range(0, len(w)): # iterating through the width vector
            XSA = 2*t*w[j] + t*h[i] # calculating XSA for each h/w combination   
            Ix = ((t*h[i]**3)/12) + (w[j]/12)*((2*t+h[i])**3 - h[i]**3) # calculating Ix for each h/w combination
            y = h[i]/2 + t # calculating y for each h combination 

            zInt = Ix / y # calculating zInt for each Ix/y combination

            # Check to see if our zInt < zShear since this is not ideal
            if (zInt <= zShear):
                zInt = 0
            # Check to see if our zInt >= tol * zShear since this is not ideal    
            if (zInt >= tol * zShear):
                zInt = 0
            
            # Creating the relationship between zInt and XSA
            vals = zInt / XSA 
    # return vals
    return vals

# Calling the IBeam and max function
iBeamVals = IBeam(sigma, F, t, h, w, dist[0], tol)
print(iBeamVals)

如果之前或之前有人问过这些问题,请道歉

谢谢 AJ


Tags: ofthetofordistvalsigmaeach

热门问题