MAKS_Enjoyer

I have been sentenced to the electric chair for multiple crimes against humanity commited in ukraine

Sep 18th, 2023
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 6.98 KB | Source Code | 0 0
  1. % Program:
  2. % rocket-trajectory-simulation.m
  3. % Multi-stage rocket dynamics and trajectory simulation.
  4. %
  5. % Description:
  6. % Predicts multi-stage rocket dynamics and trajectories based on the given
  7. % rocket mass, engine thrust, launch parameters, and drag coefficient.
  8. %
  9. % Variable List:
  10. % Delta = Time step (s)
  11. % t = Time (s)
  12. % Thrust = Thrust (N)
  13. % Mass = Mass (kg)
  14. % Mass_Rocket_With_Motor = Mass with motor (kg)
  15. % Mass_Rocket_Without_Motor = Mass without motor (kg)
  16. % Theta = Angle (deg)
  17. % C = Drag coefficient
  18. % Rho = Air density (kg/m^3)
  19. % A = Rocket projected area (m^2)
  20. % Gravity = Gravity (m/s^2)
  21. % Launch_Rod_Length = Length of launch rod (m)
  22. % n = Counter
  23. % Fn = Normal force (N)
  24. % Drag = Drag force (N)
  25. % Fx = Sum of forces in the horizontal direction (N)
  26. % Fy = Sum of forces in the vertical direction (N)
  27. % Vx = Velocity in the horizontal direction (m/s)
  28. % Vy = Velocity in the vertical direction (m/s)
  29. % Ax = Acceleration in the horizontal direction (m/s^2)
  30. % Ay = Acceleration in the vertical direction (m/s^2)
  31. % x = Horizontal position (m)
  32. % y = Vertical position (m)
  33. % Distance_x = Horizontal distance travelled (m)
  34. % Distance_y = Vertical travelled (m)
  35. % Distance = Total distance travelled (m)
  36. % Memory_Allocation = Maximum number of time steps expected
  37.  
  38. clear, clc      % Clear command window and workspace
  39.  
  40. % Parameters
  41. Delta = 0.001;                  % Time step
  42. Memory_Allocation = 30000;      % Maximum number of time steps expected
  43.  
  44. % Preallocate memory for arrays
  45. t = zeros(1, Memory_Allocation);
  46. Thrust = zeros(1, Memory_Allocation);
  47. Mass = zeros(1, Memory_Allocation);
  48. Theta = zeros(1, Memory_Allocation);
  49. Fn = zeros(1, Memory_Allocation);
  50. Drag = zeros(1, Memory_Allocation);
  51. Fx = zeros(1, Memory_Allocation);
  52. Fy = zeros(1, Memory_Allocation);
  53. Ax = zeros(1, Memory_Allocation);
  54. Ay = zeros(1, Memory_Allocation);
  55. Vx = zeros(1, Memory_Allocation);
  56. Vy = zeros(1, Memory_Allocation);
  57. x = zeros(1, Memory_Allocation);
  58. y = zeros(1, Memory_Allocation);
  59. Distance_x = zeros(1, Memory_Allocation);
  60. Distance_y = zeros(1, Memory_Allocation);
  61. Distance = zeros(1, Memory_Allocation);
  62.  
  63. C = 0.556;                              % Drag coefficient
  64. Rho = 1.204;                            % Air density (kg/m^3)
  65. A = 6.76*10^-2;                         % Rocket projected area (m^2)
  66. Gravity = 9.81;                         % Gravity (m/s^2)
  67. Launch_Rod_Length = 5;                  % Length of launch rod (m)
  68. Mass_Rocket = 239.034;                  % Mass with propllant (kg)
  69. Mass_Rocket_Empty = 138.384;            % Mass without propllant (kg)
  70. Mass_Propellant = 100.65;               % Mass of propellant (kg)
  71. Mass_Flow_Rate = 14.4;                  % Mass Loss of propellant (kg/s)
  72. Burn_Time = 6.985;                       % Rocket Burn Time (s)
  73. Engine_Max_Thrust = 29130;              % Max Thrust of rocket (N)
  74.  
  75. Theta(1) = 70;                  % Initial launch angle (deg)
  76. Vx(1) = 0;                      % Initial horizontal speed (m/s)
  77. Vy(1) = 0;                      % Initial vertical speed (m/s)
  78. x(1) = 0;                       % Initial horizontal position (m)
  79. y(1) = 0.1;                     % Initial vertical position (m)
  80. Distance_x(1) = 0;              % Initial horizontal distance travelled (m)
  81. Distance_y(1) = 0;              % Initial vertical distance travelled (m)
  82. Distance(1) = 0;                % Initial  distance travelled (m)
  83. Mass(1) = Mass_Rocket;          % Initial rocket mass (kg)
  84.  
  85. n = 1;                          % Initial time step            
  86.  
  87. while y(n) > 0                  % Run until rocket hits the ground
  88.     n = n+1;                    % Increment time step
  89.     t(n)= (n-1)*Delta;          % Elapsed time                    
  90.  
  91.     % Determine rocket thrust and mass based on launch phase
  92.     if t(n) <= 0.1                                     % Rocket Startup
  93.         Thrust(n) = (Engine_Max_Thrust*10)*t(n);  
  94.         Mass(n) = Mass_Rocket;
  95.     elseif t(n) >= 0.1 && t(n) < Burn_Time             % Rocket Boost Phase
  96.         Thrust(n) = Engine_Max_Thrust;
  97.         Mass(n) = Mass_Rocket - (t(n)*Mass_Flow_Rate);
  98.     elseif t(n) >= Burn_Time                           % Rocket Shutdown                        
  99.         Thrust(n) = 0;                                        
  100.         Mass(n) = Mass_Rocket_Empty;
  101.     end
  102.  
  103.     % Normal force calculations  
  104.     if Distance(n-1) <= Launch_Rod_Length       % Launch rod normal force
  105.         Fn(n) = Mass(n)*Gravity*cosd(Theta(1));
  106.     else
  107.         Fn(n) = 0;                              % No longer on launch rod
  108.     end
  109.    
  110.     % Drag force calculation
  111.     Drag(n)= 0.5*C*Rho*A*(Vx(n-1)^2+Vy(n-1)^2); % Calculate drag force
  112.    
  113.     % Sum of forces calculations
  114.     Fx(n)= Thrust(n)*cosd(Theta(n-1))-Drag(n)*cosd(Theta(n-1))-Fn(n)*sind(Theta(n-1)); % Sum x forces
  115.     Fy(n)= Thrust(n)*sind(Theta(n-1))-(Mass(n)*Gravity)-Drag(n)*sind(Theta(n-1))+Fn(n)*cosd(Theta(n-1)); % Sum y forces
  116.        
  117.     % Acceleration calculations
  118.     Ax(n)= Fx(n)/Mass(n);                       % Net accel in x direction
  119.     Ay(n)= Fy(n)/Mass(n);                       % Net accel in y direction
  120.    
  121.     % Velocity calculations
  122.     Vx(n)= Vx(n-1)+Ax(n)*Delta;                 % Velocity in x direction
  123.     Vy(n)= Vy(n-1)+Ay(n)*Delta;                 % Velocity in y direction
  124.    
  125.     % Position calculations
  126.     x(n)= x(n-1)+Vx(n)*Delta;                   % Position in x direction
  127.     y(n)= y(n-1)+Vy(n)*Delta;                   % Position in y direction
  128.    
  129.     % Distance calculations    
  130.     Distance_x(n) = Distance_x(n-1)+abs(Vx(n)*Delta);      % Distance in x
  131.     Distance_y(n) = Distance_y(n-1)+abs(Vy(n)*Delta);      % Distance in y
  132.     Distance(n) = (Distance_x(n)^2+Distance_y(n)^2)^(1/2); % Total distance
  133.  
  134.     % Rocket angle calculation
  135.     Theta(n)= atand(Vy(n)/Vx(n));      % Angle defined by velocity vector
  136.  
  137. end
  138.  
  139. figure('units','normalized','outerposition',[0 0 1 1]) % Maximize plot window
  140.  
  141. % Figure 1
  142. subplot(3,2,1)
  143. plot(x(1:n),y(1:n));
  144. xlim([0 inf]);
  145. ylim([0 inf]);
  146. xlabel({'Range (m)'});
  147. ylabel({'Altitude (m)'});
  148. title({'Height-Range Graph (Trajectory)'});
  149.  
  150. % Figure 2
  151. subplot(3,2,2)
  152. plot(t(1:n),Vx(1:n));
  153. xlabel({'Time (s)'});
  154. ylabel({'Vx (m/s)'});
  155. title({'Vertical Velocity Graph'});
  156.  
  157. % Figure 3
  158. subplot(3,2,3)
  159. plot(t(1:n),Vy(1:n));
  160. xlabel({'Time (s)'});
  161. ylabel({'Vy (m/s)'});
  162. title({'Horizontal Velocity Graph'});
  163.  
  164. % Figure 4
  165. subplot(3,2,4)
  166. plot(t(1:n),Mass(1:n));
  167. xlim([0 60]);
  168. ylim([0 240]);
  169. xlabel({'Time (s)'});
  170. ylabel({'Mass (kg)'});
  171. title({'Rocket Mass Graph'});
  172.  
  173. % Figure 5
  174. subplot(3,2,5)
  175. plot(t(1:n),((Ax(1:n) + Ay(1:n))/2));
  176. xlabel({'Time (s)'});
  177. ylabel({'Acceleration (m/s^2)'});
  178. title({'Acceleration-Time Graph'});
  179.  
  180. % Figure 6
  181. subplot(3,2,6)
  182. plot(t(1:n),y(1:n));
  183. xlim([0 inf]);
  184. ylim([0 inf]);
  185. xlabel({'Time (s)'});
  186. ylabel({'Altitude (m)'});
  187. title({'Altitude-Time Graph'});
  188.  
  189. % Create a table with the data and variable names
  190. T = table(Theta, 'VariableNames', {'Rocket Angle'} )
  191.  
  192. % Write data to text file
  193. writetable(T, 'MyFile.txt')
  194. disp(Theta(n))
Add Comment
Please, Sign In to add comment