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 Shaky Movie & 1 Animated GIF

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

No content

Slide 7

Slide 7 text

No content

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

we are not superheros

Slide 11

Slide 11 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 12

Slide 12 text

“matrix” “times” “vector?”

Slide 13

Slide 13 text

Vector • Underlying implementation: Array • Usually Numeric, but really anything • Native operations are different (c.f. addition) • Denoted by: ⟨x,y⟩ or x or x or or ⽷x⽹   ⽸y⽺   ⽷2⽹   ⽸0⽺  

Slide 14

Slide 14 text

Vector Example

Slide 15

Slide 15 text

Vector Example ⟨2,0⟩

Slide 16

Slide 16 text

Vector Example ⟨2,0⟩ ⟨1,3⟩

Slide 17

Slide 17 text

Matrices, or “Linear Transformations” • Structurally like a 2D Array • Used to Scale, Rotate, Skew, Perspective, Translate • Common in CSS3 • Useful for 2D & 3D graphics • Common in Kinetics & Material Mechanics • Denoted by M or M or or ⽷a  b⽹   ⽸c  d⽺   ⽷1  4⽹   ⽸9  0⽺  

Slide 18

Slide 18 text

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

Slide 19

Slide 19 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

Slide 20

Slide 20 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 21

Slide 21 text

Linear Transformation Examples

Slide 22

Slide 22 text

Hooke’s Law for Anisotropic Materials σ  =  c  *  ε

Slide 23

Slide 23 text

Hooke’s Law for Anisotropic Materials ⽷σ₁₁⽹  ⽷C₁₁  C₁₂  C₁₃  C₁₄  C₁₅  C₁₆⽹⽷  ε₁₁⽹
 ⾇σ₂₂⾇  ⾇C₁₂  C₂₂  C₂₃  C₂₄  C₂₅  C₂₆⾇⾇  ε₂₂⾇   ⾇σ₃₃⾇=⾇C₁₃  C₂₃  C₃₃  C₃₄  C₃₅  C₃₆⾇⾇  ε₃₃⾇   ⾇σ₂₃⾇  ⾇C₁₄  C₂₄  C₃₄  C₄₄  C₄₅  C₄₆⾇⾇2ε₂₃⾇   ⾇σ₁₃⾇  ⾇C₁₅  C₂₅  C₃₅  C₄₅  C₅₅  C₅₆⾇⾇2ε₁₃⾇   ⽸σ₁₂⽺  ⽸C₁₆  C₂₆  C₃₆  C₄₆  C₅₆  C₆₆⽺⽸2ε₁₂⽺

Slide 24

Slide 24 text

Hooke’s Law for Anisotropic Materials ⽷σ₁₁⽹  ⽷C₁₁  C₁₂  C₁₃  C₁₄  C₁₅  C₁₆⽹⽷  ε₁₁⽹
 ⾇σ₂₂⾇  ⾇C₁₂  C₂₂  C₂₃  C₂₄  C₂₅  C₂₆⾇⾇  ε₂₂⾇   ⾇σ₃₃⾇=⾇C₁₃  C₂₃  C₃₃  C₃₄  C₃₅  C₃₆⾇⾇  ε₃₃⾇   ⾇σ₂₃⾇  ⾇C₁₄  C₂₄  C₃₄  C₄₄  C₄₅  C₄₆⾇⾇2ε₂₃⾇   ⾇σ₁₃⾇  ⾇C₁₅  C₂₅  C₃₅  C₄₅  C₅₅  C₅₆⾇⾇2ε₁₃⾇   ⽸σ₁₂⽺  ⽸C₁₆  C₂₆  C₃₆  C₄₆  C₅₆  C₆₆⽺⽸2ε₁₂⽺

Slide 25

Slide 25 text

Our Vector ⟨1,3⟩

Slide 26

Slide 26 text

Identity Transform

Slide 27

Slide 27 text

Identity Transform I      =⽷1  0⽹
          ⽸0  1⽺


Slide 28

Slide 28 text

