0% found this document useful (0 votes)
6 views8 pages

VBA and MATLAB Algorithms for Numerical Analysis

Uploaded by

tanvirsiddique
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views8 pages

VBA and MATLAB Algorithms for Numerical Analysis

Uploaded by

tanvirsiddique
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1

CHAPTER 3
3.1 (a)
(101101) 2 = (1 × 2 5 ) + (0 × 2 4 ) + (1 × 2 3 ) + (1 × 2 2 ) + (0 × 21 ) + (1 × 2 0 )

= 32 + 8 + 4 + 1 = 45
(b)
(101.101) 2 = (1 × 2 2 ) + (0 × 21 ) + (1 × 2 0 ) + (1 × 2 −1 ) + (0 × 2 −2 ) + (1 × 2 −3 )

= 4 + 1 + 0.5 + 0.125 = 5.625


(c)
(0.01101) 2 = (0 × 2 −1 ) + (1× 2 −2 ) + (1× 2 −3 ) + (0 × 2 −4 ) + (1× 2 −5 )

= 0.25 + 0.125 + 0.03125 = 0.40625

3.2 Here are VBA and MATLAB implementations of the algorithm:

VBA Function Procedure MATLAB M-File


Option Explicit function e = geteps
Sub GetEps() e = 1;
Dim epsilon As Double while(1)
epsilon = 1 if e + 1 <= 1, break, end
Do e = e / 2;
If epsilon + 1 <= 1 Then Exit Do end
epsilon = epsilon / 2 e = 2 * e;
Loop
epsilon = 2 * epsilon
MsgBox epsilon
End Sub

Both routines yield a result of 2.22044604925031E–16 on my desktop PC. For single


precision, the result is 1.192093E–07. Note that MATLAB has a built-in function eps that
yields the same result.

3.3 Here are VBA and MATLAB implementations of the algorithm:

VBA Function Procedure MATLAB M-File


Option Explicit function xmin = getmin
Sub GetMin() x = 1;
Dim x As Double, xmin As Double while(1)
x = 1 if x <= 0, break, end
Do xmin = x;
If x <= 0 Then Exit Do x = x / 2;
xmin = x end
x = x / 2
Loop
MsgBox xmin
End Sub

Both yield a result of 4.94065645841247E–324 on my desktop PC. For single precision, the
result is 1.401298E–45.

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may
be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the
publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual
course preparation. If you are a student using this Manual, you are using it without permission.
2

3.4 Here is a VBA Program to compute the series in ascending order

Option Explicit

Sub SeriesForward()
Dim i As Integer, n As Integer
Dim sum As Single, pi As Double, truth As Double
pi = 4 * Atn(1)
truth = pi ^ 4 / 90
sum = 0
n = 10000
For i = 1 To n
sum = sum + 1 / i ^ 4
Next i
MsgBox sum
'Display true percent relative error
MsgBox 100 * Abs((truth - sum) / truth)
End Sub

This yields a result of 1.0823221 with a true relative error of 1.02838× 10–4%.

VBA Program to compute in descending order:

Option Explicit

Sub SeriesBackward()
Dim i As Integer, n As Integer
Dim sum As Single, pi As Double, truth As Double
pi = 4 * Atn(1)
truth = pi ^ 4 / 90
sum = 0
n = 10000
For i = n To 1 Step -1
sum = sum + 1 / i ^ 4
Next i
MsgBox sum
'Display true percent relative error
MsgBox 100 * Abs((truth - sum) / truth)
End Sub

This yields a result of 1.0823232 with a true relative error of 3.71× 10–6%.

The latter version yields a superior result because summing in descending order mitigates the
roundoff error that occurs when adding a large and small number.

3.5 For the first series, after 20 terms are summed, the result is

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may
be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the
publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual
course preparation. If you are a student using this Manual, you are using it without permission.
3

