To save content items to your account,
please confirm that you agree to abide by our usage policies.
If this is the first time you use this feature, you will be asked to authorise Cambridge Core to connect with your account.
Find out more about saving content to .
To save content items to your Kindle, first ensure no-reply@cambridge.org
is added to your Approved Personal Document E-mail List under your Personal Document Settings
on the Manage Your Content and Devices page of your Amazon account. Then enter the ‘name’ part
of your Kindle email address below.
Find out more about saving to your Kindle.
Note you can select to save to either the @free.kindle.com or @kindle.com variations.
‘@free.kindle.com’ emails are free but can only be saved to your device when it is connected to wi-fi.
‘@kindle.com’ emails can be delivered even when you are not connected to wi-fi, but note that service fees apply.
In this chapter, we offer advice for dealing with some common problems that you may encounter. We also list and describe the most common mistakes that MATLAB users make. Finally, we offer some simple but useful techniques for debugging your M-files.
Common Problems
Problems manifest themselves in various ways: totally unexpected or plainly wrong output appears; MATLAB produces an error message (or at least a warning); MATLAB refuses to process an input line; something that worked earlier stops working; or, worst of all, the computer freezes. Fortunately, these problems are often caused by several easily identifiable and correctable mistakes. What follows is a description of some common problems, together with a presentation of likely causes, suggested solutions, and illustrative examples. We also refer to places in the book where related issues are discussed.
Here is a list of the problems:
Wrong or unexpected output
Syntax error
Spelling error
Error or warning messages when plotting
A previously saved M-file evaluates differently
Computer won't respond.
Wrong or Unexpected Output
There are many possible causes for this problem, but they are likely to be among the following.
CAUSE: Forgetting to clear or reset variables.
SOLUTION: Clear or initialize variables before using them, especially in a long session.
In this chapter we describe more of MATLAB's graphics commands and the most common ways of manipulating and customizing graphics. For an overview of commands, type help graphics (for general graphics commands), help graph2d (for two-dimensional graphics commands), help graph3d (for three-dimensional graphics commands), and help specgraph (for specialized graphing commands).
We have already discussed the commands plot and ezplot in Chapter 2. We will begin this chapter by discussing more uses of these commands, as well as some of the other most commonly used plotting commands. Then we will discuss methods for customizing and manipulating graphics. Finally, we will introduce some commands and techniques for creating and modifying images and sounds.
✓ For most types of graphs we describe below, there is a command like plot that draws the graph from numerical data, and a command like ezplot that graphs functions specified by string or symbolic input. The latter commands may be easier to use at first, but are more limited in their capabilities and less amenable to customization. Thus, we emphasize the commands that plot data, which are likely to be more useful to you in the long run.
Two-Dimensional Plots
Often one wants to draw a curve in the x-y plane, but with y not given explicitly as a function of x. There are two main techniques for plotting such curves: parametric plotting and contour or implicit plotting.
In this chapter, we present examples showing you how to apply MATLAB to problems in several disciplines. Each example is presented in the form of a MATLAB M-file, published to LATEX. We modified the default style sheet for publishing to LATEX to adjust the spacing between input, output, and text, and to allow formatting of mathematical formulas not just as displayed equations but also within a paragraph. We also made a few minor adjustments to the published LATEX code to improve line breaks. Finally, because publish does not produce italic text, we used bold text instead in places. These examples are illustrations of the kinds of polished, integrated documents that you can create with MATLAB. The examples are:
Illuminating a Room
Mortgage Payments
Monte Carlo Simulation
Population Dynamics
Linear Economic Models
Linear Programming
The 360° Pendulum
☆ Numerical Solution of the Heat Equation
☆ A Model of Traffic Flow
We have not explained all the MATLAB commands that we use; you can learn about the new commands from the online help. Simulink is used in A Model of Traffic Flow and as an optional accessory in Population Dynamics and Numerical Solution of the Heat Equation. The example on Linear Programming also requires an M-file found (in slightly different forms) in the Simulink and Optimization toolboxes. The examples require different levels of mathematical background and expertise in other subjects.
In this chapter, we will introduce you to the tools you need in order to begin using MATLAB effectively. These include the following: some relevant information on computer platforms and software; installation protocols; how to launch MATLAB, enter commands and use online help; a roster of MATLAB's various windows; and finally, how to exit the program. We know you are anxious to get started using MATLAB, so we will keep this chapter brief. After you complete it, you can go immediately to Chapter 2 to find concrete and simple instructions for using MATLAB to do mathematics. We describe the MATLAB interface more elaborately in Chapter 3.
Platforms and Versions
It is likely that you will use MATLAB on a computer running Microsoft Windows or on some form of a UNIX operating system (such as Linux). Some previous versions of MATLAB (Releases 11 and 12) did not support Macintosh, but the most current versions (Releases 13 and 14) do. If you are using a Macintosh, you should find that our instructions for Windows will suffice for most of your needs. Like MATLAB 6 (Releases 12 and 13), and unlike earlier versions, MATLAB 7 (Release 14) looks virtually identical on these different platforms. For definitiveness, we shall assume that the reader is using a Windows computer. In those very few instances where our instructions must be tailored differently for Linux, UNIX, or Macintosh users, we shall point it out clearly.
Information in science and mathematics is often organized into rows and columns to form rectangular arrays, called “matrices” (plural of “matrix“). Matrices are often tables of numerical data that arise from physical observations, but they also occur in various mathematical contexts.
Howard Anton. Elementary Linear Algebra. Wiley (1973 1st edn, 2000 8th edn).
To be able to read or work on matrix computing, a reader must have completed a course on linear algebra. The inclusion of this Appendix A is to review some selected topics from basic linear algebra for reference purposes.
The term “preconditioning” appears to have been used for the first time in 1948 by Turing [461], … The first use of the term in connection with iterative methods is found in a paper by Evans [200] … in 1968.
Michele Benzi. Journal of Computational Physics, Vol. 182 (2002)
For such problems, the coefficient matrix A is often highly nonsymmetric and non-diagonally dominant and hence many classical preconditioning techniques are not effective. For these problems, the circulant preconditioners are often the only ones that work.
Raymond Chan and Tony Chan. Journal of Numerical Linear Algebra and Applications, Vol. 1 (1992)
In ending this book with the subject of preconditioners, we find ourselves at the philosophical center of the scientific computing of the future … Nothing will be more central to computational science in the next century than the art of transforming a problem that appears intractable into another whose solution can be approximated rapidly. For Krylov subspace matrix iterations, this is preconditioning.
Lloyd Nicholas Trefethen and David Bau III. Numerical Linear Algebra. SIAM Publications (1997)
Starting from this chapter, we shall first describe various preconditioning techniques that are based on manipulation of a given matrix. These are classified into four categories: direct matrix extraction (or operator splitting type), inverse approximation (or inverse operator splitting type), multilevel Schur complements and multi-level operator splitting (multilevel methods).
An important class of problems in which significantly higher accuracies are needed relate to low-observable applications, where the quantities of interest are small residuals of large incident fields.
Oscar P. Bruno. Fast, high-order, high-frequency integral methods for computational acoustics and electromagnetics. Lecture Notes in Computational Science and Engineering 31. Springer-Verlag (2003)
However, wavelet representation of an oscillating matrix appears to be as dense as the original, i.e. oscillatory kernels cannot be handled efficiently by representing them in wavelet bases.
A. Averbuchet al. On efficient computation of multidimensional oscillatory integrals with local Fourier bases. Nonlinear Analysis (2001)
The acoustic scattering modelling provides a typical example of utilizing a boundary element method to derive a dense matrix application as shown in Chapter 1. Such a physical problem is only a simple model of the full wave equations or the Maxell equations from electromagnetism. The challenges are:
(i) the underlying system is dense and non-Hermitian;
(ii) the kernel of a boundary integral operator is highly oscillatory for high wavenumbers, implying that a large linear system must be solved. The oscillation means that the fast multipole method and the fast wavelet methods are not immediately applicable.
This chapter reviews the recent work on using preconditioned iterative solvers for such linear systems arising from acoustic scattering modelling and points out the various challenges for future research work. We consider the following.
The experiences of Fox, Huskey, and Wilkinson [from solving systems of orders up to 20] prompted Turing to write a remarkable paper [in 1948] … In this paper, Turing made several important contributions … He used the word “preconditioning” to mean improving the condition of a system of linear equations (a term that did not come into popular use until 1970s).
Nicholas J. Higham. Accuracy and Stability of Numerical Algorithms. SIAM Publications (1996)
Matrix computing arises in the solution of almost all linear and nonlinear systems of equations. As the computer power upsurges and high resolution simulations are attempted, a method can reach its applicability limits quickly and hence there is a constant demand for new and fast matrix solvers. Preconditioning is the key to a successful iterative solver. It is the intention of this book to present a comprehensive exposition of the many useful preconditioning techniques.
Preconditioning equations mainly serve for an iterative method and are often solved by a direct solver (occasionally by another iterative solver). Therefore it is inevitable to address direct solution techniques for both sparse and dense matrices. While fast solvers are frequently associated with iterative solvers, for special problems, a direct solver can be competitive. Moreover, there are situations where preconditioning is also needed for a direct solution method. This clearly demonstrates the close relationship between a direct and an iterative method.