#### Why I get wrong values from a function result?

##### Replies

###### Shiv Thapa

User

( 8 months ago )

I am new to python programming and I am trying to write a code in python that calculates the resistance and the power of a planning craft. I have to calculate some variables that are needed for the resistance and the power for the ship with a full load as well as empty. I got the right results for some values (up to lambda0 and lambda2, I check the results from python with hand calculations). But I get wrong values for trim angle calculation (tau1, tau2). For the tau calculation, I used a function that has three variable (C_l0, lambda0, Cv_si), an all variables are in a form of an array(37,)(1,)(37,) respectively. I double check the tau equation and the formula is written correctly, I think that the problem might occur from the arrays. Also, when I use the results from t_rad to the velocity formula (Vb_1) I get the following error (TypeError: only size-1 arrays can be converted to Python scalars) then I tried to pick just the first value from the t_rad array using [:1], but I'm not sure if this is correct. There is any other way which can I use the obtained t_rad without choosing only the first value of the array (and without getting the error)?

``````def lambda0(l0, Cv_si):
return ((LCG / B) / (0.75 - (1.0 / (((5.236 * Cv_si**2) / l0**2.0) + 2.4)))) - l0

l0_try = 3.0
l0 = np.zeros(len(Cv_si))
for i in range(len(Cv_si)):
l0[i] = (fsolve(lambda0, l0_try,  Cv_si[i]))
lambda0 = l0[:1]
lambda0a = l0

""" Calculations for trim anle (t), aver. bottom velocity (V1),
Raynolds Numb. (Rn), Skin friction coeff. (C_f), friction drag (D_f),
and total Resistance (D_t) """

#tau calculation for full departure
def tau(C_l0, lambda0, Cv_si):
return ((C_l0) / (0.012 * lambda0**0.5)) + ((0.0055 * lambda0**2.5) / (Cv_si**2.0))

tau1 = tau(C_l0, lambda0, Cv_si)**(1.0/1.1)
t_deg = tau1

t_rad = t_deg * (np.pi/180)

#t_deg = (C_l0 / (0.012 * lambda0**0.5) + ((0.0055 * lambda0**2.5) / (Cv_si**2)))**(1 / 1.1)
t_deg1 = t_deg[:1]   #choose the first value from t_deg array
#t_rad = t_deg * (mt.pi / 180)
t_rad1 = t_rad[:1]  #choose the first value from t_rad array

f_beta1 = (1 - 0.0065 * beta * (0.012 * lambda0**0.5 * t_rad1**1.1)**(-0.4) * mt.cos(beta))

Vb_1 = Vs_si * (np.sqrt(1 - ((0.012 * t_rad1**1.1)/(lambda0**0.5 * mt.cos(t_rad1))))) * f_beta1``````

25-Sep-2019

1 Replies

27-Aug-2019

2 Replies

06-Sep-2019

1 Replies