Cantor function in matlab
Mostra commenti meno recenti
How can we create a recursive sequence in matlab that converges to the cantor function?
f_0(x) will be equal to x
Then, for every integer n ≥ 0, the next function fn+1(x) will be defined in terms of fn(x) as follows:
Let fn+1(x) = 1/2 × fn(3x), when 0 ≤ x ≤ 1/3 ;
Let fn+1(x) = 1/2, when 1/3 ≤ x ≤ 2/3 ;
Let fn+1(x) = 1/2 + 1/2 × fn(3 x − 2), when 2/3 ≤ x ≤ 1.
I can define a non recursive function pretty easily(even one that is piecewise), but how to do a recursive function in matlab?
Risposta accettata
Più risposte (2)
Cris LaPierre
il 29 Apr 2023
1 voto
You might find these 3 videos from the Mastering Programming in MATLAB Coursera course helpful.
To be honest, I'd probably be lazy, and just use the simple algorithm found in wikipedia.
That is, expresses the number in ternary. Easy enough to do. The largest integer power of 3 that is less than flintmax is 3^33.
flintmax
3^33
That means you can get 33 ternary digits for any decimal number between 0 and 1. We can do it like this:
ternary = @(x) dec2base(round(3^33*x),3,33);
t = ternary(0.123)
We can verify the result, as:
format long g
dot(3.^(-1:-1:-33),t - '0')
Now just use the algorithm shown on the wiki page. For example, we know that f_inf(1/4) = 1/3.
T = ternary(1/4)
ind = find(T == '1',1,'first')
T(ind + 1:end) = '0' % replace the digits after the first 1, with 0.
% replace all 2's with a 1
T(T == '2') = '1' % replace all 2's with a 1.
% finally, represent the number in base 2.
format rat
dot(T - '0',2.^(-1:-1:-33))
Unfortunately, the result will have only 33 binary bits in the final representation as I did it here.
Yes, this is probably homework, since nobody is going to be computing this for any normal reason. :) And that means you were instructed to do it recursively, using the supplied set of relations.
1 Commento
Vivaan
il 29 Apr 2023
Categorie
Scopri di più su Logical in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
