Hookes Law

Overview


Linear Algebra Approach


Using the Linear Algebra formulaiton of Newtons Equations, a mass on a spring can be coded as follows:


let k = 1;
let dt = 0.01
let mult = [[0,0,-1*k],[dt, 1, 0],[0, dt, 1]];

let init = [[0],[0],[1]];

let data = [];
let t = 0;
for(let i=0;i<1000;i++){ 
  init=la.multiply(mult, init);
  data.push({t:t, x:init[2][0]});
  t+=dt;
}
					
Try it!