Anda di halaman 1dari 20

Numerical Methods

Group No.:______________________________________
Date Performed: _________________________________

Rating:__________________________
Date Submitted: __________________

Numerical Methods
APPROXIMATION AND ROUND-OFF ERRORS
Experiment No. 2
I. OBJECTIVES
1. Develop algorithms for iterative methods in the approximation of values of functions using infinite
series expansions.
2. Determine the true and approximate relative errors due to rounding-off for each iterative method,
and establish a stopping criterion for each algorithm.
3. Evaluate the effect of the use of single and double precision floating-point numbers in the accuracy
of the numerical solution.
II. MACHINE PROBLEMS
1. If |x|<1 it is known that
1
=1+ x + x 2 + x3 +
1x

x=0.1 , evaluate this series correct to seven significant figures. How many terms are

For

required to achieve such accuracy?


5
2. Evaluate e
using two approaches

ex =1x+

x2 x3
+
2 3!

and
x

e =

1
=
ex

1
2

1+ x +

x x3
+ +
2 3!

Machine Problem No.2 Approximation and Round-Off Errors

Page 1

Numerical Methods
6.737947 103 . Use 20 terms to evaluate the series and

and compare with the true value of

compute the true and relative errors as terms are added.

3. The infinite series


n

f ( n )=
k=1

1
k4

converges on a value of

f ( n )=

4
90

as n

single and double precision to calculate

f (n )

approaches infinity. Evaluate this infinite series in


for

n=10000

by computing the sum from

k =1 to 10000 and in reverse order. Also evaluate the true and approximate relative errors

for each case.


4. Determine the roots of a quadratic equation having

a=1 ,

b=3000.001 , and

c=3 .

Use single and double precision arithmetic. Evaluate the true relative error and determine the
cause of the discrepancy between the solutions. The true roots for the equation are
x 1=0.001
x 2=3000
and
.
5. For 100 equally spaced values of

x between 109 to 107.4 , evaluate the expressions

y= x +4 x+ 3

and
y=

1
x+ 4+ x +3

Note that both expressions are mathematically equivalent. Using plotting tools, graph the function
in the given interval. Which function is better in terms of resisting loss of significance?
III. METHODOLOGY
Machine Problem No.2 Approximation and Round-Off Errors

Page 2

Numerical Methods
Machine Problem 2.1
Code 2.1.1 Pseudocode for Problem 2.1
x = 0.1
I=0
prev=0
Ea=100
If x<1 Then
tv = 1 / (1 - x)
et = ((tv - 1) / tv) * 100
Do
av = pa + x ^ i
i = i + 1
ea = ((av - pa) / av) * 100
If Et>Es Exit
pa = av
End Do
Display Ea, Et, av

Code 2.1.2 VBA Code for Problem 2.1


Sub machineproblem1()
Dim
Dim
Dim
Dim
Dim
Dim
Dim

count
pa As
av As
tv As
Ea As
Et As
Es As

As Double
Double
Double
Double
Double
Double
Double

count = 0
pa = 0
Ea = 100
Es = 0.5 * 10 ^ -5
x = 0.1
If Abs(x) < 1 Then
tv = 1 / (1 - x)
Et = ((tv - 1) / tv) * 100
Do While Abs(Et) > Es
av = pa + x ^ count
count = count + 1
Ea = ((av - pa) / av) * 100
Et = ((tv - av) / tv) * 100
Range("A" & count + 2) = count
Range("B" & count + 2) = av
Range("C" & count + 2) = Abs(Ea)
Range("D" & count + 2) = Abs(Et)
pa = av
Loop
MsgBox "Approximate Value is " & av
End If
End Sub

Machine Problem No.2 Approximation and Round-Off Errors

Page 3

Numerical Methods
Code 2.1.3 MathScript Code for Problem 2.1
format long
x = 0.1;
if abs(x)<1
count=0;pa=0;Ea=100;Es = 0.5 * 10 ^ -5;
tv = 1 / (1 - x);
Et = ((tv - 1) / tv) * 100;
while abs(Et)>Es
av = pa + x ^ count;
count = count + 1;
Ea = ((av - pa) / av) * 100;
Et = ((tv - av) / tv) * 100;
pa = av;
end
fprintf('Approximated Value is %f \n',av)
fprintf('Relative Error is %f percent \n',Ea)
fprintf('True Error is %f percent \n',Et)
fprintf('Number of Iteration %i iterations \n',count)
end

