# Solve system of nonlinear equations

H everyone

Apologies in advance for what may seem like a basic question, but I’m fairly new to julia. I’m looking to solve a system of nonlinear equations – currently I have 47 equations for 47 variables – and am looking for a solution more effective than MATLAB’s fsolve (unable to solve my problem) as I’m looking to find a steady state.

I think NLsolve would be most suitable for my needs, but I’m a bit unsure on some of the notation and syntax of julia.

Would the following format be appropriate for my problem:

using NLsolve

function f!(F, x)
F = …
F = …

F = …
end

nlsolve(f!, initial_x, autodiff = :forward)

Where x is basically a vector of my endogenous variables, and initial_x is a vector of starting guesses for the vector of endogenous variables.

Any help or advice would be much appreciated.

Yes that looks right.

It is very unlikely that just switching to Julia will help you solve your problem, as the same general algorithms are used by pretty much all simple multivariate solvers. Usually the best approach is to look at the structure of your problem, and see if you can solve for blocks in variables (ie a block-triangular structure) independently of others, then break it up into simple ones. Good initial guesses also help a lot, which also need understanding of the problem.

Failing that, homotopy continuation methods may work, but ATM I am only aware of a Julia package for polynomials.

Yup, no general purpose continuation methods in Julia.

I think your (OP) best bet is to maybe show us the problem you’re trying to solve, because I think it’s very likely that NLsolve will fail it fsolve fails.

1 Like

I have yet to code up the problem in julia, but I’ve just copied over the problem I’m trying to solve over from Atom. It’s basically a simple macroeconomic general equilibrium programming problem. Here’s the following system of equations:

``````            alpha = 1 - 0.623
n = 1.01^0.25 - 1
phiI = 132
kappa = 12
phiK = 1
delta = 0.025
omega = 1.023^(-0.25)
gamma = 1.1^(-0.25)
upsilon = 0.4
rho = -3
beta = .999
xi = 0.6
g = 0.17
b = 4
varrho = 0.4
phiPI = 2
phiR = 0.5
Rbar = (1/beta) - 1
phiNX = 1
ystar = 1
Rstar = (1/0.99 - 1)
phiS = 0.000742
fstarbar = -0.5

function f!(F,x)
F=x^(alpha)*x^(1-alpha) - x

F=(x/(1-alpha))^(1-alpha)*(x/alpha)^alpha - x

F[3)=(1-alpha)*x*x/x - x

F=alpha*x*x/x - x

F=(1-x-(phiI/2)*(x-1)^2)*x - x

F=kappa*(x-1)*x + (1+n)*(x/x)*phiI*(x-1)*x*x - phiI*(x-1)*x*x

F=x*x - (1-phiK*((x/x)-1)^2)*x - x

F=((1-delta)*x+(1-(phiK*(x/x)-1)^2)*x) - x

F=(x/x)*(x*(1-delta)+x) - x

F=(1-omega)+gamma*x - (1+n)*x

F=(x*x)^(-1/rho)*x*(((1-upsilon)/upsilon)*(1/(xi*x)))^(1-upsilon) - x

F=(x*x)*(((x*x)/x)+x+x-xi*x) - x

F=x + ((gamma*x)/x)*x - x

F=varrho*(x - x) - x

F=x - ((1-upsilon)/upsilon)*(1/(xi*x))*x - x

F=xi*x*x + ((gamma*x)/x)*x - x

F=xi*x + ((gamma*x)/x)*x - x

F=((x*x)/x) - x + (1+omega)*(((x*x)/x)+ x - x) - x

F=1 - ((x*x)/(x*x))*gamma*x^(rho/(1-rho))*beta^(rho/(1-rho))*(1/x)^((upsilon*rho)/(1-rho))*(x/x)^(((1-upsilon)*rho)/(1-rho)) - (x*x)

F=(x)^(-1/rho)*x*(((1-upsilon)/upsilon)*(1/x))^(1-upsilon) - x

F=x*(((x*x)/x)+ x + x - x) - x

F=((x*(x-omega))/(x*x))*x + ((x*omega)/(x*x))*x - x

F=1 - ((1-upsilon)/upsilon)*(1/x)*x - x

F=x*x + omega*((x*x)/(x*x)) + (1-omega)*(x)^((rho-1)/rho)*(1/xi)^(1-upsilon)*((x*x)/(x*x)) - x

F=x + omega*((x*x)/(x*x)) + (1-omega)*(x)^((rho-1)/rho)*(1/xi)^(1-upsilon)*((x*x)/(x*x)) - x

F=omega*((x*x)/x + x*x - x) - x

F=1 - beta^(1/(1-rho))*(1/x)^((rho*upsilon)/(1-rho))*x^(rho/(1-rho))*x^(rho/(1-rho))*(x/x)^(((upsilon-1)*rho)/(1-rho)) - x

F=omega + (1-omega)*x^(-(1-rho)/rho)*(1/xi)^(1-upsilon) - x

F=x + x - x

F=x/x - x

F=omega*(1 - x*x)*x*((x*A)/x) - (1+n)*(x - (1-omega))*x

F=x + x - x

F=(x/x)*x + x + x - x - (1-n)*x

F=x + xi*x - x

F=g - x

F=b - (1+n)*x

F=varrho*PSI*((1-alpha)*x*x - x) - x

F=phiR*Rbar + (1-phiR)*x + phiPI*x - x

F=(1+n)*(x+x)/x - x/x

F=(1+n)*(x+x)/x - x/x

F=x + xi*x - x

F=x + x + x + x - x*(1 - (phiI/2)*(x-1)^2 - phiK*((x/x)-1)^2)

F=x^(phiNX)*ystar - x

F=x + x + x + x*x - x

F=(1 + Rstar + x)*(x/x) - 1 - x

F=(1 + Rstar + x)*x*x + x - x*x

F=phiS*(exp(x-fstarbar)-1) - x``````

I am a little bit confused, I see 23 things which look like parameters (`alpha`, `n`, `phiI`, …) and no variables (accesses to `x`). How do you get the 47 variables?

Variables are in all caps probably?

Oh that is it probably. So maybe one could formulate this as a polynomial system after some variable substitution and clearing of denominators.

1 Like

Usually, thinking about the mathematical structure of the problem first (before programming) has large and immediate payoffs. This looks like Euler and technology equation residuals from a DSGE model, so maybe you can derive the steady state capital, then consumption and labor, etc.

1 Like

Apologies, the code was essentially pulled out of a MATLAB script. I’ll edit my post now.

Correct, it’s a DSGE model.

Worst case scenario, I may have to manually log linearise the model and put into Dynare, setting initial values equal to zero.

As I said, it should be possible to simplify a lot by understanding the model. Brute-forcing your way to a steady state will be costly (it is actually the most costly part of solving large-ish DSGE models by perturbation methods; second is the linear part, the rest is pretty mechanical).

You can find examples on how to do this in most texts, eg the handbook chapter of Fernández-Villaverde et al. (Sorry if you already know this).

1 Like

One thing to consider is the range of the valid parameter space. If you have some variables, like variances, which have to be positive, solve for the log of that variable and exp it in the objective.

Related, if there are outputs like consumption and you have log utility, then the marginal utility 1/c only makes sense for positive consumption. You might try truncating consumption to 0 so you get an Inf (and fail on that particular guess), instead of crossing the discontinuity at 0, which might make the optimizer diverge.

1 Like