MATLAB: Complex Number Issue

complex numberimaginary numberMATLAB

I'm working on an image processing problem that keeps giving me a complex number. Is it possible to get this number in some useable form? If I solve the problem in mathematica i get s approches 0. Please see code below. I'm getting an imaginary number on spos and sneg.
Xell = (P1(1)-(XSize/2))*pixelSx; %-0.2182 Xelr = (P2(1)-(XSize/2))*pixelSx; %-.0269 Xerl = (P4(1)-(XSize/2))*pixelSx; %.8085 Xerr = (P5(1)-(XSize/2))*pixelSx; %1.0348
% Yaw (The Rotation Angle of Y-Axis, Beta)
I2 = (((Xerl – Xerr)*(Xell – Xelr))/((Xell – Xerl)*(Xelr – Xerr))) Q = 1/sqrt(I2)-1; M = -1/(2+Q);
u_num = ((Xell – Xelr)*(Xerl – Xerr)*M*(Xelr – Xerl)-(M)^2*(Xell – Xerr)*(Xelr – Xerl)^2); u_div = ((Xell – Xelr)*(M*(Xelr – Xerl) – (Xerl – Xerr))); u = u_num/u_div;
spos = (Xelr – Xell + Xerl – Xerr – Q*Xell + Q*Xelr + Q*Xerl – Q*Xerr + (4*Q^2*Xell*Xerr – 4*Q^2*Xell*Xerl + 4*Q^2*Xelr*Xerl – 4*Q^2*Xelr*Xerr – 8*Q*Xell*Xerl + 8*Q*Xell*Xerr + 8*Q*Xelr*Xerl – 8*Q*Xelr*Xerr + Xell^2 – 2*Xell*Xelr – 2*Xell*Xerl + 2*Xell*Xerr + Xelr^2 + 2*Xelr*Xerl – 2*Xelr*Xerr + Xerl^2 – 2*Xerl*Xerr + Xerr^2)^(1/2))/(Q*Xell – Q*Xelr + Q*Xerl – Q*Xerr); sneg = -(Xelr – Xell + Xerl – Xerr – Q*Xell + Q*Xelr + Q*Xerl – Q*Xerr + (4*Q^2*Xell*Xerr – 4*Q^2*Xell*Xerl + 4*Q^2*Xelr*Xerl – 4*Q^2*Xelr*Xerr – 8*Q*Xell*Xerl + 8*Q*Xell*Xerr + 8*Q*Xelr*Xerl – 8*Q*Xelr*Xerr + Xell^2 – 2*Xell*Xelr – 2*Xell*Xerl + 2*Xell*Xerr + Xelr^2 + 2*Xelr*Xerl – 2*Xelr*Xerr + Xerl^2 – 2*Xerl*Xerr + Xerr^2)^(1/2))/(Q*Xell – Q*Xelr + Q*Xerl – Q*Xerr);
Beta0neg = atand(f/((sneg-1)*u)); Beta0pos = atand(f/((spos-1)*u));

Best Answer

You do appear to have transcribed the equations from the paper, except that they call I2 "I1".
I tested under the hypothesis that the values you gave might be off by a constant multiple (e.g., wrong sign), but my tests show that there is no possible common multiple of those specific values that would not lead to a complex solution. Except that is, if they are all 0, in which case the solution is undefined.
Keeping the values you have for the first three variables and varying Xerr:
The first branch of s has a solution from Xerr = -infinity to Xerr = -0.0269 except for a singularity at -0.2182, and the first branch of s has a solution from Xerr = 5.290073505 to infinity.
The second branch of s has the same domains except that at first glance it does not have the singularity (but I might not have looked closely enough).
If I use the first two variable values, and set Xerr = Xerl + t and set t as the difference between the Xerr and Xerl values you gave, then I can solve for the locations in which s would be well-defined -- the places Xerl would have to be in order to support that roughly 0.2 difference.
The two branches of the resulting equations have solutions over the range Xerr = -.44523207408446748385 to -0.026167925986932516223 excluding -.2532 to -.2182, with a singularity near -.44450000016449370396 and another at -0.026899999936273041138 . The singularities are very close to the endpoints; one of the branches is complex between the left end-point and the left singularity.
Compare the -0.2182 to -0.0269 range of Xell and Xelr, and if we rule out the possibility of the right eye being more negative than the left eye, then we are left only with the narrow range from -0.0269 to -0.026167925986932516223 excluding the singularity at -0.026899999936273041138 . One of the branches goes to -infinity, the other branch to +infinity, converging to 23.820876156282195822 at the right-hand end-point.
Thus, we cannot say that the equations are unsolvable given those left eye coordinates and the right eye width, but the domain of solutions under those conditions is tiny and completely unrealistic, requiring the eyes to be about 0.0008 apart.
So... we are left with either the equations being wrong or the coordinates being wrong.
The coordinates are probably wrong. Look at figure 4 in the paper, showing the projections that form the four variables. Now look at the top left of that figure, and notice the compass: it shows x increasing up the page. Now look at the order of the variables: it shows Xell as the most positive, Xelr just below that, Xerl below that, and Xerr least positive. That is exactly the opposite order of increasing value compared to the coordinates you gave. Even if you use a negative Beta angle, the order would be the same unless you go right past +/- Pi/2 and exchange left and right (i.e., the picture being upside down.)
Knowing the coordinates to be in the wrong order, I tried again with some permutations of the coordinates and their signs. Unfortunately, none of the permutations I tried resulted in a real-valued s.
I would have to study further to see what the relative values would have to be to increase the eye distance. But it is late on a Friday evening, and I am becoming more convinced that the equations are probably wrong.