Machine Problem 2.2


Code 2.2.1 Pseudocode for Problem 2.2
x = 5
n = 20
DO
av = pa + (-1) ^ count * x ^ count / factorial(count)
count = count + 1
Ea = ((av - pa) / av) * 100
Et = ((tv - av) / tv) * 100
pa = av
IF count>n EXIT
END DO

Code 2.2.2 VBA Code for Problem 2.2


Sub machineproblem2()
Dim count1 As Double
Dim count2 As Double
x = 5
n = 20
count1 = 0
count2 = 0
tv = Exp(-x)
Range("B2").Value = tv
'Approach 1
Do While count1 < n
av = pa + (-1) ^ count1 * x ^ count1 / factorial(count1)
count1 = count1 + 1
Ea = ((av - pa) / av) * 100
Et = ((tv - av) / tv) * 100

Machine Problem No.2 Approximation and Round-Off Errors

Page 4

Numerical Methods
Range("A"
Range("B"
Range("C"
Range("D"
pa = av
Loop

&
&
&
&

count1
count1
count1
count1

+
+
+
+

6)
6)
6)
6)

=
=
=
=

count1
av
Abs(Ea)
Abs(Et)

'Approach 2
Do While count2 < n
av2 = pa2 + x ^ count2 / factorial(count2 * 1)
count2 = count2 + 1
iav = 1 / av2
Ea2 = ((iav - ipa) / iav) * 100
Et2 = ((tv - iav) / tv) * 100
Range("F" & count2 + 6) = count2
Range("G" & count2 + 6) = iav
Range("H" & count2 + 6) = Abs(Ea2)
Range("I" & count2 + 6) = Abs(Et2)
ipa = iav
pa2 = av2
Loop
Range("B3").Value = Range("B26").Value
Range("B4").Value = Range("G26").Value
End Sub

Code 2.2.3 MathScript Code for Problem 2.2


APPROACH 1
y=5;
x=(1-y);
i=2;
table=ones(20,4);
true=.006737947;
while i<=19
r=rem(i,2);
if r == 0
xnew=x+((y^i)/(factorial(i)));
else

xnew=x-((y^i)/(factorial(i)));

end
i=i+1;
et=((true-xnew)/(true))*100;
et=abs(et);
ea=abs(((xnew-x)/xnew)*100);
x=xnew;
table(i,1)=i;
table(i,2)=xnew;
table(i,3)=et;
table(i,4)=ea;

Machine Problem No.2 Approximation and Round-Off Errors

Page 5

Numerical Methods

end
table(1,1)=1;
table(2,1)=2;
table(1,2)=1;
table(2,2)=1-y;
et1=abs(((true-1)/(true))*100);
table(1,3)=et1;
et2=((true-(1-y))/(true))*100;
table(2,3)=et2;
table(1,4)=100;
ea2=abs((((1-y)-1)/(1-y))*100);
table(2,4)=ea2;
disp(num2str(table))
APPROACH 2
y=5;
x=(1+y);
i=2;
table=ones(20,4);
z=1/x;
while i<=19
x=x+((y^i)/(factorial(i)));
i=i+1;
znew=1/x;
et=((true-z)/(true))*100;
et=abs(et);
ea=abs(((znew-z)/znew)*100);
z=znew;
table(i,1)=i;
table(i,2)=z;
table(i,3)=et;
table(i,4)=ea;
end
table(1,1)=1;
table(2,1)=2;
table(1,2)=1;
table(2,2)=1+y;
et1=abs(((true-1)/(true))*100);
table(1,3)=et1;
et2=((true-(1-y)^-1)/(true))*100;
table(2,3)=et2;
table(1,4)=100;
ea2=abs((((1+y)-1)/(1+y))*100);
table(2,4)=ea2;
disp(num2str(table))

Machine Problem No.2 Approximation and Round-Off Errors

Page 6

