How to Define a Symbolic Continued Fraction
11 views (last 30 days)
Show older comments
Maximilian Schönau
on 2 Jan 2021
Commented: Maximilian Schönau
on 6 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...
Accepted Answer
Walter Roberson
on 4 Jan 2021
Infinite case:
syms A B L n
eqn = L == A/n + 1/(B/(n-1) + L)
Lsol = solve(eqn, L)
limit(Lsol, n, inf)
5 Comments
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);
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
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!