Numerical Solvers
This section reviews numerical solvers in MATLAB.
Last updated
Was this helpful?
This section reviews numerical solvers in MATLAB.
Last updated
Was this helpful?
A very useful feature of Matlab is its ability to find solutions to nonlinear equations and, more generally, systems of nonlinear equations.
The function fzero
searches for a point where target_function. It stops searching once it finds a point where the function changes sign. This can be highly useful for example to find solutions to first order conditions.
The syntax for using fzero
is
where is the solution, fval
the value of the function at the solution, exitflag
gives the exit condition, function
is our function of interest, is the starting value (can be a single point or an interval), and options specifies the options of fzero
to be used.
Say, for example, if we want to find the root of the sine function close to 3 we write
Likewise, instead of looking close to 3 we can specify a range by writing
However, if the function of interest has hyperparameters we need to make an extra step. Take our univariate function from the previous section, . The first order conditions are then which we can solve with fzero
but we need to specify the values of and . We can write
A short cut could be to write
Say that we have a system of two equations in two variables:
Then we can pass the function to fsolve
to obtain the solution:
fsolve
The basic New Keynesian model consists of three core equations:
0.99
0.17
1
1.5
1/8
0.8
0.02
We begin by setting up our target function (recall that our target function needs to have all the choice parameters as the first input):
And now, to solve the model and to plot the impulse responses we run the following:
Often the problems we face are not simply a function of a single variable but are rather systems of nonlinear equations. To solve these problems we can use the fsolve
function which solves for (which is a vector or matrix), where is a function returning a vector value. The syntax is comparable to that of fzero
, that is
where is the solution, fval
the value of the function at the solution, exitflag
gives the exit condition, function
is our function of interest, x0
is the starting value, and options specifies the options of fsolve
to be used.
To solve this system we first need to convert the system to the form and write a function that calculates the left-hand side of the the equation:
(The NK Phillips curve)
(The Dynamic IS equation)
(Monetary policy)
where denotes the inflation rate, the output gap, the nominal interest rate and is a "financial shock" which evolves according to the process where is an iid random variable with zero mean and standard deviation .
We consider a finite horizon version of the model and use fsolve
to solve the system and give us the impulse response of a one standard deviation decrease in (i.e., , ) for the parameter values