DEV Community


Posted on

Numerical methods (part 1)

Before getting into the course, let's look at 'what numerical methods' really mean.

What are numerical methods?

Numerical methods are techniques by which mathematical problems are formulated so that they can be solved with arithmetic operations yielding numerical results instead of closed-form results. Most numerical methods involve large amounts of arithmetic calculations; most of them are trials and erros.

Sometimes, engineering problems cannot be efficiently solved for exact answer. But instead with a large number of engineering problems being solved using trials and errors (due to the development of fast and advanced computers).

let's look at an example :)

Finding irrational square root

We continue if x2 < 6. But if it still exceeds, we move on to the next decimal point. BUT we have to stop somewhere, and it's when the result is close enough; which we get from approximating. (But we have to be careful about accuracy and precision.

A picture to demonstrate what are accuracy and precision:

Let's move on to see how we solve a problem..


But hey, if there's something that comes with an approximation, it's an error. There are 2 types of errors; true error and approximation error.

True error

True error (Et) = True value - Approximation
True relation error (Ɛt) = True value - ApproximationTrue value x 100

But the problem with true error is that we don't know what the exact true value is. So instead we use Approximation error.

Approximation error

Ɛα = Approximation errorApproximation x 100
Ɛα = Present approximation - Previous approximationPresent approximation x 100

And there are 2 types of approximation error:

1) Truncation error is the error made by truncating an infinite sum and approximating it by a finite sum.
2) Round-off error is the difference between the calculated approximation of a number and its exact mathematical value due to rounding.

Ok, so back to the 'when to stop' thing and approximation. How will we be confident enough to stop the calculation?

The answer is (not really, but what we use for now) when.. |Ɛα| < Ɛs

Ɛα is approximation error
Ɛs = (0.5 x 102-n )%

  • Ɛs is already in percentage
  • n is a number of significant figures we want

And below is an octave (similar to matlab, but a free, open-source one) code for calculating the √6.

% take squareroot of 6
m = 6;
true_value = 2.44948974270319;

% nf = the number of the significant figures
nf = 4;
es = 0.5 * 10^(2-nf);
printf('es = %f\n', es);

% set x = the approximation answer
% initialize x = 0
previous_result = 0;
x = 0;

% outer loop to loop for the number of digit
% we want to do this for ndigits
n = 6;

%for d = 0:n;
ea = 100;

d = 0;

while (abs(ea) > es)
    previous_result = x;
    for i = 0:10
      x = previous_result + i/(10^d);
      if (x*x > m)
    x = x - 1/(10^d);
    ea = ((x - previous_result)/x)*100;
    et = ((true_value - x)/true_value) * 100;
    printf('x = %f, ea = %f, et = %f\n', x, ea, et);


%printf('x = %f.\n', x);

And below is the result of the above code:

es = 0.005000
x = 2.000000, ea = 100.000000, et = 18.350342
x = 2.400000, ea = 16.666667, et = 2.020410
x = 2.440000, ea = 1.639344, et = 0.387417
x = 2.449000, ea = 0.367497, et = 0.019994
x = 2.449400, ea = 0.016331, et = 0.003664
x = 2.449480, ea = 0.003266, et = 0.000398

Ok, I'm going to stop here for today, see you in the next part! xD

Top comments (3)

codevault profile image
Sergiu Mureşan • Edited

One semester about approximations in computer science didn't help me understand them and this post did. Thank you for the post!

rinsama77 profile image

Thank you! I'm so glad it helps ^ I'll be writing the next lesson next, I hope it also helps!!

xanxus_xth profile image

Thank you for the post. You saved my life.