lunar.sh 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #!/bin/sh
  2. : lunar lander shell script using ode to solve equations of motion
  3. echo "*** lunar lander ***"
  4. echo -n "grav (m/sec/sec): "; read grav
  5. echo "velocity starts at zero
  6. empty weight 1000 kg"
  7. vel=0
  8. echo -n "available impulse is 10^4 n-sec/kg
  9. total fuel (kg): "
  10. if read fuel; then :; else exit 0; fi
  11. echo -n " starting height (m): "
  12. if read height; then :; else exit 0; fi
  13. (echo "y'=v;v'=thrust*1000/m-$grav;m'=-thrust/10"
  14. dur=0
  15. while : ; do
  16. sleep 2
  17. sleep $dur
  18. echo -n " thrust (kn): " 1>&2
  19. if read thrust; then :; else break; fi
  20. echo -n " duration (sec): " 1>&2
  21. if read dur; then :; else break; fi
  22. echo "y=$height;v=$vel;thrust=$thrust;m=$fuel+1000
  23. print y,v,m from $dur;step 0,$dur"
  24. done) | ode -r 1e-5 | while read height vel mass; do
  25. echo $mass
  26. echo "velocity=${vel}m/sec; mass=${mass}kg"
  27. echo $mass
  28. fuel=`echo "5k$mass 1000-pq"|dc`
  29. case $fuel in
  30. -*|0) echo out of fuel; exit 1 ;;
  31. *) echo fuel=${fuel}kg ;;
  32. esac
  33. case $height in
  34. -*|0) case $vel in
  35. -[0-9][0-9]*) echo too fast; exit 1 ;;
  36. *) echo good landing; exit 0 ;;
  37. esac ;;
  38. *) echo height=${height}m ;;
  39. esac
  40. done