# Translate Matlab code to Julia code

**URL:** https://discourse.julialang.org/t/translate-matlab-code-to-julia-code/8485
**Category:** General Usage
**Created:** [January 19, 2018, 10:20pm UTC](https://discourse.julialang.org/t/translate-matlab-code-to-julia-code/8485 "2018-01-19T22:20:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)
#### Post date: [January 19, 2018, 10:20pm UTC](https://discourse.julialang.org/t/translate-matlab-code-to-julia-code/8485/1 "2018-01-19T22:20:25Z")

</div>

I am trying to translate my Matlab code to Julia code, so I can use Julia as my main tool in teaching. I ran into an issue that I could not solve.

Below is my Matlab code:

```julia
clear all
m = 0; % mean
sd= 1; %std deviation

charfunc = @(v) exp(1i*m*v-0.5.*(sd^2).*(v.^2));
M=1/(2*pi);
X=3;
a=-100;
b=100;
func2 = @(v) exp(-1i.*v*X).*charfunc(v);

res1 = M*real(quad(func2,a,b))
res2 = normpdf(X,m,sd)

```

Below is my Julia code:

```julia
using QuantEcon, Distributions
m = 0 #mean
sd = 1 #standard deviation

function charfunc(v)
    exp(im*m*v-0.5.*(sd^2).*(v.^2))
end

M = 1/(2*pi)
X = 3
a = -100
b = 100

function func2(v)
    exp(-im.*v*X).*charfunc(v)
end

nodes, weights = qnwlege(65, a, b)

integral = do_quad(func2, nodes, weights) #integration between a and b

res1 = real(integral)*M

d = Normal(0,1)

res2 = pdf(d, X)

```

I expect res1 to be equal to res2. In my Julia code, I could not get the correct answer, but I am not sure where the bug is.

---

<div class="post-metadata">

### Author: ![avik](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avik/32/17_2.png) [@avik](https://discourse.julialang.org/u/avik)
#### Post date: [January 19, 2018, 10:40pm UTC](https://discourse.julialang.org/t/translate-matlab-code-to-julia-code/8485/2 "2018-01-19T22:40:35Z")

</div>

Not sure what the `qnwlege` and `do_quad` function are, but using the basic quadrature function in base (`quadgk`, which returns a tuple of the result and the error), I do get similar values. Do you need lower error bounds?

```julia
julia> res1 = real(quadgk(func2, a, b)[1])*M
0.004431848411938444

julia> res2 = pdf(d, X)
0.0044318484119380075

```

---

<div class="post-metadata">

### Author: ![Yifan\_Liu](https://avatars.discourse-cdn.com/v4/letter/y/4da419/32.png) [@Yifan\_Liu](https://discourse.julialang.org/u/Yifan_Liu)
#### Post date: [January 19, 2018, 11:01pm UTC](https://discourse.julialang.org/t/translate-matlab-code-to-julia-code/8485/3 "2018-01-19T23:01:46Z")

</div>

Thanks so much. I did not know that the basic quadrature function is available in Julia.

---

<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: [January 19, 2018, 11:28pm UTC](https://discourse.julialang.org/t/translate-matlab-code-to-julia-code/8485/4 "2018-01-19T23:28:56Z")

</div>

Note also that you don’t need the `.*` in your integrand. Unlike in Matlab, the integrand is called on scalar arguments—it doesn’t need to be vectorized for performance.

(What will hurt performance in Julia is your use of a global variable in the integrand.)

---

<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: [January 20, 2018, 3:53pm UTC](https://discourse.julialang.org/t/translate-matlab-code-to-julia-code/8485/5 "2018-01-20T15:53:43Z")

</div>

Rewritten to remove global variables:

```julia
using QuadGK, Distributions

charfunc(v, μ, σ) = exp(1im * μ * v - 0.5σ^2 * v^2)
func2(v, μ, σ, X) = exp(-1im * v * X)*charfunc(v, μ, σ)
mypdf(μ, σ, X) = real(quadgk(v -> func2(v, μ, σ, X), -Inf, Inf)[1])/2pi

pdf(Normal(0,1), 3) ≈ mypdf(0, 1, 3)

```

(note that `quadgk` can use indefinite limits)
