A new app employs transformations of a graphic depicting a house to demonstrate matrix multiplication.... read more >>

]]>A new app employs transformations of a graphic depicting a house to demonstrate matrix multiplication.

The house in the animated gif has been featured in Gil Strang's textbook, *Introduction to Linear Algebra*. A quilt inspired by the house in on the cover of the third edition.

You can rotate the house with your mouse by clicking and dragging the peak of the roof. You can stretch the house horizontally by clicking and dragging either side. You can stretch the house vertically by dragging the floor.

The motion is effected by matrix multiplication. Rotating the roof through an angle $\theta$ defines a 2-by-2 orthogonal matrix $U$.

$$ U = \left( \begin{array}{rr} \cos\theta \ \ \sin\theta \\ -\sin\theta \ \ \cos\theta \\ \end{array} \right) $$

Moving a side defines a horizontal scaling $\sigma_1$. Moving the floor defines a vertical scaling $\sigma_2$. Together they form the diagonal scaling matrix $S$.

$$ S = \left( \begin{array}{rr} \sigma_1 \ \ 0 \\ 0 \ \ \sigma_2 \\ \end{array} \right) $$

The resulting 2-by-2 transformation matrix $A$ is the product of $U$ and $S$.

$$A = U S $$

Watch how the three matrices change as you manipulate the house. The scale factor $\sigma_1$ operates on the first column of $U$, while $\sigma_2$ scales the second.

The code for this app is available here.

Get
the MATLAB code

Published with MATLAB® R2016a

MATLAB Central is celebrating its 15th birthday this fall. In honor of the occasion, MathWorks bloggers are reminiscing about their first involvement with the Web site. My first contribution to the File Exchange was not MATLAB software, but rather a collection of documents that I called the Pentium Papers. I saved this material in November and December of 1994 when I was deeply involved in the Intel Pentium Floating Point Division Affair.... read more >>

]]>MATLAB Central is celebrating its 15th birthday this fall. In honor of the occasion, MathWorks bloggers are reminiscing about their first involvement with the Web site. My first contribution to the File Exchange was not MATLAB software, but rather a collection of documents that I called the Pentium Papers. I saved this material in November and December of 1994 when I was deeply involved in the Intel Pentium Floating Point Division Affair.

Pentium key chain. Image thanks to Thomas Johansson, thomas@cpucollection.se.

*Bad companies are destroyed by crises;
Good companies survive them;
Great companies are improved by them.;*

–Andy Grove, Chairman, Intel Corp., December 1994

As I said in my blog post in 2013, “The Pentium division bug episode in the fall of 1994 was a defining moment for the MathWorks, for the Internet, for Intel Corporation, and for me personally.”

In the fall of 1994 the Internet was not anything like it is today. The World Wide Web was in its infancy. All of the connections were text based. The Mosaic graphical browser was only a few months old and few people knew about it. Internet explorer did not yet exist and Google was four years in the future. We did have email. Some of us used FTP, File Transfer Protocol. And the social media of the day were the text only news groups, which had names like `comp.soft-sys.matlab` and `comp.soft-sys.intel`.

But calling the news groups “social media” is a stretch. The term “social media” didn’t exist in 1994. And almost all of the participants in the news groups were geeks and nerds in universities and industrial research labs.

Thomas Nicely, a Professor of Mathematics at Lynchburg College in Virginia, did research on prime numbers, especially *twin primes*. For his computational experiments he employed several IBM PCs running Intel 486 processors. In the summer of 1994 he added a PC with the new Pentium chip. Much to his surprise, he found the Pentium gave a different result for the reciprocal of one of his primes.

On October 30, Nicely emailed several other Pentium users that he had discovered a bug in the Pentium’s floating point arithmetic. The news soon reached Terje Mathisen, a computer jock at Norsk Hydro in Oslo, Norway, who had written about the accuracy of Intel’s transcendental functions. Mathisen confirmed the bug and, on November 3, posted a test program to `comp.soft-sys.intel`. A day later Andreas Kaiser in Germany used Mathisen’s program to post a list of numbers whose reciprocals were being computed to what appeared to be only single precision accuracy.

Tim Coe designed floating point hardware for an aerospace contractor in California. He didn’t have access to a Pentium machine. But from Kaiser’s list of erroneous reciprocals he was able to reverse engineer Intel’s division algorithm. He expected that certain divisions, involving quantities with specific bit patterns, would produce results that were much less accurate than even single precision. He drove to a local computer store, ran a calculator program on a Pentium in the showroom, and confirmed his prediction.

I first heard about the FDIV bug (FDIV is the mnemonic for Floating point Division on x86 processors) in early November from an email list for floating point arithmetic. It appeared to be a single/double precision hardware glitch. Annoying, but not surprising. I started to follow `comp.soft-sys.intel` anyway. But when Coe posted his results, and when MathWorks tech support got a couple of queries asking how this affect MATLAB, I got seriously interested.

On November 15, 1994, I made the first of what would become several postings to both `comp.soft-sys.intel` and `comp.soft-sys.matlab`, summarizing what I knew. I pointed out that the relative error in one of Coe’s examples is $6.1 \cdot 10^{-5}$. This is ten orders of magnitude larger than what we expect from MATLAB, or any other scientific computation using IEEE double precision. The error might not occur very often, but when it does, it can be very significant.

Within the next week the Net became very active. Several participants contacted newspapers and TV stations. Two engineers at the Jet Propulsion Laboratory convinced JPL to issue a press release announcing that they were no longer purchasing Pentium-based PCs. Reporters seeking more information found my posting and redistributed it.

On November 22 CNN sent a TV crew to MathWorks, interviewed me, and then led off their evening *Moneyline* with a story about Intel’s troubles. I spent the next day fielding phone calls from other reporters. A number of newspapers, including the *New York Times* and the *San Jose Mercury News*, ran stories on the 24th. The headline of the front page story in the *Boston Globe* was “Sorry, Wrong Number”.

As a corporation, Intel did not know how to react and handled the situation badly. They had little experience dealing with the public. Their customers were computer manufacturers, not individual computer users. They had no experience whatsoever dealing with the Internet. Their first response came in the form of canned statement that could be faxed back to anyone contacting tech support via fax. This FAXBACK document was soon posted to the Net, but not by Intel. I’ve included a copy in the Pentium Papers.

Intel’s statement said that they had already discovered the FDIV “flaw” themselves and had fixed it in recent releases of the Pentium chip. They claimed that it would occur only once in 9 billion divide operations and that the average spreadsheet user would encounter it only once in 27,000 years of use. They provided an 800 telephone number to call for anyone doing “prime number generation or other complex mathematics”. They would interview callers and offer to replace the chip for anyone they thought really required error-free divisions.

Needless to say, this aggressive non-apology only enflamed the criticism of Intel on the Net. Nobody believed their claims about the frequency of occurrence. The frequencies came from a study Intel had made, but initially refused to release. As far as I was concerned, the problem was not the likelihood of encountering the FDIV bug, it was the fact that we had to worry about it at all.

Tim Coe, Terje Mathisen and I devised a scheme where a Pentium FDIV hardware instruction could be replaced by less than a dozen lines of software that insured all divisions were done correctly. The idea is that the divisor of each prospective division operation would be checked for the presence of certain bit patterns in the floating point fraction that made it “at risk”. When an at risk divisor and the corresponding dividend are both scaled by 15/16, the quotient remains unchanged, but the operation can then be done safely by the FDIV instruction.

We wanted to make our workaround widely available. Intel contacted us and, along with Peter Tang, a computer scientist who was then at Argonne and who has been a consultant to Intel, we began to work, via conference calls, with a group at Intel. It was our intention to provide the workaround to compiler writers and major software vendors, and to announce its availability on the Net. The workaround macro would replace FDIV in all PC software being developed. (It was more complicated — functions like “mod” and “rem” and a few transcendental functions like “atan” that had Pentium hardware support were also involved.)

MATLAB was the proof-of-concept for the software workaround. We built and released a special “Pentium Aware” MATLAB. Its documentation says

MATLAB detects, and optionally repairs,

erroneous arithmetic results produced by Intel’s Pentium processor.

Erroneous results are infrequent, but can occur in many MATLAB operations

and functions. … When an erroneous result is detected, MATLAB prints a

message. … Options exist to suppress the printing of the messages,

count the number of occurrences, and suppress the corrections

