Download as pdf or txt
Download as pdf or txt
You are on page 1of 11

Numerical Methods – Assignment A

Accuracy of computation

Author: Mikołaj Domagalski (303852)


Tutor: dr inż. Jakub Wagner

Warsaw University of Technology


Faculty of Electronics and Information Technology

Warsaw, 01.04.2023

page 1
Table of contents

1. Introduction .................................................................................................................... 3
2. Problem #1 – propagation of data errors ........................................................................ 3
3. Problem #2 – propagation of rounding errors ................................................................ 4
4. Problem #3 – propagation of rounding errors. Influence of the order of performing
operations ....................................................................................................................... 6
5. Conclusions .................................................................................................................... 7
6. Appendix ........................................................................................................................ 8
7. References .................................................................................................................... 11

page 2
1. Introduction
Computations done by computers are nowadays a common practice and are present in almost
every engineering applications, programs and devices. However, in order to meet their
particular requirements and function properly, errors that they make during computing should
be taken into consideration in the design process. For evaluating accuracy of computation
following errors can be distinguished: data errors and rounding errors. These errors also
propagate as performing more operations on corrupted numbers continues. This propagation of
relative error from the variable 𝑥 to the variable 𝑦 = 𝑓(𝑥) can be characterized as a function
with the following formula:
𝑥 𝑑𝑦
𝑇(𝑥) = (1)
𝑦 𝑑𝑥

2. Problem #1 – propagation of data errors


Let’s check the characteristics of the 𝑇(𝑥) coefficient presented in (1) when computing the
result of expression below:
log(𝑥) 3
𝑦(𝑥) = 5 − 𝑥2 (2)
𝑥2

where 𝑥 ∈ [0.1, 3.0]. For this purpose, a simple Matlab script which sketches a graph
comparing the 𝑇(𝑥) value computed using the formula in (1) and the values computed using
“method of simulation”, was implemented. After substituting the expression from (2) to formula
in (1), it takes the form:

5 ∙ log⁡(𝑥) 3√𝑥 1
𝑥( 7 + 2 − 7)
𝑥 𝑑𝑦 2∙𝑥 2 𝑥2
𝑇(𝑥) = =− 3
(3)
𝑦 𝑑𝑥 log(𝑥)
5 − 𝑥2
𝑥2

The second method, i.e. “method of simulation”, means, that the script works iteratively (100
times) and for 𝑥 ∈ [0.1, 3.0] it computes the value of the following expression:
1 𝑦(𝑥̃) − 𝑦(𝑥)
𝑇(𝑥) = | | (4)
10−8 𝑦(𝑥)

where 𝑥̃ = 𝑥(1 + 10−8 ). After that, points obtained in this way were also sketched on the
graph. As a result, a comparison presented in Fig. 1 could be made.

page 3
Fig. 1. Values of the 𝑇(𝑥) coefficient obtained using formula (1) and “simulation method”
from (4) by applying 100 iterations

3. Problem #2 – propagation of rounding errors


Let’s consider following algorithm:
𝑣1 = log⁡(𝑥) 𝑣3 = 𝑣1 /𝑣2
[𝑥] → ⁡ [ 5 ]→[ 3 ] → [𝑦 = 𝑣3 − 𝑣4 ] (5)
𝑣2 = 𝑥2 𝑣4 = 𝑥 2

Its result is equivalent to the result of expression presented in (2). In order to compare the
coefficients KA1 characterizing the propagation of relative errors caused by rounding the
intermediate results, firstly, let’s compute the coefficient using the first method (epsilon
calculus). Analysis of the algorithm yields:

log(𝑥) (1 + 𝜂1 ) 3
𝑦̃ = [ 5
(1 + 𝜂2 ) − 𝑥 2 ] (1 + 𝜂3 ) (6)
𝑥2

where 𝜂1 is the rounding error corrupting the result of computing the logarithm, 𝜂2 is the
rounding error corrupting the result of the division and 𝜂3 is the rounding error corrupting the
result of subtraction. After applying mathematical operations, this expression takes form:

page 4
log(𝑥) 3 log(𝑥) (𝜂1 + 𝜂2 + 𝜂1 𝜂2 + 𝜂3 + 𝜂1 𝜂3 + 𝜂2 𝜂3 + 𝜂1 𝜂2 𝜂3 ) 3
𝑦̃ = 5 − 𝑥2 + 5 − 𝑥 2 𝜂3 (7)
𝑥2 𝑥2

That can be also expressed as:


3
log(𝑥) (𝜂1 + 𝜂2 + 𝜂1 𝜂2 + 𝜂3 + 𝜂1 𝜂3 + 𝜂2 𝜂3 + 𝜂1 𝜂2 𝜂3 ) 𝑥 2 𝜂3
𝑦̃ = 𝑦 [1 + 5 − ] (8)
𝑦
𝑥2 ∙ 𝑦

