Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Simplify Challenging Software Problems with Rocket Science

listrophy
October 17, 2015

Simplify Challenging Software Problems with Rocket Science

Aerospace engineering is really no more magical than programming, and yet it's shrouded in such mystery and mysticism that practically no one bothers to study it. I'll show you how to apply concepts from rocket science to challenging software problems, allowing you to spend more time coming up with answers rather than interpreting inputs. (40 minute version presented at EuRuKo 2015)

listrophy

October 17, 2015
Tweet

More Decks by listrophy

Other Decks in Programming

Transcript

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

    View full-size slide

  2. –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.”

    View full-size slide

  3. Heads up:
    1 Shaky Movie &
    1 Animated GIF

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  6. we
    are
    not
    superheros

    View full-size slide

  7. 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


    View full-size slide

  8. “matrix”
    “times”
    “vector?”

    View full-size slide

  9. 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⽺  

    View full-size slide

  10. Vector Example

    View full-size slide

  11. Vector Example
    ⟨2,0⟩

    View full-size slide

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

    View full-size slide

  13. 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⽺  

    View full-size slide

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

    View full-size slide

  15. 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

    View full-size slide

  16. 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!

    View full-size slide

  17. Linear Transformation
    Examples

    View full-size slide

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

    View full-size slide

  19. 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ε₁₂⽺

    View full-size slide

  20. 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ε₁₂⽺

    View full-size slide

  21. Our Vector
    ⟨1,3⟩

    View full-size slide

  22. Identity Transform

    View full-size slide

  23. Identity Transform
    I      =⽷1  0⽹

             ⽸0  1⽺


    View full-size slide

  24. Identity Transform
    I      =⽷1  0⽹

             ⽸0  1⽺

    I*v  =⽷1  0⽹⽷1⽹

             ⽸0  1⽺⽸3⽺


    View full-size slide

  25. 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)⽺


    View full-size slide

  26. 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⽺

    View full-size slide

  27. Scale Transform

    View full-size slide

  28. Scale Transform
    S      =⽷s  0⽹=⽷2  0⽹

             ⽸0  s⽺  ⽸0  2⽺


    View full-size slide

  29. Scale Transform
    S      =⽷s  0⽹=⽷2  0⽹

             ⽸0  s⽺  ⽸0  2⽺

    S*v  =⽷2  0⽹⽷1⽹

             ⽸0  2⽺⽸3⽺


    View full-size slide

  30. 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)⽺


    View full-size slide

  31. 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⽺

    View full-size slide

  32. Flip Horizontal Transform

    View full-size slide

  33. Flip Horizontal Transform
    H      =⽷-­‐1  0⽹

             ⽸  0  1⽺


    View full-size slide

  34. Flip Horizontal Transform
    H      =⽷-­‐1  0⽹

             ⽸  0  1⽺

    H*v  =⽷-­‐1  0⽹⽷1⽹

             ⽸  0  1⽺⽸3⽺


    View full-size slide

  35. 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)⽺


    View full-size slide

  36. 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⽺

    View full-size slide

  37. SkewX Transform

    View full-size slide

  38. SkewX Transform
    X(θ)          =⽷1  tanθ⽹

                       ⽸0        1⽺


    View full-size slide

  39. SkewX Transform
    X(θ)          =⽷1  tanθ⽹

                       ⽸0        1⽺

    X(30°)*v  =⽷1  .58⽹⽷1⽹

                       ⽸0      1⽺⽸3⽺


    View full-size slide

  40. 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)⽺


    View full-size slide

  41. 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      ⽺

    View full-size slide

  42. 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      ⽺

    View full-size slide

  43. 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      ⽺

    View full-size slide

  44. Rotation Transform

    View full-size slide

  45. Rotation Transform
    R(θ)          =⽷cosθ  -­‐sinθ⽹

                       ⽸sinθ    cosθ⽺


    View full-size slide

  46. Rotation Transform
    R(θ)          =⽷cosθ  -­‐sinθ⽹

                       ⽸sinθ    cosθ⽺

    R(90°)*v  =⽷0  -­‐1⽹⽷1⽹

                       ⽸1    0⽺⽸3⽺


    View full-size slide

  47. 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)⽺


    View full-size slide

  48. 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⽺

    View full-size slide

  49. Rotation Transform
    R(30°)*                          =  

    View full-size slide

  50. Rotation Transform
    R(30°)*                          =  

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  59. 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)

    View full-size slide

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

    View full-size slide

  61. Wait a minute.
    Derivatives?

    View full-size slide

  62. Velocity = Derivative of Position
    0 km 60 km

    View full-size slide

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

    View full-size slide

  64. Velocity = Derivative of Position
    0 km 1 km

    View full-size slide

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

    View full-size slide

  66. Velocity = Derivative of Position
    0 km 16.67 meters

    View full-size slide

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

    View full-size slide

  68. Velocity = Derivative of Position
    0 km 1.667 centimeters

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  73. Formal Derivative

    View full-size slide

  74. Derivative Applied

    View full-size slide

  75. Derivative Applied

    View full-size slide

  76. Derivative Applied

    View full-size slide

  77. Derivative Applied

    View full-size slide

  78. Derivative Applied

    View full-size slide

  79. Dimensional Analysis,
    or “Fixing Units”

    View full-size slide

  80. Dimensional Analysis,
    or “Fixing Units”

    View full-size slide

  81. Our Approximation

    View full-size slide

  82. Our Approximation

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  85. 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)

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  88. 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

    View full-size slide

  89. 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

    View full-size slide

  90. 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

    View full-size slide

  91. 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

    View full-size slide

  92. 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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  98. The Answer: React

    View full-size slide

  99. Feedback Controller

    View full-size slide

  100. Feedback Controller

    View full-size slide

  101. Feedback Controller

    View full-size slide

  102. PID Controller

    View full-size slide

  103. Controller
    P
    I
    D
    roportional
    ntegral
    erivative

    View full-size slide

  104. Feedback Controller

    View full-size slide

  105. Feedback Controller
    diff == error

    View full-size slide

  106. Proportional
    P  =  error  =  desired  -­‐  actual

    View full-size slide

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

    View full-size slide

  108. Integral
    I  +=  error*dt

    View full-size slide

  109. 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

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  112. 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/

    View full-size slide

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

    View full-size slide