'Using optim to find the equilibrium for a discrete 2-equation model

I am working with a 2-variable, 2-equation model. I would like to use the optim function to find the equilibrium of the model numerically. The model looks something like this (with f1 and f2 being defined already as functions).

X_{t+1} = f1(x_t, y_t)
Y_{t+1} = f2(x_t, y_t)

Additionally, there are various parameters to this system, so if at all possible I would like the code to make it relatively easy to vary these parameters.

I have been struggling with trying to make this work, but always get an error from optim when I try. Does anyone know how to use this function? The documentation is unfortunately pretty sparse on details.

Thank you for any help.

Edit: I should also add that part of my problem is that optim seems to only want to take values of length 1, when I have two variables and two equations. Placing it in a vector has not worked, and presented the following error:

Error in optim(initialGuess.v, sumDiffSqr, parms = parms, f1 = f1, f2 = f2) : 
  objective function in optim evaluates to length 2 not 1

Edit 2: I have solved the issue. Unfortunately SO will not allow me to accept the answer for 2 days.



Solution 1:[1]

I figured out the issue. You have to provide some value related to the functions, but not directly the functions, in order for R to optimize it. So what I did was wrote a function that found the difference between the next step and the previous step of both f1 and f2, then added the squares of the differences. This value gets plugged into optim, which then allows R to try and optimize this difference and minimize it until it produces an estimate where this difference is very small (in other words, at equilibrium).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 o's1234