Numerical methods (part 1)
Tags: numericalmethods, muict
Published: 2018-09-07 08:18:15

---
title: Numerical methods (part 1)
published: true
description: This post covers what are numerical methods, approximation errors, and an octave code for finding square root of 6 :)
tags: numerical methods, MUICT
---
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
![](https://thepracticaldev.s3.amazonaws.com/i/yrc5w4rfafovjbrka9gq.png)
> We continue if x^2 < 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:
![](https://thepracticaldev.s3.amazonaws.com/i/p4r0iwazptd2ri7n40ac.png)
Let's move on to see how we solve a problem..
![](https://thepracticaldev.s3.amazonaws.com/i/z221nlkle5cy7albl0ew.png)
__________
### ERROR
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 (E<sub>t</sub>) = True value - Approximation
> True relation error (Ɛ<sup>t</sup>) = <sup>True value - Approximation</sup>&frasl;<sub>True value</sub> 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

> Ɛ<sub>α</sub> = <sup>Approximation error</sup>&frasl;<sub>Approximation</sub> x 100
> Ɛ<sub>α</sub> = <sup>Present approximation - Previous approximation</sup>&frasl;<sub>Present approximation</sub> 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.. |Ɛ<sub>α</sub>| < Ɛ<sub>s</sub>

> Ɛ<sub>α</sub> is approximation error
> Ɛ<sub>s</sub> = (0.5 x 10^2-n )% 
> - Ɛ<sub>s</sub> 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 &#x221A;6.
```matlab
% 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)
        break;
      endif
    endfor
    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);
    d++;
    
endwhile

%printf('x = %f.\n', x);
```
And below is the result of the above code:
```text
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