1. 程式人生 > >泰勒級數展開與圓的軌跡方程曲線

泰勒級數展開與圓的軌跡方程曲線

<html>
<head>
  <meta charset="UTF-8">
  <title>圓的泰勒級數在x=0處展開</title>
   <style type="text/css">
   .box {
   	/*float: left;*/
   	background-color: black;
   }
   .ctrlBox {
   	float: left;
   	/*border: 1px solid #A0F;*/
   	width: 400;
   	/*height: 700;*/
   }
   </style>
</head>

<body>
<canvas id="canvas" class="box">
    canvas not supported, please use html5 browser.
</canvas>
<div class="ctrlBox">
	<p>泰勒級數展開的階數:<p>
  <button onclick="update(-1)"> &lt;&lt; </button>
  <span id="lvlLb">1</span>
  <button onclick="update(1)"> &gt;&gt; </button>
</div>

</body>
<script>
  var radius  = 100;
  var x0 = Math.SQRT2 * radius / 2;
  var label = document.querySelector("#lvlLb");
  var arr=[1,2,4,6,8,10,12], cur = 0;
  var canvas = document.querySelector(".box");
  var ctx = canvas.getContext('2d');

window.onload = function() {
	canvas.width = 800;
	canvas.height = 600;
	draw();
}

function update(val) {
  var preCur = cur;
  val > 0 ? cur++ : cur--;
  if (cur < 0) cur = 0;
  if (cur >= arr.length - 1) cur = arr.length - 1;
  if (preCur != cur) {
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    draw();
  }
}

function draw() {
  var cx = 400, cy = 300;
  label.innerText = arr[cur];
   ctx.beginPath();
    ctx.strokeStyle = "#EE9611"; 
    ctx.arc(cx, cy, radius, Math.PI*2, false);
    ctx.stroke();
    ctx.moveTo(0, cy);
    ctx.lineTo(canvas.width, cy);
    ctx.stroke();
    ctx.moveTo(cx, 0);
    ctx.lineTo(cx, canvas.height);
    ctx.stroke();
    ctx.closePath();
    
    var range = 150;
    for (var i = -range; i < range; i++) {
      var j = getTyler_Y(i, arr[cur]);
      if (i === -range) {
        ctx.beginPath();
        ctx.strokeStyle = "#2BD56F";
        ctx.moveTo(cx+i, cy-j);
      } else {
        ctx.lineTo(cx+i, cy-j);
      }
    }
    ctx.stroke();
}

//自變數x, 泰勒級數展開的階數lvl
function getTyler_Y(x, lvl) {
  var exp;
  if (lvl >= 1) {
    exp = radius;
  }
  if (lvl >= 2) {
    exp -= x**2 / (2 * radius);
  }
  if (lvl >= 4) {
    exp -= x**4 / (8 * radius ** 3);
  }
  if (lvl >= 6) {
    exp -= x**6 / (16 * radius ** 5);
  }
  if (lvl >= 8) {
    exp -= 5 * x**8 / (128 * radius ** 7);
  }
  if (lvl >= 10) {
    exp -= 7 * x**10 / (256 * radius ** 9);
  }
  if (lvl >= 12) {
    exp -= 21 * x**12 / (1024 * radius ** 11);
  }
  return exp;
}
</script>
</html>

圓的軌跡, 也可以用引數方程來表示

x = r * cos( t )
y = r * sin( t )

一階導數簡單, 就是 (dy/dt) / (dx/dt)
二階導數比較麻煩,參見《高等數學第五版》上冊,第107頁, (5)
y對t一階導數 記作dy_t1
x對t一階導數 記作dx_t1
y對t二階導數 記作dy_t2
x對t二階導數 記作dx_t2
y對x二階導數 記作dy_x2

dy_x2 = (dy_t2 * dx_t1 - dy_t1 * dx_t2) / (dx_t1 ** 3)

就圓的軌跡,的引數方程表達來說

一階導數 dy_x1 = -ctg( t )
二階導數 dy_x2 = -1 / (r * sin(t) ** 3)

當x=0時,t=PI/2; sin ( t ) = 1, dy_x2 = -1 / r;
和根據 圓的隱函式表達 x * x + y * y = r * r 得出的二階導數是一致的