How to Define a Symbolic Continued Fraction
조회 수: 12 (최근 30일)
이전 댓글 표시
Hello, I want to create and simplify a symbolic fraction, but dont now how. My fraction has the following form:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/478148/image.png)
I want to repeat that fraction for n times, with
beeing symbolic Variables, to work with and maybe simplify the function (or similar functions).
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/478153/image.png)
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... :)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/478158/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/478163/image.png)
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
Walter Roberson
2021년 1월 3일
I am not clear as to what the final denominator should be?
Are A and B guaranteed to be real?
채택된 답변
Walter Roberson
2021년 1월 4일
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
Walter Roberson
2021년 1월 6일
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.
추가 답변 (2개)
David Hill
2021년 1월 3일
편집: David Hill
2021년 1월 3일
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
David Hill
2021년 1월 3일
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
2021년 1월 4일
편집: Bruno Luong
2021년 1월 4일
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
Bruno Luong
2021년 1월 6일
편집: Bruno Luong
2021년 1월 6일
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.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/480358/image.png)
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.
참고 항목
카테고리
Help Center 및 File Exchange에서 Special Values에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!