altogether.

Our public relations firm had sent out a press release with the headline

THE MATHWORKS DEVELOPS FIX FOR THE INTEL PENTIUM(tm) FLOATING POINT ERROR

At the time, MathWorks had just reached its 10th anniversary. The company name was barely known in the industries we served, and completely unheard of in the public generally. So when a press release arrived saying this obscure little company in Massachusetts has fixed the Pentium bug, it created quite a stir. I got dozens of more phone calls.

I now have a folder of hundreds of press clippings from all over the world that resulted from the Pentium affair.

On December 12th IBM issued its own study. IBM had several reasons to get involved. They had invented, or at least named, the IBM PC, and one division was selling PCs employing the Pentium. Another division was developing and manufacturing its own chip, the PowerPC.

The IBM study claimed that typical spreadsheet calculations were likely to generate numbers with the “at risk” bit patterns and so FDIV errors were much more likely to occur than Intel claimed. The study also cleverly multiplied their predicted likelihood of an individual spreadsheet user encountering an error by an estimated total number of spreadsheet users worldwide to conclude “on a typical day a large number of people are making mistakes in their computation without realizing it.”

IBM announced they were suspending production of Pentium-based PCs.

Within hours of the IBM announcement, Intel’s stock price dropped 10 points. A week later Intel issued an apology and announced a no-questions-asked return policy on Pentium chips. They set up a network of service centers to handle the replacements and allocated $475 million to pay for the replacement program.

Months later, very few actual requests for replacements had been made. We had learned that encountering the FDIV error was, in fact, very unlikely. But more important, for most people, Intel’s apology was enough.

As all this was happening, I saved some of the postings from the `comp.soft-sys.intel` news groups, a few newspaper stories and a few other contemporary documents. I would respond to email requests for information with “If you have access to the Internet, you can download my Pentium Papers using anonymous ftp from `ftp.mathworks.com`.”

As the MathWorks Web site developed there was usually a note somewhere about how to get to the Pentium Papers. When we started MATLAB Central File Exchange I moved the collection there as my first contribution.

If you open the `.txt` files in the Pentium Papers with Microsoft Word, it will break up the long lines and produce a readable file.

Published with MATLAB® R2016a

]]>Jim Sanderson has had a fascinating professional life. He was my PhD student in math at the University of New Mexico in the 1970s. He spent almost 20 years as a computational scientist at Los Alamos National Laboratory, working on the lab's supercomputers. He then developed an interest in ecology, went back to school, and is now the world's leading authority on the preservation of small wild cats around the world.... read more >>

]]>Jim Sanderson has had a fascinating professional life. He was my PhD student in math at the University of New Mexico in the 1970s. He spent almost 20 years as a computational scientist at Los Alamos National Laboratory, working on the lab’s supercomputers. He then developed an interest in ecology, went back to school, and is now the world’s leading authority on the preservation of small wild cats around the world.

<http://globalwildlife.org/about-gwc/team/jim-sanderson-ph-d>

One of the great success stories of modern numerical linear algebra is the QR algorithm with the Francis shift for computing all the eigenvalues of a symmetric tridiagonal matrix. Jim Wilkinson showed that the algorithm was superior to all its competitors in the 1960s. So it was a key component of the Wilkinson and Reinsch *Handbook*, of *EISPACK*, and ultimately of the first MATLAB. It survives today as one of the primary routines in LAPACK and MATLAB.

Wilkinson established the algorithm’s global convergence and its asymptotic cubic convergence rate. But, curiously, he never analyzed its roundoff error behavior. This was Jim Sanderson’s PhD thesis. At the time Jim was writing his thesis, the academic computer science community was beginning to be interested in the notion of proving algorithms correct. Jim found it necessary to reorder the operations in Wilkinson’s implementation, but with this modification he was able to prove that the algorithm was correct. It was guaranteed to be globally, and cubically, convergent even in the presence of floating point roundoff errors.

After grad school, Jim joined the staff of the Los Alamos National Laboratory, located in the high desert mountains of northern New Mexico. Starting with the Manhattan Project, the Lab had originally developed the nation’s atomic and nuclear weapons. But with the limits imposed by international treaties, the Lab’s mission evolved from developing new weapons to maintaining existing stockpiles.

I’m not exactly sure what Jim did during his almost 20 years at Los Alamos. He is not allowed to tell me in much detail. At first it involved image processing. Later it involved large computer programs, very likely written in Fortran, run on whatever were the world’s fastest supercomputers of the day, that implemented partial differential equation models of complex nuclear reactions.

During his time at Los Alamos, Jim developed a hobby, nature photography. He says it was esoteric, artistic stuff, with photos of fallen leaves on the ground and dark clouds in a storm.

Eliot Porter was one of the first photographers to exhibit and publish color photographs of nature. He and his family eventually established a ranch in Tesuque, New Mexico, not far from Los Alamos. Jim came to work under Eliot and travel with him on several photography expeditions. Porter had a strong influence on Jim’s interest in nature photography and conservation.

In 1995 Jim left the Labs and, in middle age, returned to undergrad studies, this time majoring in biology, again at the University of New Mexico. After graduating for the second time from UNM, Jim went on to the highly regarded graduate program in Wildlife Ecology and Conservation at the University of Florida.

After completing grad school in Ecology at UFL, Jim joined Conservation International, working as a landscape ecologist.

By this time Jim was interested in small, wild cats. Big cats — lions, tigers, cheetahs, leopards — get most of the world’s attention. But there are over two dozen smaller wild cats, most of which we’ve never heard of and many of which are endangered. These cats have been an important focus in Jim’s second career.

Jim formed the Small Cat Conservation Alliance in 2006. That organization evolved into the Small Wild Cat Conservation Foundation in 2008.

One of the first cats to get Jim’s attention was the Andean Mountain Cat. Jim was the first scientist to confirm this cat’s existence. It lives above 14,000 feet in Andes of Chile and Bolivia. Before Jim’s work it was only known as a mythical creature that threatened the villagers’ chickens.

*Andean_cat_1_Jim_Sanderson.jpg*, Small Wild Cat Conservation Foundation.

Jim took this portrait of the cat in 1998. He says he brought all of his nature photography skills to bear when he took this shot. The photo was featured in an ad for Canon cameras that ran in the National Geographic magazine and that earned a sizable contribution to the Small Cat Conservation Alliance.

A few years ago Jim was on a long flight back from South America. He noticed a guy across the aisle doing some sort of mathematical looking puzzle in the airline magazine and asked him what it was. The guy introduced Jim (who had apparently been living away from the rest of civilization) to Sudoku. Jim returned to his seat, and to his laptop, and wrote a Fortran (more evidence of his lifestyle) program to play Sudoku.

When showed the solution to the puzzle, the guy across the aisle, not realizing that Jim had written an entire puzzle-solving program, was not impressed. “Took you a long time”, was the terse reaction. No more conversation there.

Jim told me about the incident some time later. I had a different reaction. “Time you learned to use MATLAB.”

I had been avoiding Sudoku because I was afraid I would get sucked in. To this day I’ve never done a puzzle by hand. But the program I wrote to demonstrate MATLAB to Jim is the same program that I’ve described in Cleve’s Corner, MathWorks News & Notes. I got sucked in in a different way.

Jared Diamond is a Professor of Geography and Physiology at UCLA. He is best known for the 1998 Pulitzer Prize winning book “Guns, Germs, and Steel: The Fates of Human Societies”. A documentary based on the book was produced by the National Geographic Society and broadcast on PBS.

Before writing about the great issues that made him famous, one of Diamond’s first interests in science was birds. How do various species of birds distribute themselves across strings of islands? Is the distribution random? Is it different from what it would be if the species did not interact?

In 1975, in a 102 page paper, “Assembly of species communities”, Diamond introduced the concepts of *competitive exclusion* and *checkboard distributions*. He suggests that species composition on a particular island is due to minimization of unutilized resources. The resulting distributions observed across an archipelago are alternating patterns that he called *checkerboards*. The data supporting his hypothesis came from his own extensive personal observations in the Bismarck Archipelago near Papua New Guinea in the western Pacific Ocean, as well as that of other visiting observers and natives.

In 1979, Edward Connor and Daniel Simberloff set off a controversy that continues to this day when they published “Assembly of species communities: chance or competition?” They pointed out that Diamond’s observed patterns had not been tested against appropriate null hypotheses. They suggested that the observed patterns could not be distinguished from patterns resulting from random distributions of the species across the subject area.