Identity Transform I      =⽷1  0⽹
          ⽸0  1⽺
 I*v  =⽷1  0⽹⽷1⽹
          ⽸0  1⽺⽸3⽺


Slide 29

Slide 29 text

Identity Transform I      =⽷1  0⽹
          ⽸0  1⽺
 I*v  =⽷1  0⽹⽷1⽹
          ⽸0  1⽺⽸3⽺
 I*v  =⽷(1*1)  +  (0*3)⽹
          ⽸(0*1)  +  (1*3)⽺


Slide 30

Slide 30 text

Identity Transform I      =⽷1  0⽹
          ⽸0  1⽺
 I*v  =⽷1  0⽹⽷1⽹
          ⽸0  1⽺⽸3⽺
 I*v  =⽷(1*1)  +  (0*3)⽹
          ⽸(0*1)  +  (1*3)⽺
 I*v  =⽷1⽹
          ⽸3⽺

Slide 31

Slide 31 text

Scale Transform

Slide 32

Slide 32 text

Scale Transform S      =⽷s  0⽹=⽷2  0⽹
          ⽸0  s⽺  ⽸0  2⽺


Slide 33

Slide 33 text

Scale Transform S      =⽷s  0⽹=⽷2  0⽹
          ⽸0  s⽺  ⽸0  2⽺
 S*v  =⽷2  0⽹⽷1⽹
          ⽸0  2⽺⽸3⽺


Slide 34

Slide 34 text

Scale Transform S      =⽷s  0⽹=⽷2  0⽹
          ⽸0  s⽺  ⽸0  2⽺
 S*v  =⽷2  0⽹⽷1⽹
          ⽸0  2⽺⽸3⽺
 S*v  =⽷(2*1)  +  (0*3)⽹
          ⽸(0*1)  +  (2*3)⽺


Slide 35

Slide 35 text

Scale Transform S      =⽷s  0⽹=⽷2  0⽹
          ⽸0  s⽺  ⽸0  2⽺
 S*v  =⽷2  0⽹⽷1⽹
          ⽸0  2⽺⽸3⽺
 S*v  =⽷(2*1)  +  (0*3)⽹
          ⽸(0*1)  +  (2*3)⽺
 S*v  =⽷2⽹
          ⽸6⽺

Slide 36

Slide 36 text

Flip Horizontal Transform

Slide 37

Slide 37 text

Flip Horizontal Transform H      =⽷-­‐1  0⽹
          ⽸  0  1⽺


Slide 38

Slide 38 text

Flip Horizontal Transform H      =⽷-­‐1  0⽹
          ⽸  0  1⽺
 H*v  =⽷-­‐1  0⽹⽷1⽹
          ⽸  0  1⽺⽸3⽺


Slide 39

Slide 39 text

Flip Horizontal Transform H      =⽷-­‐1  0⽹
          ⽸  0  1⽺
 H*v  =⽷-­‐1  0⽹⽷1⽹
          ⽸  0  1⽺⽸3⽺
 H*v  =⽷(-­‐1*1)  +  (0*3)⽹
          ⽸(  0*1)  +  (1*3)⽺


Slide 40

Slide 40 text

Flip Horizontal Transform H      =⽷-­‐1  0⽹
          ⽸  0  1⽺
 H*v  =⽷-­‐1  0⽹⽷1⽹
          ⽸  0  1⽺⽸3⽺
 H*v  =⽷(-­‐1*1)  +  (0*3)⽹
          ⽸(  0*1)  +  (1*3)⽺
 H*v  =⽷-­‐1⽹
          ⽸  3⽺

Slide 41

Slide 41 text

SkewX Transform

Slide 42

Slide 42 text

SkewX Transform X(θ)          =⽷1  tanθ⽹
                    ⽸0        1⽺


Slide 43

Slide 43 text

SkewX Transform X(θ)          =⽷1  tanθ⽹
                    ⽸0        1⽺
 X(30°)*v  =⽷1  .58⽹⽷1⽹
                    ⽸0      1⽺⽸3⽺


Slide 44

Slide 44 text

