[Math] How to solve a recurrence relation with multiple conditions

recurrence-relationswolfram alpha

Suppose I have a recurrence relation like :

\begin{equation}
f(n)=\begin{cases}
2*f(n-1) , & \text{n%2 = 1}.\\
f(n-1) + 1, & \text{n%2 = 0}.
\end{cases}
\end{equation}

\begin{equation}
f(0) = f(1) = 1
\end{equation}

How would I go about solving this? What do you call such a recurrence relation?

Is there a way I can get $f(n)$ that will solve it for both cases?

I tried using the WolframAlpha RSolve function and tried to use Mod to write a single equation hoping it could solve it. But it kept throwing a syntax error.

I am fine even if you provide a solution using WolframAlpha.

Best Answer

I'm not sure if this type of problem has a special name. It's just basically a function definition split between $2$ different types of values, in this case depending on whether $n$ is odd or even.

The way I would approach trying to solve these types of problems is to determine the first few values to see if I can find any pattern and, if so, then I will try to prove it, usually by using some form of induction.

You're given \begin{equation} f(n)=\begin{cases} 2*f(n-1) , & \text{n%2 = 1}.\\ f(n-1) + 1, & \text{n%2 = 0}. \end{cases} \tag{1}\label{eq1} \end{equation}

\begin{equation} f(0) = f(1) = 1 \end{equation}

The next few values are: \begin{align} f(2) & = f(1) + 1 = 2 \\ f(3) & = 2 \times f(2) = 4 \\ f(4) & = f(3) + 1 = 5 \\ f(5) & = 2 \times f(4) = 10 \\ f(6) & = f(5) + 1 = 11 \\ f(7) & = 2 \times f(6) = 22 \\ f(8) & = f(7) + 1 = 23 \end{align}

Since $f(n)$ is defined separately for odd & even $n$, it's likely the values for $f(n)$ would have somewhat separate patterns for odd & even values of $n$. In this case, the pattern for even $n$ seems a bit simpler, with the odd $n$ being just one less than the even $n + 1$ value. The even $n$ have $f(0) = 1$, $f(2) = 2$, $f(4) = 5$, $f(6) = 11$ and $f(8) = 23$. It's not immediately obvious, at least it wasn't for me, but for $n \ge 2$, the value is the sum of power of $2$ and the next smaller power of $2$, then less 1, with the powers of $2$ increasing by $1$ each time. In particular,

$$f(n) = 2^{n/2} + 2^{(n-2)/2} - 1 \; \text{ for } \; n \ge 2, n \, \% \, 2 = 0 \tag{2}\label{eq2}$$

Note this equation doesn't apply to $n = 0$, as it gives a result of $f(0) = \frac{1}{2}$. This is because the initial value of $f(1) = 1$ doesn't behave according to \eqref{eq1} since it gives $f(1) = 2 \times f(0) = 2$ instead.

From the recursion formula for odd $n$ being one less than the function for $n + 1$ (which is even), you get,

$$f(n) = f(n+1) - 1 = 2^{(n+1)/2} + 2^{(n-1)/2} - 2 \; \text{ for } \; n \ge 1, n \, \% \, 2 = 1 \tag{3}\label{eq3}$$

You can prove \eqref{eq2} and \eqref{eq3} by using strong induction. For $n = 1$, \eqref{eq3} gives $f(1) = 2^1 + 2^0 - 2 = 2 + 1 - 2 = 1$. For $n = 2$, \eqref{eq2} gives $f(2) = 2^1 + 2^0 - 1 = 2 + 1 - 1 = 2$. This proves the base cases. Assume \eqref{eq2} and \eqref{eq3} hold for all $n \le k$ for some $k \ge 1$. For $n = k + 1$, if $n$ is even, then \eqref{eq1}, along with \eqref{eq3} for $f(n-1)$, gives $f(n) = f(n - 1) + 1 = \left(2^{n/2} + 2^{(n - 2)/2} - 2\right) + 1 = 2^{n/2} + 2^{(n - 2)/2} - 1$, which matches \eqref{eq2}. If $n$ is odd instead, then \eqref{eq1}, along with \eqref{eq2} for $f(n-1)$, gives $f(n) = 2 \times f(n - 1) = 2 \times \left(2^{(n-1)/2} + 2^{(n-3)/2} - 1\right) = 2^{(n+1)/2} + 2^{(n-1)/2} - 2$, which matches \eqref{eq3}. Thus, this proves the inductive step, confirming that \eqref{eq2} and \eqref{eq3} are valid for all $n \ge 1$.

Update: I read some of the Wolfram-Alpha documentation to figure out how to use it. However, I didn't see any way to use a function definition split as done here. Nonetheless, I was able to use the Mod function to express it in one equation, so the expression I used was

RSolve[{f[n]==(2^Mod[n,2])f[n-1] + Mod[n-1,2],f[1]==1},f[n],n]

Note I only gave it $f(1)$ as $f(0)$ doesn't follow from the formula (when I also gave it $f(0)$, it failed to do the calculations). The results are here. It produced just one equation of

$$f(n) = 2^{\lfloor(n - 1)/2\rfloor - \lfloor(n - 2)/2\rfloor}\left(3 \times 2^{\lfloor(n - 2)/2\rfloor} - 1\right) \tag{4}\label{eq4}$$

For even $n$, $2^{\lfloor(n - 1)/2\rfloor - \lfloor(n - 2)/2\rfloor} = 2^{(n-2)/2 - (n-2)/2} = 2^0 = 1$ and $3 \times 2^{\lfloor(n - 2)/2\rfloor} - 1 = (2 + 1) \times 2^{(n - 2)/2} - 1 = 2^{n/2} + 2^{(n-2)/2} - 1$, so it matches \eqref{eq2}. For odd $n$, $2^{\lfloor(n - 1)/2\rfloor - \lfloor(n - 2)/2\rfloor} = 2^{(n-1)/2 - (n-3)/2} = 2^1 = 2$ and $3 \times 2^{\lfloor(n - 2)/2\rfloor} - 1 = (2 + 1) \times 2^{(n-3)/2} - 1 = 2^{(n-1)/2} + 2^{(n-3)/2} - 1$, so the product of the $2$ parts gives \eqref{eq3}. In my opinion, it's mainly a matter of choice as to whether it's better to have $2$ simpler equations like I do or just one somewhat more complicated equation, like Wolfram-Alpha produces.

Related Question