Sanderson became interested in the controversy when he read the papers while he was in school. He figured he could settle whether the patterns were random with some computer simulations. He needed to generate random matrices while maintaining constraints on the row and column sums. One approach involving the Knight’s Tour proved to be too slow to generate the $10^6$ random matrices required in this situation and faster alternatives were developed.

Jim’s simulations strongly supported Diamond’s thesis. Stuart Pimm, now a professor at Duke, had been a professor at Florida, knew Diamond, and sent Jim’s work to Diamond. A paper by the three of them, James Sanderson, Jared Diamond, and Stuart Pimm, “Pairwise Co-existence of Bismarck and Solomon Landbird Species”, was finally published in 2009. The paper criticized Connor and Simberloff’s methodology and conclusions in no uncertain terms.

Connor and Simberloff, together with Michael Collins, responded with “The checkered history of checkerboard distributions” in 2013. They claim that “Few, if any, ‘true checkerboards’ exist in these archipelagoes that could possibly have been influenced by competitive interactions.”

A pair of back-to-back notes published in December 2015 continue the interchange. One is by Diamond, Pimm, and Sanderson. “The checkered history of checkerboard distributions: comment”. The follow-up comes from Connor, Collins, and Simberloff. “The checkered history of checkerboard distributions: reply”. One of the arguments is over convex hulls on maps.

Jim and Stuart Pimm have written a book describing the whole affair, at least as of last year, that is accessible to a wider audience.

James G. Sanderson and Stuart L. Pimm, _Patterns in Nature_, The Analysis of Species Co-occurrences, The University of Chicago Press, 205 pages, 2015.

In the preface to the book they say, “Some of what happened thereafter was simply ugly, involving one of the most violent clashes of personalities in recent ecological history.” They quote Professor R. J. Putman of the University of Glasgow, even as far back as 1994, that the debate was “almost unprecedented in the apparent entrenchment and hostility of the opposing camps.”

Jim recently joined a conservation organization based in Austin that was established by some friends of his, Global Wildlife Conservation, <http://globalwildlife.org>. He is their new Director of Wild Cat Conservation.

Published with MATLAB® R2016a

]]>A few days ago we received email from Mike Hennessey, a mechanical engineering professor at the University of St. Thomas in St. Paul, Minnesota. He has been reading my book "Numerical Computing with MATLAB" very carefully. Chapter 7 is about "Eigenvalues and Singular Values" and section 10.3 is about one of my all-time favorite MATLAB demos, `eigshow`. Mike discovered an error in my description of the `svd` option of `eigshow` that has gone unnoticed in the over ten years that the book has been available from both the MathWorks web site and SIAM.... read more >>

A few days ago we received email from Mike Hennessey, a mechanical engineering professor at the University of St. Thomas in St. Paul, Minnesota. He has been reading my book “Numerical Computing with MATLAB” very carefully. Chapter 7 is about “Eigenvalues and Singular Values” and section 10.3 is about one of my all-time favorite MATLAB demos, `eigshow`. Mike discovered an error in my description of the `svd` option of `eigshow` that has gone unnoticed in the over ten years that the book has been available from both the MathWorks web site and SIAM.

The program `eigshow` has been in the MATLAB `demos` directory for many years. I wrote a three-part series of posts about `eigshow` in this blog three years ago, but I’m happy to write another post now.

The `svd` option of `eigshow` invites you to use your mouse to move the green vector `x` and make `A*x` perpendicular to `A*y`. The animated gif above simulates that motion. The following figure is the desired final result.

As you move `x`, the vector `y` follows along, staying perpendicular to `x`. The two trace out the green unit circle. The default matrix `A` is shown in the title. (If you run your own `eigshow` you can change the matrix by editing the text box in the title.)

A = [1 3; 4 2]/4

A = 0.2500 0.7500 1.0000 0.5000

The blue vectors `Ax` and `Ay` are the images of `x` and `y` under the multiplicative mapping induced by `A`. As you move `x` and `y` around the unit circle `Ax` and `Ay` sweep out the blue ellipse. And when you stop with `Ax` perpendicular to `Ay`, they turn out to be the major and minor axes of the ellipse.

*Numerical Computing with MATLAB*, which is known to its friends as simply *NCM*, was published over 10 years ago, in 2004. An electronic edition is available from MathWorks and a print edition from SIAM. In chapter 7, the singular value decomposition, the SVD, of a real matrix, $A$, is defined as the product

$$ A = U \Sigma V^T$$

In the simplest case where we assume $A$ is square, $U$ and $V$ are orthogonal and $\Sigma$ is diagonal.

This is illustrated by figure 10.3 in *NCM*, which is the same as the final figure here. It shows the action with the `svd` option on the default matrix. The explanation is given by the first sentence on page 7.

The vectors x and y are the columns of U in the SVD, the vectors

Ax and Ay are multiples of the columns of V, and the lengths of

the axes are the singular values.

Sounds OK, right? This is the sentence that Mike Hennessey questioned. Now that I call your attention to it, you should be able to spot the error right away. I’ll tell you what it is at the end of this post.

Here is the relevant paragraph from the `help` entry for `eigshow` . It does not get into the same trouble as *NCM*.

In svd mode, the mouse moves two perpendicular unit vectors, x and y.

The resulting A*x and A*y are plotted. When A*x is perpendicular to

A*y, then x and y are right singular vectors, A*x and A*y are

multiples of left singular vectors, and the lengths of A*x and A*y

are the corresponding singular values.

Here’s how I often think about the SVD. Take the definition,

$$A = U \Sigma V^T$$

Multiply both sides on the right by $V$.

$$A V = U \Sigma$$

The diagonal matrix $\Sigma$ is on the right so that the singular values can multiply the columns of $U$. When we write this out column by column, we have

$$A v_j = \sigma_j u_j, \ \ j = 1,\ …, n$$

A little bit more manipulation leads to

$$A^T u_j = \sigma_j v_j, \ \ j = 1,\ …, n$$

Let’s see how this works out with the default 2-by-2 matrix.

A

A = 0.2500 0.7500 1.0000 0.5000

Compute the SVD.

[U,S,V] = svd(A)

U = -0.5257 -0.8507 -0.8507 0.5257 S = 1.2792 0 0 0.4886 V = -0.7678 0.6407 -0.6407 -0.7678

The singular values are on the diagonal of `S`.

sigma = diag(S)

sigma = 1.2792 0.4886

These are the lengths of the two blue vectors.

It turns out that the components of $v_1$, the right singular vector corresponding to the largest singular value of an (irreducible) matrix with nonnegative elements all have the same sign. They could be all positive or all negative — take your pick. MATLAB happens to always pick negative. So, both components of V(:,1) are negative. But we stopped `eigshow` with a positive `x`. So we flip the sign of `V(:,1)`.

x = -V(:,1) y = V(:,2)

x = 0.7678 0.6407 y = 0.6407 -0.7678

These two look like the two green vectors in the final figure. And these look like the two blue vectors.

Ax = A*x Ay = A*y

Ax = 0.6725 1.0881 Ay = -0.4156 0.2569

Now check that the SVD relationship works.

AV = A*V USigma = U*S

AV = -0.6725 -0.4156 -1.0881 0.2569 USigma = -0.6725 -0.4156 -1.0881 0.2569

What was the error in *NCM* that Mike Hennessey reported and that prompted me to write this post? The matrices U and V should be switched. The sentence should read

The vectors x and y are the columns of V in the SVD, the vectors

Ax and Ay are multiples of the columns of U, and the lengths of

the axes are the singular values.

Every time I work with the SVD I have to think carefully about U and V. Which is which? Maybe writing this post will help.

Cleve Moler, *Numerical Computering With MATLAB*, 2004.

MathWorks, <http://www.mathworks.com/moler/chapters.html>,

SIAM, <http://epubs.siam.org/doi/book/10.1137/1.9780898717952>,

Published with MATLAB® R2016a

]]>Classical Gram-Schmidt and Modified Gram-Schmidt are two algorithms for orthogonalizing a set of vectors. Householder elementary reflectors can be used for the same task. The three algorithms have very different roundoff error properties.... read more >>

]]>Classical Gram-Schmidt and Modified Gram-Schmidt are two algorithms for orthogonalizing a set of vectors. Householder elementary reflectors can be used for the same task. The three algorithms have very different roundoff error properties.