Numerical Methods
Machine Problem 2.3
Code 2.3.1 Pseudocode for Problem 2.3
k=1;
nold=0;
table=ones(10000,5);
a=(pi^4)/90;
while k<=10000;
nnew=(1/(k^4))+nold;
ea=((nnew-nold)/nnew)*100;
nold=nnew;
table(k,1)=k;
table(k,2)=single(nnew);
table(k,3)=double(nnew);
et=((a-nnew)/a)*100;
table(k,4)=abs(et);
table(k,5)=abs(ea);
k=k+1;
end
display table

Code 2.3.2 VBA Code for Problem 2.3


Sub machineproblem3a()
Dim fn, sum, Ea, Et As Single
pi = 3.141592654
tv = pi ^ 4 / 90
k = 1
prev = 0
Range("B3").Value = tv
Do While k <= 10000
fn = prev + 1 / k ^ 4
Ea = ((fn - prev) / fn) * 100
Et = ((tv - fn) / tv) * 100
prev = fn
Range("A" & k + 6) = k
Range("B" & k + 6) = fn
Range("C" & k + 6) = Abs(Ea)
Range("D" & k + 6) = Abs(Et)
k = k + 1
Loop
End Sub
Sub machineproblem3b()
Dim fn, Ea, Et, prev, k As Double
pi = 3.141592654
tv = pi ^ 4 / 90
k = 1
prev = 0
Do While k <= 10000

Machine Problem No.2 Approximation and Round-Off Errors

Page 7

Numerical Methods
fn = prev + 1 / k ^ 4
Ea = ((fn - prev) / fn) * 100
Et = ((tv - fn) / tv) * 100
prev = fn
Range("F" & k + 6) = k
Range("G" & k + 6) = fn
Range("H" & k + 6) = Abs(Ea)
Range("I" & k + 6) = Abs(Et)
k = k + 1
Loop
Range("B3").Value = Range("B10006").Value
Range("B4").Value = Range("G10006").Value
End Sub
REVERSED
Sub machineproblem3a()
Dim fn, sum, Ea, Et As Single
pi = 3.141592654
tv = pi ^ 4 / 90
k = 10000
prev = (1 / (10000 ^ 4))
Range("B3").Value = tv
Do While k >= 1
fn = prev + 1 / k ^ 4
Ea = ((fn - prev) / fn) * 100
Et = ((tv - fn) / tv) * 100
prev = fn
Range("A" & k + 6) = k
Range("B" & k + 6) = fn
Range("C" & k + 6) = Abs(Ea)
Range("D" & k + 6) = Abs(Et)
k = k - 1
Loop
End Sub
Sub machineproblem3b()
Dim fn, Ea, Et, prev, k As Double
pi = 3.141592654
tv = pi ^ 4 / 90
k = 10000
prev = 0
Do While k >= 1
fn = prev + 1 / k ^ 4
Ea = ((fn - prev) / fn) * 100
Et = ((tv - fn) / tv) * 100
prev = fn
Range("F" & k + 6) = k
Range("G" & k + 6) = fn
Range("H" & k + 6) = Abs(Ea)
Range("I" & k + 6) = Abs(Et)
k = k - 1
Loop
Range("B3").Value = Range("B10006").Value

Machine Problem No.2 Approximation and Round-Off Errors

Page 8

Numerical Methods
Range("B4").Value = Range("G10006").Value
End Sub

Code 2.3.3 MathScript Code for Problem 2.3


k=1;
nold=0;
table=ones(10000,5);
a=(pi^4)/90;
while k<=10000;
nnew=(1/(k^4))+nold;
ea=((nnew-nold)/nnew)*100;
nold=nnew;
table(k,1)=k;
table(k,2)=single(nnew);
table(k,3)=double(nnew);
et=((a-nnew)/a)*100;
table(k,4)=abs(et);
table(k,5)=abs(ea);
k=k+1;
end
disp(num2str(table));
REVERSED
k=10000;
nold=1/(10000^4);
table=ones(10000,5);
a=(pi^4)/90
while k>=1;
nnew=(1/(k^4))+nold;
ea=((nnew-nold)/nnew)*100;
nold=nnew;
table(k,1)=k;
table(k,2)=single(nnew);
table(k,3)=double(nnew);
et=((a-nnew)/a)*100;
table(k,4)=abs(et);
table(k,5)=abs(ea);
k=k-1;
end
disp(num2str(table));