The result oscillates at first. By n = 20 (21 terms), it is starting to converge on the true value.
However, the relative error is still a substantial 0.11%. If carried out further to n = 27, the
series eventually converges to within 7 significant digits.

In contrast the second series converges much faster. It attains 6 significant digits by n = 20
with a percent relative error of 8.1× 10–6%.

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may
be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the
publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual
course preparation. If you are a student using this Manual, you are using it without permission.
4

3.6 The true value can be computed as

6(0.577)
f ' (0.577) = = 2,352,911
(1 − 3 × 0.577 2 ) 2

Using 3-digits with chopping

6 x = 6(0.577) = 3.462 chopping


→ 3.46
x = 0.577
x 2 = 0.332929 chopping
→ 0.332
3x 2 = 0.996
1 − 3x 2 = 0.004

3.46 3.46
f ' (0.577) = 2
= = 216,250
(1 − 0.996) 0.004 2

This represents a percent relative error of

2,352,911 − 216,250
εt = = 90.8%
2,352,911

Using 4-digits with chopping

6 x = 6(0.577) = 3.462 chopping


→ 3.462
x = 0.577
x 2 = 0.332929 chopping
→ 0.3329
3x 2 = 0.9987
1 − 3x 2 = 0.0013

3.462 3.462
f ' (0.577) = 2
= = 2,048,521
(1 − 0.9987) 0.0013 2

This represents a percent relative error of

2,352,911 − 2,048,521
εt = = 12.9%
2,352,911

Although using more significant digits improves the estimate, the error is still considerable.
The problem stems primarily from the fact that we are subtracting two nearly equal numbers
in the denominator. Such subtractive cancellation is worsened by the fact that the
denominator is squared.

3.7 First, the correct result can be calculated as

y = 1.37 3 − 7(1.37) 2 + 8(1.37) − 0.35 = 0.043053

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may
be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the
publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual
course preparation. If you are a student using this Manual, you are using it without permission.
5

(a) Using 3-digits with chopping

1.373 → 2.571353 → 2.57


–7(1.37)2 → –7(1.87) → –13.0
8(1.37) → 10.96 → 10.9
– 0.35
0.12

This represents an error of

0.043053 − 0.12
εt = = 178.7%
0.043053

(b) Using 3-digits with chopping

y = ((1.37 − 7)1.37 + 8)1.37 − 0.35

y = ( −5.63 × 1.37 + 8)1.37 − 0.35

y = ( −7.71 + 8)1.37 − 0.35

y = 0.29 × 1.37 − 0.35

y = 0.397 − 0.35

y = 0.047

This represents an error of

0.043053 − 0.047
εt = = 9.2%
0.043053

Hence, the second form is superior because it tends to minimize round-off error.

3.8
20 × 40 × 120 = 96,000 words @ 64 bits/word = 8 bytes/word
96,000 words @ 8 bytes/word = 768,000 bytes
768,000 bytes / 1024 bytes/kilobyte = 750 kilobytes = 0.75 Mbytes

3.9 Here is a MATLAB M-file to solve the problem. Programs in other languages would have a
similar structure and outcome.

% Given: Taylor Series Approximation for


% cos(x) = 1 - x^2/2! + x^4/4! - ...
% Find: number of terms needed to represent cos(x) to
% 8 significant figures at the point where: x = 0.3 pi

x = 0.3 * pi;

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may
be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the
publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual
course preparation. If you are a student using this Manual, you are using it without permission.
6

es = 0.5e-08;
%approximation
cosi = 1;
j = 1;
% j=terms counter
fprintf('j= %2.0f cos(x)= %0.10f\n', j,cosi)
fact = 1;
while(1)
j = j + 1;
i = 2 * j - 2;
fact = fact * i * (i - 1);
cosn = cosi + ((-1) ^ (j + 1)) * ((x) ^ i) / fact;
ea = abs((cosn - cosi) / cosn);
fprintf('j= %2.0f cos(x)= %0.10f ea = %0.1e\n',j,cosn,ea)
if ea < es, break, end
cosi = cosn;
end

