How to Define a Symbolic Continued Fraction
16 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...
2 Comments
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?
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
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.
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);
10 Comments
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);
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
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.
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!