Search code examples
matlabmappinganglecomplex-numberslogarithm

Matlab calculating radians from -pi to pi instead of 0 to 2pi


I am working on a problem where the pressure profile is supposed to be smooth. For this the angle should be from 0 to 2pi. But when I take the logarithm of certain complex numbers, MATLAB maps the angle to the pi to negative pi range (clockwise direction).

For example :

var_z = [20.0 + 0.6i,  20.0 - 0.6i];
za2 = -2.5000 + 0.5000i;
A = log (-(var_z-za2))   

yields A = [3.11 - 3.13i, 3.11 + 3.091i], but if the angle was only in the counterclockwise direction (i.e. [0, 2pi]) we'd get [3.11 + 3.14i, 3.11 + 3.09i] instead.

The latter result makes more sense in my situation as it prevents the pressure profiles from rapidly jumping up. Is there any way to force MATLAB to use 0 to 2pi for the radians?


Solution

  • According to the documentation of log, for complex numbers the performed computation is this:

    log(abs(z)) + 1i*angle(z)
    

    It appears we can just "redefine" this slightly by adding a 2π-modulus, then explicitly applying the above set of operations instead of the log function:

    log(abs(z)) + 1i*mod(angle(z),2*pi)
    

    Thus if we define a function handle and compare the results:

    l = @(z)log(abs(z)) + 1i*mod(angle(z),2*pi);
    

    >> log(-([20.0 + 0.6i,  20.0 - 0.6i]-(-2.5000 + 0.5000i)))
    
    ans =
    
       3.1135 - 3.1371i   3.1147 + 3.0927i
    
    >> l(-([20.0 + 0.6i,  20.0 - 0.6i]-(-2.5000 + 0.5000i)))
    
    ans =
    
       3.1135 + 3.1460i   3.1147 + 3.0927i
    

    ... we see that the desired result is achieved.