calculating the root of a complex number

17 views (last 30 days)
Hello, I have encountered a problem with the conversion of certain experimental measurements while programming in Matlab. The Task is to calculate the acoustic absorption degree in a range of 0 to 2000 Hz. The mistake happens when I am trying to solve the complex root of a (1,401)vector. It calculates one of the two possible answers but uses wrong solutions for other certain parts. Is it the Matlab problem when the angle is being displayed negative? I used atan2 but it does not help.
1) The faulty code:
H12=sqrt((Z1.*Z2)); %z1 and 2 being the (1,401)vectors
2) My detour:
angle1= atan2(imag(Z1),real(Z2));
angle2= atan2(imag(Z2),real(Z2));
Z1=(abs(Z1).*exp(1i*angle1));
Z2=(abs(Z2).*exp(1i*angle2));
H12=sqrt((Z1.*Z2));
->same output
Its a conversion of some parts of H12 into its negative counterpart -H12 but I cannot identify the pattern.
I am including a picture of what the faulty plot looks like (plot above) and what it should look like(right under it). The plot is correct for the part between x=400:880 Hz
Thanks in advance
  1 Comment
Andrew Newell
Andrew Newell on 10 Mar 2015
This is difficult to answer without knowing more about the calculation. Maybe there is some requirement such as the real part should be positive. If so, you could try simply multiplying the square root by the sign of the real part.

Sign in to comment.

Accepted Answer

Roger Stafford
Roger Stafford on 10 Mar 2015
In both your "faulty code" and your "detour" you are using matlab's 'sqrt' function to obtain 'H12', and apparently in each case the answer is not always appropriate for your computations with it. The rule followed by 'sqrt' for complex numbers is that the real part of the square root is never negative. Another way of expressing that is that the 'angle' of the square root result always falls in the interval -pi/2 < angle <= +pi/2.
Consequently in searching for a "pattern" of what your procedure considers as incorrect, you should probably be investigating the 'angle' of H12 that would be correct versus incorrect. For example, suppose it should always satisfy 0 <= angle < pi. In that particular case, the necessary correction would be this:
H12 = sqrt(Z1.*Z2);
H12 = abs(H12).*exp(1i*mod(angle(H12,pi));
We who are responding to your question cannot of course tell what values would be appropriate, but you are in a position to do so. Just make a record of the angles of H12 for those points on your plot that produce the wrong result, and devise an appropriate correction by pi in the angle of H12.
  2 Comments
A C
A C on 11 Mar 2015
Edited: A C on 11 Mar 2015
Thanks a lot ! You had a typo in your code:
H12 = abs(H12).*exp(1i*(mod(angle(H12)),pi));
for some reason I had to add a minus and it returned a correct plot
H12 = -abs(H12).*exp(1i*(mod(angle(H12)),pi));
Again, thank you :)
Roger Stafford
Roger Stafford on 11 Mar 2015
It wasn't a typo. It was my answer to a conjecture that you wanted the angle to be in the interval 0 <= angle < pi, and it succeeds in that as is. Your minus sign will correspond to the angle always being in the interval -pi < angle <= 0. In other words the imaginary part of H12 will never be positive. Is that really what is needed in your problem?

Sign in to comment.

More Answers (0)

Categories

Find more on Elementary Math in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!