add Hamilton
This commit is contained in:
parent
775efe0f4a
commit
6743d655b3
9 changed files with 946 additions and 0 deletions
114
Hamilton/equations.html
Normal file
114
Hamilton/equations.html
Normal file
|
@ -0,0 +1,114 @@
|
||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=utf-8">
|
||||||
|
</META>
|
||||||
|
<title>Lagrange
|
||||||
|
</title>
|
||||||
|
<style>body {background-color: rgb(192,255,255);} h2 {color: rgb(64,0,192);} h3 {color: rgb(192,0,0);} table {color: rgb(128,0,128);}
|
||||||
|
</style>
|
||||||
|
<script type="text/x-mathjax-config">MathJax.Hub.Config({
|
||||||
|
extensions: ["tex2jax.js","TeX/AMSmath.js"],
|
||||||
|
jax: ["input/TeX","output/HTML-CSS"],
|
||||||
|
tex2jax: {inlineMath: [['$','$']]},
|
||||||
|
displayAlign: "left"});
|
||||||
|
</script>
|
||||||
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js">
|
||||||
|
</script>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<div>
|
||||||
|
<h1>Automatický výpočet Hamiltonových rovnic z lagranžiánu v pythonu pomocí sympy - vyzkoušené příklady.
|
||||||
|
</h1>
|
||||||
|
<p>
|
||||||
|
Tento jednoduchý skript řeší poměrně otravný problém, který
|
||||||
|
je však snadno algoritmizovatelný. Jsou to jen opakované parciální derivace,
|
||||||
|
jejichž výsledky jsou dosazovány do daných výrazů. Protože se přitom člověk
|
||||||
|
snadno splete (hlavně ve znaménku), je lepší to nechat na programu.
|
||||||
|
Stačí nadefinovat potřebné symboly pro konstanty a časově závislé proměnné
|
||||||
|
(zobecněné souřadnice), pomocí nich pak vyjádřit lagranžián a o zbytek se postará python.
|
||||||
|
|
||||||
|
</p>
|
||||||
|
<div><hr>
|
||||||
|
<h2>Řešený problém : Railgun
|
||||||
|
</h2>
|
||||||
|
<h3>1.1. Zadaný lagranžián :
|
||||||
|
</h3>
|
||||||
|
<p class="formulaDsp">\[L = \frac{m \left(\dot{x}\right)^{2}}{2} + \frac{\mu \left(\dot{Q}\right)^{2}}{2} x - \frac{Q^{2}}{2 C}\]
|
||||||
|
</p>
|
||||||
|
<h3>1.2. Lagrangeovy rovnice :
|
||||||
|
</h3>
|
||||||
|
<p class="formulaDsp">\[\left [ m \ddot{x} - \frac{\mu \left(\dot{Q}\right)^{2}}{2} = 0, \quad \mu x \ddot{Q} + \mu \dot{Q} \dot{x} + \frac{Q}{C} = 0\right ]\]
|
||||||
|
</p>
|
||||||
|
<h3>1.3. Hamiltonova funkce :
|
||||||
|
</h3>
|
||||||
|
<p class="formulaDsp">\[H = \frac{p^{2}_{Q}}{2 \mu x} + \frac{p^{2}_{x}}{2 m} + \frac{Q^{2}}{2 C}\]
|
||||||
|
</p>
|
||||||
|
<h3>1.4. Hamiltonovy kanonické rovnice :
|
||||||
|
</h3>
|
||||||
|
<table align="center">
|
||||||
|
<tr>
|
||||||
|
<td class="formulaDsp">\[\dot{x}=\frac{p_{x}}{m}\qquad\]</td>
|
||||||
|
<td class="formulaDsp">\[\dot{p}_{x}=\frac{p^{2}_{Q}}{2 \mu x^{2}}\qquad\]</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td class="formulaDsp">\[\dot{Q}=\frac{p_{Q}}{\mu x}\qquad\]</td>
|
||||||
|
<td class="formulaDsp">\[\dot{p}_{Q}=- \frac{Q}{C}\qquad\]</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<div><hr>
|
||||||
|
<h2>Řešený problém : Pohyb planety
|
||||||
|
</h2>
|
||||||
|
<h3>2.1. Zadaný lagranžián :
|
||||||
|
</h3>
|
||||||
|
<p class="formulaDsp">\[L = \frac{G M}{r} m + \frac{m}{2} \left(r^{2} \left(\dot{\phi}\right)^{2} + \left(\dot{r}\right)^{2}\right)\]
|
||||||
|
</p>
|
||||||
|
<h3>2.2. Lagrangeovy rovnice :
|
||||||
|
</h3>
|
||||||
|
<p class="formulaDsp">\[\left [ \frac{G M}{r^{2}} - r \left(\dot{\phi}\right)^{2} + \ddot{r} = 0, \quad r^{2} \ddot{\phi} + 2 r \dot{\phi} \dot{r} = 0\right ]\]
|
||||||
|
</p>
|
||||||
|
<h3>2.3. Hamiltonova funkce :
|
||||||
|
</h3>
|
||||||
|
<p class="formulaDsp">\[H = - \frac{G M}{r} m + \frac{p^{2}_{\phi}}{2 m r^{2}} + \frac{p^{2}_{r}}{2 m}\]
|
||||||
|
</p>
|
||||||
|
<h3>2.4. Hamiltonovy kanonické rovnice :
|
||||||
|
</h3>
|
||||||
|
<table align="center">
|
||||||
|
<tr>
|
||||||
|
<td class="formulaDsp">\[\dot{r}=\frac{p_{r}}{m}\qquad\]</td>
|
||||||
|
<td class="formulaDsp">\[\dot{p}_{r}=- \frac{G M}{r^{2}} m + \frac{p^{2}_{\phi}}{m r^{3}}\qquad\]</td>
|
||||||
|
</tr>
|
||||||
|
<tr>
|
||||||
|
<td class="formulaDsp">\[\dot{\phi}=\frac{p_{\phi}}{m r^{2}}\qquad\]</td>
|
||||||
|
<td class="formulaDsp">\[\dot{p}_{\phi}=0\qquad\]</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
<div><hr>
|
||||||
|
<h2>Řešený problém : Harmonický oscilátor
|
||||||
|
</h2>
|
||||||
|
<h3>3.1. Zadaný lagranžián :
|
||||||
|
</h3>
|
||||||
|
<p class="formulaDsp">\[L = - \frac{m \omega^{2}}{2} x^{2} + \frac{m \left(\dot{x}\right)^{2}}{2}\]
|
||||||
|
</p>
|
||||||
|
<h3>3.2. Lagrangeovy rovnice :
|
||||||
|
</h3>
|
||||||
|
<p class="formulaDsp">\[\left [ \omega^{2} x + \ddot{x} = 0\right ]\]
|
||||||
|
</p>
|
||||||
|
<h3>3.3. Hamiltonova funkce :
|
||||||
|
</h3>
|
||||||
|
<p class="formulaDsp">\[H = \frac{m \omega^{2}}{2} x^{2} + \frac{p^{2}_{x}}{2 m}\]
|
||||||
|
</p>
|
||||||
|
<h3>3.4. Hamiltonovy kanonické rovnice :
|
||||||
|
</h3>
|
||||||
|
<table align="center">
|
||||||
|
<tr>
|
||||||
|
<td class="formulaDsp">\[\dot{x}=\frac{p_{x}}{m}\qquad\]</td>
|
||||||
|
<td class="formulaDsp">\[\dot{p}_{x}=- m \omega^{2} x\qquad\]</td>
|
||||||
|
</tr>
|
||||||
|
</table>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
63
Hamilton/index.html
Normal file
63
Hamilton/index.html
Normal file
|
@ -0,0 +1,63 @@
|
||||||
|
<!doctype html>
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<meta charset="utf-8" http-equiv="Content-Type" content="text/html; charset=utf-8" />
|
||||||
|
<title>SOLVER</title>
|
||||||
|
<style>
|
||||||
|
tr, td { border-collapse: collapse; }
|
||||||
|
table { width:100%; border-collapse: collapse; text-align: center; }
|
||||||
|
.emscripten { padding-right: 0; margin-left: auto; margin-right: auto; display: block; }
|
||||||
|
textarea.emscripten { font-family: monospace; font-size: 16px; width: 100%; overflow-x: scroll; white-space: pre; background: black; color: rgb(0,255,0);}
|
||||||
|
.frame1 { width: 96%; margin: 0; padding: 10px; background-color: #FFFFC0; border: 10px solid #F0C0F0; }
|
||||||
|
.canvas { width: 100%; height: 65vh; background-color: black; }
|
||||||
|
.a {color:#800080;}
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<script type="text/javascript" src="index.js"></script>
|
||||||
|
<div class="frame1"><canvas id="canvas" class="canvas"></canvas></div>
|
||||||
|
<div class="frame1"><input id="buttonTest" type="button" value="Submit"></div>
|
||||||
|
<div class="frame1">
|
||||||
|
<table><tr>
|
||||||
|
<td width="40%"><textarea class="emscripten" id="input" rows="25" spellcheck="false"></textarea></td>
|
||||||
|
<td><textarea readonly class="emscripten" id="stdout" rows="25" spellcheck="false"></textarea></td>
|
||||||
|
</tr></table>
|
||||||
|
</div>
|
||||||
|
<script type="text/x-mathjax-config">MathJax.Hub.Config({
|
||||||
|
extensions: ["tex2jax.js"],
|
||||||
|
jax: ["input/TeX","output/HTML-CSS"],
|
||||||
|
tex2jax: {inlineMath: [['$','$']]},
|
||||||
|
displayAlign: "left"});
|
||||||
|
</script>
|
||||||
|
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js"></script>
|
||||||
|
<div class="frame1">
|
||||||
|
<h2>Solver pro soustavu Hamiltonových rovnic.</h2>
|
||||||
|
<p>Nebudu se tady zabývat tím, co to jsou Hamiltonovy rovnice, na to je teoretická mechanika.
|
||||||
|
Pro tento účel je to soustava diferenciálních rovnic, převedených do tvaru
|
||||||
|
$$ \dot q_1 = f_1 (q_1, p_1, ..., q_i, p_i) $$
|
||||||
|
$$ ... $$
|
||||||
|
$$ \dot q_i = f_i (q_1, p_1, ..., q_i, p_i) $$
|
||||||
|
$$ \dot p_1 = g_1 (q_1, p_1, ..., q_i, p_i) $$
|
||||||
|
$$ ... $$
|
||||||
|
$$ \dot p_i = g_i (q_1, p_1, ..., q_i, p_i) $$
|
||||||
|
kde $ \dot q_i, \dot p_i $ jsou derivace zobecněných souřadnic a hybností podle času. Celkově
|
||||||
|
je to tedy sudý počet rovnic 2*i. Tyto rovnice zapíšeme jednoduše ve správném pořadí a se správným
|
||||||
|
pořadím proměnných ve tvaru, který používá javascript, stanovíme 2*i počátečních podmínek a stanovíme
|
||||||
|
co se má vykreslit. Osy to nemá, každý graf jde od minima do maxima, je to jen pro představu, jak funkce
|
||||||
|
vypadá. Objekt, vstupující do rovnic má tvar {t:t, w:[q1, q2 ... qn, p1, p2, ... pn]}, t značí čas.
|
||||||
|
Default příklad je harmonický oscilátor, lze použít i
|
||||||
|
<a class="a" onclick="getFile('nvcr.txt');">nelineární oscilátor</a> nebo pro příklad
|
||||||
|
<a class="a" onclick="getFile('lorenz.txt');">Lorenzův atraktor</a> a nakonec
|
||||||
|
<a class="a" onclick="getFile('railgun.txt');">railgun</a> s disipací energie.
|
||||||
|
Stačí kliknout na zvýrazněný text
|
||||||
|
a stisknout tlačítko Submit. Parametry simulace lze jednoduše měnit. Připadá mi to o hodně jednodušší
|
||||||
|
popsat takto problém textem než vymýšlet celé IDE s mnoha parametry. Parametry funkce solve (krok, počet_kroků, počáteční_podmínky, seznam_funkcí),
|
||||||
|
plot (výsledek_ze_solve, popis_os, barva). Objekt popis_os má tvar {x:[list], y:[list]}, přičemž list je buď 't' nebo 'w', číslo_proměnné.
|
||||||
|
</p>
|
||||||
|
<p>Vlastní solver používá metodu Runge-Kutta 4.řádu, je poměrně přesná a není to o moc složitější
|
||||||
|
než prostý Euler. Pokud hledáte ty Hamiltonovy rovnice, pomůže tento <a href="lagrange.html" target="_blank">skript</a>,
|
||||||
|
jeho výstup je pak zhruba <a href="equations.html" target="_blank">tento</a>.
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</body>
|
||||||
|
</html>
|
126
Hamilton/index.js
Normal file
126
Hamilton/index.js
Normal file
|
@ -0,0 +1,126 @@
|
||||||
|
var Outs, Inps, Btnt, Canvas;
|
||||||
|
// async/await z příkladu na webu
|
||||||
|
window.onload = async function() {
|
||||||
|
Outs = document.getElementById('stdout');
|
||||||
|
Inps = document.getElementById('input');
|
||||||
|
Btnt = document.getElementById('buttonTest');
|
||||||
|
Canvas = document.getElementById('canvas');
|
||||||
|
Outs.value = '';
|
||||||
|
await getFile ('osc.txt');
|
||||||
|
Btnt.onclick = () => {
|
||||||
|
Outs.value = '';
|
||||||
|
run ();
|
||||||
|
};
|
||||||
|
};
|
||||||
|
function print (str) {
|
||||||
|
Outs.value += str;
|
||||||
|
}
|
||||||
|
async function getFile (name) {
|
||||||
|
console.log(name);
|
||||||
|
const response = await fetch (name);
|
||||||
|
if (!response.ok) return;
|
||||||
|
const bytes = await response.arrayBuffer();
|
||||||
|
const array = new Uint8Array(bytes);
|
||||||
|
const decoder = new TextDecoder();
|
||||||
|
Inps.value = decoder.decode(bytes);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
///////////////////////////////////////////////
|
||||||
|
function run () {
|
||||||
|
|
||||||
|
let code = Inps.value;
|
||||||
|
try {
|
||||||
|
eval (code);
|
||||||
|
} catch (err) {
|
||||||
|
print('Error: ' + err.message + '\n');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function solve (hh, steps, data, pf) {
|
||||||
|
let p = data;
|
||||||
|
|
||||||
|
const N = pf.length;
|
||||||
|
const hal = 0.5 * hh;
|
||||||
|
const hhs = hh / 6.0;
|
||||||
|
let min = {t:+1.0e100, w:[]};
|
||||||
|
for (let k=0; k<N; k++) { min.w[k] = min.t; }
|
||||||
|
let max = {t:-1.0e100, w:[]};
|
||||||
|
for (let k=0; k<N; k++) { max.w[k] = max.t; }
|
||||||
|
|
||||||
|
let result = new Array(steps + 1);
|
||||||
|
if (p.t < min.t) min.t = p.t;
|
||||||
|
for (let k=0; k<N; k++) { if (p.w[k] < min.w[k]) min.w[k] = p.w[k]; }
|
||||||
|
if (p.t > max.t) max.t = p.t;
|
||||||
|
for (let k=0; k<N; k++) { if (p.w[k] > max.w[k]) max.w[k] = p.w[k]; }
|
||||||
|
result [0] = new Object (structuredClone(p));
|
||||||
|
for (let index=1; index<result.length; index++) {
|
||||||
|
let k1 = [];
|
||||||
|
for (let k=0; k<N; k++) { k1[k] = pf[k] (p); }
|
||||||
|
let p2 = structuredClone(p); p2.t += hal;
|
||||||
|
for (let k=0; k<N; k++) { p2.w[k] += hal * k1[k]; }
|
||||||
|
let k2 = [];
|
||||||
|
for (let k=0; k<N; k++) { k2[k] = pf[k](p2); }
|
||||||
|
let p3 = structuredClone(p); p3.t += hal;
|
||||||
|
for (let k=0; k<N; k++) { p3.w[k] += hal * k2[k]; }
|
||||||
|
let k3 = [];
|
||||||
|
for (let k=0; k<N; k++) { k3[k] = pf[k](p3); }
|
||||||
|
let p4 = structuredClone(p); p4.t += hh;
|
||||||
|
for (let k=0; k<N; k++) { p4.w[k] += hh * k3[k]; }
|
||||||
|
let k4 = [];
|
||||||
|
for (let k=0; k<N; k++) { k4[k] = pf[k](p4); }
|
||||||
|
p.t += hh;
|
||||||
|
for (let k=0; k<N; k++) { p.w[k] += hhs * (k1[k] + 2.0 * k2[k] + 2.0 * k3[k] + k4[k]); }
|
||||||
|
|
||||||
|
let np = structuredClone (p);
|
||||||
|
if (np.t < min.t) min.t = np.t;
|
||||||
|
for (let k=0; k<N; k++) { if (np.w[k] < min.w[k]) min.w[k] = np.w[k]; }
|
||||||
|
if (np.t > max.t) max.t = np.t;
|
||||||
|
for (let k=0; k<N; k++) { if (np.w[k] > max.w[k]) max.w[k] = np.w[k]; }
|
||||||
|
result [index] = new Object (np);
|
||||||
|
}
|
||||||
|
initCanvas ()
|
||||||
|
return {data:result, m:min, p:max};
|
||||||
|
}
|
||||||
|
function getValue (pt, si) {
|
||||||
|
if (si.length > 1) { // w
|
||||||
|
return pt[si[0]][si[1]];
|
||||||
|
} else { // t
|
||||||
|
return pt[si[0]];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
function initCanvas () {
|
||||||
|
const width = canvas.clientWidth;
|
||||||
|
const height = canvas.clientHeight;
|
||||||
|
canvas.width = width;
|
||||||
|
canvas.height = height;
|
||||||
|
const ctx = canvas.getContext("2d");
|
||||||
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||||
|
}
|
||||||
|
function plot (r, po, color) {
|
||||||
|
print('plot:' + JSON.stringify(po) + '\n');
|
||||||
|
const p0 = r.data[0];
|
||||||
|
print('min: ' + JSON.stringify(r.m) + '\n');
|
||||||
|
print('max: ' + JSON.stringify(r.p) + '\n');
|
||||||
|
const xmin = getValue (r.m, po.x);
|
||||||
|
const xmax = getValue (r.p, po.x);
|
||||||
|
const ymin = getValue (r.m, po.y);
|
||||||
|
const ymax = getValue (r.p, po.y);
|
||||||
|
const xzom = canvas.width / (xmax - xmin);
|
||||||
|
const yzom = canvas.height / (ymin - ymax);
|
||||||
|
const xofs = xmin * canvas.width / (xmin - xmax);
|
||||||
|
const yofs = ymax * canvas.height / (ymax - ymin);
|
||||||
|
//console.log (xmin, xmax, ymin, ymax);
|
||||||
|
const ctx = canvas.getContext("2d");
|
||||||
|
ctx.beginPath();
|
||||||
|
const x0 = xzom * getValue(p0, po.x) + xofs;
|
||||||
|
const y0 = yzom * getValue(p0, po.y) + yofs;
|
||||||
|
ctx.moveTo(x0, y0);
|
||||||
|
for (let n=1; n<r.data.length; n++) {
|
||||||
|
const p = r.data[n];
|
||||||
|
const x = xzom * getValue(p, po.x) + xofs;
|
||||||
|
const y = yzom * getValue(p, po.y) + yofs;
|
||||||
|
ctx.lineTo(x, y);
|
||||||
|
}
|
||||||
|
ctx.lineWidth = 3;
|
||||||
|
ctx.strokeStyle = color;
|
||||||
|
ctx.stroke();
|
||||||
|
}
|
538
Hamilton/lagrange.html
Normal file
538
Hamilton/lagrange.html
Normal file
|
@ -0,0 +1,538 @@
|
||||||
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
|
||||||
|
"http://www.w3.org/TR/html4/strict.dtd">
|
||||||
|
|
||||||
|
<html>
|
||||||
|
<head>
|
||||||
|
<title></title>
|
||||||
|
<meta http-equiv="content-type" content="text/html; charset=utf-8">
|
||||||
|
<style type="text/css">
|
||||||
|
td.linenos { background-color: #f0f0f0; padding-right: 10px; }
|
||||||
|
span.lineno { background-color: #f0f0f0; padding: 0 5px 0 5px; }
|
||||||
|
pre { line-height: 125%; }
|
||||||
|
body .hll { background-color: #ffffcc }
|
||||||
|
body { background: #f8f8f8; }
|
||||||
|
body .c { color: #408080; font-style: italic } /* Comment */
|
||||||
|
body .err { border: 1px solid #FF0000 } /* Error */
|
||||||
|
body .k { color: #008000; font-weight: bold } /* Keyword */
|
||||||
|
body .o { color: #666666 } /* Operator */
|
||||||
|
body .ch { color: #408080; font-style: italic } /* Comment.Hashbang */
|
||||||
|
body .cm { color: #408080; font-style: italic } /* Comment.Multiline */
|
||||||
|
body .cp { color: #BC7A00 } /* Comment.Preproc */
|
||||||
|
body .cpf { color: #408080; font-style: italic } /* Comment.PreprocFile */
|
||||||
|
body .c1 { color: #408080; font-style: italic } /* Comment.Single */
|
||||||
|
body .cs { color: #408080; font-style: italic } /* Comment.Special */
|
||||||
|
body .gd { color: #A00000 } /* Generic.Deleted */
|
||||||
|
body .ge { font-style: italic } /* Generic.Emph */
|
||||||
|
body .gr { color: #FF0000 } /* Generic.Error */
|
||||||
|
body .gh { color: #000080; font-weight: bold } /* Generic.Heading */
|
||||||
|
body .gi { color: #00A000 } /* Generic.Inserted */
|
||||||
|
body .go { color: #888888 } /* Generic.Output */
|
||||||
|
body .gp { color: #000080; font-weight: bold } /* Generic.Prompt */
|
||||||
|
body .gs { font-weight: bold } /* Generic.Strong */
|
||||||
|
body .gu { color: #800080; font-weight: bold } /* Generic.Subheading */
|
||||||
|
body .gt { color: #0044DD } /* Generic.Traceback */
|
||||||
|
body .kc { color: #008000; font-weight: bold } /* Keyword.Constant */
|
||||||
|
body .kd { color: #008000; font-weight: bold } /* Keyword.Declaration */
|
||||||
|
body .kn { color: #008000; font-weight: bold } /* Keyword.Namespace */
|
||||||
|
body .kp { color: #008000 } /* Keyword.Pseudo */
|
||||||
|
body .kr { color: #008000; font-weight: bold } /* Keyword.Reserved */
|
||||||
|
body .kt { color: #B00040 } /* Keyword.Type */
|
||||||
|
body .m { color: #666666 } /* Literal.Number */
|
||||||
|
body .s { color: #BA2121 } /* Literal.String */
|
||||||
|
body .na { color: #7D9029 } /* Name.Attribute */
|
||||||
|
body .nb { color: #008000 } /* Name.Builtin */
|
||||||
|
body .nc { color: #0000FF; font-weight: bold } /* Name.Class */
|
||||||
|
body .no { color: #880000 } /* Name.Constant */
|
||||||
|
body .nd { color: #AA22FF } /* Name.Decorator */
|
||||||
|
body .ni { color: #999999; font-weight: bold } /* Name.Entity */
|
||||||
|
body .ne { color: #D2413A; font-weight: bold } /* Name.Exception */
|
||||||
|
body .nf { color: #0000FF } /* Name.Function */
|
||||||
|
body .nl { color: #A0A000 } /* Name.Label */
|
||||||
|
body .nn { color: #0000FF; font-weight: bold } /* Name.Namespace */
|
||||||
|
body .nt { color: #008000; font-weight: bold } /* Name.Tag */
|
||||||
|
body .nv { color: #19177C } /* Name.Variable */
|
||||||
|
body .ow { color: #AA22FF; font-weight: bold } /* Operator.Word */
|
||||||
|
body .w { color: #bbbbbb } /* Text.Whitespace */
|
||||||
|
body .mb { color: #666666 } /* Literal.Number.Bin */
|
||||||
|
body .mf { color: #666666 } /* Literal.Number.Float */
|
||||||
|
body .mh { color: #666666 } /* Literal.Number.Hex */
|
||||||
|
body .mi { color: #666666 } /* Literal.Number.Integer */
|
||||||
|
body .mo { color: #666666 } /* Literal.Number.Oct */
|
||||||
|
body .sb { color: #BA2121 } /* Literal.String.Backtick */
|
||||||
|
body .sc { color: #BA2121 } /* Literal.String.Char */
|
||||||
|
body .sd { color: #BA2121; font-style: italic } /* Literal.String.Doc */
|
||||||
|
body .s2 { color: #BA2121 } /* Literal.String.Double */
|
||||||
|
body .se { color: #BB6622; font-weight: bold } /* Literal.String.Escape */
|
||||||
|
body .sh { color: #BA2121 } /* Literal.String.Heredoc */
|
||||||
|
body .si { color: #BB6688; font-weight: bold } /* Literal.String.Interpol */
|
||||||
|
body .sx { color: #008000 } /* Literal.String.Other */
|
||||||
|
body .sr { color: #BB6688 } /* Literal.String.Regex */
|
||||||
|
body .s1 { color: #BA2121 } /* Literal.String.Single */
|
||||||
|
body .ss { color: #19177C } /* Literal.String.Symbol */
|
||||||
|
body .bp { color: #008000 } /* Name.Builtin.Pseudo */
|
||||||
|
body .vc { color: #19177C } /* Name.Variable.Class */
|
||||||
|
body .vg { color: #19177C } /* Name.Variable.Global */
|
||||||
|
body .vi { color: #19177C } /* Name.Variable.Instance */
|
||||||
|
body .il { color: #666666 } /* Literal.Number.Integer.Long */
|
||||||
|
|
||||||
|
</style>
|
||||||
|
</head>
|
||||||
|
<body>
|
||||||
|
<h2></h2>
|
||||||
|
|
||||||
|
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre> 1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
||||||
|
5
|
||||||
|
6
|
||||||
|
7
|
||||||
|
8
|
||||||
|
9
|
||||||
|
10
|
||||||
|
11
|
||||||
|
12
|
||||||
|
13
|
||||||
|
14
|
||||||
|
15
|
||||||
|
16
|
||||||
|
17
|
||||||
|
18
|
||||||
|
19
|
||||||
|
20
|
||||||
|
21
|
||||||
|
22
|
||||||
|
23
|
||||||
|
24
|
||||||
|
25
|
||||||
|
26
|
||||||
|
27
|
||||||
|
28
|
||||||
|
29
|
||||||
|
30
|
||||||
|
31
|
||||||
|
32
|
||||||
|
33
|
||||||
|
34
|
||||||
|
35
|
||||||
|
36
|
||||||
|
37
|
||||||
|
38
|
||||||
|
39
|
||||||
|
40
|
||||||
|
41
|
||||||
|
42
|
||||||
|
43
|
||||||
|
44
|
||||||
|
45
|
||||||
|
46
|
||||||
|
47
|
||||||
|
48
|
||||||
|
49
|
||||||
|
50
|
||||||
|
51
|
||||||
|
52
|
||||||
|
53
|
||||||
|
54
|
||||||
|
55
|
||||||
|
56
|
||||||
|
57
|
||||||
|
58
|
||||||
|
59
|
||||||
|
60
|
||||||
|
61
|
||||||
|
62
|
||||||
|
63
|
||||||
|
64
|
||||||
|
65
|
||||||
|
66
|
||||||
|
67
|
||||||
|
68
|
||||||
|
69
|
||||||
|
70
|
||||||
|
71
|
||||||
|
72
|
||||||
|
73
|
||||||
|
74
|
||||||
|
75
|
||||||
|
76
|
||||||
|
77
|
||||||
|
78
|
||||||
|
79
|
||||||
|
80
|
||||||
|
81
|
||||||
|
82
|
||||||
|
83
|
||||||
|
84
|
||||||
|
85
|
||||||
|
86
|
||||||
|
87
|
||||||
|
88
|
||||||
|
89
|
||||||
|
90
|
||||||
|
91
|
||||||
|
92
|
||||||
|
93
|
||||||
|
94
|
||||||
|
95
|
||||||
|
96
|
||||||
|
97
|
||||||
|
98
|
||||||
|
99
|
||||||
|
100
|
||||||
|
101
|
||||||
|
102
|
||||||
|
103
|
||||||
|
104
|
||||||
|
105
|
||||||
|
106
|
||||||
|
107
|
||||||
|
108
|
||||||
|
109
|
||||||
|
110
|
||||||
|
111
|
||||||
|
112
|
||||||
|
113
|
||||||
|
114
|
||||||
|
115
|
||||||
|
116
|
||||||
|
117
|
||||||
|
118
|
||||||
|
119
|
||||||
|
120
|
||||||
|
121
|
||||||
|
122
|
||||||
|
123
|
||||||
|
124
|
||||||
|
125
|
||||||
|
126
|
||||||
|
127
|
||||||
|
128
|
||||||
|
129
|
||||||
|
130
|
||||||
|
131
|
||||||
|
132
|
||||||
|
133
|
||||||
|
134
|
||||||
|
135
|
||||||
|
136
|
||||||
|
137
|
||||||
|
138
|
||||||
|
139
|
||||||
|
140
|
||||||
|
141
|
||||||
|
142
|
||||||
|
143
|
||||||
|
144
|
||||||
|
145
|
||||||
|
146
|
||||||
|
147
|
||||||
|
148
|
||||||
|
149
|
||||||
|
150
|
||||||
|
151
|
||||||
|
152
|
||||||
|
153
|
||||||
|
154
|
||||||
|
155
|
||||||
|
156
|
||||||
|
157
|
||||||
|
158
|
||||||
|
159
|
||||||
|
160
|
||||||
|
161
|
||||||
|
162
|
||||||
|
163
|
||||||
|
164
|
||||||
|
165
|
||||||
|
166
|
||||||
|
167
|
||||||
|
168
|
||||||
|
169
|
||||||
|
170
|
||||||
|
171
|
||||||
|
172
|
||||||
|
173
|
||||||
|
174
|
||||||
|
175
|
||||||
|
176
|
||||||
|
177
|
||||||
|
178
|
||||||
|
179
|
||||||
|
180
|
||||||
|
181
|
||||||
|
182
|
||||||
|
183
|
||||||
|
184
|
||||||
|
185
|
||||||
|
186
|
||||||
|
187
|
||||||
|
188
|
||||||
|
189
|
||||||
|
190
|
||||||
|
191
|
||||||
|
192
|
||||||
|
193
|
||||||
|
194
|
||||||
|
195
|
||||||
|
196
|
||||||
|
197
|
||||||
|
198
|
||||||
|
199
|
||||||
|
200
|
||||||
|
201
|
||||||
|
202
|
||||||
|
203
|
||||||
|
204
|
||||||
|
205
|
||||||
|
206
|
||||||
|
207
|
||||||
|
208
|
||||||
|
209
|
||||||
|
210
|
||||||
|
211
|
||||||
|
212
|
||||||
|
213
|
||||||
|
214
|
||||||
|
215
|
||||||
|
216
|
||||||
|
217
|
||||||
|
218
|
||||||
|
219
|
||||||
|
220
|
||||||
|
221
|
||||||
|
222
|
||||||
|
223
|
||||||
|
224
|
||||||
|
225
|
||||||
|
226
|
||||||
|
227</pre></div></td><td class="code"><div class="highlight"><pre><span class="ch">#!/usr/bin/env python</span>
|
||||||
|
<span class="c1"># -*- coding: utf-8 -*-</span>
|
||||||
|
|
||||||
|
<span class="n">description</span> <span class="o">=</span> <span class="s2">"""</span>
|
||||||
|
<span class="s2">Tento jednoduchý skript řeší poměrně otravný problém, který</span>
|
||||||
|
<span class="s2">je však snadno algoritmizovatelný. Jsou to jen opakované parciální derivace,</span>
|
||||||
|
<span class="s2">jejichž výsledky jsou dosazovány do daných výrazů. Protože se přitom člověk</span>
|
||||||
|
<span class="s2">snadno splete (hlavně ve znaménku), je lepší to nechat na programu.</span>
|
||||||
|
<span class="s2">Stačí nadefinovat potřebné symboly pro konstanty a časově závislé proměnné</span>
|
||||||
|
<span class="s2">(zobecněné souřadnice), pomocí nich pak vyjádřit lagranžián a o zbytek se postará python.</span>
|
||||||
|
<span class="s2">"""</span>
|
||||||
|
<span class="kn">from</span> <span class="nn">sympy.core</span> <span class="kn">import</span> <span class="n">mul</span><span class="p">,</span> <span class="n">add</span>
|
||||||
|
<span class="kn">from</span> <span class="nn">sympy.physics.vector.printing</span> <span class="kn">import</span> <span class="n">vlatex</span>
|
||||||
|
<span class="kn">from</span> <span class="nn">sympy.physics.mechanics</span> <span class="kn">import</span> <span class="n">dynamicsymbols</span>
|
||||||
|
<span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="p">(</span><span class="n">Symbol</span><span class="p">,</span> <span class="n">symbols</span><span class="p">,</span> <span class="n">Derivative</span><span class="p">,</span>
|
||||||
|
<span class="n">Eq</span><span class="p">,</span> <span class="n">pprint</span><span class="p">,</span> <span class="n">solve</span><span class="p">,</span> <span class="n">latex</span><span class="p">,</span> <span class="n">simplify</span><span class="p">,</span> <span class="n">expand</span><span class="p">)</span>
|
||||||
|
|
||||||
|
<span class="c1">################# HTML dekorace ###############################################</span>
|
||||||
|
<span class="k">class</span> <span class="nc">Tag</span><span class="p">:</span>
|
||||||
|
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">v</span><span class="p">):</span>
|
||||||
|
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">n</span>
|
||||||
|
<span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">v</span>
|
||||||
|
<span class="k">def</span> <span class="nf">to_str</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||||
|
<span class="k">return</span> <span class="s1">' '</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">+</span> <span class="s1">'=</span><span class="se">\"</span><span class="s1">'</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">+</span> <span class="s1">'</span><span class="se">\"</span><span class="s1">'</span>
|
||||||
|
|
||||||
|
<span class="k">class</span> <span class="nc">Element</span><span class="p">:</span>
|
||||||
|
<span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">n</span><span class="p">,</span> <span class="n">v</span><span class="o">=</span><span class="bp">None</span><span class="p">):</span>
|
||||||
|
<span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">=</span> <span class="n">n</span>
|
||||||
|
<span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">=</span> <span class="n">v</span>
|
||||||
|
<span class="bp">self</span><span class="o">.</span><span class="n">tags</span> <span class="o">=</span> <span class="p">[]</span>
|
||||||
|
<span class="bp">self</span><span class="o">.</span><span class="n">childs</span> <span class="o">=</span> <span class="p">[]</span>
|
||||||
|
<span class="bp">self</span><span class="o">.</span><span class="n">id</span> <span class="o">=</span> <span class="mi">0</span>
|
||||||
|
<span class="k">def</span> <span class="nf">addE</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">e</span><span class="p">):</span>
|
||||||
|
<span class="bp">self</span><span class="o">.</span><span class="n">childs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">e</span><span class="p">)</span>
|
||||||
|
<span class="k">def</span> <span class="nf">addT</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">t</span><span class="p">):</span>
|
||||||
|
<span class="bp">self</span><span class="o">.</span><span class="n">tags</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
|
||||||
|
<span class="k">def</span> <span class="nf">addF</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">f</span><span class="p">):</span>
|
||||||
|
<span class="n">t</span> <span class="o">=</span> <span class="n">Tag</span> <span class="p">(</span><span class="s1">'class'</span><span class="p">,</span><span class="s1">'formulaDsp'</span><span class="p">)</span>
|
||||||
|
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">f</span><span class="p">:</span>
|
||||||
|
<span class="n">n</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'p'</span><span class="p">,</span> <span class="s1">'</span><span class="se">\\</span><span class="s1">['</span> <span class="o">+</span> <span class="n">p</span> <span class="o">+</span> <span class="s1">'</span><span class="se">\\</span><span class="s1">]'</span><span class="p">)</span>
|
||||||
|
<span class="n">n</span><span class="o">.</span><span class="n">addT</span><span class="p">(</span><span class="n">t</span><span class="p">)</span>
|
||||||
|
<span class="bp">self</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
|
||||||
|
|
||||||
|
<span class="k">def</span> <span class="nf">to_str</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||||
|
<span class="n">s</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">indent</span><span class="p">()</span>
|
||||||
|
<span class="n">s</span><span class="o">+=</span> <span class="s1">'<'</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span>
|
||||||
|
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">tags</span><span class="p">:</span> <span class="n">s</span> <span class="o">+=</span> <span class="n">n</span><span class="o">.</span><span class="n">to_str</span><span class="p">()</span>
|
||||||
|
<span class="n">s</span><span class="o">+=</span> <span class="s1">'>'</span>
|
||||||
|
<span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span> <span class="o">!=</span> <span class="bp">None</span><span class="p">:</span> <span class="n">s</span> <span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">value</span>
|
||||||
|
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">childs</span><span class="p">:</span> <span class="n">s</span> <span class="o">+=</span> <span class="n">n</span><span class="o">.</span><span class="n">to_str</span><span class="p">()</span>
|
||||||
|
<span class="n">s</span><span class="o">+=</span> <span class="bp">self</span><span class="o">.</span><span class="n">indent</span><span class="p">()</span>
|
||||||
|
<span class="n">s</span><span class="o">+=</span> <span class="s1">'</'</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">name</span> <span class="o">+</span> <span class="s1">'>'</span>
|
||||||
|
<span class="k">return</span> <span class="n">s</span>
|
||||||
|
<span class="k">def</span> <span class="nf">cal</span> <span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">k</span><span class="o">=</span><span class="mi">0</span><span class="p">):</span>
|
||||||
|
<span class="bp">self</span><span class="o">.</span><span class="n">id</span> <span class="o">=</span> <span class="n">k</span>
|
||||||
|
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">childs</span><span class="p">:</span> <span class="n">n</span><span class="o">.</span><span class="n">cal</span> <span class="p">(</span><span class="n">k</span><span class="o">+</span><span class="mi">2</span><span class="p">)</span>
|
||||||
|
<span class="k">def</span> <span class="nf">indent</span> <span class="p">(</span><span class="bp">self</span><span class="p">):</span>
|
||||||
|
<span class="n">s</span> <span class="o">=</span> <span class="s1">'</span><span class="se">\n</span><span class="s1">'</span> <span class="o">+</span> <span class="bp">self</span><span class="o">.</span><span class="n">id</span> <span class="o">*</span> <span class="s1">' '</span>
|
||||||
|
<span class="k">return</span> <span class="n">s</span>
|
||||||
|
|
||||||
|
<span class="k">def</span> <span class="nf">set_root</span> <span class="p">(</span><span class="n">e</span><span class="p">,</span> <span class="n">root</span><span class="p">):</span>
|
||||||
|
<span class="n">styl</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'style'</span><span class="p">,</span> <span class="s1">'body {background-color: rgb(192,255,255);} h2 {color: rgb(64,0,192);} h3 {color: rgb(192,0,0);} table {color: rgb(128,0,128);}'</span><span class="p">)</span>
|
||||||
|
<span class="n">scfg</span> <span class="o">=</span> <span class="s1">'MathJax.Hub.Config({</span><span class="se">\n</span><span class="s1"> extensions: [</span><span class="se">\"</span><span class="s1">tex2jax.js</span><span class="se">\"</span><span class="s1">,"TeX/AMSmath.js"],</span><span class="se">\n</span><span class="s1"> jax: [</span><span class="se">\"</span><span class="s1">input/TeX</span><span class="se">\"</span><span class="s1">,'</span>
|
||||||
|
<span class="n">scfg</span><span class="o">+=</span> <span class="s1">'</span><span class="se">\"</span><span class="s1">output/HTML-CSS</span><span class="se">\"</span><span class="s1">],</span><span class="se">\n</span><span class="s1"> tex2jax: {inlineMath: [[</span><span class="se">\'</span><span class="s1">$</span><span class="se">\'</span><span class="s1">,</span><span class="se">\'</span><span class="s1">$</span><span class="se">\'</span><span class="s1">]]},</span><span class="se">\n</span><span class="s1"> displayAlign: </span><span class="se">\"</span><span class="s1">left</span><span class="se">\"</span><span class="s1">});'</span>
|
||||||
|
<span class="n">head</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'head'</span><span class="p">)</span>
|
||||||
|
<span class="n">meta</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'META'</span><span class="p">)</span>
|
||||||
|
<span class="n">titl</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'title'</span><span class="p">,</span> <span class="s1">'Lagrange'</span><span class="p">)</span>
|
||||||
|
<span class="n">s1</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'script'</span><span class="p">,</span> <span class="n">scfg</span><span class="p">)</span>
|
||||||
|
<span class="n">s2</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'script'</span><span class="p">)</span>
|
||||||
|
<span class="n">s1</span><span class="o">.</span><span class="n">addT</span><span class="p">(</span><span class="n">Tag</span><span class="p">(</span><span class="s1">'type'</span><span class="p">,</span><span class="s1">'text/x-mathjax-config'</span><span class="p">))</span>
|
||||||
|
<span class="n">s2</span><span class="o">.</span><span class="n">addT</span><span class="p">(</span><span class="n">Tag</span><span class="p">(</span><span class="s1">'type'</span><span class="p">,</span><span class="s1">'text/javascript'</span><span class="p">))</span>
|
||||||
|
<span class="n">s2</span><span class="o">.</span><span class="n">addT</span><span class="p">(</span><span class="n">Tag</span><span class="p">(</span><span class="s1">'src'</span><span class="p">,</span><span class="s1">'https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js'</span><span class="p">))</span>
|
||||||
|
<span class="n">meta</span><span class="o">.</span><span class="n">addT</span><span class="p">(</span><span class="n">Tag</span><span class="p">(</span><span class="s1">'HTTP-EQUIV'</span><span class="p">,</span><span class="s1">'CONTENT-TYPE'</span><span class="p">))</span>
|
||||||
|
<span class="n">meta</span><span class="o">.</span><span class="n">addT</span><span class="p">(</span><span class="n">Tag</span><span class="p">(</span><span class="s1">'CONTENT'</span><span class="p">,</span><span class="s1">'text/html; charset=utf-8'</span><span class="p">))</span>
|
||||||
|
<span class="n">head</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">meta</span><span class="p">)</span>
|
||||||
|
<span class="n">head</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">titl</span><span class="p">)</span>
|
||||||
|
<span class="n">head</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">styl</span><span class="p">)</span>
|
||||||
|
<span class="n">head</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">s1</span><span class="p">)</span>
|
||||||
|
<span class="n">head</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">s2</span><span class="p">)</span>
|
||||||
|
<span class="n">e</span><span class="o">.</span><span class="n">addE</span> <span class="p">(</span><span class="n">head</span><span class="p">)</span>
|
||||||
|
<span class="n">body</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'body'</span><span class="p">)</span>
|
||||||
|
<span class="n">body</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">root</span><span class="p">)</span>
|
||||||
|
<span class="n">e</span><span class="o">.</span><span class="n">addE</span> <span class="p">(</span><span class="n">body</span><span class="p">)</span>
|
||||||
|
<span class="n">e</span><span class="o">.</span><span class="n">cal</span><span class="p">()</span>
|
||||||
|
<span class="k">def</span> <span class="nf">html_head</span> <span class="p">():</span>
|
||||||
|
<span class="n">s</span> <span class="o">=</span> <span class="s1">'<!DOCTYPE html PUBLIC </span><span class="se">\"</span><span class="s1">-//W3C//DTD XHTML 1.0 Transitional//EN</span><span class="se">\"</span><span class="s1"> </span><span class="se">\"</span><span class="s1">http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd</span><span class="se">\"</span><span class="s1">>'</span>
|
||||||
|
<span class="k">return</span> <span class="n">s</span>
|
||||||
|
|
||||||
|
<span class="k">def</span> <span class="nf">CreateHTML</span> <span class="p">(</span><span class="n">filename</span><span class="p">,</span> <span class="n">html</span><span class="p">):</span>
|
||||||
|
<span class="n">s</span> <span class="o">=</span> <span class="n">html_head</span> <span class="p">()</span>
|
||||||
|
<span class="n">r</span> <span class="o">=</span> <span class="n">Element</span> <span class="p">(</span><span class="s1">'html'</span><span class="p">)</span>
|
||||||
|
<span class="n">set_root</span> <span class="p">(</span><span class="n">r</span><span class="p">,</span> <span class="n">html</span><span class="p">)</span>
|
||||||
|
<span class="n">s</span> <span class="o">+=</span> <span class="n">r</span><span class="o">.</span><span class="n">to_str</span><span class="p">()</span>
|
||||||
|
<span class="nb">file</span> <span class="o">=</span> <span class="nb">open</span> <span class="p">(</span><span class="n">filename</span><span class="p">,</span><span class="s1">'w'</span><span class="p">)</span>
|
||||||
|
<span class="nb">file</span><span class="o">.</span><span class="n">write</span><span class="p">(</span><span class="n">s</span><span class="p">)</span>
|
||||||
|
<span class="nb">file</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
|
||||||
|
|
||||||
|
<span class="k">def</span> <span class="nf">output</span> <span class="p">(</span><span class="n">counter</span><span class="p">,</span> <span class="n">problem</span><span class="p">,</span> <span class="n">L</span><span class="p">,</span> <span class="n">H</span><span class="p">,</span> <span class="n">hce</span><span class="p">,</span> <span class="n">lce</span><span class="p">):</span>
|
||||||
|
<span class="n">html</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'div'</span><span class="p">,</span> <span class="s1">'<hr>'</span><span class="p">)</span>
|
||||||
|
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">'h2'</span><span class="p">,</span><span class="s1">'Řešený problém : {0:s}'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">problem</span><span class="p">)))</span>
|
||||||
|
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">'h3'</span><span class="p">,</span><span class="s1">'{0:d}.1. Zadaný lagranžián :'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">counter</span><span class="p">)))</span>
|
||||||
|
<span class="n">html</span><span class="o">.</span><span class="n">addF</span> <span class="p">([</span><span class="s1">'L = '</span> <span class="o">+</span> <span class="n">vlatex</span><span class="p">(</span><span class="n">L</span><span class="p">)]);</span>
|
||||||
|
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">'h3'</span><span class="p">,</span><span class="s1">'{0:d}.2. Lagrangeovy rovnice :'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">counter</span><span class="p">)))</span>
|
||||||
|
<span class="n">html</span><span class="o">.</span><span class="n">addF</span> <span class="p">([</span><span class="n">vlatex</span><span class="p">(</span><span class="n">lce</span><span class="p">)]);</span>
|
||||||
|
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">'h3'</span><span class="p">,</span><span class="s1">'{0:d}.3. Hamiltonova funkce :'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">counter</span><span class="p">)))</span>
|
||||||
|
<span class="n">html</span><span class="o">.</span><span class="n">addF</span> <span class="p">([</span><span class="s1">'H = '</span> <span class="o">+</span> <span class="n">vlatex</span><span class="p">(</span><span class="n">H</span><span class="p">)]);</span>
|
||||||
|
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">'h3'</span><span class="p">,</span><span class="s1">'{0:d}.4. Hamiltonovy kanonické rovnice :'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">counter</span><span class="p">)))</span>
|
||||||
|
<span class="n">table</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'table'</span><span class="p">)</span>
|
||||||
|
<span class="n">table</span><span class="o">.</span><span class="n">addT</span><span class="p">(</span><span class="n">Tag</span><span class="p">(</span><span class="s1">'align'</span><span class="p">,</span><span class="s1">'center'</span><span class="p">))</span>
|
||||||
|
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">hce</span><span class="p">:</span>
|
||||||
|
<span class="n">td</span> <span class="o">=</span> <span class="s1">''</span>
|
||||||
|
<span class="k">for</span> <span class="n">q</span> <span class="ow">in</span> <span class="n">p</span><span class="p">:</span> <span class="n">td</span> <span class="o">+=</span> <span class="s1">'</span><span class="se">\n</span><span class="s1"><td class="formulaDsp"></span><span class="se">\\</span><span class="s1">[{0:s}={1:s}\qquad</span><span class="se">\\</span><span class="s1">]</td>'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">vlatex</span><span class="p">(</span><span class="n">q</span><span class="p">[</span><span class="mi">0</span><span class="p">]),</span> <span class="n">vlatex</span><span class="p">(</span><span class="n">expand</span><span class="p">(</span><span class="n">q</span><span class="p">[</span><span class="mi">1</span><span class="p">])))</span>
|
||||||
|
<span class="n">tr</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'tr'</span><span class="p">,</span> <span class="n">td</span><span class="p">)</span>
|
||||||
|
<span class="n">table</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">tr</span><span class="p">)</span>
|
||||||
|
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">table</span><span class="p">)</span>
|
||||||
|
<span class="k">return</span> <span class="n">html</span>
|
||||||
|
<span class="c1">#################### Vlastní výpočty ##########################################</span>
|
||||||
|
<span class="n">t</span> <span class="o">=</span> <span class="n">Symbol</span><span class="p">(</span><span class="s1">'t'</span><span class="p">)</span> <span class="c1"># Globální symbol pro čas</span>
|
||||||
|
<span class="c1"># vykrácení konstantou uděláme ručně, ale nemusí to fungovat, v podstatě jde jen o zbytečné hmotnosti</span>
|
||||||
|
<span class="k">def</span> <span class="nf">parse</span> <span class="p">(</span><span class="n">ex</span><span class="p">):</span>
|
||||||
|
<span class="n">ex</span> <span class="o">=</span> <span class="n">expand</span> <span class="p">(</span><span class="n">ex</span><span class="p">)</span>
|
||||||
|
<span class="k">if</span> <span class="n">ex</span><span class="o">.</span><span class="n">func</span> <span class="o">!=</span> <span class="n">add</span><span class="o">.</span><span class="n">Add</span><span class="p">:</span> <span class="k">return</span> <span class="n">ex</span>
|
||||||
|
<span class="n">e1</span> <span class="o">=</span> <span class="n">ex</span><span class="o">.</span><span class="n">args</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
||||||
|
<span class="k">if</span> <span class="n">e1</span><span class="o">.</span><span class="n">func</span> <span class="o">!=</span> <span class="n">mul</span><span class="o">.</span><span class="n">Mul</span><span class="p">:</span> <span class="k">return</span> <span class="n">ex</span>
|
||||||
|
<span class="n">p</span> <span class="o">=</span> <span class="n">e1</span><span class="o">.</span><span class="n">args</span>
|
||||||
|
<span class="n">x</span> <span class="o">=</span> <span class="p">[]</span>
|
||||||
|
<span class="k">for</span> <span class="n">b</span> <span class="ow">in</span> <span class="n">p</span><span class="p">:</span> <span class="n">x</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="mi">0</span><span class="p">)</span>
|
||||||
|
<span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">ex</span><span class="o">.</span><span class="n">args</span><span class="p">:</span>
|
||||||
|
<span class="k">if</span> <span class="n">e</span><span class="o">.</span><span class="n">func</span> <span class="o">!=</span> <span class="n">mul</span><span class="o">.</span><span class="n">Mul</span><span class="p">:</span> <span class="k">break</span>
|
||||||
|
<span class="k">for</span> <span class="n">a</span> <span class="ow">in</span> <span class="n">e</span><span class="o">.</span><span class="n">args</span><span class="p">:</span>
|
||||||
|
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">b</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">p</span><span class="p">):</span>
|
||||||
|
<span class="k">if</span> <span class="n">a</span> <span class="o">==</span> <span class="n">b</span><span class="p">:</span> <span class="n">x</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">+=</span> <span class="mi">1</span> <span class="c1"># TODO: or plus power of expression</span>
|
||||||
|
<span class="n">l</span> <span class="o">=</span> <span class="nb">len</span> <span class="p">(</span><span class="n">ex</span><span class="o">.</span><span class="n">args</span><span class="p">)</span>
|
||||||
|
<span class="n">z</span> <span class="o">=</span> <span class="p">[]</span>
|
||||||
|
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">y</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">x</span><span class="p">):</span>
|
||||||
|
<span class="k">if</span> <span class="n">y</span> <span class="o">==</span> <span class="n">l</span><span class="p">:</span> <span class="n">z</span><span class="o">.</span><span class="n">append</span> <span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="n">i</span><span class="p">])</span>
|
||||||
|
<span class="k">for</span> <span class="n">e</span> <span class="ow">in</span> <span class="n">z</span><span class="p">:</span> <span class="n">ex</span> <span class="o">=</span> <span class="n">ex</span> <span class="o">/</span> <span class="n">e</span>
|
||||||
|
<span class="n">ex</span> <span class="o">=</span> <span class="n">simplify</span> <span class="p">(</span><span class="n">ex</span><span class="p">)</span>
|
||||||
|
<span class="n">ex</span> <span class="o">=</span> <span class="n">expand</span> <span class="p">(</span><span class="n">ex</span><span class="p">)</span>
|
||||||
|
<span class="k">return</span> <span class="n">ex</span>
|
||||||
|
|
||||||
|
<span class="k">def</span> <span class="nf">lagrange</span> <span class="p">(</span><span class="n">L</span><span class="p">,</span> <span class="n">coord</span><span class="p">):</span>
|
||||||
|
<span class="n">lce</span> <span class="o">=</span> <span class="p">[]</span>
|
||||||
|
<span class="k">for</span> <span class="n">c</span> <span class="ow">in</span> <span class="n">coord</span><span class="p">:</span>
|
||||||
|
<span class="n">ex</span> <span class="o">=</span> <span class="n">L</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">)</span> <span class="o">-</span> <span class="n">L</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">c</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span> <span class="c1"># výraz pro Lagrangeovu rovnici</span>
|
||||||
|
<span class="n">ex</span> <span class="o">=</span> <span class="n">parse</span> <span class="p">(</span><span class="n">ex</span><span class="p">)</span> <span class="c1"># vykrátit případné konstanty (lze i ručně)</span>
|
||||||
|
<span class="n">le</span> <span class="o">=</span> <span class="n">Eq</span> <span class="p">(</span><span class="n">ex</span><span class="p">,</span> <span class="mi">0</span><span class="p">)</span> <span class="c1"># a udělat z toho rovnici</span>
|
||||||
|
<span class="n">lce</span><span class="o">.</span><span class="n">append</span> <span class="p">(</span><span class="n">le</span><span class="p">)</span>
|
||||||
|
<span class="n">pprint</span><span class="p">(</span><span class="n">lce</span><span class="p">)</span>
|
||||||
|
<span class="k">return</span> <span class="n">lce</span>
|
||||||
|
|
||||||
|
<span class="k">def</span> <span class="nf">compute</span> <span class="p">(</span><span class="nb">input</span><span class="p">,</span> <span class="n">Counter</span><span class="p">):</span>
|
||||||
|
<span class="n">res</span> <span class="o">=</span> <span class="nb">input</span><span class="p">()</span>
|
||||||
|
<span class="n">coord__x</span> <span class="o">=</span> <span class="n">res</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
|
||||||
|
<span class="n">coord__p</span> <span class="o">=</span> <span class="n">res</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
|
||||||
|
<span class="n">L</span> <span class="o">=</span> <span class="n">res</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
|
||||||
|
<span class="n">pprint</span> <span class="p">(</span><span class="n">L</span><span class="p">)</span> <span class="c1"># Pro kontrolu</span>
|
||||||
|
<span class="n">coord_dp</span> <span class="o">=</span> <span class="p">[]</span>
|
||||||
|
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">coord__p</span><span class="p">:</span> <span class="n">coord_dp</span><span class="o">.</span><span class="n">append</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">))</span>
|
||||||
|
<span class="n">coord_dx</span> <span class="o">=</span> <span class="p">[]</span>
|
||||||
|
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">coord__x</span><span class="p">:</span> <span class="n">coord_dx</span><span class="o">.</span><span class="n">append</span> <span class="p">(</span><span class="n">p</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">))</span>
|
||||||
|
<span class="n">lce</span> <span class="o">=</span> <span class="n">lagrange</span> <span class="p">(</span><span class="n">L</span><span class="p">,</span> <span class="nb">zip</span><span class="p">(</span><span class="n">coord__x</span><span class="p">,</span> <span class="n">coord_dx</span><span class="p">))</span>
|
||||||
|
<span class="n">E</span> <span class="o">=</span> <span class="o">-</span><span class="n">L</span>
|
||||||
|
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">coord_dx</span><span class="p">:</span> <span class="n">E</span> <span class="o">+=</span> <span class="n">L</span><span class="o">.</span><span class="n">diff</span> <span class="p">(</span><span class="n">p</span><span class="p">)</span> <span class="o">*</span> <span class="n">p</span>
|
||||||
|
<span class="n">E</span> <span class="o">=</span> <span class="n">simplify</span> <span class="p">(</span><span class="n">E</span><span class="p">)</span>
|
||||||
|
<span class="c1">#pprint (E) # Energie</span>
|
||||||
|
<span class="n">coord_ps</span> <span class="o">=</span> <span class="p">[]</span>
|
||||||
|
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">coord_dx</span><span class="p">:</span> <span class="n">coord_ps</span><span class="o">.</span><span class="n">append</span> <span class="p">(</span><span class="n">L</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">p</span><span class="p">))</span>
|
||||||
|
<span class="n">coord__h</span> <span class="o">=</span> <span class="nb">zip</span> <span class="p">(</span><span class="n">coord__p</span><span class="p">,</span> <span class="n">coord_ps</span><span class="p">)</span>
|
||||||
|
<span class="n">eqs</span> <span class="o">=</span> <span class="p">[]</span> <span class="c1"># Legendreova duální transformace</span>
|
||||||
|
<span class="k">for</span> <span class="n">p</span> <span class="ow">in</span> <span class="n">coord__h</span><span class="p">:</span> <span class="n">eqs</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">Eq</span><span class="p">(</span><span class="n">p</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">p</span><span class="p">[</span><span class="mi">1</span><span class="p">]))</span>
|
||||||
|
<span class="n">sol</span> <span class="o">=</span> <span class="p">[]</span>
|
||||||
|
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">eqs</span><span class="p">):</span> <span class="n">sol</span><span class="o">.</span><span class="n">append</span> <span class="p">(</span><span class="n">solve</span> <span class="p">([</span><span class="n">p</span><span class="p">],</span> <span class="p">[</span><span class="n">coord_dx</span><span class="p">[</span><span class="n">i</span><span class="p">]]))</span>
|
||||||
|
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">sol</span><span class="p">):</span> <span class="n">E</span> <span class="o">=</span> <span class="n">E</span><span class="o">.</span><span class="n">subs</span> <span class="p">(</span><span class="n">coord_dx</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">p</span><span class="p">[</span><span class="n">coord_dx</span><span class="p">[</span><span class="n">i</span><span class="p">]])</span>
|
||||||
|
<span class="n">H</span> <span class="o">=</span> <span class="n">simplify</span> <span class="p">(</span><span class="n">E</span><span class="p">)</span>
|
||||||
|
<span class="n">H</span> <span class="o">=</span> <span class="n">expand</span> <span class="p">(</span><span class="n">H</span><span class="p">)</span> <span class="c1"># O něco čitelněji</span>
|
||||||
|
<span class="n">pprint</span> <span class="p">(</span><span class="n">H</span><span class="p">)</span> <span class="c1"># Hamiltonova funkce</span>
|
||||||
|
<span class="n">hce</span> <span class="o">=</span> <span class="p">[]</span>
|
||||||
|
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">coord__x</span><span class="p">):</span>
|
||||||
|
<span class="n">eqx</span> <span class="o">=</span> <span class="n">Eq</span> <span class="p">(</span><span class="o">+</span><span class="n">H</span><span class="o">.</span><span class="n">diff</span> <span class="p">(</span><span class="n">coord__p</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">-</span> <span class="n">coord_dx</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="mi">0</span><span class="p">)</span>
|
||||||
|
<span class="n">eqx</span> <span class="o">=</span> <span class="n">solve</span> <span class="p">([</span><span class="n">eqx</span><span class="p">],[</span><span class="n">coord_dx</span><span class="p">[</span><span class="n">i</span><span class="p">]])</span>
|
||||||
|
<span class="n">eqx</span> <span class="o">=</span> <span class="p">[</span><span class="n">coord_dx</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">eqx</span><span class="p">[</span><span class="n">coord_dx</span><span class="p">[</span><span class="n">i</span><span class="p">]]]</span>
|
||||||
|
<span class="n">eqp</span> <span class="o">=</span> <span class="n">Eq</span> <span class="p">(</span><span class="o">-</span><span class="n">H</span><span class="o">.</span><span class="n">diff</span> <span class="p">(</span><span class="n">coord__x</span><span class="p">[</span><span class="n">i</span><span class="p">])</span> <span class="o">-</span> <span class="n">coord_dp</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="mi">0</span><span class="p">)</span>
|
||||||
|
<span class="n">eqp</span> <span class="o">=</span> <span class="n">solve</span> <span class="p">([</span><span class="n">eqp</span><span class="p">],[</span><span class="n">coord_dp</span><span class="p">[</span><span class="n">i</span><span class="p">]])</span>
|
||||||
|
<span class="n">eqp</span> <span class="o">=</span> <span class="p">[</span><span class="n">coord_dp</span><span class="p">[</span><span class="n">i</span><span class="p">],</span> <span class="n">eqp</span><span class="p">[</span><span class="n">coord_dp</span><span class="p">[</span><span class="n">i</span><span class="p">]]]</span>
|
||||||
|
<span class="n">hce</span><span class="o">.</span><span class="n">append</span><span class="p">([</span><span class="n">eqx</span><span class="p">,</span> <span class="n">eqp</span><span class="p">])</span>
|
||||||
|
<span class="n">pprint</span> <span class="p">(</span><span class="n">hce</span><span class="p">)</span> <span class="c1"># Hamiltonovy kanonické rovnice</span>
|
||||||
|
<span class="k">return</span> <span class="n">output</span> <span class="p">(</span><span class="n">Counter</span><span class="p">,</span> <span class="n">res</span><span class="p">[</span><span class="mi">3</span><span class="p">],</span> <span class="n">L</span><span class="p">,</span> <span class="n">H</span><span class="p">,</span> <span class="n">hce</span><span class="p">,</span> <span class="n">lce</span><span class="p">)</span>
|
||||||
|
|
||||||
|
<span class="c1">######################## Uživatelská část #####################################</span>
|
||||||
|
<span class="k">def</span> <span class="nf">entry1</span> <span class="p">():</span>
|
||||||
|
<span class="c1">##### Zadání #####</span>
|
||||||
|
<span class="n">m</span><span class="p">,</span><span class="n">mu</span><span class="p">,</span><span class="n">C</span> <span class="o">=</span> <span class="n">symbols</span> <span class="p">(</span><span class="s1">'m mu C'</span><span class="p">)</span> <span class="c1"># konstanty</span>
|
||||||
|
<span class="n">x</span><span class="p">,</span> <span class="n">Q</span> <span class="o">=</span> <span class="n">dynamicsymbols</span> <span class="p">(</span><span class="s1">'x Q'</span><span class="p">)</span> <span class="c1"># proměnné (zobecněné souřadnice)</span>
|
||||||
|
<span class="n">px</span><span class="p">,</span><span class="n">pQ</span> <span class="o">=</span> <span class="n">dynamicsymbols</span> <span class="p">(</span><span class="s1">'p_x p_Q'</span><span class="p">)</span> <span class="c1"># symboly pro příslušné zobecněné hybnosti</span>
|
||||||
|
<span class="c1"># Lagranžián</span>
|
||||||
|
<span class="n">L</span> <span class="o">=</span> <span class="p">(</span><span class="n">m</span> <span class="o">*</span> <span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">+</span> <span class="p">(</span><span class="n">mu</span> <span class="o">*</span> <span class="n">x</span> <span class="o">*</span> <span class="p">(</span><span class="n">Q</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span><span class="p">)</span> <span class="o">-</span> <span class="p">(</span><span class="n">Q</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">/</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">C</span><span class="p">)</span>
|
||||||
|
<span class="c1">#############################################################################</span>
|
||||||
|
<span class="k">return</span> <span class="p">[[</span><span class="n">x</span><span class="p">,</span> <span class="n">Q</span><span class="p">],</span> <span class="p">[</span><span class="n">px</span><span class="p">,</span> <span class="n">pQ</span><span class="p">],</span> <span class="n">L</span><span class="p">,</span> <span class="s1">'Railgun'</span><span class="p">]</span> <span class="c1"># Zobecníme výstup</span>
|
||||||
|
<span class="k">def</span> <span class="nf">entry2</span> <span class="p">():</span> <span class="c1"># Z učebnice - pohyb planety kolem Slunce, vychází</span>
|
||||||
|
<span class="n">G</span><span class="p">,</span><span class="n">m</span><span class="p">,</span><span class="n">M</span> <span class="o">=</span> <span class="n">symbols</span> <span class="p">(</span><span class="s1">'G m M'</span><span class="p">)</span> <span class="c1"># konstanty</span>
|
||||||
|
<span class="c1">##### Zadání #####</span>
|
||||||
|
<span class="n">r</span><span class="p">,</span> <span class="n">phi</span> <span class="o">=</span> <span class="n">dynamicsymbols</span> <span class="p">(</span><span class="s1">'r phi'</span><span class="p">)</span> <span class="c1"># proměnné (zobecněné souřadnice)</span>
|
||||||
|
<span class="n">pr</span><span class="p">,</span> <span class="n">pphi</span> <span class="o">=</span> <span class="n">dynamicsymbols</span> <span class="p">(</span><span class="s1">'p_r p_phi'</span><span class="p">)</span> <span class="c1"># symboly pro příslušné zobecněné hybnosti</span>
|
||||||
|
<span class="c1"># Lagranžián</span>
|
||||||
|
<span class="n">L</span> <span class="o">=</span> <span class="p">(</span><span class="n">m</span><span class="o">/</span><span class="mi">2</span> <span class="o">*</span> <span class="p">((</span><span class="n">r</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span> <span class="o">+</span> <span class="p">(</span><span class="n">r</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span><span class="o">*</span><span class="p">((</span><span class="n">phi</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span><span class="p">)))</span> <span class="o">+</span> <span class="p">((</span><span class="n">G</span> <span class="o">*</span> <span class="n">m</span> <span class="o">*</span> <span class="n">M</span><span class="p">)</span> <span class="o">/</span> <span class="n">r</span><span class="p">)</span>
|
||||||
|
<span class="c1">#############################################################################</span>
|
||||||
|
<span class="k">return</span> <span class="p">[[</span><span class="n">r</span><span class="p">,</span> <span class="n">phi</span><span class="p">],</span> <span class="p">[</span><span class="n">pr</span><span class="p">,</span> <span class="n">pphi</span><span class="p">],</span> <span class="n">L</span><span class="p">,</span> <span class="s1">'Pohyb planety'</span><span class="p">]</span> <span class="c1"># Zobecníme výstup</span>
|
||||||
|
<span class="k">def</span> <span class="nf">entry3</span> <span class="p">():</span> <span class="c1"># Z učebnice - harmonický oscilátor, vychází</span>
|
||||||
|
<span class="c1">##### Zadání #####</span>
|
||||||
|
<span class="n">m</span><span class="p">,</span><span class="n">omega</span> <span class="o">=</span> <span class="n">symbols</span> <span class="p">(</span><span class="s1">'m omega'</span><span class="p">)</span> <span class="c1"># konstanty</span>
|
||||||
|
<span class="n">x</span> <span class="o">=</span> <span class="n">dynamicsymbols</span> <span class="p">(</span><span class="s1">'x'</span><span class="p">)</span> <span class="c1"># proměnné (zobecněné souřadnice)</span>
|
||||||
|
<span class="n">px</span> <span class="o">=</span> <span class="n">dynamicsymbols</span> <span class="p">(</span><span class="s1">'p_x'</span><span class="p">)</span> <span class="c1"># symboly pro příslušné zobecněné hybnosti</span>
|
||||||
|
<span class="c1"># Lagranžián</span>
|
||||||
|
<span class="n">L</span> <span class="o">=</span> <span class="p">(</span><span class="n">m</span> <span class="o">*</span> <span class="p">(</span><span class="n">x</span><span class="o">.</span><span class="n">diff</span><span class="p">(</span><span class="n">t</span><span class="p">))</span><span class="o">**</span><span class="mi">2</span> <span class="o">-</span> <span class="n">m</span> <span class="o">*</span> <span class="n">omega</span><span class="o">**</span><span class="mi">2</span> <span class="o">*</span> <span class="n">x</span><span class="o">**</span><span class="mi">2</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span>
|
||||||
|
<span class="c1">#############################################################################</span>
|
||||||
|
<span class="k">return</span> <span class="p">[[</span><span class="n">x</span><span class="p">],</span> <span class="p">[</span><span class="n">px</span><span class="p">],</span> <span class="n">L</span><span class="p">,</span> <span class="s1">'Harmonický oscilátor'</span><span class="p">]</span> <span class="c1"># Zobecníme výstup</span>
|
||||||
|
<span class="c1">###############################################################################</span>
|
||||||
|
|
||||||
|
<span class="k">if</span> <span class="n">__name__</span> <span class="o">==</span> <span class="s2">"__main__"</span><span class="p">:</span>
|
||||||
|
<span class="n">computed</span> <span class="o">=</span> <span class="p">[</span><span class="n">entry1</span><span class="p">,</span> <span class="n">entry2</span><span class="p">,</span> <span class="n">entry3</span><span class="p">]</span> <span class="c1"># Co vše se bude počítat</span>
|
||||||
|
<span class="n">html</span> <span class="o">=</span> <span class="n">Element</span><span class="p">(</span><span class="s1">'div'</span><span class="p">)</span>
|
||||||
|
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">'h1'</span><span class="p">,</span><span class="s1">'Automatický výpočet Hamiltonových rovnic z lagranžiánu v pythonu pomocí sympy - vyzkoušené příklady.'</span><span class="p">))</span>
|
||||||
|
<span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">Element</span><span class="p">(</span><span class="s1">'p'</span><span class="p">,</span> <span class="n">description</span><span class="p">))</span>
|
||||||
|
<span class="k">for</span> <span class="n">i</span><span class="p">,</span><span class="n">p</span> <span class="ow">in</span> <span class="nb">enumerate</span><span class="p">(</span><span class="n">computed</span><span class="p">,</span> <span class="n">start</span> <span class="o">=</span> <span class="mi">1</span><span class="p">):</span> <span class="n">html</span><span class="o">.</span><span class="n">addE</span><span class="p">(</span><span class="n">compute</span><span class="p">(</span><span class="n">p</span><span class="p">,</span> <span class="n">i</span><span class="p">))</span>
|
||||||
|
<span class="n">CreateHTML</span><span class="p">(</span><span class="s1">'equations.html'</span><span class="p">,</span> <span class="n">html</span><span class="p">)</span> <span class="c1"># převod do lidsky čitelné formy (mathjax latex v html)</span>
|
||||||
|
|
||||||
|
</pre></div>
|
||||||
|
</td></tr></table></body>
|
||||||
|
</html>
|
29
Hamilton/lorenz.txt
Normal file
29
Hamilton/lorenz.txt
Normal file
|
@ -0,0 +1,29 @@
|
||||||
|
let bc = {t:0.0, w:[0.0, 1.0, 10.0]}
|
||||||
|
const omega = 27.0;
|
||||||
|
const sigma = 10.0;
|
||||||
|
const beta = 8.0 / 3.0;
|
||||||
|
let pf = [
|
||||||
|
(q) => {
|
||||||
|
const x = q.w[0];
|
||||||
|
const y = q.w[1];
|
||||||
|
return sigma * (y - x);
|
||||||
|
},
|
||||||
|
(q) => {
|
||||||
|
const x = q.w[0];
|
||||||
|
const y = q.w[1];
|
||||||
|
const z = q.w[2];
|
||||||
|
return x * (omega - z) - y;
|
||||||
|
},
|
||||||
|
(q) => {
|
||||||
|
const x = q.w[0];
|
||||||
|
const y = q.w[1];
|
||||||
|
const z = q.w[2];
|
||||||
|
return x * y - beta * z;
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let r = solve (0.01, 2000, bc, pf);
|
||||||
|
plot (r, {x:['t'], y:['w', 0]}, '#00ff00');
|
||||||
|
plot (r, {x:['t'], y:['w', 1]}, '#ff0000');
|
||||||
|
plot (r, {x:['t'], y:['w', 2]}, '#0000ff');
|
||||||
|
plot (r, {x:['w', 0], y:['w', 2]}, '#00ffff');
|
||||||
|
|
21
Hamilton/nvcr.txt
Normal file
21
Hamilton/nvcr.txt
Normal file
|
@ -0,0 +1,21 @@
|
||||||
|
let bc = {t:0.0, w:[0.1, 0.0]}
|
||||||
|
const C = 1.0;
|
||||||
|
const L = 1.0;
|
||||||
|
const G = 0.2;
|
||||||
|
const U0= 1.0;
|
||||||
|
let pf = [
|
||||||
|
(o) => {
|
||||||
|
const u = o.w[0];
|
||||||
|
const v = o.w[1];
|
||||||
|
return -(v + G*u*(u*u - U0*U0)) / C;
|
||||||
|
},
|
||||||
|
(o) => {
|
||||||
|
const u = o.w[0];
|
||||||
|
return u / L;
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let r = solve (0.02, 4000, bc, pf);
|
||||||
|
plot (r, {x:['t'], y:['w', 0]}, '#00ff00');
|
||||||
|
plot (r, {x:['t'], y:['w', 1]}, '#ff0000');
|
||||||
|
plot (r, {x:['w', 0], y:['w', 1]}, '#00ffff');
|
||||||
|
|
19
Hamilton/osc.txt
Normal file
19
Hamilton/osc.txt
Normal file
|
@ -0,0 +1,19 @@
|
||||||
|
/* Počáteční podmínky */
|
||||||
|
let bc = {t:0.0, w:[1.0, 0.0]}
|
||||||
|
/* Konstanty */
|
||||||
|
const f = 6.2831853
|
||||||
|
const fq = f * f;
|
||||||
|
/* Hamiltonovy rovnice */
|
||||||
|
let pf = [
|
||||||
|
(o) => {
|
||||||
|
return o.w[1];
|
||||||
|
},
|
||||||
|
(o) => {
|
||||||
|
return - fq * o.w[0];
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let r = solve (0.01, 100, bc, pf); /* řešení */
|
||||||
|
/* Grafy */
|
||||||
|
plot (r, {x:['t'], y:['w', 0]}, '#00ff00');
|
||||||
|
plot (r, {x:['t'], y:['w', 1]}, '#ff0000');
|
||||||
|
plot (r, {x:['w', 0], y:['w', 1]}, '#00ffff');
|
32
Hamilton/railgun.txt
Normal file
32
Hamilton/railgun.txt
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
const m = 0.0007, mu = 1.2e-6, C = 0.020, U0 = 250.0, R = 0.01;
|
||||||
|
/* {t:t , w:[ x, p_x, Q , p_Q]}*/
|
||||||
|
let bc = {t:0.0, w:[0.2, 0.0, C * U0, 0.0]}
|
||||||
|
let ls = [
|
||||||
|
(z) => {
|
||||||
|
const p_x = z.w[1];
|
||||||
|
return p_x / m;
|
||||||
|
},
|
||||||
|
(z) => {
|
||||||
|
const x = z.w[0];
|
||||||
|
const p_Q = z.w[3];
|
||||||
|
const d = p_Q / x;
|
||||||
|
return 0.5 * d * d / mu;
|
||||||
|
},
|
||||||
|
(z) => {
|
||||||
|
const x = z.w[0];
|
||||||
|
const p_Q = z.w[3];
|
||||||
|
return p_Q / (mu * x);
|
||||||
|
},
|
||||||
|
(z) => {
|
||||||
|
const x = z.w[0];
|
||||||
|
const Q = z.w[2];
|
||||||
|
const p_Q = z.w[3];
|
||||||
|
const Rp = - (R * p_Q) / (mu * x);
|
||||||
|
return Rp - Q/C;
|
||||||
|
},
|
||||||
|
];
|
||||||
|
let r = solve (5.0e-7, 2000, bc, ls); /* řešení */
|
||||||
|
|
||||||
|
plot (r, {x:['t'], y:['w', 1]}, '#00ff00');
|
||||||
|
plot (r, {x:['t'], y:['w', 3]}, '#ff0000');
|
||||||
|
|
|
@ -45,6 +45,10 @@
|
||||||
MIT <a href="https://code.nolog.cz/Kizarm/Calculator" target="_blank">přikládám</a>. Pro kompilaci je použit jen
|
MIT <a href="https://code.nolog.cz/Kizarm/Calculator" target="_blank">přikládám</a>. Pro kompilaci je použit jen
|
||||||
clang a jím kompilovaná C-čková knihovna <a href="https://sourceware.org/newlib/" target="_blank">newlib</a>.
|
clang a jím kompilovaná C-čková knihovna <a href="https://sourceware.org/newlib/" target="_blank">newlib</a>.
|
||||||
</p>
|
</p>
|
||||||
|
<p>Že jde něco takového napsat v čistém javascriptu, viz <a href="./Hamilton/index.html">zde</a>.
|
||||||
|
Není to dokonalé, ale funguje to. Python by na to byl asi lepší, ale tady není potřeba nic jiného
|
||||||
|
než prohlížeč.
|
||||||
|
</p>
|
||||||
</div>
|
</div>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|
Loading…
Reference in a new issue