SkewX Transform X(θ)          =⽷1  tanθ⽹
                    ⽸0        1⽺
 X(30°)*v  =⽷1  .58⽹⽷1⽹
                    ⽸0      1⽺⽸3⽺
 X(30°)*v  =⽷(1*1)  +  (.58*3)⽹
                    ⽸(0*1)  +  (    1*3)⽺


Slide 45

Slide 45 text

SkewX Transform X(θ)          =⽷1  tanθ⽹
                    ⽸0        1⽺
 X(30°)*v  =⽷1  .58⽹⽷1⽹
                    ⽸0      1⽺⽸3⽺
 X(30°)*v  =⽷(1*1)  +  (.58*3)⽹
                    ⽸(0*1)  +  (    1*3)⽺
 X(30°)*v  =⽷2.73⽹
                    ⽸3      ⽺

Slide 46

Slide 46 text

SkewX Transform X(θ)          =⽷1  tanθ⽹
                    ⽸0        1⽺
 X(30°)*v  =⽷1  .58⽹⽷1⽹
                    ⽸0      1⽺⽸3⽺
 X(30°)*v  =⽷(1*1)  +  (.58*3)⽹
                    ⽸(0*1)  +  (    1*3)⽺
 X(30°)*v  =⽷2.73⽹
                    ⽸3      ⽺

Slide 47

Slide 47 text

SkewX Transform X(θ)          =⽷1  tanθ⽹
                    ⽸0        1⽺
 X(30°)*v  =⽷1  .58⽹⽷1⽹
                    ⽸0      1⽺⽸3⽺
 X(30°)*v  =⽷(1*1)  +  (.58*3)⽹
                    ⽸(0*1)  +  (    1*3)⽺
 X(30°)*v  =⽷2.73⽹
                    ⽸3      ⽺

Slide 48

Slide 48 text

Rotation Transform

Slide 49

Slide 49 text

Rotation Transform R(θ)          =⽷cosθ  -­‐sinθ⽹
                    ⽸sinθ    cosθ⽺


Slide 50

Slide 50 text

Rotation Transform R(θ)          =⽷cosθ  -­‐sinθ⽹
                    ⽸sinθ    cosθ⽺
 R(90°)*v  =⽷0  -­‐1⽹⽷1⽹
                    ⽸1    0⽺⽸3⽺


Slide 51

Slide 51 text

Rotation Transform R(θ)          =⽷cosθ  -­‐sinθ⽹
                    ⽸sinθ    cosθ⽺
 R(90°)*v  =⽷0  -­‐1⽹⽷1⽹
                    ⽸1    0⽺⽸3⽺
 R(90°)*v  =⽷(0*1)  +  (-­‐1*3)⽹
                    ⽸(1*1)  +  (  0*3)⽺


Slide 52

Slide 52 text

Rotation Transform R(θ)          =⽷cosθ  -­‐sinθ⽹
                    ⽸sinθ    cosθ⽺
 R(90°)*v  =⽷0  -­‐1⽹⽷1⽹
                    ⽸1    0⽺⽸3⽺
 R(90°)*v  =⽷(0*1)  +  (-­‐1*3)⽹
                    ⽸(1*1)  +  (  0*3)⽺
 R(90°)*v  =⽷-­‐3⽹
                    ⽸  1⽺

Slide 53

Slide 53 text

Rotation Transform R(30°)*                          =  

Slide 54

Slide 54 text

Rotation Transform R(30°)*                          =  

Slide 55

Slide 55 text

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

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

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

Slide 58

Slide 58 text

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

Slide 59

Slide 59 text

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

Slide 60

Slide 60 text

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

Slide 61

Slide 61 text

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

Slide 62

Slide 62 text

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

Slide 63

Slide 63 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 64

Slide 64 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 65

Slide 65 text

Wait a minute. Derivatives?

Slide 66

Slide 66 text

Velocity = Derivative of Position 0 km 60 km

Slide 67

Slide 67 text

Velocity = Derivative of Position 0 km 60 km t = 1 hour

Slide 68

Slide 68 text

Velocity = Derivative of Position 0 km 1 km

