Slide 1

Slide 1 text

Simplify Challenging Software Problems with Rocket Science @bendyworks Brad Grzesiak — @listrophy

Slide 2

Slide 2 text

–Ashe Dryden “Whenever Brad is drinking, you wanna make sure you're sitting next to him, because instead of being obnoxious, he teaches you orbital mechanics.”

Slide 3

Slide 3 text

Heads up: 1 Animated GIF

Slide 4

Slide 4 text

Agenda • Reduce Cognitive Load • Visualize Simulations with Graphics • Control and Respond to Real World Processes

Slide 5

Slide 5 text

Agenda • Reduce Cognitive Load • Visualize Simulations with Graphics • Control and Respond to Real World Processes

Slide 6

Slide 6 text

we are not superheros

Slide 7

Slide 7 text

Scalars & Collections Primitives & Arrays • x, y, z = 2, 3, 5 • Math.sqrt(x**2 + y**2 + z**2) • array.zip(other).map{|(i,j)| i+j} • [m[0][0]*v[0]+m[0][1]*v[1], m[1][0]*v[0]+m[1][1]*v[1]] Vectors & Matrices • vector = Vector[2, 3, 5] • vector.magnitude # or vector.r • vector_1 + vector_2 • matrix * vector


Slide 8

Slide 8 text

“matrix” “times” “vector?”

Slide 9

Slide 9 text

Linear Transformations • Scale, Rotate, Skew, Translate (with a trick) • Common in CSS3 • Common in Kinetics & Kinematics

Slide 10

Slide 10 text

Matrix Times Vector 1.  ⽷a    b⽹⽷x⽹        ⽸c    d⽺⽸y⽺   2.  [x    y]        ⽷a    b⽹        ⽸c    d⽺   3.  ⽷ax+by⽹        ⽸cx+dy⽺  

Slide 11

Slide 11 text

Matrix Times Vector def matrix_times_vector(m, v) a, b = m[0] c, d = m[1] x, y = v [ a*x + b*y, c*x + d*y ] end require 'matrix' def matrix_times_vector(m, v) m * v end NOW WITH DIM CHECKS!

Slide 12

Slide 12 text

Rotation Matrix R(θ)  =  ⽷cosθ  -­‐sinθ⽹                ⽸sinθ    cosθ⽺   R(30deg)*⟨1,0⟩  =  ⟨0.87,0.5⟩   R(90deg)*⟨1,0⟩  =  ⟨0,1⟩

Slide 13

Slide 13 text

Rotation Matrix R(-­‐30deg)*                        =  

Slide 14

Slide 14 text

Agenda • Reduce Cognitive Load • Visualize Simulations with Graphics • Control and Respond to Real World Processes

Slide 15

Slide 15 text

Why Graphic Simulations? • Complex Systems • High Sensitivity • Pattern Recognition Required • Seeking “Good Enough”

Slide 16

Slide 16 text

In Aerospace Engineering • Launch Vibration Testing • “Kick” tests • Thermal Equilibrium • Designing to Safety Factor

Slide 17

Slide 17 text

In Aerospace Engineering • Launch Vibration Testing • “Kick” tests • Thermal Equilibrium • Designing to Safety Factor • N-Body Orbital Trajectories

Slide 18

Slide 18 text

The n-Body Problem • Newton’s Laws + Gravity • Solved for N=2 • For N>2, “no general analytical solution,” except for special cases (e.g., Lagrangian Points)

Slide 19

Slide 19 text

The n-Body Problem Gravity Force = x Newton’s 2nd Law Force = mass ⨉ acceleration Newton’s 3rd Law Force₁ = -Force₂ Velocity-Acceleration Position-Velocity

Slide 20

Slide 20 text

Wait a minute. Derivatives?

Slide 21

Slide 21 text

Formal Derivative

Slide 22

Slide 22 text

Our Approximation

Slide 23

Slide 23 text

Our Approximation

Slide 24

Slide 24 text

Our Approximation

Slide 25

Slide 25 text

The n-Body Problem Gravity Force = x Newton’s 2nd Law Force = mass ⨉ acceleration Newton’s 3rd Law Force₁ = -Force₂ Velocity-Acceleration Position-Velocity