My colleague and friend G. W. Stewart is a Distinguished University Professor Emeritus at the Department of Computer Science, University of Maryland. Everybody knows him as “Pete”. He has never been able to satisfactorily explain the origins of “Pete” to me. It somehow goes back through his father to his grandfather and maybe great grandfather, who were also nicknamed “Pete”.

Pete has written several books on numerical linear algebra. For my blog today I am going to rely on the descriptions and pseudocode from his book “Matrix Algorithms, Volume I: Basic Decompositions”. His pseudocode is MATLAB ready.

The classic Gram-Schmidt algorithm is the first thing you might think of for producing an orthogonal set of vectors. For each vector in your data set, remove its projection onto the data set, normalize what is left, and add it to the orthogonal set. Here is the code. `X` is the original set of vectors, `Q` is the resulting set of orthogonal vectors, and `R` is the set of coefficients, organized into an upper triangular matrix.

```
type gs
```

function [Q,R] = gs(X) % Classical Gram-Schmidt. [Q,R] = gs(X); % G. W. Stewart, "Matrix Algorithms, Volume 1", SIAM, 1998. [n,p] = size(X); Q = zeros(n,p); R = zeros(p,p); for k = 1:p Q(:,k) = X(:,k); if k ~= 1 R(1:k-1,k) = Q(:,k-1)'*Q(:,k); Q(:,k) = Q(:,k) - Q(:,1:k-1)*R(1:k-1,k); end R(k,k) = norm(Q(:,k)); Q(:,k) = Q(:,k)/R(k,k); end end

The entire process can be expressed by matrix multiplication, with orthogonal $Q$ and upper triangular $R$.

$$X = QR$$

This is a rather different algorithm, not just a simple modification of classical Gram-Schmidt. The idea is to orthogonalize against the emerging set of vectors instead of against the original set. There are two variants, a column-oriented one and a row-oriented one. They produce the same results, in different order. Here is the column version,

```
type mgs
```

function [Q,R] = mgs(X) % Modified Gram-Schmidt. [Q,R] = mgs(X); % G. W. Stewart, "Matrix Algorithms, Volume 1", SIAM, 1998. [n,p] = size(X); Q = zeros(n,p); R = zeros(p,p); for k = 1:p Q(:,k) = X(:,k); for i = 1:k-1 R(i,k) = Q(:,i)'*Q(:,k); Q(:,k) = Q(:,k) - R(i,k)*Q(:,i); end R(k,k) = norm(Q(:,k))'; Q(:,k) = Q(:,k)/R(k,k); end end

A Householder reflection $H$ transforms a given vector $x$ into a multiple of a unit vector $e_k$ while preserving length, so $Hx = \pm \sigma e_k$ where $\sigma = ||x||$ .

The matrix $H$ is never formed. The reflection is expressed as a rank-one modification of the identity

$$H = I – uu^T \ \mbox{where} \ ||u|| = \sqrt{2}$$

(The use of $\sqrt{2}$ here is Pete’s signature normalization.)

```
type housegen
```

function [u,nu] = housegen(x) % [u,nu] = housegen(x) % Generate Householder reflection. % G. W. Stewart, "Matrix Algorithms, Volume 1", SIAM, 1998. % [u,nu] = housegen(x). % H = I - uu' with Hx = -+ nu e_1 % returns nu = norm(x). u = x; nu = norm(x); if nu == 0 u(1) = sqrt(2); return end u = x/nu; if u(1) >= 0 u(1) = u(1) + 1; nu = -nu; else u(1) = u(1) - 1; end u = u/sqrt(abs(u(1))); end

Now to apply $H$ to any other $y$, compute the inner product

$$\tau = u^Ty$$

and then simply subtract

$$Hy = x – \tau u$$

This program does not actually compute the QR orthogonalization, but rather computes `R` and a matrix `U` containing vectors that generate the Householder reflectors whose product is Q.

```
type hqrd
```

function [U,R] = hqrd(X) % Householder triangularization. [U,R] = hqrd(X); % Generators of Householder reflections stored in U. % H_k = I - U(:,k)*U(:,k)'. % prod(H_m ... H_1)X = [R; 0] % where m = min(size(X)) % G. W. Stewart, "Matrix Algorithms, Volume 1", SIAM, 1998. [n,p] = size(X); U = zeros(size(X)); m = min(n,p); R = zeros(m,m); for k = 1:min(n,p) [U(k:n,k),R(k,k)] = housegen(X(k:n,k)); v = U(k:n,k)'*X(k:n,k+1:p); X(k:n,k+1:p) = X(k:n,k+1:p) - U(k:n,k)*v; R(k,k+1:p) = X(k,k+1:p); end end

All three of these algorithms compute `Q` and `R` that do a good job of reproducing the data `X`, that is

`Q`*`R`is always close to`X`for all three algorithms.

On the other hand, their behavior is very different when it comes to producing orthogonality. Is `Q'*Q` close to the identity?

- Classic Gram-Schmidt. Usually very poor orthogonality.
- Modified Gram-Schmidt. Depends upon condition of
`X`. Fails completely when`X`is singular. - Householder triangularization. Always good orthogonality.

```
type compare.m
```

function compare(X); % compare(X) % Compare three QR decompositions, % I = eye(size(X)); %% Classic Gram Schmidt [Q,R] = gs(X); qrerr_gs = norm(Q*R-X,inf)/norm(X,inf); ortherr_gs = norm(Q'*Q-I,inf); %% Modified Gram Schmidt [Q,R] = mgs(X); qrerr_mgs = norm(Q*R-X,inf)/norm(X,inf); ortherr_mgs = norm(Q'*Q-I,inf); %% Householder QR Decomposition [U,R] = hqrd(X); QR = R; E = I; for k = size(X,2):-1:1 uk = U(:,k); QR = QR - uk*(uk'*QR); E = E - uk*(uk'*E) - (E*uk)*uk' + uk*(uk'*E*uk)*uk'; end qrerr_h = norm(QR-X,inf)/norm(X,inf); ortherr_h = norm(E-I,inf); %% Report results fprintf('QR error\n') fprintf('Classic: %10.3e\n',qrerr_gs) fprintf('Modified: %10.3e\n',qrerr_mgs) fprintf('Householder: %10.3e\n',qrerr_h) fprintf('\n') fprintf('Orthogonality error\n') fprintf('Classic: %10.3e\n',ortherr_gs) fprintf('Modified: %10.3e\n',ortherr_mgs) fprintf('Householder: %10.3e\n',ortherr_h)

Well conditioned.

compare(magic(7))

QR error Classic: 1.726e-16 Modified: 6.090e-17 Householder: 3.654e-16 Orthogonality error Classic: 3.201e+00 Modified: 1.534e-15 Householder: 1.069e-15

Poorly conditioned, nonsingular.

compare(hilb(7))

QR error Classic: 5.352e-17 Modified: 5.352e-17 Householder: 7.172e-16 Orthogonality error Classic: 5.215e+00 Modified: 1.219e-08 Householder: 1.686e-15

Singular.

compare(magic(8))

QR error Classic: 1.435e-16 Modified: 8.540e-17 Householder: 2.460e-16 Orthogonality error Classic: 5.413e+00 Modified: 2.162e+00 Householder: 2.356e-15

G. W. Stewart, “Matrix Algorithms, Volume 1: Basic Decompositions”, SIAM, 1998.

https://www.amazon.com/Matrix-Algorithms-1-Basic-Decompositions/dp/0898714141

Published with MATLAB® R2016a

]]>At a minisymposium honoring Charlie Van Loan this week during the SIAM Annual Meeting, I will describe several dubious methods for computing the zeros of polynomials. One of the methods is the Graeffe Root-squaring method, which I will demonstrate using my favorite cubic, $x^3-2x-5$.... read more >>

]]>At a minisymposium honoring Charlie Van Loan this week during the SIAM Annual Meeting, I will describe several dubious methods for computing the zeros of polynomials. One of the methods is the Graeffe Root-squaring method, which I will demonstrate using my favorite cubic, $x^3-2x-5$.

What is today often called the Graeffe Root-Squaring method was discovered independently by Dandelin, Lobacevskii, and Graeffe in 1826, 1834 and 1837. A 1959 article by Alston Householder referenced below straightens out the history.

