2016年12月1日木曜日

数学 - 解析学 - JavaScript - 数列と級数 - 数列の収束条件(下に有界な単調減少数列、極限値、平方根、近似(ニュートン法))

1. $\begin{array}{l}{a}_{n}-{a}_{n+1}\\ ={a}_{n}-\frac{1}{2}\left({a}_{n}+\frac{\alpha }{{a}_{n}}\right)\\ =\frac{1}{2}\left({a}_{n}-\frac{\alpha }{{a}_{n}}\right)\\ =\frac{1}{2}\left(\frac{{a}_{n}{}^{2}-\alpha }{{a}_{n}}\right)\\ \\ {a}_{n}{}^{2}>\alpha \\ と仮定する。\\ {a}_{n+1}^{2}-\alpha \\ ={\left(\frac{1}{2}\left({a}_{n}+\frac{\alpha }{{a}_{n}}\right)\right)}^{2}-\alpha \\ =\frac{{\left({a}_{n}{}^{2}+\alpha \right)}^{2}}{4{a}_{n}{}^{2}}-\alpha \\ =\frac{{\left({a}_{n}^{2}+\alpha \right)}^{2}-4\alpha {a}_{n}{}^{2}}{4{a}_{n}{}^{2}}\\ =\frac{{a}_{n}{}^{4}+2\alpha {a}_{n}{}^{2}+{\alpha }^{2}-4\alpha {a}_{n}{}^{2}}{4{a}_{n}{}^{2}}\\ =\frac{{a}_{n}{}^{4}-2\alpha {a}_{n}{}^{2}+{\alpha }^{2}}{4{a}_{n}{}^{2}}\\ =\frac{{\left({a}_{n}{}^{2}-\alpha \right)}^{2}}{4{a}_{n}{}^{2}}>0\\ {a}_{n+1}{}^{2}>\alpha \\ \\ {a}_{n}-{a}_{n+1}>0\\ {a}_{n}>{a}_{n+1}\\ \\ {a}_{n}>0\\ \left({a}_{n}\right)は単調減少で下に有界なので収束する。\\ \beta =\frac{1}{2}\left(\beta +\frac{\alpha }{\beta }\right)\\ 2\beta =\beta +\frac{\alpha }{\beta }\\ {\beta }^{2}=\alpha \\ \beta =\sqrt{\alpha }\\ \underset{n\to \infty }{\mathrm{lim}}{a}_{n}=\sqrt{\alpha }\end{array}$

2. $\begin{array}{l}{\epsilon }_{n+1}={a}_{n+1}-\sqrt{\alpha }\\ =\frac{1}{2}\left({a}_{n}+\frac{\alpha }{{a}_{n}}\right)-\sqrt{\alpha }\\ =\frac{{a}_{n}{}^{2}+\alpha -2{a}_{n}\sqrt{\alpha }}{2{a}_{n}}\\ =\frac{{\left({a}_{n}-\sqrt{\alpha }\right)}^{2}}{2{a}_{n}}\\ =\frac{{\epsilon }_{n}{}^{2}}{2{a}_{n}}\\ <\frac{{\epsilon }_{n}{}^{2·}}{2\sqrt{\alpha }}\\ =\frac{{\epsilon }_{n}{}^{2}}{\beta }\\ {\epsilon }_{n+1}<\frac{{\epsilon }_{n}{}^{2}}{\beta }\\ \frac{{\epsilon }_{n+1}}{\beta }<\frac{{\epsilon }_{n}{}^{2}}{{\beta }^{2}}={\left(\frac{{\epsilon }_{n}}{\beta }\right)}^{2}<{\left(\frac{{\epsilon }_{n-1}}{\beta }\right)}^{2·{2}^{1}}<···<{\left(\frac{{\epsilon }_{1}}{\beta }\right)}^{2·{2}^{n-1}}={\left(\frac{\epsilon }{\beta }\right)}^{2n}\\ \frac{{\epsilon }_{n+1}}{\beta }<{\left(\frac{\epsilon }{\beta }\right)}^{2n}\end{array}$

HTML5

<div id="graph0"></div>
<div id="output0"></div>
<label for="alpha0">α = </label>
<input id="alpha0" type="number" min="2" value="2">
<label for="a1">a1 = </label>
<input id="a1" type="number" min="2" value="2">
<label for="n0">n = </label>
<input id="n0" type="number" min="1" step="1" value="10">

<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="sample3.js"></script>


JavaScript

{
let div_graph = document.querySelector('#graph0'),
div_output = document.querySelector('#output0'),
input_alpha = document.querySelector('#alpha0'),
input_a = document.querySelector('#a1'),
input_n = document.querySelector('#n0'),
inputs = [input_alpha, input_a, input_n],
width = 600,
height = 600,

let f = (alpha, prev) => {
return 1 / 2 * (prev + alpha / prev);
};
let getPoints = (alpha, a1, n) => {
let prev = a1,
result = [[1, prev]];

for (let i = 2; i <= n; i += 1) {
let next = f(alpha, prev);
result.push([i, next]);
prev = next;
}
return result;
}
let plot = () => {
let alpha = parseFloat(input_alpha.value),
a1 = parseFloat(input_a.value);

if (a1 > Math.sqrt(alpha)) {
let n = parseInt(input_n.value, 10),
points = getPoints(alpha, a1, n);

console.log(points);
let xscale = d3.scaleLinear()
.domain([1, n])
let yscale = d3.scaleLinear()
.domain([points[points.length - 1][1] * 0.9, a1])

console.log(points.map((x) => x[1]));
div_graph.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('color', 'green');

let xaxis = d3.axisBottom().scale(xscale);
let yaxis = d3.axisLeft().scale(yscale);

svg.append('line')
.attr('x1', xscale(1))
.attr('y1', yscale(Math.sqrt(alpha)))
.attr('x2', xscale(n))
.attr('y2', yscale(Math.sqrt(alpha)))
.attr('stroke', 'red');

svg.append('g')
.attr('transform', translate(0, ${height - padding})) .call(xaxis); svg.append('g') .attr('transform', translate(${padding}, 0))
.call(yaxis);

div_output.innerHTML =
points.map((x) => ${x[0]}:${x[1]}).join('<br>') +
<br>Math.sqrt(${alpha}):${Math.sqrt(alpha)};

} else {
div_output.innerHTML = '注意: a1は√α より大きい数';
}
};

inputs.forEach((input) => input.onchange = plot);
plot();

}