# An algorithm to calculate $R_n$s from this recursive equation

How can I write an algorithm to calculate $R_n$s for any desired $n$ from the following equation with given $f,R_0$?

So the output of the code would be $R_1,R_2,...$

$$R_n^2-2fR_n=R_0^2-2fR_0+\sum_{i=0}^n\sum_{j=0}^nR_iR_j$$

I have derived this equation for my specific problem myself and I can calculate every $R_n$ manually but since $R_n$ is not separable I don't know how to write a code for it.

Any ideas?

• Move everything over to one side so you have $g(R_n) = 0$, then you can compute the roots of this equation using any nonlinear solver or "root-finding algorithm" that you like, e.g., bisection method, iterative methods. Nov 22 '17 at 23:19
• It's a linear equation in $R_n$, isn't it, because $R_n^2$ cancels from both sides? Nov 23 '17 at 2:53
• @Kirill Thanks, I did a little math since yesterday and could move the $R_n$ to one side of the equation. I'll post an answer myself. Nov 23 '17 at 9:15

So I did a little math and I noticed $R_n^2$ cancels from both sides as @Kirill noticed in the comments.
$$R_n^2-2fR_n=R_0^2-2fR_0+\sum_{i=0}^n\sum_{j=0}^nR_iR_j$$ $$R_n^2-2fR_n=R_0^2-2fR_0+\sum_{i=0}^{n-1}\sum_{j=0}^{n-1}R_iR_j+R_n\sum_{j=0}^nR_j+R_n\sum_{i=0}^{n-1}R_i$$ $$R_n^2-2fR_n=R_0^2-2fR_0+\sum_{i=0}^{n-1}\sum_{j=0}^{n-1}R_iR_j+R_n\sum_{j=0}^{n-1}R_j+R_n^2+R_n\sum_{i=0}^{n-1}R_i$$ $$R_n=\frac{R_0^2-2fR_0+\sum_{i=0}^{n-1}\sum_{j=0}^{n-1}R_iR_j}{-2(f+\sum_{i=0}^{n-1}R_i)}$$