function v=collatziter(s,t,myfl); %s=value to test, t=no. of iterations, myfl 0=plot vals 1= web plot %Example collatziter(-17,50,1) %process stop and reports when it reaches a period clf; %upper=6; steps=t; w=1; res=200; %xi=zeros(1,steps); xi(1)=s; mymax=s; mymin=s; i=2; flag=0; while (i<=steps)&(flag==0) xi(i)=myfn(xi(i-1)); if xi(i)>mymax mymax=xi(i); end if xi(i)1)&(flag==0) j=j-1; if xi(j)==xi(i) flag=1; end end i=i+1; end its=i-1; if myfl steps=i-1; x=zeros(2*steps-2); y=x; for i=w:steps-1 y(2*i-1)=xi(i+1); y(2*i)=xi(i+1); x(2*i-1)=xi(i); x(2*i)=xi(i+1); end hold on xx=linspace(mymin,mymax,res); yy=myfn(xx); plot(xx,yy,'y'); yy=xx; plot(xx,yy,'g'); line(x,y); else plot(xi); end hold off for i=w:its fprintf('%2.0f \n',xi(i)); end fprintf('Steps=%2.0f \n',steps); fprintf('Period=%2.0f \n',i-j); function myval=myfn(x) two=x; for i=1:length(x) two(i)=2; end myval=(two+7*x-(two+5*x).*cos(pi*x))/4;