# Julia Complex Exponential Evaluation vs Matlab

**URL:** <https://discourse.julialang.org/t/julia-complex-exponential-evaluation-vs-matlab/57759>\
**Category:** New to Julia\
**Tags:** numerics\
**Created:** [March 22, 2021, 11:48pm UTC](https://discourse.julialang.org/t/julia-complex-exponential-evaluation-vs-matlab/57759 "2021-03-22T23:48:17Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pulpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pulpo/32/8502_2.png) [@Pulpo](https://discourse.julialang.org/u/Pulpo)\
**Post date:** [March 22, 2021, 11:48pm UTC](https://discourse.julialang.org/t/julia-complex-exponential-evaluation-vs-matlab/57759/1 "2021-03-22T23:48:17Z")

</div>

I have a vector that is 2048 that I have coded up in Julia and Matlab. The values of the vector in both Matlab is given by the following function:

> nn=2048 ;  
> h= 2 \* pi/nn;  
> vec=[cos(-pi/2),sin(-pi/2)];  
> g = x-\>exp(im \* _k \* dot(vec,x));  
> G = [g( [cos(i_ h),sin(i\* h)] ) for i=0:nn-1]

Where x is a discretion of the unit circle. I have two different values of k that I need to evaluate the function for above: 9 and 9\*im. For the real k, both Matlab and Julia seem to agree fairly well, but there seems to be an issue with the complex value of k. When I compare the outputs of G from the two languages these are the maximum errors that I observe:

> k=9 : 1.8209011484034673e-15  
> k=9\*im : 6.366462912410498e-12

So far as I can tell, the discretization of the unit circle and all other parameters are the same in both Julia and Matlab, however the error for the complex k seems to be unreasonably high. From prior checks, I am fairly sure that the Matlab code is correct, so is there any explanation for why the values differ so drastically? Or a possible fix to this?

---

<div class="post-metadata">

**Author:** ![stillyslalom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stillyslalom/32/45687_2.png) [@stillyslalom](https://discourse.julialang.org/u/stillyslalom)\
**Post date:** [March 23, 2021, 12:08am UTC](https://discourse.julialang.org/t/julia-complex-exponential-evaluation-vs-matlab/57759/2 "2021-03-23T00:08:49Z")

</div>

Can you take a look at the guidelines [here](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) to make sure your example is reproducible? In particular, you didn’t define `nn` or `h`, so we can’t reproduce the behavior you’re seeing.

---

<div class="post-metadata">

**Author:** ![Pulpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pulpo/32/8502_2.png) [@Pulpo](https://discourse.julialang.org/u/Pulpo)\
**Post date:** [March 23, 2021, 12:50am UTC](https://discourse.julialang.org/t/julia-complex-exponential-evaluation-vs-matlab/57759/3 "2021-03-23T00:50:10Z")

</div>

You are right! Sorry about that. I have included the parameters.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 23, 2021, 1:01am UTC](https://discourse.julialang.org/t/julia-complex-exponential-evaluation-vs-matlab/57759/4 "2021-03-23T01:01:56Z")

</div>

You didn’t say which element of `G` you are comparing to Matlab, or what you think the correct answer is for that element, or how you are doing the comparison.

(For example, for `k=9im`, the maximum element of `G` has an absolute value of `≈ 8103.08`. An absolute difference of `≈ 6.36646e-12` in _that_ element corresponds to a _relative_ error of about `7.9e-16`, or about 3.5[ulps](https://en.wikipedia.org/wiki/Unit_in_the_last_place), which is quite reasonable for a difference between two calculations of this sort. You really need to look at relative errors to understand accuracy.)

---

<div class="post-metadata">

**Author:** ![Pulpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pulpo/32/8502_2.png) [@Pulpo](https://discourse.julialang.org/u/Pulpo)\
**Post date:** [March 23, 2021, 1:25am UTC](https://discourse.julialang.org/t/julia-complex-exponential-evaluation-vs-matlab/57759/5 "2021-03-23T01:25:46Z")

</div>

The error I calculated was the following:

> maximum(abs.(G-GM))

Where GM is the corresponding vector calculated from Matlab. So for that entry you calculated the relative error, which looks fine, but for calculations of the sort, shouldn’t the absolute error be smaller than 6.3e-12?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 23, 2021, 4:31am UTC](https://discourse.julialang.org/t/julia-complex-exponential-evaluation-vs-matlab/57759/6 "2021-03-23T04:31:08Z")

</div>

> [@Pulpo](#):
>
> So for that entry you calculated the relative error, which looks fine, but for calculations of the sort, shouldn’t the absolute error be smaller than 6.3e-12?

No, that’s not how computer arithmetic works. The absolute error depends on the magnitude of the quantities involved — if you have quantities on the order of `1e4`, then you can’t usually expect absolute errors better than `1e4 * eps() ≈ 2e-12`. (And usually the errors will be worse than that, because errors accumulate through several calculations.)

Nor is there any reason to think that the errors are bigger in Julia than in Matlab — most likely they are _both_ off by similar amounts (perhaps in different directions).

(If you could report the value of `diff, i = findmax(abs.(G-GM))` as well as `G[i]` and `GM[i]`, this would tell us which value you are concerned about and allow us to compare _both_ the Julia and Matlab results to a more accurate `BigFloat` calculation.)

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [March 23, 2021, 8:08am UTC](https://discourse.julialang.org/t/julia-complex-exponential-evaluation-vs-matlab/57759/7 "2021-03-23T08:08:47Z")

</div>

Comparing `Float64` to `BigFloat` gives an error of 1.05e-11, which seems to indicate that the error in Matlab is in the same direction as in Julia, but half as big.

(The maximum absolute error is at `i = 539`, at an angle just past pi/2, although the relative error seems to grow with increasing `i`.)

Here’s the code that I used to compare:

```julia
function G(T, k=9*im)
    nn = 2048 
    h = 2 * T(pi)/nn
    vec = [cos(-T(pi)/2), sin(-T(pi)/2)]
    g = x -> exp(im * Complex{T}(k) * dot(vec, x))
    [g( [cos(i * h), sin(i * h)] ) for i=0:nn-1]
end

diff, i = findmax( abs.(G(Float64) .- G(BigFloat)) )

```

---

<div class="post-metadata">

**Author:** ![lobingera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lobingera/32/211_2.png) [@lobingera](https://discourse.julialang.org/u/lobingera)\
**Post date:** [March 23, 2021, 11:47am UTC](https://discourse.julialang.org/t/julia-complex-exponential-evaluation-vs-matlab/57759/8 "2021-03-23T11:47:24Z")

</div>

Just to mention this: You can get different errors via different matlab versions, too.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 23, 2021, 12:17pm UTC](https://discourse.julialang.org/t/julia-complex-exponential-evaluation-vs-matlab/57759/9 "2021-03-23T12:17:11Z")

</div>

> [@Per](#):
>
> Comparing `Float64` to `BigFloat` gives an error of 1.05e-11, which seems to indicate that the error in Matlab is in the same direction as in Julia, but half as big.

No, because for one thing you don’t know that you’re looking at the same element. The element with the biggest difference with `BigFloat`—the biggest actual error—need not be the same as the element with the biggest difference with `Matlab`.

(Also, I don’t doubt that there are some elements where the actual error of Julia is bigger than the actual error in the Matlab result…and that there will be elements where the reverse is true. Floating-point errors fluctuate with differences in algorithm etc, and can sometimes be smaller by luck.)

Overall, it looks like the relative errors of the Julia code are all within reasonable bounds here, and I’m sure the same is true of the Matlab code.

---

<div class="post-metadata">

**Author:** ![Per](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/per/32/10387_2.png) [@Per](https://discourse.julialang.org/u/Per)\
**Post date:** [March 23, 2021, 1:04pm UTC](https://discourse.julialang.org/t/julia-complex-exponential-evaluation-vs-matlab/57759/10 "2021-03-23T13:04:03Z")

</div>

True. It could also be that the error in Matlab is 1.5 times that of Julia.

Either way, it would probably be best to compute this in `BigFloat` if errors of that magnitude are a concern.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [March 23, 2021, 1:19pm UTC](https://discourse.julialang.org/t/julia-complex-exponential-evaluation-vs-matlab/57759/11 "2021-03-23T13:19:01Z")

</div>

> [@Per](#):
>
> Either way, it would probably be best to compute this in `BigFloat` if errors of that magnitude are a concern.

I think it’s more likely that there is just a misunderstanding here about floating-point arithmetic. Matlab’s algorithms are not any more accurate for this sort of thing AFAIK, and it is extremely unlikely that their code happens to work only for the precise floating-point errors made by Matlab.

---

<div class="post-metadata">

**Author:** ![Pulpo](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pulpo/32/8502_2.png) [@Pulpo](https://discourse.julialang.org/u/Pulpo)\
**Post date:** [March 25, 2021, 3:37pm UTC](https://discourse.julialang.org/t/julia-complex-exponential-evaluation-vs-matlab/57759/12 "2021-03-25T15:37:54Z")

</div>

Thank you all for responding to this. This clarifies some things! Much appreciated.
