Rasmus Damgaard Olsen
Rasmus Damgaard Olsen

Reputation: 33

Julia: NLsolve providing different solution each run and sometimes returns NaN?

I am trying to replicate some matlab code as a part of my master thesis from the paper Zhu, H. (2014). Do dark pools harm price discovery?

I have a system of two equations which i solve using the "NLsolve" package in Julia. The issue is that NLsolve returns a different solution for f[1] each time i run the code and sometimes it returns a "NaN".

I am not sure what is causing this?

Here is a code excerpt:

using NLsolve;

function G(x)
    min(1,x/C)
end;

function Ginv(x)
    min(C,x*C) 
end;

function F(x)
    1-exp(-x/2)
end;

μᵤ = 60;
σᵤ = sqrt(μᵤ);
a = [0 0];
b = [3*μᵤ 3*μᵤ];
r_bar = 0.91;
μ_bar  = 20;
C = 2;
x₀ = [μ_bar, 1.0];

function f_temp!(x,f)
   f[1] = μ_bar*F((1-G(1))*μᵤ*x[2]/(x[1]+(1-G(1))μᵤ))-x[1]
   f[2] = 1-x[1]/(x[1]+(1-G(1))μᵤ)-r_bar
end;

x = nlsolve(f_temp!, x₀)

Best regards, Rasmus

Upvotes: 0

Views: 354

Answers (1)

Bogumił Kamiński
Bogumił Kamiński

Reputation: 69949

You have a wrong order of arguments in f_temp!. Use:

function f_temp!(f, x)
   f[1] = μ_bar*F((1-0.5)*μᵤ*x[2]/(x[1]+(1-0.5)μᵤ))-x[1]
   f[2] = 1-x[1]/(x[1]+(1-0.5)μᵤ)-r_bar
end;

and all should be fine.

Upvotes: 2

Related Questions