# How to Define a Symbolic Continued Fraction

24 views (last 30 days)
Maximilian Schönau on 2 Jan 2021
Hello, I want to create and simplify a symbolic fraction, but dont now how. My fraction has the following form: I want to repeat that fraction for n times, with beeing symbolic Variables, to work with and maybe simplify the function (or similar functions).
Edit:
It seems to me now, that the question I want the answer to is:
Is it possible to define and simplify a function in MATLAB which form depends on a parameter?
The function above has no fixed form, since the repetitions of depend on n, which shall be and stay a symbolic variable. In the end I would like a simplified form of . This may not be possible with the function I gave as an example here, but this is not the first time I had this problem, and I would be really glad if there is something I can do here... :)
Old:
I dont know if this is possible in MATLAB, two things would help me to find a way:
• Is there a way to have recursion in symbolic functions? I want to define something like:
syms f(n) n x
f(n) = 1/f(n-1) + x; % Example of recursion in a function
this does not result in an error but does also not return the solution when I substitue n with a numeric value.
• Is there a smart way to define the fraction above with n beeing not a symbolic variable? I could print my fraction n times in a string, and convert that string to a symbolic function, but that does not seem smart to me...
##### 2 CommentsShowHide 1 older comment
Maximilian Schönau on 3 Jan 2021
The final denominator is b/(n-1) + A/n (After n repetitions of the scheme). I‘ll edit my Formular to make it clearer :)
A and B are real.

Walter Roberson on 4 Jan 2021
Infinite case:
syms A B L n
eqn = L == A/n + 1/(B/(n-1) + L)
eqn = Lsol = solve(eqn, L)
Lsol = limit(Lsol, n, inf)
ans = Maximilian Schönau on 6 Jan 2021
Edited: Maximilian Schönau on 6 Jan 2021
If you just want results of your function (and its shape) in a simulation or similar, of course a numerical way is enough.
But it happenend to me now to times, that I really wanted to have the number of iteration as a symbolic value. The reason for that is that I wanted a simplification of the expression. If your expression can be simplified, you can understand bigger interrelationships within your model. This can be very helpfull and sometimes necessary when you model something (and you want to know what is happening). It could be done in my last time such function came to me and can not really be done in my second time I wanted to do this.
"You must be aware that there is rarely a close formula for iterative a function. (Bruno Luong)"
I did not know that, the last time I found the solution to my iterative function by seeing a pattern when solving it numerical and I got my simplification by doing the necessary math on my own.
MATLAB can do much more and much better math than I can do, it would be cool and really helpfull in some cases, if MATLAB could do "symbolic functional power (Bruno Luong)", if this could simplify and find patterns in my iterative functions...

### More Answers (2)

David Hill on 3 Jan 2021
Edited: David Hill on 3 Jan 2021
function L = continuedFraction(N,n)
syms A B;
if n==0
L=0;
else
L=1/(B/(N-1)+A/N+continuedFraction(N,n-1));
end
Run the function for the number of times (n) you want, then add in A/N and simplify.
syms A B;
n=10;
L=continuedFraction(n,n);
L=simplify(L+A/n);
Maximilian Schönau on 4 Jan 2021
You still did define n, I dont want to do that. I want something like:
function L = continuedFraction(N)
syms A B n;
if N==0
L=0;
else
L=1/(B/(n-1)+A/n+continuedFraction(N-1));
end
syms A n;
L=continuedFraction(n); % Matlab will return an error
L=simplify(L+A/n);
I know that this syntax does not make much sense, since you cant use symbolic variables as an input of this function.

Bruno Luong on 4 Jan 2021
Edited: Bruno Luong on 4 Jan 2021
I don't have the symbolic tbx to try, you function can be defined sequentially in n by loop
L = Inf;
for k=1:n
L = A/n + 1/(B/(n-1)+L);
end
Maximilian Schönau on 6 Jan 2021
Your math is absolutly beautiful and I am sorry that I did not invest the time to learn it.
Your solution is in this case inarguably more beautiful than mine.