% % Code to perform simple iteration and show cobweb plot for the Tent Map % % x_{n+1} = 2(1/2-abs(1/2-x_n)) n=0,1,2,... % clear clf global nrepeat h=figure(1); %(NB without these 2 lines MATLAB bug gives annoying flicker) set(h,'doublebuffer','on'); pp=input(' length of pause between cobwebs: '); x(1) = input(' Enter the initial condition x_0: '); nmax=input(' Enter the number of iterations: '); nrepeat=input(' Enter repeat count for mapping: '); amp=input(' Enter random amplitude: '); xx=0:0.0001:1; plot(xx,functent(xx),'r-') % Plot function in file functent.m hold on plot(xx,xx,'b') % Superpose plot of y=x axis([0 1 0 1]); % Scale picture axis('square'); % Ensure plot is square fprintf('\n n iterate \n') % Print table header for n = 1:nmax x(n+1) = functent(x(n))+amp*rand; fprintf(' %3.0f %10.6f \n', n,x(n+1)) %Print iterate if n>1 xplot = [x(n-1) x(n)]; yplot = [x(n) x(n)]; plot(xplot,yplot,'k:') % Draw horizontal part of cobweb pause(pp) end xplot = [x(n) x(n)]; yplot = [x(n) x(n+1)]; plot(xplot,yplot,'k') % Draw vertical part of cobweb pause(pp) end fprintf( ' Final iteration is %10.6f \n', x(n+1)) % Has huge magnitude when % iterates are unbounded