Вы находитесь на странице: 1из 4

TheMathWorks News&Notes

Cleve’s Corner

Experiments with MATLAB


By Cleve Moler

Experiments with MATLAB is a free, online book for educators and high school students looking for material that goes
beyond their standard courses. College students early in their careers will also find value in the materials and exercises.

N ow a full-fledged technical computing


language, MATLAB® started in the late 1970s
as a simple “Matrix Laboratory.” Experiments
with MATLAB builds on this laboratory tradi-
tion by describing experiments involving ap-
plied mathematics and technical computing.
MATLAB programming is introduced with
code snippets and small programs, many of
which use interactive graphics.
To make the most of the book’s experi-
ments, readers need a high-school-level
knowledge of geometry, algebra, and trigo-
nometry. Experiments with MATLAB intro-
duces topics involving calculus, matrices,
and differential equations, but it does not as-
sume that students have completed courses
in these subjects. Figure 1. MATLAB plot showing that the 13th day of a month is more likely to be
a Friday than any other day of the week.
In this Cleve’s Corner, I’ve included edited
excerpts from Experiments with MATLAB to
give you some idea of the level and tone of Here are the first and last few lines that value, 1.6180339… . Do you recognize this
the book. I encourage you not only to read you will see if you choose x = 3 as your number?
about the experiments but also to modify starting value. This experiment motivates a discussion of
and improve them. the two meanings of the equals sign: as the as-
3.000000000000000 signment operator in programming languages
Iteration 2.000000000000000 and as the symbol for equality in equations.
The first chapter asks you to pick a number 1.732050807568877 How would you solve the following
—any number. Enter it into MATLAB with 1.652891650281070 equation?
1.628769980777233 x = √1 + x
x = your number
. . . Don’t use a computer! Do it in your head
1.618033988749915 or on paper. Square both sides and move
Now enter the statement 1.618033988749901 everything to one side of the equals sign to
1.618033988749897 get a quadratic equation.
x = sqrt(1+x) 1.618033988749895 x2 - x – 1 = 0
1.618033988749895 Then use the quadratic formula to find the
Use the up-arrow and enter keys to re- positive root,
peatedly execute the statement until the No matter what value you start with, φ = (1 + √5)/2 = 1.6180339 …
displayed values stop changing. you always converge to the same final This is our old friend, the golden ratio.

Reprinted from TheMathWorks News&Notes | 2 0 0 8 w w w. m a t h w o r k s .c o m 1


TheMathWorks
TheMathWorksNews&Notes
News&Notes

How does MATLAB compute ax without


knowing about e? Well, actually, it can’t, so
our whole argument is circular. But if any-
body asks that question, we have a terrific
topic for discussion.

Solving the Amazing T Puzzle