Machine Problem 2.4


Machine Problem No.2 Approximation and Round-Off Errors

Page 9

Numerical Methods
Code 2.4.1 Pseudocode for Problem 2.4
a=1;
b=3000.001;
c=3;
d=(b^2)-(4*a*c);
x1=(-b+(sqrt(b^2-(4*a*c))))/(2*a);
x2=(-b-(sqrt(b^2-(4*a*c))))/(2*a);

r1
r2
r3
r4

=
=
=
=

(single(x1));
(single(x2));
(double(x1));
(double(x2));

%Single precision
TrueRelativeErrorSingle1 = ((((-0.001)-(r1)) / -0.001)* 100)
TrueRelativeErrorSingle2 = ((((-3000)-(r2)) / -3000) * 100)
%Double precision
TrueRelativeErrorDouble1 = ((((-0.001)-(r3)) / -0.001)* 100)
TrueRelativeErrorDouble2 = ((((-3000)-(r4)) / -3000) * 100)

Code 2.4.2 VBA Code for Problem 2.4


Sub machineproblem4()
Dim r1, r2 As Single
Dim r3, r4 As Double
Dim a, b, c As Double
a = 1
b = 3000.001
c = 3
x1 = (-b + ((b ^ 2 - (4 * a * c)) ^ 0.5)) / (2 * a)
x2 = (-b - ((b ^ 2 - (4 * a * c)) ^ 0.5)) / (2 * a)
r1
r2
r3
r4

=
=
=
=

x1
x2
x1
x2

'Single precision
Range("B3").Value = ((((-0.001) - (r1)) / -0.001) * 100)
Range("B4").Value = ((((-3000) - (r2)) / -3000) * 100)
'Double precision
Range("B6").Value = ((((-0.001) - (r3)) / -0.001) * 100)
Range("B7").Value = ((((-3000) - (r4)) / -3000) * 100)

Machine Problem No.2 Approximation and Round-Off Errors

Page 10

Numerical Methods
End Sub

Code 2.4.3 MathScript Code for Problem 2.4


a=1;
b=3000.001;
c=3;
d=(b^2)-(4*a*c);
x1=(-b+(sqrt(b^2-(4*a*c))))/(2*a);
x2=(-b-(sqrt(b^2-(4*a*c))))/(2*a);

r1
r2
r3
r4

=
=
=
=

(single(x1));
(single(x2));
(double(x1));
(double(x2));

%Single precision
TrueRelativeErrorSingle1 = ((((-0.001)-(r1)) / -0.001)* 100)
TrueRelativeErrorSingle2 = ((((-3000)-(r2)) / -3000) * 100)
%Double precision
TrueRelativeErrorDouble1 = ((((-0.001)-(r3)) / -0.001)* 100)
TrueRelativeErrorDouble2 = ((((-3000)-(r4)) / -3000) * 100)

Machine Problem 2.5


Code 2.5.1 Pseudocode for Problem 2.5
Machine Problem No.2 Approximation and Round-Off Errors

Page 11

Numerical Methods
Sub
Dim
i =
x =
n =

machineproblem5()
x, y, y2 As Double
-9 - 8 / 495
10 ^ i
1

Do While i <= -37 / 5


y = Sqr(x + 4) - Sqr(x + 3)
y2 = 1 / (Sqr(x + 4) + Sqr(x + 3))
Range("A" & n + 2) = n
Range("B" & n + 2) = x
Range("C" & n + 2) = y
Range("D" & n + 2) = y2
Range("E" & n + 2) = y2 - y
n = n + 1
i = i + 8 / 495
x = 10 ^ i
Loop
End Sub

Code 2.5.2 VBA Code for Problem 2.5


Sub
Dim
i =
x =
n =

machineproblem5()
x, y, y2 As Double
-9 - 8 / 495
10 ^ i
1

Do While i <= -37 / 5


y = Sqr(x + 4) - Sqr(x + 3)
y2 = 1 / (Sqr(x + 4) + Sqr(x + 3))
Range("A" & n + 2) = n
Range("B" & n + 2) = x
Range("C" & n + 2) = y
Range("D" & n + 2) = y2
Range("E" & n + 2) = y2 - y
n = n + 1
i = i + 8 / 495
x = 10 ^ i
Loop
End Sub

