Solution (source code)

= Solution

Let $V(n,x)$ be maximal expected utility from dates $n+1,\ldots,N$ plus terminal wealth, given $X_n=x$. Then $V(N,x)=U(x)$ and
$$
\boxed{V(n,x)=\sup_{c,h}\{U(c)+E[V(n+1,x-c+hR_{n+1})]\}.}
$$