I first saw the wooden puzzle shown in
Figure 3 at Puzzling World in Wanaka, New
Zealand. Now their most popular puzzle, it
was a well-known toy in the 1800s and an
advertising tool in the early 1900s.
Figure 4 shows the MATLAB electronic
version of the four pieces. They all have the
Figure 2. Varying the base in a plot of ax and its approximate derivative to discover e.
same width, but different heights. One has
an unshapely chunk cut out of it, resulting
in an irregular pentagon. Using MATLAB
Calendars and Clocks We can also compute an approximate and the GUI for the book’s sample T Puzzle
Friday the 13th is sometimes considered un- slope or approximate derivative, without Experiments with MATLAB M-file, you could move the pieces around
© Shop New Zealand
lucky, but is it unlikely? What is the prob- any formal rules of differentiation, using with the mouse. It turns out that the four
ability that the 13th day of any month will Table of Contents pieces can be arranged to form the capital
fall on a Friday? The quick answer is 1/7, h = 0.0001 ■■ Iteration “T” shape shown in Figure 5. What happened
Figure 3. This wooden puzzle is manipulated by hand. Image courtesy of
but that is not quite right. The rules for leap yp = (a.^(x+h) – a.^x)/h
■■ Fibonacci Numbers to all those 45° angles­­—and to that chunk? Shop New Zealand www.shopnewzealand.co.nz
years ensure that our Gregorian calendar re- ■■ Calendars and Clocks Our program for manipulating the T puz-
peats itself every 400 years, or 4800 months. Then the statement ■■ T Puzzle zle uses complex arithmetic. For example,
A simple experiment using the MATLAB ■■ Matrices the coordinates of the largest puzzle piece
datenum and weekday functions can count plot(x,[y; yp])
■■ Fractal Fern are given by the complex vector
how often the 13th day of each month falls ■■ Magic Squares
on a Friday, producing the graph in Figure 1. produces the first plot shown in Figure 2. The ■■ Tic Tac Toe Magic z = [0 1 1+2i 3i]
It turns out that the 13th is more likely to blue curve is the graph of 2x. The green curve ■■ Game of Life
fall on a Friday than on any other day of the is its approximate derivative. The graph of ■■ Mandelbrot Set where the horizontal and vertical dimen-
week. The probability is 688/4800 = .143333, the approximate derivative has the same ■■ Linear Equations sions are represented by the real and imagi-
which is slightly larger than 1/7 = .142857. shape as the graph of the original function ■■ Google PageRank nary components, respectively.
but is below it. In fact, the ratio yp./y is a ■■ Ordinary Differential Equations Translating the piece in response to mouse
The Exponential Function constant independent of x. ■■ Exponential Function motion is easily done by a complex vector
Too many students who have recently taken The M-file expgui lets you move the blue ■■ Predators and Prey subtraction
a beginning calculus course think the de- line with the mouse and slowly vary the base ■■ Shallow Water Equations Figure 4. This electronic puzzle is manipulated Figure 5. Make this T.
rivative of ex is xex-1. How can we better un- of ax. The second shows the graph of 3x and z = z - w
by complex arithmetic.
derstand the number e and the function ex? its approximate derivative. The green curve is Download Experiments with MATLAB at
MATLAB can compute powers, y = ax, for a now above the blue curve. As you move from www.mathworks.com/nn8/moler We rotate the piece about its center by an
scalar a and a vector x. a = 2 to a = 3, you pass the situation shown in angle θ using the following statements: The Game of Life In “Life”, the universe is an infinite, two-
the third plot, where the green curve is on top John Horton Conway’s “Game of Life” made dimensional rectangular grid. The popula-
a = 2
of the blue curve. For this value of a, the func- mu = mean(z) its debut on the cover of Scientific American tion is a collection of grid cells marked as
x = 0:0.01:2 tion is its own derivative. The value displayed omega = exp(i*theta)
in October 1970 and has had a small but alive. The population evolves at discrete time
y = a.^x for a is e and the function is ex, one of the most z = omega*(z – mu) + mu
loyal following ever since. It is an example of steps known as generations. At each step, the
important functions in mathematics. the complexities that can be generated by ap- fate of each cell is determined by the vital-
We’ve skipped an important but subtle Where do you learn that complex multi- parently simple cellular automata. ity of its eight nearest neighbors and by this
mathematical and computational question: plication by eiθ is a rotation?

2 Reprinted
2 Reprinted TheMathWorks
from from News&Notes
TheMathWorks News&Notes
| 2 0 0| 82 0 0 8 w w ww. w
mwa t. hmwaot hr kwso. cr ko sm. c o m w w w. m a t h w o r k s .c o m Reprinted from TheMathWorks News&Notes | 2 0 0 8 3
TheMathWorks
TheMathWorksNews&Notes
News&Notes

How does MATLAB compute ax without


knowing about e? Well, actually, it can’t, so
our whole argument is circular. But if any-
body asks that question, we have a terrific
topic for discussion.

Solving the Amazing T Puzzle