Hence:
3
log(𝑥) 𝑥2
|𝛿[𝑦̃]| = (| 5 | + | |) 𝑒𝑝𝑠 (9)
𝑦
𝑥2 ∙𝑦

The second method (method of simulation) again works iteratively, i.e. 100 times, it computes
the result of expression in (4) for 𝑥 ∈ [0.1, 3.0]. However, this time, a random sign of error 𝜀𝑠𝑖𝑚
is assigned, so the result is divided by either 10−8 or −10−8. What is more, from 300 tries, the
one with the largest obtained KA1 is picked.
As a result, a following graph presented in Fig. 2 comparing both methods could be sketched.

Fig. 2. Values of the 𝐾𝐴1 (𝑥) coefficient obtained using ”epsilon calculus” method and
“simulation method”

page 5
4. Problem #3 – propagation of rounding errors. Influence of
the order of performing operations
Let’s again consider the problem of propagation of rounding errors, this time for the following
algorithm:
𝑣1 = log⁡(𝑥) 𝑣3 = 𝑣1 − 𝑣2
[𝑥] → ⁡ [ ]→[ 5 ] → [𝑦 = 𝑣3 /𝑣4 ] (10)
𝑣2 = 𝑥 4 𝑣4 = 𝑥 2

It can be rewritten as an expression:


log(𝑥) − 𝑥 4
𝑦(𝑥) = 5 (11)
𝑥2

It can be observed, that the expression is the same as the one presented in (2). It differs however
in the order of performing computations – in this case the order of dividing and subtracting has
been swapped. So, by this example it will be possible to deduce, if the order of operations will
influence the propagation of rounding errors. Firstly, the relative error in terms of epsilon
calculus can be assessed:

(log(𝑥) (1 + 𝜂1 ) − 𝑥 4 )(1 + 𝜂2 )
𝑦̃ = [ 5 ] (1 + 𝜂3 ) (12)
𝑥2

where 𝜂1 is the rounding error corrupting the result of computing the logarithm, 𝜂2 is the
rounding error corrupting the result of the subtraction and 𝜂3 is the rounding error corrupting
the result of division. After applying mathematical operations, this expression takes form:
log(𝑥) − 𝑥 4
𝑦̃ = 5 +
𝑥2
(13)
log(𝑥) (𝜂1 + 𝜂2 + 𝜂1 𝜂2 + 𝜂3 + 𝜂1 𝜂3 + 𝜂2 𝜂3 + 𝜂1 𝜂2 𝜂3 ) − 𝑥 4 (𝜂2 + 𝜂3 + 𝜂2 𝜂3 )
+ 5
𝑥2

That can be also expressed as:

log(𝑥) (𝜂1 + ⋯ ) 𝑥 4 (𝜂2 + ⋯ )


𝑦̃ = 𝑦 [1 + 5 − 5 ] (14)
𝑥2 ∙𝑦 𝑥2 ∙𝑦

Hence:

log(𝑥) 𝑥4
|𝛿[𝑦̃]| = (| 5 | + | 5 |) 𝑒𝑝𝑠 (15)
𝑥2 ∙ 𝑦 𝑥2 ∙ 𝑦

page 6
Now, we also repeat the simulation method – in the same way as it was previously described in
Problem #2.
Finally, the graph comparing both methods can be sketched.

Fig. 3. Values of the 𝐾𝐴2 (𝑥) coefficient obtained using ”epsilon calculus” method and
“simulation method”
Graph presented in Fig. 3 also differs from the one in Fig. 2, so indeed the order of performing
operations matters when concerning the propagation of rounding errors.

5. Conclusions
There are several aspects that may influence the errors corrupting data during computations. By
collecting the results obtained during above made computations, a following graph comparing
all of them could be sketched:

page 7
Fig. 4. Values of the 𝑇(𝑥), 𝐾𝐴1 (𝑥), 𝐾𝐴2 (𝑥) coefficients with different
obtaining methods – comparison for 𝑥 ∈ [0.1, 3.0]
As presented above, for this particular case, the K coefficient has the largest value when
performing subtraction before division. This problem can be minimized by performing division
firstly, instead. What is more, the data error propagation coefficient T came out to have the
smallest value, but with the most dynamic changes.
This set of problems proved, that assessing accuracy of computation is a complex process and
while designing engineering applications one should be aware of actions hidden behind it, as in
some cases, even decisions on the order of operations may in a significant way affect further
computations.

page 8
6. Appendix
Matlab code used to generate results presented in the report:
a) ex1.m
syms y(x) d;
eps_sim=10^(-8);

y=log(x)/x^(5/2)-x^(3/2);

x_errcorr=x*(1+eps_sim);
y_errcorr=log(x_errcorr)/x_errcorr^(5/2)-x_errcorr^(3/2);

