0

So basically I have to: Firstly calculate a certain value 'e'

be = b/(n+1)
h=np.arange(1,800,1)
e1 = (h*(h+t1))/(2*(be[1]+h))
e2 = (h*(h+t1))/(2*(be[2]+h)) 

'e' continues up to 25. As you can imagine it is rather slow to define a new variable e#n every time. I need all this values 'e' to put in in a different formula:

Iyy1= ((1/12)*be[1]*t1**3 + be[1]*t1*e1**3) + ((1/12)*t1*h**3 + 0.25*t1*h**2 - t1*e1*h**2 + 0.25*(h**2)*(t1**2) + h*t1*e1**2 - 0.5*e1*h*t1**2)  + ((2/15)*h*t1**3 + ((2/5)*h*t1)*((t1+0.5*h)+(0.5*h-(e1-0.5*t1)))**2)

Here I need to do the same thing with a range of 'e' instead of 'be'. So in the end i would have to get 25 arrays Iyy. I've tried some thing with loops, but i couldn't solve this (I'm quite a beginnner). Can Anyone help out?

  • 3
    Look up `for` loops in Python. Then try something with it. If it doesn't work, come back and ask a more specific question, containing your trials. Your question doesn't show enough basic knowledge on programming. (No offense, I'm just talking about your question.) – Alfe Jan 03 '18 at 15:34
  • As a rule, btw, instead of one variable `e1`, a second variable `e2`, etc., you should have an array `e`, which has `e[0]`, `e[1]`, `e[2]`, etc. as elements. See [How do you create different variable names while in a loop?](https://stackoverflow.com/questions/6181935/how-do-you-create-different-variable-names-while-in-a-loop) – Charles Duffy Jan 03 '18 at 16:09
  • ...and when asking a question here about trouble you're having trying to use a loop, do show your actual attempt to use a loop and the specific way it failed. – Charles Duffy Jan 03 '18 at 16:11

1 Answers1

0

You may want to use lambda functions and list comprehension.

func = lambda x: (h*(h+t1))/(2*(be[x]+h))

long_func = lambda x: ((1/12)*be[1]*t1**3 + be[1]*t1*x**3) + ((1/12)*t1*h**3 + 0.25*t1*h**2 - t1*x*h**2 + 0.25*(h**2)*(t1**2) + h*t1*x**2 - 0.5*x*h*t1**2)  + ((2/15)*h*t1**3 + ((2/5)*h*t1)*((t1+0.5*h)+(0.5*h-(x-0.5*t1)))**2)

Iyy_list = [long_func(func(x)) for x in range(1,26)]

Iyy_list will contain your 25 values, the result of both your functions.

With range(1,26) we generate numbers from 1 to 25.

Each number generated will be applied in the first function as x (so it will be the index for be[x])

The result of this function will be applied in the longer function, in each of x's position.

The range for the index goes from 1 to 25, but i'm not sure that you're aware that in python, lists start from index 0. If you want to start from index 0, use range(25) instead of range(1, 26)

Gsk
  • 2,929
  • 5
  • 22
  • 29