The idea is to manipulate the coefficients of a polynomial to produce a second polynomial whose roots are the squares of the roots of the first. If the original has a dominant real root, it will become even more dominant. When the process is iterated you eventually reach a point where the dominant root can be read off as the ratio of the first two coefficients. All that remains is to take an n-th root to undo the iteration.

Here is an elegant bit of code for producing a cubic whose roots are the squares of the roots of a given cubic.

```
type graeffe
```

function b = graeffe(a) % a = a(1)*x^3 + ... + a(4) is a cubic. % b = graffe(a) is a cubic whose roots are the squares of the roots of a. b = [ a(1)^2 -a(2)^2 + 2*a(1)*a(3) a(3)^2 - 2*a(2)*a(4) -a(4)^2 ];

I discussed my favorite cubic, $z^3-2z-5$, in a series of posts beginning with a historic cubic last December 21st.

A contour plot of the magnitude of this cubic on a square region in the plane shows the dominant real root at approximately $x=2.09$ and the pair of complex conjugate roots with smaller magnitude approximately $|z|=1.55$.

graphic

Repeated application of the transformation essentially squares the coefficients. So the concern is overflow. When I first ran this years ago as a student on the Burroughs B205, I had a limited floating point exponent range and overflow was a severe constraint. Today with IEEE doubles we're a little better off.

Here is a run on my cubic. I'm just showing a few significant digits of the polynomial coefficients because the important thing is their exponents.

a = [1 0 -2 -5]; k = 0; f = '%5.0f %4.0f %5.0f %8.3e %8.3e %8.3e %20.15f \n'; fprintf(f,k,1,a,Inf); while all(isfinite(a)) k = k+1; a = graeffe(a); n = 2^k; r = (-a(2))^(1/n); fprintf(f,k,n,a,r) end

0 1 1 0.000e+00 -2.000e+00 -5.000e+00 Inf 1 2 1 -4.000e+00 4.000e+00 -2.500e+01 2.000000000000000 2 4 1 -8.000e+00 -1.840e+02 -6.250e+02 1.681792830507429 3 8 1 -4.320e+02 2.386e+04 -3.906e+05 2.135184796196703 4 16 1 -1.389e+05 2.316e+08 -1.526e+11 2.096144583759898 5 32 1 -1.883e+10 1.125e+16 -2.328e+22 2.094553557477412 6 64 1 -3.547e+20 -7.504e+32 -5.421e+44 2.094551481347086 7 128 1 -1.258e+41 1.786e+65 -2.939e+89 2.094551481542327 8 256 1 -1.582e+82 -4.203e+130 -8.636e+178 2.094551481542327 9 512 1 -2.504e+164 -9.665e+260 -Inf 2.094551481542327

So after seven steps we have computed the dominant root to double precision accuracy, but it takes the eighth step to confirm that. And after nine steps we run out of exponent range.

This is a really easy example. To make a robust polynomial root finder we would have to confront under/overflow, scaling, multiplicities, complex roots, and higher degree. As far as I know, no one has ever made a serious piece of mathematical software out of the Graeffe Root-squaring method. If you know of one, please contribute a comment.

Alston S. Householder, "Dandelin, Lobacevskii, or Graeffe?," *Amer. Math. Monthly*, 66, 1959, pp. 464–466. <http://www.jstor.org/stable/2310626?seq=1#>

Get
the MATLAB code

Published with MATLAB® R2016a

