Python数组变量循环实现:批量计算25组Iyy数组求助
Hey there! I get that manually defining 25 variables is a huge pain—let's fix this with loops and numpy arrays, which are perfect for this kind of repetitive calculation. I'll walk you through each step clearly, since you're new to Python.
Step 1: Set Up Your Variables and Calculate be & e Arrays
First, let's define all your base variables, then generate the be and e values in bulk instead of one by one. We'll use numpy for the h array and vectorized operations to keep things efficient.
import numpy as np # First, define your known values (replace these with your actual b and t1) b = 10 # Example value, swap with your real b t1 = 2 # Example value, swap with your real t1 # Define h as you specified h = np.arange(1, 800, 1) # Generate be array for n from 1 to 25 # Python uses 0-indexing, so be[0] corresponds to your n=1, be[1] to n=2, etc. be = np.array([b / (n + 1) for n in range(1, 26)]) # 25 elements total: b/2 to b/26 # Calculate e array: each row corresponds to eₙ for all h values # We reshape be to (25,1) so numpy can pair each be element with every h element e = (h * (h + t1)) / (2 * (be[:, np.newaxis] + h))
The [:, np.newaxis] trick lets us match the shape of be (25 elements) to h (799 elements), so numpy automatically calculates eₙ for every h value in one go—no need to loop through each h manually.
Step 2: Calculate the 25 Iyy Arrays
Now let's turn your Iyy formula into a reusable calculation that we can apply to each be[n] and e[n] with a loop:
# Initialize a list to store all 25 Iyy arrays Iyy_list = [] # Loop through each of the 25 cases for n in range(25): current_be = be[n] current_e = e[n] # Break the Iyy formula into parts for readability (you can combine them if you want) part1 = (1/12) * current_be * t1**3 + current_be * t1 * current_e**3 part2 = (1/12) * t1 * h**3 + 0.25 * t1 * h**2 - t1 * current_e * h**2 + 0.25 * (h**2) * (t1**2) + h * t1 * current_e**2 - 0.5 * current_e * h * t1**2 part3 = (2/15) * h * t1**3 + ((2/5) * h * t1) * ((t1 + 0.5*h) + (0.5*h - (current_e - 0.5*t1)))**2 # Combine all parts to get the full Iyy array for this n current_Iyy = part1 + part2 + part3 # Add the result to our list Iyy_list.append(current_Iyy) # Now you can access each Iyy array by index: # Iyy_list[0] = your original Iyy1, Iyy_list[1] = Iyy2, ..., Iyy_list[24] = Iyy25
Why This Works Better Than Manual Variables
- No repetitive code: One loop handles all 25 cases, so you don't have to copy-paste the formula 24 times.
- Faster calculations: Numpy's vectorized operations are way quicker than looping through each h value in pure Python.
- Organized storage: All your results live in a single list, so you can easily reference any Iyy array without remembering 25 separate variable names.
Troubleshooting Common Beginner Mistakes
If your earlier loop failed, it was probably due to one of these:
- Index confusion: Python starts counting at 0, but your formula uses n=1 to 25. Our code adjusts for this by generating
bewithrange(1,26)and looping throughrange(25). - Shape mismatches: Forgetting to reshape
beto pair withhwould throw a numpy error—we fixed that withbe[:, np.newaxis]. - Lost results: If you didn't append each Iyy to a list, you'd overwrite the variable each time instead of saving all 25 arrays.
内容的提问来源于stack exchange,提问作者joshua9719

