Elastic and Inelastic Collisions Lab

node v10.24.1
version: 2.0.0
endpointsharetweet
const _ = require("lodash") const attrib = "% [ from https://runkit.com/wgoodall01/elastic-and-inelastic-collisions-lab ] \n" class Cart { constructor(cart){ // {mass, vInitial, vFinal} Object.assign(this, cart) } get pInitial(){ return this.mass * this.vInitial } get pFinal(){ return this.mass * this.vFinal } get keInitial(){return (0.5) * this.mass * Math.pow(this.vInitial, 2)} get keFinal(){return (0.5) * this.mass * Math.pow(this.vFinal, 2)} } class Trial{ constructor(trial){ // {trial, elastic, blue, red} Object.assign(this, trial) } get pInitial(){return this.red.pInitial + this.blue.pInitial} get pFinal(){return this.red.pFinal + this.blue.pFinal} get pRatio(){return this.pFinal / this.pInitial} get keInitial(){return this.red.keInitial + this.blue.keInitial} get keFinal(){return this.red.keFinal + this.blue.keFinal} get keRatio(){return this.keFinal / this.keInitial} } // This is copy-pasted straight from Excel const records = ` Inelastic A 0.250 0.37 0.18 0.250 0 0.18 Inelastic B 0.250 0.64 0.16 0.750 0 0.16 Inelastic C 0.250 0.35 0.06 0.250 -0.24 0.06 Elastic A 0.250 0.39 0 0.250 0 0.38 Elastic B 0.250 0.66 -0.32 0.750 0 0.34 Elastic C 0.250 0.6 -0.22 0.250 -0.24 0.59 `.trim().split("\n").map(e => e.split('\t')) const data = records.map((e,i) => { const n = e.map(e => parseFloat(e)) return new Trial({ trial: i+1, elastic: e[0].indexOf("Inelastic") == -1, blue: new Cart({mass: n[1], vInitial: n[2], vFinal:n[3]}), red: new Cart({mass: n[4], vInitial:n[5], vFinal:n[6]}) }) }) // Format a latex table, from data, with a function to map over rows. const mkTable = rowMapper => ( attrib + data.map( e => rowMapper(e).join(" & ") ).join("\\\\\n") + "\\\\\n" ) const mkPadTable = (width, rowMapper) => ( mkTable(row => rowMapper(row).map(e => e.toString().padStart(width))) ) // SI unit, with given precision. var sp = (x,precision,unit) => String.raw`\SI[round-precision=${precision}]{${x}}{${unit}}` data
Masses, Starting and Ending Velocity of Colliding Carts
const dataTable = mkPadTable(7, e => [ e.trial, e.elastic? "Yes":"No", ..._.flatMap([e.blue, e.red], e => [ e.mass, e.vInitial, e.vFinal ]) ])
Initial and Final Momentum and Kinetic Energy for Colliding Carts
const resultsTableCarts = mkTable(e => [ e.trial, ..._.flatMap([e.blue, e.red], e => [ e.pInitial, e.pFinal, e.keInitial, e.keFinal ]) ])
Initial, Final, and Ratios of Total Momentum and Kinetic Energy for Colliding Carts
const resultsTableTrials = mkTable(e => [ e.trial, e.elastic? "Yes":"No", e.pInitial, e.pFinal, e.pRatio, e.keInitial, e.keFinal, e.keRatio, ])
exports.endpoint = function(req, res){ return res.end(` % Table 1: Masses, Starting and Ending Velocity of Colliding Carts ${dataTable} % Table 2: Initial and Final Momentum and Kinetic Energy for Colliding Carts ${resultsTableCarts} % Table 3: Initial, Final, and Ratios of Total Momentum and Kinetic Energy for Colliding Carts ${resultsTableTrials}`) }
Equation 1: Calculating stuff about cart trials
var t = data[0].blue const eq1 = attrib + String.raw`\begin{equation} \sisetup{round-mode=figures} \setlength{\extrarowheight}{5px} \begin{array}{r *{3}{@{}l}} \pinitial &\ = m\vinitial &\ = ${sp(t.mass, 3, 'kg')} * ${sp(t.vInitial,2,'m/s')} &\ = ${sp(t.pInitial, 2, 'kg.m/s')}\\ \pfinal &\ = m\vfinal &\ = ${sp(t.mass, 3, 'kg')} * ${sp(t.vFinal,2,'m/s')} &\ = ${sp(t.pFinal, 2, 'kg.m/s')}\\ \KEinitial &\ = \frac{1}{2}m\vinitial^2 &\ = \frac{1}{2}*${sp(t.mass, 3, 'kg')}*(${sp(t.vInitial,2,'m/s')})^2 &\ = ${sp(t.keInitial,2,'J')} \\ \KEfinal &\ = \frac{1}{2}m\vfinal^2 &\ = \frac{1}{2}*${sp(t.mass, 3, 'kg')}*(${sp(t.vFinal,2,'m/s')})^2 &\ = ${sp(t.keFinal,2,'J')} \\ \end{array} \end{equation}`
Equation 2: calculating stuff per-trial
var t = data[0] const eq2 = attrib + String.raw`\begin{equation} \sisetup{round-mode=figures} \renewcommand{\arraystretch}{1.5} \begin{array}{r *{2}{@{}l}} \sum\pinitial &\ = ${sp(t.blue.pInitial, 2, 'kg.m/s')} + ${sp(t.red.pInitial, 2, 'kg.m/s')} &\ = ${sp(t.pInitial, 2, 'kg.m/s')}\\ \sum\pfinal &\ = ${sp(t.blue.pFinal, 2, 'kg.m/s')} + ${sp(t.red.pFinal, 2, 'kg.m/s')} &\ = ${sp(t.pFinal, 2, 'kg.m/s')}\\[8px] \dfrac{\sum \pfinal}{\sum \pinitial} &\ = \dfrac{${sp(t.pFinal,2,'kg.m/s')}}{${sp(t.pInitial, 2, 'kg.m/s')}} &\ = ${sp(t.pRatio, 2, '')}\\[8px] \sum\KEinitial &\ = ${sp(t.blue.keInitial, 2, 'J')} + ${sp(t.red.keInitial, 2, 'J')} &\ = ${sp(t.keInitial, 2, 'J')}\\ \sum\KEfinal &\ = ${sp(t.blue.keFinal, 2, 'J')} + ${sp(t.red.keFinal, 2, 'J')} &\ = ${sp(t.keFinal, 2, 'J')}\\[8px] \dfrac{\sum \KEfinal}{\sum \KEinitial} &\ = \dfrac{${sp(t.keFinal,2,'J')}}{${sp(t.keInitial, 2, 'J')}} &\ = ${sp(t.keRatio, 2, '')}\\ \end{array} \end{equation}`
Loading…

no comments

    sign in to comment