3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

テイラー展開を使っていろんな数を求めてみよう😀

Last updated at Posted at 2019-05-31

[追記]
@scivola さんのコメントより一部修正させて頂きました。修正箇所は下段コメントをご確認ください。

テイラー展開の応用

テイラー級数を応用し、平方根などの近似を求めよう:poop:

1 + x のk乗のテイラー級数への展開

\begin{eqnarray}
(1 + x)^k &=& 1 + kx + \frac{k(k-1)}{2!} + \frac{k(k-1)(k-2)}{3!}x^3 + \dots \\
          &=& \sum_{n-1}^{\infty} \frac{k(k-1) \dots (k-n+1)}{n!}
\end{eqnarray}

$(|x| < 1)$

これに近似を行うと

$$
(1+x)^k \fallingdotseq 1 + kx
$$

これは平方根などの近似計算をするとき便利 :thumbsup:

平方根を求める

$$
\sqrt{1+x} \fallingdotseq 1 + \frac{1}{2}x
$$

例えば

$$
\sqrt{1.1} = \sqrt{1+0.1} \fallingdotseq 1+ \frac{1}{2} \times 0.1 = 1.05
$$

Python3で計算してみる

Python3
Python 3.7.3 (default, Mar 27 2019, 09:23:15) 
[Clang 10.0.1 (clang-1001.0.46.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import math
>>> math.sqrt(1.1)
1.0488088481701516

結構合ってる :smile:

そういえば高校の物理でやったなーって人もいるかも

円周率を求められるかな 🤔

オンライン整数列大辞典 A000796より
$\pi = 3.14159265358979323846...$ らしい

\begin{eqnarray}
\tan{x}^{-1} &=& x- \frac{1}{3}x^3+ \frac{1}{5}x^5- \frac{1}{7}+ \dots \\
          &=& \sum_{n-1}^{\infty} (-1)^{n-1} \frac{x^{2n-1}}{2n-1} 
\end{eqnarray}

$$
(|x| \leq 1)
$$

ここで $\tan{ \pi/4}=1$ より

$$
\frac{\pi}{4} = 1- \frac{1}{3}+ \frac{1}{5}- \frac{1}{7}+ \dots
$$

Rustで計算してみる

Rust
fn main() {
    let mut pi = 1.0;
    let n = 10;
    let mut s = -1.0;

    print!("1");
    for i in 1..=n {
        print!("{}1/{}", if s == 1.0 {'+'} else {'-'}, 2 * i + 1);
        pi += s * 1.0 / (2 * i + 1) as f64;
        s *= -s;
    }
    println!("\n{}", pi * 4.0);
}

結果 $n = 10$

$ cargo run
   Compiling
    Finished dev [unoptimized + debuginfo] target(s) in 0.56s
     Running `target/debug/unko`
1-1/3+1/5-1/7+1/9-1/11+1/13-1/15+1/17-1/19
 pi = 3.0418396189294032 (n = 10)

一桁目しか合ってないぞ 🤔

結果 $n = 10^7$

Rust
$ cargo run
   Compiling unko v0.1.0 (unko)
    Finished dev [unoptimized + debuginfo] target(s) in 0.44s
     Running `target/debug/unko`

 pi = 3.1415925535897915 (n = 10000000)

7桁しか合ってないぞ 🤔

マチンの公式を使ってみよう

マチンの公式

$$
\frac{\pi}{4}=4 tan^{-1} { \frac{1}{5}}- tan^{-1}{ \frac{1}{239}}
$$

\begin{eqnarray}
tan^{-1} \frac{1}{5} &=& \frac{1}{5}- \frac{1}{3} {\frac{1}{5}}^3 + \frac{1}{5}{\frac{1}{5}}^5- \dots \\
tan^{-1} \frac{1}{239} &=& \frac{1}{239}- \frac{1}{3} {\frac{1}{239}}^3 + \frac{1}{5}{\frac{1}{239}}^5- \dots
\end{eqnarray}
Rust
fn main() {
    let d5   = 1.0 / 5.0;
    let d239 = 1.0 / 239.0;
    let n = 10;
    let mut arc_tan5 = d5;
    let mut arc_tan239 = d239;
    let mut s = -1.0;

    for k in 1..=n {
        let odd = (2 * k + 1) as f64;
        arc_tan5     += s * 1.0 / odd as f64 * f64::powf(d5, odd);
        arc_tan239   += s * 1.0 / odd as f64 * f64::powf(d239, odd);
        s *= -s;
    }
    println!("pi = {}", 4.0 * (4.0 * arc_tan5 - arc_tan239));
}

結果

n = 10 の時

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.11s
     Running `target/debug/unko`
pi = 3.1415926535897922

強い 😳

ネイピア数を求めよう

$e^x = 1 + x + \frac{1}{2!}x^2 + \frac{1}{3!} x^3 + \dots + \frac{1}{n!}x^n + \dots$

x = 1 とすると

$e = 1 + 1 + \frac{1}{2!} + \frac{1}{3!} + \dots$

Rust
fn exp(n: usize) -> f64 {
    let mut e = 1.0_f64;
    let mut d = 1.0_f64;
    for i in 1..=n {
        d *= i as f64;
        e += 1.0 / d;
    }
    e
}

fn main() {
    println!("{}", exp(10));
    println!("{}", exp(100));
}
結果
n = 10 の時 2.7182815255731922                                                 
n = 100 の時 2.7182818284590455 

オンライン整数列大辞典A001113よりネイピア数は
$e = 2.71828182845904523...$

らしい。かなりいい線いっている!

参考

鷹尾 洋保 数列と級数のはなし―等差数列からテイラー級数・フーリエ級数まで

3
4
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?