You can, of course, calculate this in a real number, but you run the risk of encountering accuracy problems (depending on the starting point). If you are interested in studying orbits, you may prefer to work in representing a rational fraction. There are more efficient ways to do this, but the following code illustrates one way to calculate the series obtained from this map. You will see the definition of period-n on page 2 of your link 2. You can see from this code how you could easily work in real space of numbers as an alternative (in this case, the matlab rat function will restore a rational approximation from your real number) .
[EDIT] Now with a binary sequence made explicit!
% start at some point on period-n orbit period = 6; num = 3; den = 2^period-1; % compute for this many steps of the sequence num_steps = 20; % for each step for n = 1:num_steps % * 2 num = num * 2; % mod 1 if num >= den num = num - den; end % simplify rational fraction g = gcd(num, den); if g > 1 num = num / g; den = den / g; end % recover 8-bit binary representation bits = 8; q = 2^bits; x = num / den * q; b = dec2bin(x, bits); % display fprintf('%4i / %4i == 0.%s\n', num, den, b); end
Ach ... for completeness, here is the real version. Pure mathematicians should now look away.
% start at some point on period-n orbit period = 6; num = 3; den = 2^period-1; % use floating point approximation x = num / den; % compute for this many steps of the sequence num_steps = 20; % for each step for n = 1:num_steps % apply map x = mod(x*2, 1); % display [num, den] = rat(x); fprintf('%i / %i\n', num, den); end
And, for extra credit, why is this implementation quick, but stupid? (TIP: try setting num_steps to 50) ...
% matlab vectorised version period = 6; num = 3; den = 2^period-1; x = zeros(1, num_steps); x(1) = num / den; y = filter(1, [1 -2], x); [a, b] = rat(mod(y, 1)); disp([a' b']);
OK, this should be the answer, not the question, so let me answer my questions ...
This is fast because it uses the built-in (and optimized) Matlab filter function to handle the iteration (that is, in practice, iteration is performed in C rather than in M-script). It is always worth remembering the filter in Matlab, I am constantly wondering how it can be used for use in applications that do not look like filtering problems. filter cannot perform conditional processing, but does not support modular arithmetic, so how do we avoid this? Just because this card has the property that whole periods on the input card have whole periods on the output (because the card operation is multiplied by an integer).
This is stupid because it quickly gets to the above accuracy issues. Set num_steps to 50 and watch it start to get the wrong answers. What happens is, the number inside the filter operation becomes so large (10 ^ 14 order) that the bit that we really care (the fractional part) can no longer be represented in the same variable with double precision.
This last bit is a bit of a leak that has more to do with computation than mathematics - stick with the first implementation if your interest is in character sequences.