Chapter 1. Introduction to MATLAB while get(stop, 'value) x=Al*x+ b1 elseif r< p(2) elseif r< p(3) X=A3*+b3 =A4*X set(h, 'xdata, x(1),'ydata',x(2)); end s= sprintf('78Of points in 26 3f seconds,, cnt, t); 'bold) set(stop, 'style,'pushbutton,'string,'close callback','close(gcf)') Let's examine this program a few statements at a time h stands for show graph window. It brings an existing graphics window forward or creates a new one if necessary clf reset resets most of the figure properties to their default values set(gcf, 'color','white,'menubar','none mumbertit1e’,'off’,name, Fractal Fern3) changes the background color of the figure window from the default gray to white and provides a customized title for the window provides the initial coordinates of the point h=plot(x(1),x(2),’.); plots a single dot in the plane and saves a handle, h, so that we can later modify the properties of the plot. darkgreen = [0 2/3 0]
16 Chapter 1. Introduction to MATLAB tic while ~get(stop,’value’) r = rand; if r < p(1) x = A1*x + b1; elseif r < p(2) x = A2*x + b2; elseif r < p(3) x = A3*x + b3; else x = A4*x; end set(h,’xdata’,x(1),’ydata’,x(2)); cnt = cnt + 1; drawnow end t = toc; s = sprintf(’%8.0f points in %6.3f seconds’,cnt,t); text(-1.5,-0.5,s,’fontweight’,’bold’); set(stop,’style’,’pushbutton’,’string’,’close’, ... ’callback’,’close(gcf)’) Let’s examine this program a few statements at a time. shg stands for “show graph window.” It brings an existing graphics window forward, or creates a new one if necessary. clf reset resets most of the figure properties to their default values. set(gcf,’color’,’white’,’menubar’,’none’, ... ’numbertitle’,’off’,’name’,’Fractal Fern’) changes the background color of the figure window from the default gray to white and provides a customized title for the window. x = [.5; .5]; provides the initial coordinates of the point. h = plot(x(1),x(2),’.’); plots a single dot in the plane and saves a handle, h, so that we can later modify the properties of the plot. darkgreen = [0 2/3 0];
1.3. Fractal Fer defines a color where the red and blue components are zero and the green component is two-thirds of its full intensity set(h,’ markersize),1,’ color, darkgreen,' erasemode’,none3); kes the dot referenced by h smaller, changes its color, and specifies that the image of the dot on the screen should not be erased when its coordinates are changed. a record of these old points is kept by the computer's graphics hardware(until the figure is reset), but MATLAB itself does not remember them ([-33010]) axis off specifies that the plot should cover the region 3≤x1≤3,0≤x2≤10 but that the axes should not be drawn stop uicontrol('style,'toggle,,'string,,'stop', ,white creates a toggle user interface control, labeled 'stop' and colored white, in the default position near the lower left corner of the figure. The handle for the control drawno causes the initial figure, including the initial point, to actually be plotted on the computer screen The statement 991.00]; sets up a vector of probabilities. The statements A1=[ 5];b1=[0;1.6] A2=[.20-.26;.23.22];b2=[0;1.6] A3=[.15.28 A4= 0.16] define the four affine transformations The statement initializes a counter that keeps track of the number of points plotted. The statement initializes a stopwatch timer. The statement while get(stop,'value,)
1.3. Fractal Fern 17 defines a color where the red and blue components are zero and the green component is two-thirds of its full intensity. set(h,’markersize’,1,’color’,darkgreen,’erasemode’,’none’); makes the dot referenced by h smaller, changes its color, and specifies that the image of the dot on the screen should not be erased when its coordinates are changed. A record of these old points is kept by the computer’s graphics hardware (until the figure is reset), but Matlab itself does not remember them. axis([-3 3 0 10]) axis off specifies that the plot should cover the region −3 ≤ x1 ≤ 3, 0 ≤ x2 ≤ 10, but that the axes should not be drawn. stop = uicontrol(’style’,’toggle’,’string’,’stop’, ... ’background’,’white’); creates a toggle user interface control, labeled ’stop’ and colored white, in the default position near the lower left corner of the figure. The handle for the control is saved in the variable stop. drawnow causes the initial figure, including the initial point, to actually be plotted on the computer screen. The statement p = [ .85 .92 .99 1.00]; sets up a vector of probabilities. The statements A1 = [ .85 .04; -.04 .85]; b1 = [0; 1.6]; A2 = [ .20 -.26; .23 .22]; b2 = [0; 1.6]; A3 = [-.15 .28; .26 .24]; b3 = [0; .44]; A4 = [ 0 0 ; 0 .16]; define the four affine transformations. The statement cnt = 1; initializes a counter that keeps track of the number of points plotted. The statement tic initializes a stopwatch timer. The statement while ~get(stop,’value’)
Chapter 1. Introduction to MATLAB begins a while loop that runs as long as the 'value, property of the stop toggle is equal to 0. Clicking the stop toggle changes the value from 0 to l and terminates generates a pseudorandom value between 0 and 1. The compound if statement if r p(1) x=A1*x+b1; elseif r p(2) x=A2*x+b2; elseif r p(3) x=A3*x+b3; x =A4*x picks one of the four affine transformations. Because p(1) is 0. 85, the first trans- formation is chosen 85% of the time. The other three transformations are chosen relatively infrequently set(h,'xdata, x(1),'ydata, x(2)); changes the coordinates of the point h to the new (a1, a2) and plots this new point But get(h,'erasemode') is 'none,, so the old point also remains on the screen. counts one more point drawnon tells Matlab to take the time to redraw the figure, showing the new with all the old ones. Without this command, nothing would be plotted Is end matches the while at the beginning of the loop. Finally, t= toc reads the timer s= sprintf('78 of points in 76 3f seconds', cnt, t) text(-1.5,-0.5,s, fontweight’,bold’) displays the elapsed time since tic was called and the final count of the number of oints plotted. Finally, set(stop, 'style,,'pusl shbutton','string c⊥ose 'callback','close(gcf)') hanges the control to a push button that closes the window
18 Chapter 1. Introduction to MATLAB begins a while loop that runs as long as the ’value’ property of the stop toggle is equal to 0. Clicking the stop toggle changes the value from 0 to 1 and terminates the loop. r = rand; generates a pseudorandom value between 0 and 1. The compound if statement if r < p(1) x = A1*x + b1; elseif r < p(2) x = A2*x + b2; elseif r < p(3) x = A3*x + b3; else x = A4*x; end picks one of the four affine transformations. Because p(1) is 0.85, the first transformation is chosen 85% of the time. The other three transformations are chosen relatively infrequently. set(h,’xdata’,x(1),’ydata’,x(2)); changes the coordinates of the point h to the new (x1, x2) and plots this new point. But get(h,’erasemode’) is ’none’, so the old point also remains on the screen. cnt = cnt + 1; counts one more point. drawnow tells Matlab to take the time to redraw the figure, showing the new point along with all the old ones. Without this command, nothing would be plotted until stop is toggled. end matches the while at the beginning of the loop. Finally, t = toc; reads the timer. s = sprintf(’%8.0f points in %6.3f seconds’,cnt,t); text(-1.5,-0.5,s,’fontweight’,’bold’); displays the elapsed time since tic was called and the final count of the number of points plotted. Finally, set(stop,’style’,’pushbutton’,’string’,’close’, ... ’callback’,’close(gcf)’) changes the control to a push button that closes the window
1.4. Magic squares 1.4 agic Squares MATLAB stands for Matric Laboratory. Over the years, MATLAB has evolved into a general-purpose technical computing environment, but operations involving vectors atrices, and linear algebra continue to be its most distinguishing feature Magic squares provide an interesting set of sample matrices. The elp magic MAGIC(N) is an N-by-N matrix constructed from the integers 1 through N 2 with equal row, column, and diagonal sums Produces valid magic squares for all N>0 N=2 The agic squares were known in China over 2,000 years before the birth of Christ The 3-by-3 magic square is known as Lo Shu. Legend has it that Lo Shu was century B.C. Lo Shu provides a mathematical basis for feng shui the.ver in the 23rd discovered on the shell of a turtle that crawled out of the lo ri cient Chinese philosophy of balance and harmony. MATLAB can generate Lo Shu with A magic(3) 3 7 The command sum(A) sums the elements in each column to produce The command sum (A) transposes the matrix, sums the columns of the transpose, and then transposes the results to produce the row sums The command sum(diag(A)) sums the main diagonal of A, which runs from upper left to lower right, to produce
1.4. Magic Squares 19 1.4 Magic Squares Matlab stands for Matrix Laboratory. Over the years, Matlab has evolved into a general-purpose technical computing environment, but operations involving vectors, matrices, and linear algebra continue to be its most distinguishing feature. Magic squares provide an interesting set of sample matrices. The command help magic tells us the following: MAGIC(N) is an N-by-N matrix constructed from the integers 1 through N^2 with equal row, column, and diagonal sums. Produces valid magic squares for all N > 0 except N = 2. Magic squares were known in China over 2,000 years before the birth of Christ. The 3-by-3 magic square is known as Lo Shu. Legend has it that Lo Shu was discovered on the shell of a turtle that crawled out of the Lo River in the 23rd century b.c. Lo Shu provides a mathematical basis for feng shui, the ancient Chinese philosophy of balance and harmony. Matlab can generate Lo Shu with A = magic(3) which produces A = 8 1 6 3 5 7 4 9 2 The command sum(A) sums the elements in each column to produce 15 15 15 The command sum(A’)’ transposes the matrix, sums the columns of the transpose, and then transposes the results to produce the row sums 15 15 15 The command sum(diag(A)) sums the main diagonal of A, which runs from upper left to lower right, to produce 15