⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 threenplus1.m

📁 有趣的可视的数值方法 出自网站http://www.mathworks.com/moler
💻 M
字号:
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(''>'')');   uicontrol( ...      'position',[480 5 40 22], ...      'string','close', ...      'callback','close(gcf)')   set(gcf,'tag','3n+1');endif nargin == 0   n = 1;elseif isequal(n,'<')   n = get(gcf,'userdata') - 1;elseif isequal(n,'>')   n = get(gcf,'userdata') + 1;endif n < 1, n = 1; endset(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];endsemilogy(y,'.-')axis tightymax = max(y);ytick = [2.^(0:ceil(log2(ymax))-1) ymax];if length(ytick) > 8, ytick(end-1) = []; endset(gca,'ytick',ytick)title(['n = ' num2str(y(1))]);

⌨️ 快捷键说明

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