j= 1 cos(x)= 1.0000000000
j= 2 cos(x)= 0.5558678020 ea = 8.0e-001
j= 3 cos(x)= 0.5887433702 ea = 5.6e-002
j= 4 cos(x)= 0.5877699636 ea = 1.7e-003
j= 5 cos(x)= 0.5877854037 ea = 2.6e-005
j= 6 cos(x)= 0.5877852513 ea = 2.6e-007
j= 7 cos(x)= 0.5877852523 ea = 1.7e-009

The true value of cos(0.3π) is 0.5877852525. Therefore, 6 terms of the Maclaurin series are
necessary to approximate the true value to 8 significant figures.

3.10 First we can evaluate the exact values using the standard formula with double-precision
arithmetic as

2
x1 5,000.002 ± (5,000.002) − 4(1)10 5,000
= =
x2 2(1) 0.002

We can then determine the square root term with 5-digit arithmetic and chopping

(5,000.0) 2 − 4(1)10 = 2,500,000 − 4(1)10 = 24,999,960 chopping


 → 24,999,000

= 4,999.996 chopping
 → 4,999.9
Equation (3.12):

5,000.2 + 4,999.9 9,999.95 chopping 9,999.9


x1 = =   → = 4,999.95 chopping
 → 4,999.9
2 2 2

5,000.2 − 4,999.9 0.1


x2 = = = 0.05
2 2

Thus, although the first root is reasonably close to the true value (εt = 0.002%), the second is
considerably off (εt = 2400%) due primarily to subtractive cancellation.

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may
be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the
publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual
course preparation. If you are a student using this Manual, you are using it without permission.
7

Equation (3.13):

− 2(10) − 20
x1 = = = 200
− 5,000.0 + 4,999.9 − 0.1

− 2(10) − 20
x2 = = = 0.002
− 5,000.0 − 4,999.9 − 9,999.9

For this case, the second root is well approximated, whereas the first is considerably off (εt =
96%). Again, the culprit is the subtraction of two nearly equal numbers.

3.11 Remember that the machine epsilon is related to the number of significant digits by Eq. 3.11

ξ = b1−t


which can be solved in base 10 for a machine epsilon of 1.19209× 10 7 for

t = 1 − log10 (ξ ) = 1 − log10 (1.19209 × 10 -7 ) = 7.92

To be conservative, assume that 7 significant figures are good enough. Recall that Eq. 3.7 can
then be used to estimate a stopping criterion,

ε s = (0.5 × 10 2− n )%

Thus, for 7 significant digits, the result would be

ε s = (0.5 × 10 2−7 )% = 5 × 10 −6 %

The total calculation can be expressed in one formula as

ε s = (0.5 × 10 2−Int (1−log10 (ξ )) )%

It should be noted that iterating to the machine precision is often overkill. Consequently,
many applications use the old engineering rule of thumb that you should iterate to 3
significant digits or better.

As an application, I used Excel to evaluate the second series from Prob. 3.5. The results are:

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may
be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the
publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual
course preparation. If you are a student using this Manual, you are using it without permission.
8

Notice how after summing 21 terms, the result is correct to 7 significant figures. At this point,
− −
the true and the approximate percent relative errors are at 1.82× 10 6% and 6.29× 10 6%,
respectively. The process would repeat one more time so that the error estimates would fall

below the precalculated stopping criterion of 5× 10 6 %.

PROPRIETARY MATERIAL. © The McGraw-Hill Companies, Inc. All rights reserved. No part of this Manual may
be displayed, reproduced or distributed in any form or by any means, without the prior written permission of the
publisher, or used beyond the limited distribution to teachers and educators permitted by McGraw-Hill for their individual
course preparation. If you are a student using this Manual, you are using it without permission.

You might also like