How to Define a Symbolic Continued Fraction

16 views (last 30 days)
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 Comments
Walter Roberson
Walter Roberson on 3 Jan 2021
I am not clear as to what the final denominator should be?
Are A and B guaranteed to be real?
Maximilian Schönau
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.

Sign in to comment.

Accepted Answer

Walter Roberson
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 = 
  5 Comments
Walter Roberson
Walter Roberson on 6 Jan 2021
It is possible to define symbolic procedures using MuPAD, if you happen to know the MuPAD programming language... which is no longer documented. However, if you happen to request displaying a variable that holds a symbolic procedure, or you happen to request displaying a hold construct (often needed for symbolic procedures) then the symbolic engine will reset itself.
But if you could do such a thing, then what interface would you want for the function? You would want to be able to pass a specific integer to the function and have it calculate for that integer, and you would want to be able to pass inf to the function to get the limit, and what else? For example do you need to be able to pass symbolic n and also symbolic n+1 and calculate the difference between the two results? Do you ever need to pass in a symbolic approximation order, calculating N levels with symbolic n ?
Is it mandatory that this all be a symbolic function, or could it be done through a function handle instead?
The ability to create a symbolic function that calculates recursively for any given integer n does not imply the existence of tools that can reason about the function given symbolic parameters.
Maximilian Schönau
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...

Sign in to comment.

More Answers (2)

David Hill
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);
  10 Comments
David Hill
David Hill on 3 Jan 2021
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
Can you just do this?
syms A n;
L=continuedFraction(10);
L=simplify(L+A/n);
Maximilian Schönau
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.

Sign in to comment.


Bruno Luong
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
  11 Comments
Bruno Luong
Bruno Luong on 6 Jan 2021
Edited: Bruno Luong on 6 Jan 2021
I did not expect you give up that soon, when I gave all the hints for you to compute the finite continuous fraction. And btw IMO your second comment is far from being useful for anyone who read it.
I give here the exact formula of your editted fraction.
Now this is a gift for someone who reads this and does or doe not want to do the math.
n = 10;
A = rand();
B = rand();
bn = B/(n-1) + A/n;
% Compute using iterative of fraction
L = Inf;
for k=1:n
L = bn + 1/L;
end
L = L-B/(n-1)
% Direct formula
Mpn = [0, 1;
1, bn]^n;
Y = Mpn(:,2)./Mpn(:,1);
L = Y(2);
L = L-B/(n-1)
If you don't find this formula and the way to find it is beautiful, then you might be a simple good engineer that does absolutely amazing empirical polynomial fitting model.
Maximilian Schönau
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.

Sign in to comment.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!