threenplus1.m

来自「matlab 数值计算 数据和源文件」· M 代码 · 共 50 行

M
50
字号
function threenplus1(n)
%"Three n plus 1".
%   Study the 3n+1 sequence.
%   threenplus1(n) plots the sequence starting with n.
%   threenplus1 with no arguments starts with n = 1.
%   uicontrols decrement or increment the starting n.
%   Is it possible for this to run forever?

if ~isequal(get(gcf,'tag'),'3n+1')
   shg
   clf reset
   uicontrol( ...
      'position',[260 5 25 22], ...
      'string','<', ...
      'callback','threenplus1(''<'')');
   uicontrol( ...
      'position',[300 5 25 22], ...
      'string','>', ...
      'callback','threenplus1(''>'')');
   set(gcf,'tag','3n+1');
end

if nargin == 0
   n = 1;
elseif isequal(n,'<')
   n = get(gcf,'userdata') - 1;
elseif isequal(n,'>')
   n = get(gcf,'userdata') + 1;
end
if n < 1, n = 1; end
set(gcf,'userdata',n)

y = n;
while n > 1
   if rem(n,2)==0
      n = n/2;
   else
      n = 3*n+1;
   end
   y = [y n];
end

semilogy(y,'.-')
axis tight
ymax = max(y);
ytick = [2.^(0:ceil(log2(ymax))-1) ymax];
if length(ytick) > 8, ytick(end-1) = []; end
set(gca,'ytick',ytick)
title(['n = ' num2str(y(1))]);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?