Home > Software engineering >  Implementing For loops correctly
Implementing For loops correctly

Time:10-14

I am currently needing some brushing up on my python... for some reason, I have had troubles converting this lengthy code into a simple for loop that returns the values in an array.

I am creating a code that solves direct stress for a beam of the unsymmetrical cross section. Coding up the correct answers isn't hard and I had no problem with but I know I can utilize a for loop to cut this code down to a few lines.

Here is the code I wrote up:

Ixx = 5.6667 * 10**5 
Iyy = 1.493 * 10**5
Ixy = - .8 * 10**5
x = 66.7 
y = -50
Sy = np.array([400, 600, 800, 1000, 1200, -400, -800,-1000, -1200])  
Sx = np.array([600, 600, 400, -400, -600, 600, 400, -400, 600])
Mx = -Sx * 1000
My = Sy * 1000
DEN = (Ixx * Iyy) - (Ixy**2)


Sigma0 = (((((My[0] * Ixx) - (Mx[0]*Ixy)) * x)   ((Mx[0] * Iyy) - (My[0] * Ixy)) * y)) / DEN
print(Sigma0, 'MPa')

Sigma1 = (((((My[1] * Ixx) - (Mx[1]*Ixy)) * x)   ((Mx[1] * Iyy) - (My[1] * Ixy)) * y)) / DEN
print(Sigma1, 'MPa')

Sigma2 = (((((My[2] * Ixx) - (Mx[2]*Ixy)) * x)   ((Mx[2] * Iyy) - (My[2] * Ixy)) * y)) / DEN
print(Sigma2, 'MPa')

Sigma3 = (((((My[3] * Ixx) - (Mx[3]*Ixy)) * x)   ((Mx[3] * Iyy) - (My[3] * Ixy)) * y)) / DEN
print(Sigma3, 'MPa')

Sigma4 = (((((My[4] * Ixx) - (Mx[4]*Ixy)) * x)   ((Mx[4] * Iyy) - (My[4] * Ixy)) * y)) / DEN
print(Sigma4, 'MPa')

Sigma5 = (((((My[5] * Ixx) - (Mx[5]*Ixy)) * x)   ((Mx[5] * Iyy) - (My[5] * Ixy)) * y)) / DEN
print(Sigma5, 'MPa')

Sigma6 = (((((My[6] * Ixx) - (Mx[6]*Ixy)) * x)   ((Mx[6] * Iyy) - (My[6] * Ixy)) * y)) / DEN
print(Sigma6, 'MPa')

Sigma7 = (((((My[7] * Ixx) - (Mx[7]*Ixy)) * x)   ((Mx[7] * Iyy) - (My[7] * Ixy)) * y)) / DEN
print(Sigma7, 'MPa')

Sigma8 = (((((My[8] * Ixx) - (Mx[8]*Ixy)) * x)   ((Mx[8] * Iyy) - (My[8] * Ixy)) * y)) / DEN
print(Sigma8, 'MPa')

I've tried converting this code into a for loop by doing the following:

def CalcStress(Ixx, Iyy, Ixy, Mx, My, x, y, DEN):
    
         
    n = My.shape[0]
    result = np.zeros_like(Mx)
    
    for i in range(0, n-1):
        result[i] = (((((My[i] * Ixx) - (Mx[i]*Ixy)) * x)   ((Mx[i] * Iyy) - (My[i] * Ixy)) * y)) / DEN

    return result

I am trying to print the results but nothing comes up no matter where I put the print statement. My last python class was almost 2 years ago and I am attempting to brush up on it by coding up simple stress problems I know the answers to. Seems like a simple issue that I am missing but cant seem to find what. Thank you for the help in advance!

CodePudding user response:

You can use this for the loop:

for ix in range(len(Sy)):

    Sigma = (((((My[ix] * Ixx) - (Mx[ix]*Ixy)) * x)   ((Mx[ix] * Iyy) - (My[ix] * Ixy)) * y)) / DEN
    print(Sigma, 'MPa')

or if you want to use the function you would have to call it and could use this alternatively:

def CalcStress(Ixx, Iyy, Ixy, Mx, My, x, y, DEN):
    
         
    n = My.shape[0]
    result = np.zeros_like(Mx)
    
    for i in range(0, n-1):
        result[i] = (((((My[i] * Ixx) - (Mx[i]*Ixy)) * x)   ((Mx[i] * Iyy) - (My[i] * Ixy)) * y)) / DEN

    return result

xy = CalcStress(Ixx, Iyy, Ixy, Mx, My, x, y, DEN)

for element in xy:
    print (element)

CodePudding user response:

I ended up finding out what was wrong. When trying to print CalcStress, I was not including the arguments. This code I did now gives me the answers in the array I wanted.

import numpy as np


Ixx = 5.6667 * 10**5 
Iyy = 1.493 * 10**5
Ixy = - .8 * 10**5
x = 66.7 
y = -50

Sy = np.array([400, 600, 800, 1000, 1200, -400, -800,-1000, -1200])  
Sx = np.array([600, 600, 400, -400, -600, 600, 400, -400, 600])

Mx = -Sx * 1000
My = Sy * 1000


DEN = (Ixx * Iyy) - (Ixy**2)

def CalcStress(Ixx, Iyy, Ixy, Mx, My, x, y, DEN):
    
    n = My.shape[0]
    result = np.zeros_like(Mx)
    
    for i in range(0, n):
        result[i] = (((((My[i] * Ixx) - (Mx[i]*Ixy)) * x)   ((Mx[i] * Iyy) - (My[i] * Ixy)) * y)) / DEN

    return result
        
print(CalcStress(Ixx, Iyy, Ixy, Mx, My, x, y, DEN))

Results =[ 189, 275, 356, 421, 502, -156, -334, -443, -502]

CodePudding user response:

I will preffer something more readable:

def calc_stress(Ixx, Iyy, Ixy, Mx, My, x, y, DEN):
    out = []
    for xx, yy in zip(Mx, My):
        pt1 = x * (yy * Ixx - xx * Ixy)
        pt2 = y * (xx * Iyy - yy * Ixy)
        out  = [(pt1   pt2) / DEN]
    return out


res = calc_stress(Ixx, Iyy, Ixy, Mx, My, x, y, DEN)
[print(f"{x} MPa") for x in res]

OUTPUT:

189.19988203646955 MPa
275.6327039784023 MPa
356.620779869467 MPa
...
-443.05360181139974 MPa
-502.2626934989924 MPa

Version for printing output with index of original data:

[print(f"{idx}: {x} MPa") for idx, x in enumerate(aaa)]

Output:

0: 189.19988203646955 MPa
1: 275.6327039784023 MPa
2: 356.620779869467 MPa
...
7: -443.05360181139974 MPa
8: -502.2626934989924 MPa
  • Related