MATLAB Programming
MATLAB Programming MATLAB Programming
2 Data Types Note that the order of operations can matter in the computation: b = 1e-16 + 1 - 1e-16; c = 1e-16 - 1e-16 + 1; b == c ans = 0 There are gaps between floating-point numbers. As the numbers get larger, so do the gaps, as evidenced by: (2^53 + 1) - 2^53 ans = 0 Since pi is not really pi, it is not surprising that sin(pi) is not exactly zero: sin(pi) ans = 1.224646799147353e-016 Example 2 — Catastrophic Cancellation. When subtractions are performed with nearly equal operands, sometimes cancellation can occur unexpectedly. The following is an example of a cancellation caused by swamping (loss of precision that makes the addition insignificant): sqrt(1e-16 + 1) - 1 ans = 0 Some functions in MATLAB, such as expm1 and log1p, maybeusedto compensate for the effects of catastrophic cancellation. 2-22
Numeric Types Example 3 — Floating-Point Operations and Linear Algebra. Round-off, cancellation, and other traits of floating-point arithmetic combine to produce startling computations when solving the problems of linear algebra. MATLAB warns that the following matrix A is ill-conditioned, and therefore the system Ax = b may be sensitive to small perturbations. Although the computation differs from what you expect in exact arithmetic, the result is correct. A = [2 eps -eps; eps 1 1; -eps 1 1]; b = [2; eps + 2; -eps + 2]; x = A\b x = 1.0e+015 * 0.000000000000001 2.251799813685249 -2.251799813685247 These are only a few of the examples showing how IEEE floating-point arithmetic affects computations in MATLAB. Note that all computations performed in IEEE 754 arithmetic are affected, this includes applications written in C or FORTRAN, as well as MATLAB. For more examples and information, see Technical Note 1108 Common Problems with Floating-Point Arithmetic. Floating-Point Functions See Floating-Point Functions on page 2-30 for a list of functions most commonly used with floating-point numbers in MATLAB. References The following references provide more information about floating-point arithmetic. [1] Moler, Cleve, “Floating Points,” MATLAB News and Notes, Fall, 1996. A PDF version is available on the MathWorks Web site at http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf [2] Moler, Cleve, Numerical Computing with MATLAB, S.I.A.M. A PDF version is available on the MathWorks Web site at http://www.mathworks.com/moler/. 2-23
- Page 72 and 73: 1 Data Structures Use the isscalar
- Page 74 and 75: 1 Data Structures Full and Sparse M
- Page 76 and 77: 1 Data Structures Multidimensional
- Page 78 and 79: 1 Data Structures As you add dimens
- Page 80 and 81: 1 Data Structures Generating Arrays
- Page 82 and 83: 1 Data Structures • size — Retu
- Page 84 and 85: 1 Data Structures For example, cons
- Page 86 and 87: 1 Data Structures Avoiding Ambiguit
- Page 88 and 89: 1 Data Structures Permuting Array D
- Page 90 and 91: 1 Data Structures Computing with Mu
- Page 92 and 93: 1 Data Structures • As multivaria
- Page 94 and 95: 1 Data Structures A{2,2} = 7; B{1,1
- Page 96 and 97: 1 Data Structures Summary of Matrix
- Page 98 and 99: 1 Data Structures Functions to Dete
- Page 100 and 101: 1 Data Structures 1-76
- Page 102 and 103: 2 Data Types Cell Arrays (p. 2-94)
- Page 104 and 105: 2 Data Types Data Type Summary The
- Page 106 and 107: 2 Data Types Numeric Types Numeric
- Page 108 and 109: 2 Data Types the two equally nearby
- Page 110 and 111: 2 Data Types x = x = 127 -128 Also,
- Page 112 and 113: 2 Data Types Set the warning state
- Page 114 and 115: 2 Data Types Floating-Point Numbers
- Page 116 and 117: 2 Data Types isfloat(x) ans = 1 You
- Page 118 and 119: 2 Data Types In this example, 7.5 d
- Page 120 and 121: 2 Data Types 8.881784197001252e-016
- Page 124 and 125: 2 Data Types Complex Numbers Comple
- Page 126 and 127: 2 Data Types NaN MATLAB represents
- Page 128 and 129: 2 Data Types All available formats
- Page 130 and 131: 2 Data Types Integer Functions Func
- Page 132 and 133: 2 Data Types Infinity and NaN Funct
- Page 134 and 135: 2 Data Types 1 0 0 1 0 1 1 0 The MA
- Page 136 and 137: 2 Data Types A(B) ans = 5 15 30 35
- Page 138 and 139: 2 Data Types Characters and Strings
- Page 140 and 141: 2 Data Types name = ['Thomas R. Lee
- Page 142 and 143: 2 Data Types Phoenix length(strings
- Page 144 and 145: 2 Data Types • “Setting Field W
- Page 146 and 147: 2 Data Types Ordered Sequentially s
- Page 148 and 149: 2 Data Types An alternate syntax, t
- Page 150 and 151: 2 Data Types Precision is not usual
- Page 152 and 153: 2 Data Types Note You can specify m
- Page 154 and 155: 2 Data Types operator, specifying t
- Page 156 and 157: 2 Data Types Valid Syntax Invalid S
- Page 158 and 159: 2 Data Types A = 'fate'; B = 'cake'
- Page 160 and 161: 2 Data Types while (any(remainder))
- Page 162 and 163: 2 Data Types Converting from String
- Page 164 and 165: 2 Data Types Function Summary MATLA
- Page 166 and 167: 2 Data Types Functions to Determine
- Page 168 and 169: 2 Data Types Date Strings There are
- Page 170 and 171: 2 Data Types • The day field is a
Numeric Types<br />
Example 3 — Floating-Point Operations and Linear Algebra.<br />
Round-off, cancellation, and other traits of floating-point arithmetic combine<br />
to produce startling computations when solving the problems of linear<br />
algebra. <strong>MATLAB</strong> warns that the following matrix A is ill-conditioned,<br />
and therefore the system Ax = b may be sensitive to small perturbations.<br />
Although the computation differs from what you expect in exact arithmetic,<br />
the result is correct.<br />
A = [2 eps -eps; eps 1 1; -eps 1 1];<br />
b = [2; eps + 2; -eps + 2];<br />
x = A\b<br />
x =<br />
1.0e+015 *<br />
0.000000000000001<br />
2.251799813685249<br />
-2.251799813685247<br />
These are only a few of the examples showing how IEEE floating-point<br />
arithmetic affects computations in <strong>MATLAB</strong>. Note that all computations<br />
performed in IEEE 754 arithmetic are affected, this includes applications<br />
written in C or FORTRAN, as well as <strong>MATLAB</strong>. For more examples<br />
and information, see Technical Note 1108 Common Problems with<br />
Floating-Point Arithmetic.<br />
Floating-Point Functions<br />
See Floating-Point Functions on page 2-30 for a list of functions most<br />
commonly used with floating-point numbers in <strong>MATLAB</strong>.<br />
References<br />
The following references provide more information about floating-point<br />
arithmetic.<br />
[1] Moler, Cleve, “Floating Points,” <strong>MATLAB</strong> News<br />
and Notes, Fall, 1996. A PDF version is available on the MathWorks Web site at<br />
http://www.mathworks.com/company/newsletters/news_notes/pdf/Fall96Cleve.pdf<br />
[2] Moler, Cleve, Numerical Computing with <strong>MATLAB</strong>, S.I.A.M. A PDF version<br />
is available on the MathWorks Web site at http://www.mathworks.com/moler/.<br />
2-23