I first saw the wooden puzzle shown in
Figure 3 at Puzzling World in Wanaka, New
Zealand. Now their most popular puzzle, it
was a well-known toy in the 1800s and an
advertising tool in the early 1900s.
Figure 4 shows the MATLAB electronic
version of the four pieces. They all have the
Figure 2. Varying the base in a plot of ax and its approximate derivative to discover e.
same width, but different heights. One has
an unshapely chunk cut out of it, resulting
in an irregular pentagon. Using MATLAB
Calendars and Clocks We can also compute an approximate and the GUI for the book’s sample T Puzzle
Friday the 13th is sometimes considered un- slope or approximate derivative, without Experiments with MATLAB M-file, you could move the pieces around
© Shop New Zealand
lucky, but is it unlikely? What is the prob- any formal rules of differentiation, using with the mouse. It turns out that the four
ability that the 13th day of any month will Table of Contents pieces can be arranged to form the capital
fall on a Friday? The quick answer is 1/7, h = 0.0001 ■■ Iteration “T” shape shown in Figure 5. What happened
Figure 3. This wooden puzzle is manipulated by hand. Image courtesy of
but that is not quite right. The rules for leap yp = (a.^(x+h) – a.^h)/h
■■ Fibonacci Numbers to all those 45° angles­­—and to that chunk? Shop New Zealand www.shopnewzealand.co.nz
years ensure that our Gregorian calendar re- ■■ Calendars and Clocks Our program for manipulating the T puz-
peats itself every 400 years, or 4800 months. Then the statement ■■ T Puzzle zle uses complex arithmetic. For example,
A simple experiment using the MATLAB ■■ Matrices the coordinates of the largest puzzle piece
datenum and weekday functions can count plot(x,[y; yp])
■■ Fractal Fern are given by the complex vector
how often the 13th day of each month falls ■■ Magic Squares
on a Friday, producing the graph in Figure 1. produces the first plot shown in Figure 2. The ■■ Tic Tac Toe Magic z = [0 1 1+2i 3i]
It turns out that the 13th is more likely to blue curve is the graph of 2x. The green curve ■■ Game of Life
fall on a Friday than on any other day of the is its approximate derivative. The graph of ■■ Mandelbrot Set where the horizontal and vertical dimen-
week. The probability is 688/4800 = .143333, the approximate derivative has the same ■■ Linear Equations sions are represented by the real and imagi-
which is slightly larger than 1/7 = .142857. shape as the graph of the original function ■■ Google PageRank nary components, respectively.
but is below it. In fact, the ratio yp./y is a ■■ Ordinary Differential Equations Translating the piece in response to mouse
The Exponential Function constant independent of x. ■■ Exponential Function motion is easily done by a complex vector
Too many students who have recently taken The M-file expgui lets you move the blue ■■ Predators and Prey subtraction
a beginning calculus course think the de- line with the mouse and slowly vary the base ■■ Shallow Water Equations Figure 4. This electronic puzzle is manipulated Figure 5. Make this T.
rivative of ex is xex-1. How can we better un- of ax. The second shows the graph of 3x and z = z - w
by complex arithmetic.
derstand the number e and the function ex? its approximate derivative. The green curve is Download Experiments with MATLAB at
MATLAB can compute powers, y = ax, for a now above the blue curve. As you move from www.mathworks.com/nn8/moler We rotate the piece about its center by an
scalar a and a vector x. a = 2 to a = 3, you pass the situation shown in angle θ using the following statements: The Game of Life In “Life”, the universe is an infinite, two-
the third plot, where the green curve is on top John Horton Conway’s “Game of Life” made dimensional rectangular grid. The popula-
a = 2
of the blue curve. For this value of a, the func- mu = mean(z) its debut on the cover of Scientific American tion is a collection of grid cells marked as
x = 0:0.01:2 tion is its own derivative. The value displayed omega = exp(i*theta)
in October 1970 and has had a small but alive. The population evolves at discrete time
y = a.^x for a is e and the function is ex, one of the most z = omega*(z – mu) + mu
loyal following ever since. It is an example of steps known as generations. At each step, the
important functions in mathematics. the complexities that can be generated by ap- fate of each cell is determined by the vital-
We’ve skipped an important but subtle Where do you learn that complex multi- parently simple cellular automata. ity of its eight nearest neighbors and by this
mathematical and computational question: plication by eiθ is a rotation?

2 Reprinted
2 Reprinted TheMathWorks
from from News&Notes
TheMathWorks News&Notes
| 2 0 0| 82 0 0 8 w w ww. w
mwa t. hmwaot hr kwso. cr ko sm. c o m w w w. m a t h w o r k s .c o m Reprinted from TheMathWorks News&Notes | 2 0 0 8 3
TheMathWorks News&Notes

rule: A live cell with two live neighbors, or The statements Because all populations follow this rule,
any cell with three live neighbors, is alive at n = size(X,1); the initial population configuration deter-
the next step. p = [n 1:n-1]; mines how the game of life plays out. In Bill
This deceptively simple rule leads to an q = [2:n 1]; Gosper’s initial population, known as the
incredible variety of patterns, puzzles, and Y = X(:,p)+X(:,q)+X(p,:)+X(q,:)+... glider gun (Figure 6), the central portion of
unsolved mathematical problems. X(p,p)+X(q,q)+X(p,q)+X(q,p); the gun oscillates, emitting an infinite stream
The “Game of Life” MATLAB program of gliders that pass out of view and into the
discussed in Experiments with MATLAB is generate another sparse matrix Y with ele- void. Gosper’s configuration was the first to
a beautiful example of the use of the sparse ments between 0 and 8 that count the num- create an unbounded population.
matrix data structure. The universe is a ber of live neighbors. The rule of “Life” is
sparse matrix X with a finite number of 1s then implemented in a single MATLAB Further Experimentation
marking the live cells. The size of X grows to statement: Reading about the exercises in Experiments
accommodate any expanding population. with MATLAB is not nearly as fun—or as
X = (X & (Y == 2)) | (Y == 3); educational—as running them yourself. So
if you find them interesting, or if you know
students who would be interested, please
take a look at Experiments with MATLAB.
Then run, discuss, and improve the ex-
periments together. Programs, especially
MATLAB programs, are vehicles of dis-
course with people, and not simply ways to
send commands to a machine. ■

Resources

Experiments with MATLAB


www.mathworks.com/nn8/moler
Figure 6. Bill Gosper’s glider gun.
Cleve’s Corner Collection
www.mathworks.com/nn8/clevescorner

© 2008 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See
4 www.mathworks.com/trademarks for a list of additional trademarks. Other product or brand names may be 91610v0010/08
0000v00 10/08
trademarks or registered trademarks of their respective holders.

Вам также может понравиться