During the SIAM Annual Meeting this summer in Boston there will be a special minisymposium Wednesday afternoon, July 13, honoring Charlie Van Loan, who is retiring at Cornell. (I use "at" because he's not leaving Ithaca.) I will give a talk titled "19 Dubious Way to Compute the Zeros of a Polynomial", following in the footsteps of the paper about the matrix exponential that Charlie and I wrote in 1978 and updated 25 years later. I really don't have 19 ways to compute polynomial zeros, but then I only have a half hour for my talk. Most of the methods have been described previously in this blog. Today's post is mostly about "roots".... read more >>

]]>During the SIAM Annual Meeting this summer in Boston there will be a special minisymposium Wednesday afternoon, July 13, honoring Charlie Van Loan, who is retiring at Cornell. (I use "at" because he's not leaving Ithaca.) I will give a talk titled "19 Dubious Way to Compute the Zeros of a Polynomial", following in the footsteps of the paper about the matrix exponential that Charlie and I wrote in 1978 and updated 25 years later. I really don't have 19 ways to compute polynomial zeros, but then I only have a half hour for my talk. Most of the methods have been described previously in this blog. Today's post is mostly about "roots".

Almost 40 years ago, in the late 1970s, when I was developing the original Fortran-based MATLAB, I wanted to have a command to find the roots of a polynomial. At the time MATLAB was just a primitive matrix calculator. It was not yet a programming environment; I did not yet have M-files or toolboxes.

I had two objectives. I needed to keep the amount of software to a minimum. Mike Jenkins was a friend of mine who had recently submitted his Ph. D. thesis, under Joe Traub's direction, to Stanford, with what is now known as the Jenkins-Traub algorithm for polynomial zero finding. But that would require way too much memory for my MATLAB. And I wanted to turn the tables on the traditional role the characteristic polynomial plays in linear algebra. First year students are taught to compute eigenvalues of a matrix by finding the zeros of the determinant of its companion matrix. Wilkinson had shown that in the presence of roundoff error this is a bad idea. I wanted to emphasize Wilkinson's point by using powerful matrix eigenvalue techniques on the companion matrix to compute polynomial roots.

I already had the EISPACK code for the Francis double shift QR iteration to find the eigenvalues of a Hessenberg matrix. It took just a few more lines to find polynomial roots.

For example, MATLAB represents this polynomial $z^5 - 15 z^4 + 85 z^3 - 225 z^2 + 274 z - 120$ by a vector of coefficients.

p = [1 -15 85 -225 274 -120]

p = 1 -15 85 -225 274 -120

The `roots` function finds its roots.

roots(p)

ans = 5.0000 4.0000 3.0000 2.0000 1.0000

MATLAB does this by forming the companion matrix, which is upper Hessenberg, and computing its eigenvalues.

A = [15 -85 225 -274 120 eye(4,5) ] eig(A)

A = 15 -85 225 -274 120 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 ans = 5.0000 4.0000 3.0000 2.0000 1.0000

In the original documentation for "roots" I wrote

It uses order n^2 storage and order n^3 time. An algorithm designed specifically for polynomial roots might use order n storage and n^2 time. And the roundoff errors introduced correspond to perturbations in the elements of the companion matrix, not the polynomial coefficients.

So, at the time I knew that "roots" had impeccable numerical properties from the matrix eigenvalue point of view, but I wasn't sure about the numerical properties from the polynomial of view. Were we computing the exact roots of some polynomial near the original one? Maybe not. It depends crucially upon the scaling.

"Roots" is possibly dubious numerically, and certainly dubious from a computation cost point of view.

Almost twenty years later, in 1994 and 1995, a pair of papers, one by Nick Trefethen and K. C. Toh, and one by Alan Edelman and H. Murakami, finally gave "roots" a passing grade numerically, provided the companion matrix is first scaled by balancing.

Balancing was introduced by Parlett and Reinsch in the Algol procedures that spawned EISPACK. Balancing is a diagonal similarity transformation by a matrix of powers of two to attempt to bring a nonsymmetric matrix closer to symmetric by making its row and column norms equal. It is available in MATLAB through the function `balance` and is used by default by `eig`.

To see the importance of balancing, consider the companion matrix of $W_{20}$, the Wilkinson polynomial of degree 20 whose roots are the integers from 1 to 20. The coefficients of this polynomial are huge. The constant term is 20!, which is 2432902008176640000. But that's not the largest. The coefficient of $z^2$ is 13803759753640704000. Five of the coefficients, those of $z^3$ through $z^7$ are so large that they cannot be exactly represented as double precision floating pointing numbers. So

roots(poly(1:20))

starts with a companion matrix that has coefficients of magnitude `10^19` along its first row and ones on its subdiagonal. It is seriously nonsymmetric.

The elements of the balanced matrix are much more reasonable. They range from just 1/4 to 256. This is the matrix involved in the actual eigenvalue calculation. All the scaling action is captured in the diagonal similarity, whose elements range from $2^{-35}$ to $2^{27}$. This matrix is only involved in scaling the eigenvectors.

So I was lucky on the numerical side. Balancing turned out to provide "roots" satisfactory stability.

Let's measure the execution time and memory requirements for "roots" with polynomials of very large degree -- tens of thousands. Here I just wanted to see how bad it is. Short answer: it takes "roots" almost 40 minutes to find the zeros of a polynomial of order sixteen thousand on my Lenovo X250 laptop.

Here is the experiment. Polynomials of degree `1000*n` for `n = 1:16`.

T = zeros(16,1); for n = 1:16 n r = randn(1,1000*n); tic roots(r); t = toc T(n) = t; !systeminfo | grep 'In Use' end

As expected, the observed exection times are fit nicely by a cubic in the polynomial degree.

time_roots

c = 0 1.8134 0.4632

On the other hand, the measured virtual memory requirements are all other the map. A least squares fit by a quadratic in the polynomial degree $n$ shows very little dependence on the $n^2$ term. It's fair to say that on today's machines memory requirements are not a significant restriction for this algorithm.

memory_roots

c = 1.0e+03 * 3.0328 0.0177 0.0002

AMVW are the initials of the last names of Jared Aurentz, Thomas Mach, Raf Vandebril, and David Watkins. It is also the name of their Fortran code for what they claim is a competitor to "roots". Their recent paper in SIMAX, referenced below, summarizes all the work that has been done on computing the eigenvalues of companion matrices, exploiting their special structure.

They also present their own algorithm where the companion matrix is never formed explicitly, but is represented as a product of Givens rotations. The QR algorithm is carried out on this representation. As a result only $O(n)$ storage and $O(n^2)$ operations are required.

But their software is in Fortran. I was a big Fortran fan for a long time. I used to be considered one of the Fortran gurus at MathWorks. I now don't even have access to a Fortran compiler. So I can't try their stuff myself.

So far I've been talking about the classic companion matrix where the polynomial coefficients occupy the entire first or last row or column. In 2003 Miroslav Fiedler introduced another version of a companion matrix, a pentadiagonal matrix with the polynomial coefficients alternating with zeros on the super- and subdiagonal and ones and zeros alternating on the next diagonals of the nonsymmetric pentadiagonal matrix. I wrote about it my blog just before last Christmas.

Here's an example, W_8. First, the coefficients.

c = poly(1:8)'

c = 1 -36 546 -4536 22449 -67284 118124 -109584 40320

The Fiedler companion matrix.

F = fiedler(c(2:end))

F = Columns 1 through 6 36 -546 1 0 0 0 1 0 0 0 0 0 0 4536 0 -22449 1 0 0 1 0 0 0 0 0 0 0 67284 0 -118124 0 0 0 1 0 0 0 0 0 0 0 109584 0 0 0 0 0 1 Columns 7 through 8 0 0 0 0 0 0 0 0 1 0 0 0 0 -40320 0 0

Balance it.

B = balance(F)

B = Columns 1 through 7 36.0000 -17.0625 16.0000 0 0 0 0 32.0000 0 0 0 0 0 0 0 8.8594 0 -5.4807 8.0000 0 0 0 8.0000 0 0 0 0 0 0 0 0 2.0533 0 -0.9012 1.0000 0 0 0 4.0000 0 0 0 0 0 0 0 0 0.8361 0 0 0 0 0 0 0.5000 0 Column 8 0 0 0 0 0 0 -0.6152 0

Check the eigenvalues

e = eig(B)

e = 8.0000 7.0000 6.0000 5.0000 4.0000 3.0000 2.0000 1.0000

If we could find a way to compute the eigenvalues and eigenvectors of the Fiedler companion matrix while exploiting its structure, then we would have a gorgeous algorithm. Beresford Parlett tells me that he's working on it.

If you have software that I don't know about, please comment.

Kim-Chuan Toh and Lloyd N. Trefethen, "Pseudozeros of polynomials and pseudospectra of companion matrices," *Numer. Math.*, 68, 1994. <http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.49.9072&rep=rep1&type=pdf>

Alan Edelman and H. Murakami, "Polynomial roots from companion matrices," *Mathematics of Computation*, 64, 1995. <http://www.ams.org/journals/mcom/1995-64-210/S0025-5718-1995-1262279-2/>

Jared L. Aurentz, Thomas Mach, Raf Vandebril, and David S. Watkins, "Fast and backward stable computation of roots of polynomials," *SIAM J. Matrix Anal. Appl.*, 36, 2015. <http://www.math.wsu.edu/faculty/watkins/pdfiles/AMVW15_SIMAX.pdf>

Miroslav Fiedler, A note on companion matrices, Linear Algebra and its Applications 372 (2003), 325-331, <http://citeseerx.ist.psu.edu/viewdoc/download>

Get
the MATLAB code

Published with MATLAB® R2016a

What does $\sqrt[12]{2}$ have to do with music? What are *equal temperament* and *just intonation*? How can the MATLAB function `rats` help tune a piano? (This post is based in part on the *Music* chapter in my online book, *Experiments in MATLAB*.)... read more >>

What does $\sqrt[12]{2}$ have to do with music? What are *equal temperament* and *just intonation*? How can the MATLAB function `rats` help tune a piano? (This post is based in part on the *Music* chapter in my online book, *Experiments in MATLAB*.)

In the theory of music, an *octave* is an interval with frequencies that range over a factor of two. In most Western music, an octave is divided into 12 *semitones* in a geometric progression. In other words the notes have equal frequency ratios. Since 12 semitones comprise a factor of 2, a single semitone is a factor of $\sqrt[12]{2}$. And because this quantity occurs so often in this discussion, let

$$ \sigma = \sqrt[12]{2} $$

Our MATLAB programs use

```
format short
sigma = 2^(1/12)
```

sigma = 1.0595

Think of $\sigma$ as an important mathematical constant, like $\pi$ and $\phi$.

Here is our miniature piano keyboard with 25 keys.

small_keyboard

This keyboard has two octaves, with white keys labeled **C D … G A B**, plus another **C** key. Counting both white and black, there are twelves keys in each octave. The frequency of each key is a semitone above or below its neighbors. Each black key can be regarded as either the *sharp* of the white below it or the *flat* of the white above it. So the black key between **C** and **D** is both **C** $\sharp$ and **D** $\flat$. There are no **E** $\sharp$ / **F** $\flat$ or **B** $\sharp$ / **C** $\flat$.

A conventional full piano keyboard has 88 keys. Seven complete octaves account for $7 \times 12 = 84$ keys. There are three additional keys at the lower left and one additional key at the upper end. If the octaves are numbered 0 through 7, then a key letter followed by an octave number specifies a unique key. In this notation, two important keys are **C4** and **A4**. The **C4** key is near the center of the keyboard and so is also known as *middle C*. A piano is usually tuned so that the frequency of the **A4** key is 440 Hz. **C4** is nine keys to the left of **A4** so its frequency is

$$\mbox{C4} = 440 \sigma^{-9} \approx 261.6256 \mbox{Hz}$$

Our miniature keyboard has the **A4** key colored green and the **C4** key colored blue.

A piano is almost always tuned with *equal temperament*, the system used by musical instruments with fixed pitch. Starting with **A4** at 440 Hz the keys are tuned in strict geometric progression with ratio $\sigma$. Once this is done, a piano rarely needs adjustment.

The alternative system, *just intonation*, is defined by a vector of ratios involving small integers.

```
format rat
just = [1 16/15 9/8 6/5 5/4 4/3 7/5 3/2 8/5 5/3 7/4 15/8 2]'
```

just = 1 16/15 9/8 6/5 5/4 4/3 7/5 3/2 8/5 5/3 7/4 15/8 2

These ratios allow the generation of overtones, chords and harmony. More about that in a moment. Many musical instruments can be tuned to the key of a particular composition just before playing it. Singers, choirs, and barbershop quartets can naturally use just intonation.

Here is a comparison of equal temperament and just intonation from a strictly numerical point of view. Equal temperament is defined by repeated powers of $\sigma$. while just intonation is defined by a sequence of ratios.

sigma = 2^(1/12); k = (0:12)'; equal = sigma.^k; num = [1 16 9 6 5 4 7 3 8 5 7 15 2]'; den = [1 15 8 5 4 3 5 2 5 3 4 8 1]'; just = num./den; delta = (equal - just)./equal; T = [k equal num den just delta]; fprintf(' k equal just delta\n') fprintf('%4d %12.6f %7d/%d %11.6f %10.4f\n',T')