Slide 69

Slide 69 text

Velocity = Derivative of Position 0 km 1 km t = 1 minute

Slide 70

Slide 70 text

Velocity = Derivative of Position 0 km 16.67 meters

Slide 71

Slide 71 text

Velocity = Derivative of Position 0 km 16.67 meters t = 1 second

Slide 72

Slide 72 text

Velocity = Derivative of Position 0 km 1.667 centimeters

Slide 73

Slide 73 text

Velocity = Derivative of Position 0 km 1.667 centimeters t = 1 millisecond

Slide 74

Slide 74 text

Velocity = Derivative of Position now 1 hour later 60 km 0 km

Slide 75

Slide 75 text

Velocity = Derivative of Position now 1 hour later 60 km/hr 0 km/hr

Slide 76

Slide 76 text

Velocity = Derivative of Position now 1 hour later 60 km/hr 0 km/hr 1 minute later

Slide 77

Slide 77 text

Formal Derivative

Slide 78

Slide 78 text

Derivative Applied

Slide 79

Slide 79 text

Derivative Applied

Slide 80

Slide 80 text

Derivative Applied

Slide 81

Slide 81 text

Derivative Applied

Slide 82

Slide 82 text

Derivative Applied

Slide 83

Slide 83 text

Dimensional Analysis, or “Fixing Units”

Slide 84

Slide 84 text

Dimensional Analysis, or “Fixing Units”

Slide 85

Slide 85 text

Our Approximation

Slide 86

Slide 86 text

Our Approximation

Slide 87

Slide 87 text

Our Approximation velocity = [position(now) - position(recent)] / (now - recent)

Slide 88

Slide 88 text

Our Approximation velocity = [position(now) - position(recent)] / (now - recent) acceleration = [velocity(now) - velocity(recent)] / (now - recent)

Slide 89

Slide 89 text

Our Approximation velocity = [position(now) - position(recent)] / (now - recent) acceleration = [velocity(now) - velocity(recent)] / (now - recent) position(now) = position(recent) + velocity * (now - recent) velocity(now) = velocity(recent) + acceleration * (now - recent)

Slide 90

Slide 90 text

Our Approximation position(now) = position(recent) + velocity * (now - recent) velocity(now) = velocity(recent) + acceleration * (now - recent)

Slide 91

Slide 91 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 92

Slide 92 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 93

Slide 93 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 94

Slide 94 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 95

Slide 95 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 96

Slide 96 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 97

Slide 97 text

(demo)

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

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

Slide 100

Slide 100 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 101

Slide 101 text

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

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

The Answer: React

Slide 104

Slide 104 text

Feedback Controller

Slide 105

Slide 105 text

Feedback Controller

Slide 106

Slide 106 text

Feedback Controller

Slide 107

Slide 107 text

PID Controller

Slide 108

Slide 108 text

Controller P I D roportional ntegral erivative

Slide 109

Slide 109 text

Feedback Controller

Slide 110

Slide 110 text

Feedback Controller diff == error

Slide 111

Slide 111 text

Proportional P  =  error  =  desired  -­‐  actual

Slide 112

Slide 112 text

Derivative D  =  (error_now  -­‐  error_recent)/dt

Slide 113

Slide 113 text

Integral I  +=  error*dt

Slide 114

Slide 114 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 115

Slide 115 text

(demo)

Slide 116

Slide 116 text

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

Slide 117

Slide 117 text

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

Slide 118

Slide 118 text

Photo Credits • “LPE II” (Bradley Grzesiak) • “Zero G Robot” (Bradley Grzesiak) • “Outredgeous Lettuce” (Public Domain):
 https://www.nasa.gov/mission_pages/station/ research/news/meals_ready_to_eat/ • “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 • Car (Public Domain):
 https://thenounproject.com/search/?q=car&i=13094 • Curing Sausage (Used with permission):
 https://www.flickr.com/photos/aaronp/7532277486/

Slide 119

Slide 119 text

Thank You! Brad Grzesiak [email protected] @listrophy gitlab.com/listrophy