Code 2.5.3 MathScript Code for Problem 2.5


x = linspace((1/10^9),(1/10^7.4));
>>y=sqrt(x+4)-sqrt(x+3);
>>figure
>>subplot(2,2,1);
>> plot(x,y,'g');
>>title('sqrt(x+4)-sqrt(x+3)')
>>z=1./(sqrt(x+4)+sqrt(x+3));
>>subplot(2,2,2);
>> plot(x,z);
>>title('1/(sqrt(x+4)+sqrt(x+3))')

Machine Problem No.2 Approximation and Round-Off Errors

Page 12

Numerical Methods
IV. RESULTS AND INTERPRETATION
Machine Problem 2.1 Power Series
VBA:

Figure 2.1.1 Once ran, the program will display the approximated values, the approximation errors and
true errors in every iteration.
MATLAB:

Machine Problem No.2 Approximation and Round-Off Errors

Page 13

Numerical Methods

Figure 2.1.2 Once ran, the program will display the approximated value, relative error, true error, and the
number of iterations of the power series corrected with seven significant figures
Machine Problem 2.2
VBA:

Machine Problem No.2 Approximation and Round-Off Errors

Page 14

Numerical Methods
Figure 2.2.1 Once ran, the program will display the data of the two approaches used in the program, with
20 iterations as stated in the problem.
MATLAB:

Figure 2.2.2 Once the mathscript file (MachineProblem2a) is ran in MATLAB, the program will display the
20 iterations with the corresponding approximate values, true error and relative error

Machine Problem No.2 Approximation and Round-Off Errors

Page 15

Numerical Methods
Figure 2.2.3 For the second approach, once the mathscript file (MachineProblem2b) is ran in MATLAB,
the program will display the 20 iterations with the corresponding approximate values, true error and relative
error.
Machine Problem 2.3
VBA:

Figure 2.3.1 Once ran, the program will display the data in single and double data types in 10000
iterations.

Machine Problem No.2 Approximation and Round-Off Errors

Page 16

Numerical Methods

Figure 2.3.2 Once ran, the program will display the data in single and double data types in 10000
iterations where k = 10000.
MATLAB:

Figure 2.3.3 Once the mathscript file (MachineProblem3a) is ran in MATLAB, the program will display the
10000 iterations with the corresponding approximate values, true error and relative error

Machine Problem No.2 Approximation and Round-Off Errors

Page 17

Numerical Methods

Figure 2.3.4 For the reversed order, once the mathscript file (MachineProblem3b) is ran in MATLAB, the
program will display the 10000 iterations with the corresponding approximate values, true error and relative
error.
Machine Problem 2.4
VBA:

Machine Problem No.2 Approximation and Round-Off Errors

Page 18

Numerical Methods
Figure 2.4.1 Once ran, the program will display the roots of the given parameters in single and double
data types.
MATLAB:

Figure 2.4.2 Once the mathscript file (MachineProblem4) is ran in MATLAB, the program will display the
roots of the equation in single and double data types.
Machine Problem 2.5
VBA:

Machine Problem No.2 Approximation and Round-Off Errors

Page 19

Numerical Methods

Figure 2.5.1 - After configuring the x-scale of the graphs, the last step is labelling the axis, graphs, and the
figure to relay informative data.
V. CONCLUSIONS AND RECOMMENDATIONS
Conclusion:
In the series of machine problem done. It was proven that problems involving Taylor series, Infinite
Series, Loss of significance is easily done once ran using computer aided softwares such as MATLAB and
Excel. And also data type plays a key role in numerical analysis. It was further proven in the Loss of
Significance problem that number of decimal places or how precise the data affects the relative error of it.
Recommendations:
The students encountered problems in plotting data in Excel, specifically on how to employ
through visual basic codes. The group recommends to the instructor to give background information on
plotting in Excel and also on data types existing for data.
VI. REFERENCES
https://en.wikipedia.org/wiki/Loss_of_significance
https://msdn.microsoft.com/en-us/library/xay7978z.aspx
http://www.functionx.com/vbaexcel/Lesson03.htm

Machine Problem No.2 Approximation and Round-Off Errors

Page 20

Anda mungkin juga menyukai