k equal just delta 0 1.000000 1/1 1.000000 0.0000 1 1.059463 16/15 1.066667 -0.0068 2 1.122462 9/8 1.125000 -0.0023 3 1.189207 6/5 1.200000 -0.0091 4 1.259921 5/4 1.250000 0.0079 5 1.334840 4/3 1.333333 0.0011 6 1.414214 7/5 1.400000 0.0101 7 1.498307 3/2 1.500000 -0.0011 8 1.587401 8/5 1.600000 -0.0079 9 1.681793 5/3 1.666667 0.0090 10 1.781797 7/4 1.750000 0.0178 11 1.887749 15/8 1.875000 0.0068 12 2.000000 2/1 2.000000 0.0000

The last column in the table, `delta`, is the relative difference between the two. We see that `delta` is less than one percent, except for `k = 10`.

This is why the 12-note scale has proved to be so popular. Powers of $\sqrt[12]{2}$ turn out to come very close to important rational numbers, especially 5/4, 4/3, and 3/2. A pair of pitches with a ratio of 3:2 is known as a *perfect fifth*; a ratio of 4:3 is a *perfect fourth*; a ratio of 5:4 is a *major third*.

One of the first songs you learned to sing was

*Do Re Mi Fa So La Ti Do*

If you start at middle C, you will be singing the *major scale* in the key of C. This scale is played on a piano using only the white keys. The steps are not equally spaced. Most of the steps skip over black keys and so are two semitones. But the steps between *Mi* and *Fa* and *Ti* and *Do* are the steps from **E** to **F** and **B** to **C**. There are no intervening black keys and so these steps are only one semitone. In terms of $\sigma$, the C-major scale is

$$\sigma^0 \ \sigma^2 \ \sigma^4 \ \sigma^5 \ \sigma^7 \ \sigma^9

\ \sigma^{11} \ \sigma^{12}$$

The number of semitones between the notes is given by the vector

```
format short
diff([0 2 4 5 7 9 11 12])
```

ans = 2 2 1 2 2 2 1

This sequence of frequencies in our most common scale is surprising.

Like everything else, it all comes down to *eigenvalues*. Musical instruments create sound through the action of vibrating strings or vibrating columns of air that, in turn, produce vibrations in the body of the instrument and the surrounding air. Mathematically, vibrations can be modeled by weighted sums of characteristic functions known as *modes* or *eigenfunctions*. Different modes vibrate at different characteristic frequencies or *eigenvalues*. These frequencies are determined by physical parameters such as the length, thickness and tension in a string, or the geometry of the air cavity. Short, thin, tightly stretched strings have high frequencies, while long, thick, loosely stretched strings have low frequencies.

The simplest model is a one-dimensional vibrating string, held fixed at its ends. The units of the various physical parameters can be chosen so that the length of the string is $2 \pi$. The modes are then simply the functions

$$v_n(x) = \sin{n x}, \ \, n = 1, 2, …$$

Each of these functions satisfy the fixed end point conditions

$$v_n(0) = v_n(2 \pi) = 0$$

The time-dependent modal vibrations are

$$u_n(x,t) = \sin{n x} \sin{n t}, \ \, n = 1, 2, …$$

and the frequency is simply the integer $n$. (Two- and three-dimensional models are much more complicated. This one-dimensional model is all we need here.)

Our Experiments with MATLAB program `vibrating_string` provides a dynamic view. Here is a snapshot showing the first nine modes and the resulting wave traveling along the string.

The ratios in just intonation or equal temperament produce instruments with tunings that generate vibrations with these desired frequencies.

The MATLAB expression `rat(X,tol)` creates a truncated continued fraction approximation of `X` with an accuracy of `tol`. The default `tol` is `1.e-6*norm(X,1)`.