Slide 26

Slide 26 text

The n-Body Problem Gravity Force = x Newton’s 2nd Law Force = mass ⨉ acceleration Newton’s 3rd Law Force₁ = -Force₂ Velocity-Acceleration vnow = vprev + anow dt Position-Velocity xnow = xprev + vnow dt

Slide 27

Slide 27 text

The n-Body Problem Gravity Force = x Newton’s 2nd Law acceleration = Force ÷ mass Newton’s 3rd Law Force₁ = -Force₂ Velocity-Acceleration vnow = vprev + anow dt Position-Velocity xnow = xprev + vnow dt

Slide 28

Slide 28 text

Gravity Fnow = x Newton’s 2nd Law anow = Fnow ÷ m Newton’s 3rd Law (loop over each mass-pair) Velocity-Acceleration vnow = vprev + anow dt Position-Velocity xnow = xprev + vnow dt The n-Body Problem

Slide 29

Slide 29 text

The n-Body Problem bodies.map do |body| [body, gravity(body, bodies-[body])] end.each do |(body, force)| accel = force / body.mass body.velocity += accel * dt body.position += body.velocity * dt end

Slide 30

Slide 30 text

The n-Body Problem def gravity(body, others) others.reduce(Vector[0,0]) do |memo, other| offset = other.position - body.position memo + offset * G * body.mass * other.mass / offset.r**3 end end

Slide 31

Slide 31 text

(demo)

Slide 32

Slide 32 text

Agenda • Reduce Cognitive Load • Visualize Simulations with Graphics • Control and Respond to Real World Processes

Slide 33

Slide 33 text

The World is Complicated • “Assume no air resistance” • “Assume a frictionless surface” • “Assume perfect DC voltage” • “Assume no impact from general or special relativity”

Slide 34

Slide 34 text

You simply cannot predefine a specific solution to a problem that is perturbed by the real world

Slide 35

Slide 35 text

–Helmuth von Moltke the Elder “No battle plan ever survives contact with the enemy.”

Slide 36

Slide 36 text

The Answer: React

Slide 37

Slide 37 text

Feedback Controller

Slide 38

Slide 38 text

Feedback Controller

Slide 39

Slide 39 text

Feedback Controller

Slide 40

Slide 40 text

PID Controller

Slide 41

Slide 41 text

Controller P I D roportional ntegral erivative

Slide 42

Slide 42 text

Feedback Controller diff == error

Slide 43

Slide 43 text

Proportional P  =  error  =  desired  -­‐  actual

Slide 44

Slide 44 text

Derivative D  =  (error  -­‐  prev_error)/dt

Slide 45

Slide 45 text

Integral I  =  I  +  error*dt

Slide 46

Slide 46 text

Our PID Controller loop do sleep 0.2 curr_time = Time.now dt = curr_time - prev_time p = error = desired - actual i = i + error * dt d = (error - prev_error)/dt setThrust(0.1*p + 0.003*i + 0.08*d) prev_error, prev_time = error, curr_time end

Slide 47

Slide 47 text

(demo)

Slide 48

Slide 48 text

Agenda • Reduce Cognitive Load • Visualize Simulations with Graphics • Control and Respond to Real World Processes

Slide 49

Slide 49 text

Photo Credits • “ABS/EUTELSAT-1 Launch” (Public Domain):
 http://www.spacex.com/media-gallery/detail/127081/4896 • “Beer Actor” (CC-BY):
 https://www.flickr.com/photos/cogdog/21127813750/ • “Up, up and away!” (CC-BY-SA):
 https://www.flickr.com/photos/kindercapes/5694816038 • “I’m confused” (CC-BY-SA):
 https://www.flickr.com/photos/doctabu/342220423 • “Cookie Monster Waiting” (no idea):
 https://www.google.com/search?q=cookie+monster+waiting • “Elmer Pump Heat Equation” (CC-BY-SA):
 https://commons.wikimedia.org/wiki/File:Elmer-pump-heatequation.png • “Owl Stuck” (CC-BY):
 https://www.flickr.com/photos/eben-frostey/7147015007


Slide 50

Slide 50 text

Thank You! Brad Grzesiak [email protected] @listrophy