Fixing things and adding springPendulum

This commit is contained in:
2021-10-02 17:20:25 -07:00
parent deda6ebd18
commit 533a9b71bb
6 changed files with 272 additions and 185 deletions

View File

@@ -61,14 +61,7 @@
</head>
<body>
<canvas id="myCanvas" width="1" height="1" style="border:1px solid #ffffff;">
Your browser does not support the HTML5 canvas tag.</canvas>
<div class="dropdown">
<button class="dropbtn" id="btn">Mode 1</button>
<div class="dropdown-content">
<button onclick="num(0)">Mode 1</button>
<button onclick="num(1)">Mode 2</button>
</div>
</div>
Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var c = document.getElementById("myCanvas");

View File

@@ -135,7 +135,7 @@
}
}
function drawSpring(a, b, tSize, rSize, numLoops, x, y, tStep, deltaY)
function drawSpring(a, b, xSize, ySize, numLoops, x, y, tStep, deltaY)
{
var tStart = Math.PI / 2;
var tEnd = (3 * Math.PI / 2) + (2 * Math.PI * numLoops);

View File

@@ -88,15 +88,21 @@
var w = 160;
var h = 100;
var x = 0;
var x2 = 0;
var sdt = 3;
var st = 3;
var sdt2 = 3;
var st2 = 3;
var sr = 5;
var six = -100;
var dist = 150;
var snowballPositions = new Array();
var snowballDirections = new Array();
var snowballPositions2 = new Array();
var snowballDirections2 = new Array();
var dt = 0.1;
var xPos = 0;
var yPos = 0;
var xPos = -200;
var yPos = 100;
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2);
ctx.scale(1.5, 1.5);
@@ -111,9 +117,12 @@
{
Clear(ctx);
Draw();
Draw2();
t += dt;
st += dt;
st2 += dt;
x = calcX(t);
x2 = calcX2(t);
setTimeout(Update, 1000/60)
}
@@ -154,6 +163,48 @@
}
}
function calcX2(gt)
{
return u * gt - m * u * Math.sqrt(1 + 2 * s * gt / m) / (s) + m * u / s;
}
function Draw2()
{
ctx.beginPath();
ctx.rect(xPos + x2 - w / 2, yPos - h / 2 - dist, w, h);
ctx.stroke();
ctx.beginPath();
ctx.arc(xPos + x2 - w / 4 - h/8, yPos + h / 2 + h / 8 - dist, h/8, 0, Math.PI * 2);
ctx.stroke();
ctx.beginPath();
ctx.arc(xPos + x2 + w / 4 + h/8, yPos + h / 2 + h / 8 - dist, h/8, 0, Math.PI * 2);
ctx.stroke();
if (st2 > sdt)
{
st2 -= sdt;
snowballPositions2.push(six);
snowballDirections2.push(1);
}
for (i = 0; i < snowballPositions2.length; i++)
{
y = -h/4;
if (snowballPositions2[i] < x2 - w/2 - sr)
{
snowballPositions2[i] += u * dt * snowballDirections2[i];
} else
{
snowballDirections2[i] = 0;
nr = Math.floor(w / (2 * sr));
snowballPositions2[i] = x2 - w/2 + sr + 2 * sr * (i % nr);
y = h/2 - sr - 2 * sr * Math.floor(i / nr);
}
ctx.beginPath();
ctx.arc(xPos + snowballPositions2[i], yPos + y - dist, sr, 0, Math.PI * 2);
ctx.stroke();
}
}
window.addEventListener('resize', function(event) {
c.width = window.innerWidth;
c.height = window.innerHeight;

174
5-16.html
View File

@@ -1,174 +0,0 @@
<!--
This work is licensed under CC BY-NC-ND 4.0
Link to license: http://creativecommons.org/licenses/by-nc-nd/4.0/
Attribute to Russell Georgi
-->
<html>
<head>
<title>
Problem 5-16
</title>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0px;
border: 0;
overflow: hidden;
display: block;
}
canvas {
position: absolute;
}
.dropbtn {
background-color: #04AA6D;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
.dropbtnm {
background-color: #04AA6D;
color: white;
padding: 16px;
font-size: 16px;
border: none;
}
.dropdown {
position: absolute;
display: inline-block;
}
.dropdown-content {
display: none;
position: relative;
background-color: #f1f1f1;
box-shadow: 0px 8px 16px 0px rgba(0,0,0,0.2);
z-index: 1;
}
.dropdown-content button {
color: black;
padding: 12px 16px;
width: 100px;
text-decoration: none;
display: block;
}
#m {
left: 110px;
}
.dropdown-content a:hover {background-color: #ddd;}
.dropdown:hover .dropdown-content {display: block;}
.dropdown:hover .dropbtn {background-color: #3e8e41;}
</style>
</head>
<body>
<canvas id="myCanvas" width="1" height="1" style="border:1px solid #ffffff;">
Your browser does not support the HTML5 canvas tag.</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
ctx.canvas.width = window.innerWidth;
ctx.canvas.height = window.innerHeight;
var m = 250;
var s = 1;
var u = 10;
var t = 0;
var w = 160;
var h = 100;
var x = 0;
var sdt = 3;
var st = 3;
var sr = 5;
var six = -100;
var snowballPositions = new Array();
var snowballDirections = new Array();
var dt = 0.1;
var xPos = 0;
var yPos = 0;
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2);
ctx.scale(1.5, 1.5);
function Clear(ctx)
{
ctx.clearRect(-c.width, -c.height, c.width * 2, c.height * 2);
}
function Update()
{
Clear(ctx);
Draw();
t += dt;
st += dt;
x = calcX(t);
setTimeout(Update, 1000/60)
}
function calcX(gt)
{
return u * gt - m * u * Math.log(1 + 2 * s * gt / m) / (2 * s);
}
function Draw()
{
ctx.beginPath();
ctx.rect(xPos + x - w / 2, yPos - h / 2, w, h);
ctx.stroke();
ctx.beginPath();
ctx.arc(xPos + x - w / 4 - h/8, yPos + h / 2 + h / 8, h/8, 0, Math.PI * 2);
ctx.stroke();
ctx.beginPath();
ctx.arc(xPos + x + w / 4 + h/8, yPos + h / 2 + h / 8, h/8, 0, Math.PI * 2);
ctx.stroke();
if (st > sdt)
{
st -= sdt;
snowballPositions.push(six);
snowballDirections.push(1);
}
for (i = 0; i < snowballPositions.length; i++)
{
y = -h/4;
if (snowballPositions[i] < x - w/2 - sr)
{
snowballPositions[i] += u * dt * snowballDirections[i];
} else
{
snowballDirections[i] = 0;
nr = Math.floor(w / (2 * sr));
snowballPositions[i] = x - w/2 + sr + 2 * sr * (i % nr);
y = h/2 - sr - 2 * sr * Math.floor(i / nr);
}
ctx.beginPath();
ctx.arc(xPos + snowballPositions[i], yPos + y, sr, 0, Math.PI * 2);
ctx.stroke();
}
}
window.addEventListener('resize', function(event) {
c.width = window.innerWidth;
c.height = window.innerHeight;
ctx.translate(c.width / 2, c.height / 2);
}, true);
Update();
</script>
</body>
<p xmlns:cc="http://creativecommons.org/ns#" style="font-size: 1vw; bottom: 0px; position: absolute;">
This work is licensed under
<a href="http://creativecommons.org/licenses/by-nc-nd/4.0/?ref=chooser-v1" target="_blank" rel="license noopener noreferrer" style="display:inline-block;">CC BY-NC-ND 4.0<img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/nc.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/nd.svg?ref=chooser-v1"></a></p>
</html>

View File

@@ -184,6 +184,71 @@
<t>Other example problem from chapter 4</t>
</td>
</tr>
<tr>
<td>
<a href='4-9.html'>Problem 4-9</a>
</td>
<td>
<t>Unequal masses</t>
</td>
</tr>
<tr>
<td>
<a href='4-11.html'>Problem 4-11</a>
</td>
<td>
<t>Driven mass on a circle</t>
</td>
</tr><tr>
<td>
<a href='4-12.html'>Problem 4-12</a>
</td>
<td>
<t>Springs on a circle</t>
</td>
</tr>
<tr>
<th colspan = "2">
<t>Chapter 5: Conservation of energy and momentum</t>
</th>
</tr>
</tr>
<td>
<a href='5-15and5-16.html'>Problems 5-15 and 5-16</a>
</td>
<td>
<t>Comparison of propelling a car elastically and inelastically</t>
</td>
</tr>
<tr>
<td>
<a href='5-23.html'>Problem 5-23</a>
</td>
<td>
<t>Basketball and tennis ball</t>
</td>
</tr>
<tr>
<td>
<a href='5-29.html'>Problem 5-29</a>
</td>
<td>
<t>Falling energy conserving chain (only mostly correct)</t>
</td>
</tr>
<tr>
<th colspan = "2">
<t>Chapter 6: The Lagrangian method</t>
</th>
</tr>
<tr>
<td>
<a href='springPendulum.html'>Spring pendulum</a>
</td>
<td>
<t>Spring pendulum example problem</t>
</td>
</tr>
<tr>
<th colspan = "2">
<t>Chapter 8: Angular momentum part 1</t>

152
springPendulum.html Normal file
View File

@@ -0,0 +1,152 @@
<!--
This work is licensed under CC BY-NC-ND 4.0
Link to license: http://creativecommons.org/licenses/by-nc-nd/4.0/
Attribute to Russell Georgi
-->
<html>
<head>
<title>
Spring Pendulum Example
</title>
<style>
html, body {
width: 100%;
height: 100%;
margin: 0px;
border: 0;
overflow: hidden;
display: block;
}
canvas {
position: absolute;
}
t {
position: absolute;
font-size: 75px;
left: 5%;
top: 5%;
}
</style>
</head>
<body>
<canvas id="myCanvas" width="1" height="1" style="border:1px solid #ffffff;">
Your browser does not support the HTML5 canvas tag.</canvas>
<!--t id="time">0</t-->
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
c.width = window.innerWidth;
c.height = window.innerHeight;
//Time step
var dt = 0.02;
var t = 0;
var g = 98;
var l = 200;
var x = 0;
var xp = 0;
var theta = Math.PI / 8;
var thetap = 0;
var m = 1;
var k = 2;
//Relative position of canvas
var xPos = 0;
var yPos = 200;
//Moves and scales canvas
ctx.translate(ctx.canvas.width / 2, ctx.canvas.height / 2)
ctx.scale(1, -1);
function Clear(ctx)
{
ctx.clearRect(-c.width, -c.height, c.width * 2, c.height * 2);
}
function Update()
{
Clear(ctx);
Draw();
t += dt;
xp += (m * (l + x) * thetap * thetap + m * g * Math.cos(theta) - k * x) * dt / m;
thetap += (-m * g * Math.sin(theta) - 2 * m * xp * thetap) * dt / (m * (l + x));
x += xp * dt;
theta += thetap * dt;
setTimeout(Update, 1);
}
function Reset()
{
theta = 0;
x = 0;
thetap = 0;
xp = 0;
t = 0;
}
function Draw()
{
ctx.beginPath();
ctx.moveTo(-c.width, yPos);
ctx.lineTo(c.width, yPos);
ctx.stroke();
drawSpring(xPos, 0, xPos + (l + x) * Math.sin(theta), - (l + x) * Math.cos(theta), 1, 3, 10, 10);
ctx.beginPath();
ctx.arc(xPos + (l + x) * Math.sin(theta), yPos - (l + x) * Math.cos(theta), 10, 0, Math.PI * 2);
ctx.fill();
}
function drawSpring(x1, y1, x2, y2, a, b, perpSize, numLoops, tStep = 0.1)
{
ctx.translate(0, yPos);
if (Math.sign(theta) == 1)
{
ctx.rotate(Math.atan((y2 - y1) / (x2 - x1)) * (Math.sign(theta)));
} else
{
ctx.rotate(Math.PI - Math.atan((y2 - y1) / (x2 - x1)) * (Math.sign(theta)));
}
var xSize = Math.sqrt(((x1 - x2) ** 2 + (y1 - y2) ** 2));
var tStart = Math.PI / 2;
var tEnd = (3 * Math.PI / 2) + (2 * Math.PI * numLoops);
var xEnd = a * tEnd - b * Math.sin(tEnd);
var yEnd = a + b;
var xFactor = xSize / xEnd;
var yFactor = perpSize / yEnd;
var xShift = x1 - a * (Math.PI / 2) + b * Math.sin(Math.PI / 2);
var yShift = y1 - a + b * Math.cos(Math.PI / 2);
for (p = tStart; p <= tEnd; p += tStep)
{
ctx.beginPath();
ctx.moveTo(xShift + xFactor * (a * p - b * Math.sin(p)), yShift + yFactor * (a - b * Math.cos(p)));
ctx.lineTo(xShift + xFactor * (a * (p + tStep) - b * Math.sin(p + tStep)), yShift + yFactor * (a - b * Math.cos(p + tStep)));
ctx.stroke();
}
if (Math.sign(theta) == 1)
{
console.log("sign = 1");
ctx.rotate(-Math.atan((y2 - y1) / (x2 - x1)) * (Math.sign(theta)));
} else
{
console.log("sign = -1");
ctx.rotate(-Math.PI + Math.atan((y2 - y1) / (x2 - x1)) * (Math.sign(theta)));
}
ctx.translate(0, -yPos);
}
window.addEventListener('resize', function(event) {
c.width = window.innerWidth;
c.height = window.innerHeight;
ctx.translate(c.width / 2, c.height / 2);
ctx.scale(1, -1);
}, true);
Update();
</script>
</body>
<p xmlns:cc="http://creativecommons.org/ns#" style="font-size: 1vw; bottom: 0px; position: absolute;">
This work is licensed under
<a href="http://creativecommons.org/licenses/by-nc-nd/4.0/?ref=chooser-v1" target="_blank" rel="license noopener noreferrer" style="display:inline-block;">CC BY-NC-ND 4.0<img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/cc.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/by.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/nc.svg?ref=chooser-v1"><img style="height:22px!important;margin-left:3px;vertical-align:text-bottom;" src="https://mirrors.creativecommons.org/presskit/icons/nd.svg?ref=chooser-v1"></a></p>
</html>