2017年5月14日日曜日

学習環境

解析入門〈1〉(松坂 和夫(著)、岩波書店)の第5章(各種の初等関数)、5.4(三角関数(続き)、逆三角関数)、問題2.を取り組んでみる。

n

  1. cos θ 2 = sinθ 2sin θ 2 cos θ 2 2 = sin θ 2 2sin θ 2 2 cos θ 2 cos θ 2 2 = sinθ 2sin θ 2 · sin θ 2 2sin θ 2 2 2 = sinθ 2 2 sin θ 2 2 = sinθ θ · θ 2 2 sin θ 2 2 sinθ θ · θ 2 n sin θ 2 n ·cos θ 2 n+1 = sinθ θ · θ 2 n sin θ 2 n · sin θ 2 n 2sin θ 2 n+1 = sinθ θ · θ 2 n+1 sin θ 2 n+1 lim n sinθ θ · θ 2 n sin θ 2 n = sinθ θ

コード(Emacs)

Python 3

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from sympy import Symbol, Limit, S, sin, cos, pi, solve, product, pprint


θ = Symbol('θ', positive=True)
i = Symbol('i')
n = Symbol('n', positive=True)

expr = product(cos(θ / 2**i), (i, 1, n))
pprint(expr)
try:
    l = Limit(expr, n, S.Infinity)
    pprint(l)
    result = l.doit()
    pprint(result)
except Exception as err:
    print(type(err), err)

入出力結果(Terminal, IPython)

$ ./sample2.py
  n              
┬────┬           
│    │    ⎛ -i  ⎞
│    │ cos⎝2  ⋅θ⎠
│    │           
i = 1            
      n              
    ┬────┬           
    │    │    ⎛ -i  ⎞
lim │    │ cos⎝2  ⋅θ⎠
n─→∞│    │           
    i = 1            
<class 'NotImplementedError'>
$

HTML5

<div id="graph0"></div>
<pre id="output0"></pre>

<label for="theta0">θ = </label>
<input id="theta0" type="number" min="0.001" step="0.001" max="1.5707963267948965" value="1.2">
<label for="n0">n = </label>
<input id="n0" type="number" min="1" step="1" value="10">

<button id="draw0">draw</button>
<button id="clear0">clear</button>

<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/4.2.6/d3.min.js" integrity="sha256-5idA201uSwHAROtCops7codXJ0vja+6wbBrZdQ6ETQc=" crossorigin="anonymous"></script>

<script src="sample2.js"></script>    

JavaScript

let div0 = document.querySelector('#graph0'),
    pre0 = document.querySelector('#output0'),
    width = 600,
    height = 600,
    padding = 50,
    btn0 = document.querySelector('#draw0'),
    btn1 = document.querySelector('#clear0'),
    input_theta = document.querySelector('#theta0'),
    input_n = document.querySelector('#n0'),
    inputs = [input_theta, input_n],
    p = (x) => pre0.textContent += x + '\n';

let draw = () => {
    pre0.textContent = '';

    let theta = parseFloat(input_theta.value),
        n = parseInt(input_n.value, 10),
        l = Math.sin(theta) / theta;

    let f = (n) => {
        let result = 1;
        
        for (let i = 1; i <= n; i += 1) {
            result *= Math.cos(theta / 2 ** i);
        }
        return result;
    };

    let points = [];
    for (let i = 1; i <= n; i += 1) {
        points.push([i, f(i)]);
    }

    let ys = points.map((o) => o[1]),
        d1 = Math.min(l - 0.1, ...ys),
        d2 = Math.max(l + 0.1, ...ys);
    
    let xscale = d3.scaleLinear()
        .domain([0, n])
        .range([padding, width - padding]);
    let yscale = d3.scaleLinear()
        .domain([d1, d2])
        .range([height - padding, padding]);

    let xaxis = d3.axisBottom().scale(xscale);
    let yaxis = d3.axisLeft().scale(yscale);
    div0.innerHTML = '';
    let svg = d3.select('#graph0')
        .append('svg')
        .attr('width', width)
        .attr('height', height);

    svg.selectAll('circle')
        .data(points)
        .enter()
        .append('circle')
        .attr('cx', (d) => xscale(d[0]))
        .attr('cy', (d) => yscale(d[1]))
        .attr('r', 2)
        .attr('fill', 'green');

    svg.selectAll('line')
        .data([[0, l, n, l]])
        .enter()
        .append('line')
        .attr('x1', (d) => xscale(d[0]))
        .attr('y1', (d) => yscale(d[1]))
        .attr('x2', (d) => xscale(d[2]))
        .attr('y2', (d) => yscale(d[3]))
        .attr('stroke', 'blue');
    
    svg.append('g')
        .attr('transform', `translate(0, ${yscale(0)})`)
        .call(xaxis);

    svg.append('g')
        .attr('transform', `translate(${xscale(0)}, 0)`)
        .call(yaxis);
}

inputs.forEach((input) => input.onchange = draw);
btn0.onclick = draw;
btn1.onclick = () => pre0.textContent = '';
draw();























						

0 コメント:

コメントを投稿