Skip to content

Instantly share code, notes, and snippets.

@felipec
Created February 16, 2025 04:00
Show Gist options
  • Save felipec/a3aa28786ded4a96196408a1e958a3d9 to your computer and use it in GitHub Desktop.
Save felipec/a3aa28786ded4a96196408a1e958a3d9 to your computer and use it in GitHub Desktop.
Examples of lambdas and objects for physics
function vdp1(t, y) {
return [y[1], (1 - y[0] ** 2) * y[1]- y[0]];
}
function van_der_pol(mu) {
return (t, y) => [y[1], mu * (1 - y[0] ** 2) * y[1] - y[0]];
}
class VdP {
constructor(mu) {
this.mu = mu;
}
calc(t, y) {
return [y[1], this.mu * (1 - y[0] ** 2) * y[1] - y[0]];
}
}
const ovdp1 = new VdP(1);
console.log('vdp a:', vdp1(0, [2, 0]));
console.log('vdp b:', van_der_pol(1)(0, [2, 0]));
console.log('vdp c:', ovdp1.calc(0, [2, 0]));
function pendulum(t, theta) {
g = 9.81;
l = 1;
return [-g/l * Math.sin(theta[1]), theta[0]];;
}
function gen_pendulum(g, l) {
return (t, theta) => [-g/l * Math.sin(theta[1]), theta[0]];
}
class Pendulum {
constructor(g, l) {
this.g = g;
this.l = l;
}
calc(t, theta) {
return [-this.g/this.l * Math.sin(theta[1]), theta[0]];
}
}
const opendulum = new Pendulum(9.81, 1);
console.log('pendulum a:', pendulum(0, [0, Math.PI / 4]));
console.log('pendulum b:', gen_pendulum(9.81, 1)(0, [0, Math.PI / 4]));
console.log('pendulum c:', opendulum.calc(0, [0, Math.PI / 4]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment