Advertisement
Guest User

rocket.m

a guest
Apr 9th, 2014
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.00 KB | None | 0 0
  1. 1; %Need this to make sure Matlab/Octave reads it as a script file.
  2.  
  3. % Some initial conditions.
  4. h0 = 0.0; % Assumes sea level. Adjust to correct pressure altitude.
  5. v0 = 0.0; % Assuming start with zero velocity.
  6. m0 = 0.7; % Initial mass of the rocket. Adjust as necessary.
  7. dt = 0.01; % Time step;
  8.  
  9. % Function definitions.
  10.  
  11. % Standard quadratic drag.
  12. function f = drag(v)
  13.     % Drag with A = 30cm^2, Cd = 1.
  14.     % Density will be computed separately.
  15.     k = 0.5 * 1 * 0.003;
  16.     f = k*v^2;
  17. end
  18.  
  19. % Returns constant thrust of 80N for 10s.
  20. % You can replace it with a more realistic thrust profile.
  21. function f = thrust(t)
  22.     if t<=10
  23.         f = 80.0;
  24.     else
  25.         f = 0.0;
  26.     end
  27. end
  28.  
  29. % Mass as funciton fo time.
  30. % Since thrust is constant above, this is going to be a simple function.
  31. % You might want to adjust it, or even integrate over it for general case.
  32. function m = mass(m0, t)
  33.     % Assuming composite rocket Isp of 2,000m/s.
  34.     isp = 2000.0;
  35.  
  36.     if t<=10
  37.         m = m0 - 80.0*t/isp;
  38.     else
  39.         m = m0 - 80.0*10.0/isp;
  40.     end
  41. end
  42.  
  43. % Air density as function of altitude.
  44. function r = density(h)
  45.     % Using standard atmosphere and 20 deg C for density.
  46.     r = 1.2041;
  47.     % Using 8.5km for scale height.
  48.     sh = 8500.0;
  49.  
  50.     r = r * exp(-h/sh);
  51. end
  52.  
  53. % Gravity currently returns a constant value.
  54. % Adjust to function of altitude if needed.
  55. function g = gravity(h)
  56.     g = 9.807;
  57. end
  58.  
  59. printf("Initializing...\n");
  60.  
  61. % Actual code starts here.
  62.  
  63. v = v0;
  64. h = h0;
  65. aold = 0.0;
  66. t = 0.0;
  67. heng = 0.0;
  68. veng = 0.0;
  69. teng = 0.0;
  70.  
  71. while v >=0.0
  72. %while t<10
  73.     a = -gravity(h) + (thrust(t) - density(h)*drag(v))/mass(m0, t);
  74.  
  75.     % Do a Velocity Verlet step.
  76.     v = v + 0.5*(a + aold)*dt;
  77.     h = h + v*dt + 0.5*a*dt*dt;
  78.  
  79.     if thrust(t)>0.0
  80.         heng = h;
  81.         veng = v;
  82.         teng = t;  
  83.     end
  84.  
  85.     aold = a;
  86.     t = t+dt;
  87.  
  88.     %printf("<%f\t%f\t%f\t%f\t%f>\n", t, h, v, a, mass(m0,t));
  89. end
  90.  
  91. printf("Maximum altitude achieved: %f meters at %f seconds.\n", h, t);
  92. printf("Engine cut out at %f seconds, cruising at %f meters and %f m/s.\n", teng, heng, veng);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement