NLSolve vs fsolve (matlab): What am I doing wrong?

So, I have a code in matlab and I am trying to translate it to Julia. Can anyone help me an tell me what am I doing wrong?

Basically it is a function and I need to use fsolve/ NLSolve. I put first fig below matlab codes (function and de fsolve code together just to make it easy here). My problem however is in Julia, I cant translate and run the code above in nlsolve. What am I doing wrong?

It returns the following error: ERROR: InexactError: Int64(NaN)

If I change Julia for x0 = 10 and take of the brackets, leaving it as scalar, it give me the error
MethodError: no method matching nlsolve(::var"#81#82", ::Int64)

Thank you very much

hi!, can you provide your kappa, zeta, eta, beta parameters? i can’t reproduce your code without those. meanwhile, can you try using x0 = [10.0] instead?
Also, i recommend you to paste your code in triple backticks to make debbuging easier:

```julia
function f(x, y)
    x + y
end```

Please read: make it easier to help you

3 Likes

I’m not sure how function definition works in Matlab these days so I’m a bit confused by what the F and PFunction are respectively. In Julia you would probably write:

julia> function p_function(x, κ, ζ, η, β)
           κ*x[1]^(1-η)/ζ - (1-β)*(β*ζ*x[1]^η)
       end
p_function (generic function with 1 method)

julia> obj(x) = p_function(x, 0.5, 0.5, 0.5, 0.5) # or whatever your values are
obj (generic function with 1 method)

So, first I need to say that there is a typo in both functions, those “F=” does not exist

Most important, however, is that I still cant run this NLSolve in Julia. It returns me a domain error… probably because there is x^eta, where eta = 0.5.

But what makes no sense for me is that it runs in matlab. I have even tried to use ’ fzero ’ in Julia, and it still does not work: returns domain problem (but with different number).

I would not say there is an error in my function (PrivateGF)itself because, when I run only this function with arbitrary values, both Julia and Matlab returns the same value. For example, if x=10, both matlab and julia results are equal.

Therefore, I think the problem is in the optimization part of NLsolve / fzero (roots package).

The code is just below, both for matlab and Julia. If anyoen could help me, I would be extremaly grateful.

#Parameters
beta=0.5
r=0.0040;     
tauu=0.0019; 
kappa=14.1425;
zeta=1;
eta=0.5; 
b=0.7395;       
y=1.741;
s_p=0.007382165;         

#%% Matlab
function F = PrivateF(x, kappa , zeta , eta , beta , y , b , s_p, rr, tauu)
F = [kappa *x(1)^(1-eta ) /zeta  - (1-beta )*(y -b )/(rr+tauu+s_p+beta *zeta *x(1)^eta )];
#

#% Solving system in Matlab 
[theta ,fval,exitflag] = fsolve(@(x) PrivateF(x,kappa , zeta , eta , beta , y , b , s_p, r, tauu), x0 ,optimset('Display','off', 'TolFun',1.0e-009));

# end matlab code

### Julia
function PrivateF(x, kappa , zeta , eta , beta , y , b , s_p, rr, tauu)
    F = [kappa *(x[1])^(1-eta ) / zeta  - (1-beta ) * (y -b )/(rr+tauu+s_p+beta *zeta *x[1]^eta )];
    return F
end

#Solving system
x0 = [10.0]; #starting guess

f_aux = (x -> PrivateF(x, kappa , zeta , eta , beta , y , b , s_p, r, tauu))
theta  = nlsolve(f_aux, x0).zero

#even tried with fzero
theta = fzero(f_aux, x0[1])

Thank you very much!

it was a mistake. I just talk about ir below. I think this obj(x) is the same as i did usinf faux.

Thank you

it indeed helped me using ‘’’ x0 =[10.0]‘’'. I end up with another problem: domain error. I just put the code below (with some updat in function PrivateF).

Now I have a problem with nlsolve indeed, since it does not find value of theta that minimizes the function. HOwever, it fsolve in matlab is able to do it. Dont know what is happening.

If you could help me again, I would appreciate it a lot.