# How to optimize a series of equations whose outputs are a variable of the subsequent equatinos

The basic question is, given $$f(x) = y$$ and $$f(y) = z$$, how can you find $$x$$ such that $$z$$ is at its maximum?

I can optimize each equation independently, but I do not know how to optimize when combining equations. A concrete example is as follows:

Imagine forex market that is made up of $$x$$ and $$y$$, where $$x$$ and $$y$$ are both currencies. Users can send in $$x$$ to receive $$y$$, and vice versa. The market structure is defined by $$\begin{equation} x * y = k \end{equation}$$ where $$k$$ is a constant number, say $$1$$, and the product of $$x$$ and $$y$$ must always be equal to this number.

The price of $$x$$ or $$y$$ is simply $$x / y$$, such that $$k$$ always stays the same. If someone sends $$x'$$ of the currency as payment and receives $$y'$$ in return, the new equation for the market must be true.

$$\begin{equation} \dfrac{(x + x')}{(y - y')} = k \end{equation}$$

Given all this information, imagine you were to make a trade on two markets of this structure. How would you optimize your input, $$x0'$$, such that your output $$x_1'$$, is maximized, and $$y_0'$$ is equivalent on both trades?

$$\begin{equation} \dfrac{(x_0 + x_0')}{(y_0 - y_0')} = k_0 \;\;\; and \;\;\; \dfrac{(y_1 + y_0')}{(x_1 - x_1')} = k_1 \end{equation}$$

• I kind of see what you are asking, but my instinct is saying there is a really simple mathematical way of expressing this problem but the description and choice of symbols are obfuscating it....
– Attack68
Mar 21 '19 at 19:22
• @Attack68 I truly believe it is simple but cannot figure out how to attack it. I tried to simplify it with the first sentence. Mar 21 '19 at 19:39
• But then you are just optimising $z=f(f(x))$ or $f(g(x))=z$ if your 2 functions $f$ are not actually the same, so not sure where the difficulty arises (besides it not resulting in a convex or differentiable function)
– Attack68
Mar 21 '19 at 19:42
• @Attack68 then how do you optimize $z=f(g(x))$? Is that simply a derivative of each function? Mar 21 '19 at 19:47

$$z = f(g(x))$$
$$\frac{dz}{dx} = \frac{df}{dg} \frac{dg}{dx}$$
Set $$\frac{dz}{dx} = 0$$ and that will determine either minimum, maximum or saddle points.