d=diff(y);
dm=matlabFunction(d);

T=(x/y)*diff(y);
T_sim=(1/eps_sim)*(abs((y_errcorr-y)/y));
pretty(T)

x=linspace(0.1,3.0,100);

for n=1:100
y=log(x(n))/x(n)^(5/2)-x(n)^(3/2);
x_errcorr=x(n)*(1+eps_sim);
y_errcorr=log(x_errcorr)/x_errcorr^(5/2)-x_errcorr^(3/2);
T(n)=abs((x(n)/y)*dm(x(n)));
T_sim(n)=abs((y_errcorr-y)/y)/eps_sim;
end

semilogy(x,T);hold on;grid on;


semilogy(x,T_sim,'o');
xlabel('x');
ylabel('T(x)');
title('y=log(x)/x^5^/^2-x^3^/^2');
legend('T(x)','T(x) simulation');
axis([0.1 3 0 10]);

b) ex2.m
syms y(x);
eps_sim=10^(-8);

y=log(x)/x^(5/2)-x^(3/2);
ym=matlabFunction(y);

KA1_eps=zeros(1,100);
KA1_sim=zeros(1,300);

page 9
KA1_sim_f=zeros(1,100);

x=linspace(0.1,3.0,100);

for n=1:100

KA1_eps(n)=3*abs(log(x(n))/(x(n)^(5/2)*ym(x(n))))+abs(x(n)^(3/2)/ym(
x(n)))+1;
KA1_sim=zeros(1,300);

for m=1:300
v1=log(x(n))*(1+((rand()>0.5)*2-1)*eps_sim);
v2=(x(n)^(5/2))*(1+((rand()>0.5)*2-1)*eps_sim);
v3=v1/v2*(1+((rand()>0.5)*2-1)*eps_sim);
v4=(x(n)^(3/2))*(1+((rand()>0.5)*2-1)*eps_sim);
ye=(v3-v4)*(1+((rand()>0.5)*2-1)*eps_sim);
KA1_sim(m)=(abs((ye-ym(x(n)))/ym(x(n))))/eps_sim;
end

KA1_sim_f(n)=max(KA1_sim);
end

semilogy(x,KA1_eps);hold on;grid on;


semilogy(x,KA1_sim_f,'o');
xlabel('x'); ylabel('KA1(x)');
title('y=log(x)/x^5^/^2-x^3^/^2')
legend('KA1(x) epsilon calculus','KA1(x) simulation method');
axis([0.1 3 0 5]);

c) ex3.m
syms y(x);
eps_sim=10^(-8);

y=log(x)/x^(5/2)-x^(3/2);
ym=matlabFunction(y);

KA2_eps=zeros(1,100);
KA2_sim=zeros(1,300);
KA2_sim_f=zeros(1,100);

x=linspace(0.1,3.0,100);

for n=1:100

KA2_eps(n)=abs(log(x(n))/(x(n)^(5/2)*ym(x(n))))+abs(x(n)^4/(x(n)^(5/
2)*ym(x(n))))+3;
KA2_sim=zeros(1,300);

page 10
for m=1:300
v1=log(x(n))*(1+((rand()>0.5)*2-1)*eps_sim);
v2=(x(n)^4)*(1+((rand()>0.5)*2-1)*eps_sim);
v3=(v1-v2)*(1+((rand()>0.5)*2-1)*eps_sim);
v4=(x(n)^(5/2))*(1+((rand()>0.5)*2-1)*eps_sim);
ye=(v3/v4)*(1+((rand()>0.5)*2-1)*eps_sim);
KA2_sim(m)=(abs((ye-ym(x(n)))/ym(x(n))))/eps_sim;
end

KA2_sim_f(n)=max(KA2_sim);
end

semilogy(x,KA2_eps);hold on;grid on;


semilogy(x,KA2_sim_f,'o');
xlabel('x'); ylabel('KA2(x)');
title('y=(log(x)-x^4)/x^5^/^2')
legend('KA2(x) epsilon calculus', 'KA2(x) simulation');
axis([0.1 3 0 4.5]);

d) Also, “ex4.m” file was used, but it contains simply all 3 files above combined
together

7. References
Sources used during the preparation of the report:
• R. Z. Morawski, A. Miękina, “Solved Problems in Numerical Methods for Students of
Electronics and Information Technology”, Oficyna Wydawnicza Politechniki
Warszawskiej, 2021.
• J. Krupka, A. Miękina, R. Z. Morawski, L. J. Opalski: „Wstęp do metod numerycznych
dla studentów elektroniki i technik informacyjnych”, Oficyna Wydawnicza Politechniki
Warszawskiej, 2009.
• R. Z. Morawski, “Numerical Methods (ENUME) – Lecture notes for Spring Semester
2022/2023”.

page 11

You might also like