# why am I getting imaginary values?

20 views (last 30 days)
safi58 on 13 Mar 2017
Edited: Stephen23 on 13 Mar 2017
Hi all, I have tried this one but I am not quite sure why I am getting imaginary values? The values should be approximately j_L0=-0.2 and theta1=1.13 can anyone help?
k1=0.423;
F=1.05;
gama=2.992;
theta2=0.79;
l=0.218;
M=0.575;
j_L0=(2*gama*l*tan(theta2/2) - 2*l*theta2*tan(theta2/2) + 4*k1*tan(theta2/2)*tan((k1*(gama - theta2))/2) + k1*l*theta2*tan((k1*(gama - theta2))/2) + 2*gama*l*tan(theta2/2)*tan((k1*(gama - theta2))/2)^2 - 4*l*theta2*tan(theta2/2)*tan((k1*(gama - theta2))/2)^2 - k1*l*theta2*tan(theta2/2)^2*tan((k1*(gama - theta2))/2))/(2*(k1*tan((k1*(gama - theta2))/2) + k1*tan(theta2/2)^2*tan((k1*(gama - theta2))/2))) + (((M*(l*theta2 - gama*l + 2*k1*tan(theta2/2)^2*tan((k1*(gama - theta2))/2) + gama*l*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)^2 - 2*l*theta2*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)^2 + k1*l*theta2*tan(theta2/2)*tan((k1*(gama - theta2))/2)))/(2*k1*tan((k1*(gama - theta2))/2) + 2*k1*tan(theta2/2)^2*tan((k1*(gama - theta2))/2) + M*gama*l - M*l*theta2 - 2*M*k1*tan(theta2/2)^2*tan((k1*(gama - theta2))/2) - M*gama*l*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)^2 + 2*M*l*theta2*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)^2 - M*k1*l*theta2*tan(theta2/2)*tan((k1*(gama - theta2))/2)))^(1/2)*(2*k1*tan((k1*(gama - theta2))/2) + 2*k1*tan(theta2/2)^2*tan((k1*(gama - theta2))/2) + M*gama*l - M*l*theta2 - 2*M*k1*tan(theta2/2)^2*tan((k1*(gama - theta2))/2) - M*gama*l*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)^2 + 2*M*l*theta2*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)^2 - M*k1*l*theta2*tan(theta2/2)*tan((k1*(gama - theta2))/2)))/(M*(k1*tan((k1*(gama - theta2))/2) + k1*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)))
theta1=2*atan(((M*(l*theta2 - gama*l + 2*k1*tan(theta2/2)^2*tan((k1*(gama - theta2))/2) + gama*l*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)^2 - 2*l*theta2*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)^2 + k1*l*theta2*tan(theta2/2)*tan((k1*(gama - theta2))/2)))/(2*k1*tan((k1*(gama - theta2))/2) + 2*k1*tan(theta2/2)^2*tan((k1*(gama - theta2))/2) + M*gama*l - M*l*theta2 - 2*M*k1*tan(theta2/2)^2*tan((k1*(gama - theta2))/2) - M*gama*l*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)^2 + 2*M*l*theta2*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)^2 - M*k1*l*theta2*tan(theta2/2)*tan((k1*(gama - theta2))/2)))^(1/2))
##### 2 CommentsShowHide 1 older comment
safi58 on 13 Mar 2017
i did not find any imaginary values for each individual part.

Stephen23 on 13 Mar 2017
Edited: Stephen23 on 13 Mar 2017
Because you are taking the square root of negative numbers:
>> (...
(...
M*(l*theta2 - gama*l + 2*k1*tan(theta2/2)^2*tan((k1*(gama - theta2))/2) + gama*l*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)^2 - 2*l*theta2*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)^2 + k1*l*theta2*tan(theta2/2)*tan((k1*(gama - theta2))/2))...
)...
/...
(2*k1*tan((k1*(gama - theta2))/2) + 2*k1*tan(theta2/2)^2*tan((k1*(gama - theta2))/2) + M*gama*l - M*l*theta2 - 2*M*k1*tan(theta2/2)^2*tan((k1*(gama - theta2))/2) - M*gama*l*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)^2 + 2*M*l*theta2*tan(theta2/2)^2*tan((k1*(gama - theta2))/2)^2 - M*k1*l*theta2*tan(theta2/2)*tan((k1*(gama - theta2))/2))...
)...
^(1/2)
ans =
3.3706e-017 + 0.55046i
What do you expect the square root of a negative number to give you?
By the way, finding those was easy: I simply put each group in parentheses onto its own line, and evaluated them using f9 until I find lines that generated imaginary output. Putting both of those long formulas onto one line is liable to be buggy, particularly if you get parentheses in the wrong place, and I would suggest that you split the formulas into multiple separate parts joined at the end by some simple operations and parentheses.
In particular you seem to use the same core formula as the basis for calculating both j_LO and theta, so why bother writing that twice? Split the formula in to parts using temporary variables, and make your code much simpler to check.