rat(sigma.^(0:12)')

ans = 1 1 + 1/(17 + 1/(-5 + 1/(-2))) 1 + 1/(8 + 1/(6)) 1 + 1/(5 + 1/(4 + 1/(-2))) 1 + 1/(4 + 1/(-7 + 1/(2 + 1/(5)))) 1 + 1/(3 + 1/(-74)) 1 + 1/(2 + 1/(2 + 1/(2 + 1/(2 + 1/(2 + 1/(2)))))) 1 + 1/(2 + 1/(147)) 2 + 1/(-2 + 1/(-2 + 1/(-3 + 1/(4 + 1/(2))))) 2 + 1/(-3 + 1/(-7 + 1/(-81))) 2 + 1/(-5 + 1/(2 + 1/(3 + 1/(-2 + 1/(-15))))) 2 + 1/(-9 + 1/(11)) 2

These are unconventional continued fractions because they contain negative terms. This algorithm is used for `format rat`. The default tolerance is much too picky. The equal temperament ratios do not give small integers.

```
format rat
sigma.^(0:12)'
```

ans = 1 1657/1564 1769/1576 1785/1501 635/504 3544/2655 1393/985 2655/1772 1008/635 3002/1785 1527/857 2943/1559 2

This would not be a satisfactory way to try to tune an instrument. But if I’m much more tolerant and allow a 2 percent error, I get short continued fractions.

rat(sigma.^(0:12)',.02)

ans = 1 1 + 1/(17) 1 + 1/(8) 1 + 1/(5) 1 + 1/(4) 1 + 1/(3) 1 + 1/(2 + 1/(2)) 1 + 1/(2) 2 + 1/(-2 + 1/(-2)) 2 + 1/(-3) 2 + 1/(-5) 2 + 1/(-9) 2

I can make `format rat` use these fractions by telling `rats` that it has only 6 columns to work with. Now I get almost the same ratios as just intonation.

rats(sigma.^(0:12)',6)

ans = 1 1 9/8 6/5 5/4 4/3 7/5 3/2 8/5 5/3 9/5 17/9 2

If any of the music theorists from ancient Greece or the Renaissance had access to MATLAB, they could have used our rational approximation to generate a tuning strategy.

Published with MATLAB® R2016a

]]>The MATLAB functions for the numerical evaluation of integrals has evolved from `quad`, through `quadl` and `quadgk`, to today's `integral`.... read more >>

The MATLAB functions for the numerical evaluation of integrals has evolved from `quad`, through `quadl` and `quadgk`, to today's `integral`.

Several years ago I regarded myself as an expert on numerical methods for approximating integrals. But then I stopped paying attention for a while. So this is an opportunity to get reacquainted. The task involves a real-valued function $f(x)$ of a real variable $x$, defined on an interval $a \le x \le b$. We seek to compute the value of the definite integral,

$$ \int_a^b \ f(x) \ dx $$

I wrote a chapter on the subject in my textbook *Numerical Computing with MATLAB*. Here is a link to that chapter. The chapter is titled "Quadrature". One of the first things I have to do in that chapter is explain that *quadrature* is an old-fashioned term derived from the notion that the value of a definite integral can be approximated by plotting the function on graph paper and then counting the number of little squares that fall underneath the graph. Here is a coarse picture.

For most of the time since the beginning of MATLAB, up until when I wrote the chapter on quadrature twelve years ago, the MATLAB function for evaluating definite integrals was one named `quad`. It was based on adaptive Simpson's method, pioneered by my grad school buddy Bill McKeeman to show off recursion in Algol.

These two figures illustrate the basis for adaptive Simpson's method.

The algorithm works recursively on subintervals of the original interval [a,b]. If the values obtained from the 3-point Simpson's rule and the 5-point composite Simpson's rule agree to within a specified tolerance (which they clearly do not for these figures), then an extrapolation taken from the two values is accepted as the value for the integral over the subinterval. If not, the algorithm is recursively applied to the two halves of the subinterval.

I have used a function named `humps` to demonstrate numerical integration for a long time. It has a strong peak at x = 0.3 and a milder peak at x = 0.9. It has found its way into the MATLAB `demos` directory, so you have it on your own machine. A graph appears below.

$$ h(x) = \frac{1}{(x-0.3)^2+.01} + \frac{1}{(x-0.9)^2+.04} - 6 $$

An interactive graphical app named `quadgui` is included with the programs for *Numerical Computing with MATLAB*, available from MATLAB Central File Exchange. It shows `quad` in action. Here is a snapshot of `quad` part way through computing the integral of `humps` over the interval [0,1]. Integration over the interval to the left of 1/4 is finished. The integration over the subinterval [1/4,3/8] has just been successfully completed. A few samples to the right of 3/8 were computed during the initialization. The integrand has been evaluated 19 times.

Here is the final result. The default tolerance for `quad` is $10^{-6}$ and the value of the integral obtained to six significant digits is 29.8583. The integrand has been evaluated 93 times. Most of those evaluations took place near the peaks where the `humps` varies rapidly. This indicates that the adaptivity works as expected.

`Humps` is a rational function. It is possible to use the Symbolic Toolbox to obtain the indefinite and then the definite integral analytically.

syms x h = 1/((x-3/10)^2+1/100) + 1/((x-9/10)^2+4/100) - 6 I = int(h,x) D = subs(I,1)-subs(I,0) format long Q = double(D)

h = 1/((x - 9/10)^2 + 1/25) + 1/((x - 3/10)^2 + 1/100) - 6 I = 10*atan(10*x - 3) - 6*x + 5*atan(5*x - 9/2) D = 5*atan(1/2) + 10*atan(3) + 10*atan(7) + 5*atan(9/2) - 6 Q = 29.858325395498674

This confirms the six figure value shown by `quadqui`

About the time in 2004 that I was writing the *Quadrature* chapter for *NCM*, the `quadl` function showed up. This uses the *Lobatto* rule.

$$ \int_{-1}^1 \ f(x) \ dx \ \approx \ w_1 f(-1) + w_2 f(-x_1) + w_2 f(x_1) + w_1 f(1) $$

with $x_1 = 1/\sqrt{5}, \ w_1 = 1/6, \ w_2 = 5/6$.

`Quadl` also employs the associated *Kronrod* rule, which provides 13 additional irrational abscissa and weights. The Lobatto-Kronrod rule is much higher order and consequently more accurate than Simpson's rule. As a result `quadl` is usually faster and more accurate than `quad`. But I just devoted a couple of exercises in the book to `quadl`. And I think it never became very popular.

In the mathematical software business when we were developing libraries using Fortran or C, we evaluated the cost of numerical quadrature by counting the number of function evaluations. In 2006 Larry Shampine, the SMU Professor who is a consultant to MathWorks and principal author of our ODE Suite, published the paper referenced below where he changed the ground rules for quadrature functions in the MATLAB environment. Since MATLAB function evaluations are vectorized, we should count the number of vectorized evaluations, not the number of individual evaluations. This is a better predictor of execution time.

Shampine's paper includes a MATLAB function that he calls `quadva`, where `va` stands for "vectorized adaptive". His code uses a 15-point Gauss-Kronrod formula. The Gauss formula is not only higher order than the Lobatto formula with the same number of points, it has the additional advantage that it does not require evaluating the integrand at the end points of an interval. This makes it possible to treat infinite intervals and singularites at the end points.

When we added Shampine's `quadva` to MATLAB, we called it `quadgk`, for "Gauss-Kronrod".

Let's instrument `humps` to investigate the behavior of our quadrature routines on just one simple test case. The functions `quad` and `quadl` call the integrand once with a vector argument to initialize the integration and then call it more times with a shorter vector argument during the adaptive process. This is such an easy problem that `quadgk` gets all the information that it needs during the initialization.

In the following table, "init" is the length of the vector in the initialization call, "kount" is the number of times that `humps` is called after the initialization, "length" is the length of the vector argument in those calls, "err" is the error in the final result, and "time" is the execution time in milliseconds.

$$\begin{array}{lrrrrr} & \mbox{init} & \mbox{kount} & \mbox{length} & \mbox{err\ \ \ \ } & \mbox{time}\\ \mbox{quad} & 7 & 69 & 2 & 7.3 \cdot 10^{-7} & 0.87 \\ \mbox{quadl} & 13 & 37 & 5 & 1.8 \cdot 10^{-10} & 0.78 \\ \mbox{quadgk} & 150 & 0 & - & 2.5 \cdot 10^{-14} & 0.34 \end{array}$$

We see that the three functions perform very differently in this one experiment. The oldest function, `quad`, actually requires the fewest total function evaluations, if we count them in the traditional scalar sense, but it is the least accurate and the slowest. The newest function, `quadgk`, requires only one vectorized function evaluation, and is the fastest and the most accurate.

In 2012, with release 2012a, we came to the realization that most new MATLAB users who wanted to evaluate an integral had a hard time finding the function named "quadgk" that does the job. For all these years, we numerical analysts have been explaining to the rest of the world our quaint term *quadrature*. If that weren't enough, we have to tack on initials of mathematicians. So, we introduced `integral`. Actually `quadgk` is still available. `Integral` is just an easier to find and easier to use version of `quadgk`.

L.F. Shampine, "Vectorized Adaptive Quadrature in MATLAB, *Journal of Computational and Applied Mathematics* 211 (2006), pp.131–140, available at <http://faculty.smu.edu/shampine/rev2vadapt.pdf>

Get
the MATLAB code

Published with MATLAB® R2016a

Gil Strang has produced a MOOC-style video course on Differential Equations and Linear Algebra. I have added some videos about the MATLAB ODE suite. The series is available from the MathWorks Web site, MIT OpenCourseWare and several other popular sources.... read more >>

]]>Gil Strang has produced a MOOC-style video course on Differential Equations and Linear Algebra. I have added some videos about the MATLAB ODE suite. The series is available from the MathWorks Web site, MIT OpenCourseWare and several other popular sources.

MIT's Professor Gilbert Strang has taught 18.06, Linear Algebra, regularly for many years. Over fifteen years ago MIT videotaped the lectures. MIT was also taping 8.01, the first-year physics course, taught by the flamboyant Walter Lewin, in the same lecture room the hour before. They were careful with the videography, with professional camera work, good lighting and good audio. There was no computer graphics, no PowerPoint. Just excellent lectures, using big chalk on actual black boards. You can still see Gil's original lectures by Googling "18.06", or by following this link.

MIT decided to make the lectures freely available on the web, with no strings attached. That was an unusual move at the time. The two courses proved to be very popular. They led to the creation of what MIT now calls OpenCourseWare and were in part responsible for the MOOC, Massive Open Online Courses, revolution in higher education.

Gil told me the experience changed his life. He realized he could reach millions of students instead of just one classroom.

Strang recently published a new book, Differential Equations and Linear Algebra.

He invited MathWorks to join him in producing a series of videos on the subject. I joined the project to describe how MATLAB provides tools for solving ODEs.

Here is a link to the home page for the project at the MathWorks. Learn Differential Equations.

And here is a link to a video where Gil and I have a short chat. Gil and Cleve chat.

Gil produced 55 videos, averaging about 15 minutes each. There are eight topic areas, which map to and complement the textbook, DELA.

- Introduction
- First Order Equations
- Second Order Equations
- Graphical and Numerical Methods
- Vector Spaces and Subspaces
- Eigenvalues and Eigenvectors
- Applied Mathematics and ATA
- Fourier and Laplace Transforms

Here is a screen shot from one of the videos to give you a feel for the style of the presentation.

My part of the series is much smaller, only a dozen videos, also about 15 minutes each. The first video is about Euler's method. My Euler's method code is `ode1.m`. It is 16 lines long; half of the lines are comments. The last three videos are examples. One of them is based on my blog post about the tumbling box ode. Gil also discusses the tumbling box in one of his videos. I have included several hands on exercises that you can try out by downloading the MATLAB code available on the MATLAB Central File Exchange, solving ODEs in MATLAB.

Here is a screen shot from my discussion of stiffness.

The home page for the project at the MathWorks is <http://www.mathworks.com/academia/courseware/learn-differential-equations.html>.

The series is available from MIT OpenCourseWare under the title *Learn Differential Equations: Up Close with Gilbert Strang and Cleve Moler*. The OCW course number is 18-009. The URL is <http://ocw.mit.edu/resources/res-18-009-learn-differential-equations-up-close-with-gilbert-strang-and-cleve-moler-fall-2015>.

The videos are also available through the following distribution sites.

YouTube: <http://www.youtube.com/playlist?list=PLUl4u3cNGP63oTpyxCMLKt_JmB0WtSZfG>.

iTunes: <http://itunes.apple.com/us/itunes-u/id1102923623>.

Internet Archive: <http://archive.org/details/MITRES18-009F15>.

Get
the MATLAB code

Published with MATLAB® R2016a