├── README.md ├── getAcceleration.m ├── getCD.m ├── getConditions.m ├── getDrag.m ├── integrationFunction.m └── simulation.m /README.md: -------------------------------------------------------------------------------- 1 | # 3DOF-Rocket-Simulation 2 | Using MATLAB to simulate a rocket launch with constant acceleration (3DOF). 3 | 4 | My brother had this project in one of his Aerospace classes, and I thought it sounded like a fun project. I got the physics formulas via text documents the teacher put out with the assignment. 5 | 6 | ## Objective 7 | Send a rocket from an initial altitude of 210m across a full parabolic flight path and end at 220m. 8 | 9 | This is accomplished by using MATLAB's ode45 function to integrate the calculated Velocity and Acceleration vector over each time step. 10 | 11 | ## Initial Values 12 | - Position: 210 13 | - Velocity: .001 14 | - Time: 0s 15 | - dTime: 0.1s 16 | - rocketLength: 2.87m 17 | - rocketDiameter: 0.122m 18 | - wetMass: 65.6 19 | - dryMass: 45.2 20 | - thrustDuration: 1.8s 21 | - thrustMagnitude: 21525 22 | - initialTemperature: 300 (kelvin) 23 | - initialPressure: 99000 24 | - specificGasConstant: 287.058 25 | - gravity: 9.8067 26 | - lapseRate = -0.0065 27 | -------------------------------------------------------------------------------- /getAcceleration.m: -------------------------------------------------------------------------------- 1 | function [ newAcceleration] = getAcceleration(altitude, launchClear, thrust, drag, gravity, mass, launchClearSpeed, magVelocity) 2 | newAccelerationX = (thrust(1,1)+(drag(1,1)))/(mass); 3 | newAccelerationY = (thrust(1,2)+(drag(1,2)))/(mass); 4 | 5 | if launchClearSpeed > magVelocity 6 | newAccelerationZ = (thrust(1,3)+(drag(1,3)))/(mass); 7 | else 8 | newAccelerationZ = ((thrust(1,3)+(drag(1,3)))- mass*gravity)/(mass); 9 | end 10 | newAcceleration = [newAccelerationX, newAccelerationY, newAccelerationZ]; 11 | 12 | 13 | end 14 | 15 | -------------------------------------------------------------------------------- /getCD.m: -------------------------------------------------------------------------------- 1 | function [ cd ] = getCD( machNumber, time, thrustDuration ) 2 | machTable = [0, 0.5, 0.75, 0.9, 0.95, 1.1, 1.2, 1.3, 1.5, 2.0, 3.0]; 3 | coeffDragCoast = [0.292, 0.264, 0.277, 0.392, 0.474, 0.557, 0.557, 0.545, 0.492, 0.428, 0.335]; 4 | coeffDragBoost = [0.148, 0.127, 0.129, 0.167, 0.197, 0.245, 0.245, 0.241, 0.227, 0.207, 0.174]; 5 | 6 | if time < thrustDuration 7 | cd = interp1(machTable,coeffDragBoost, machNumber); 8 | 9 | else 10 | cd = interp1(machTable, coeffDragCoast, machNumber); 11 | 12 | end 13 | end 14 | 15 | -------------------------------------------------------------------------------- /getConditions.m: -------------------------------------------------------------------------------- 1 | function [ temperature, pressure, density, acousticSpeed ] = getConditions( altitude ) 2 | gravity = 9.8067; 3 | initialTemperature = 300; 4 | initialPressure = 99000; 5 | lapseRate = -0.0065; 6 | initialAltitude = 210; 7 | specificGasConstant = 287.058; 8 | 9 | temperature = initialTemperature + lapseRate*(altitude-initialAltitude); 10 | 11 | pressure = initialPressure*((temperature/initialTemperature)^(-gravity/(lapseRate*specificGasConstant))); 12 | 13 | density = pressure/(specificGasConstant*temperature); 14 | 15 | acousticSpeed = (1.4*specificGasConstant*temperature)^0.5; 16 | end 17 | 18 | -------------------------------------------------------------------------------- /getDrag.m: -------------------------------------------------------------------------------- 1 | function [ drag ] = getDrag(s, density, velocity, coeffOfDrag, magVelocity ) 2 | x = (velocity(1,1)); 3 | y = (velocity(1,2)); 4 | z = (velocity(1,3)); 5 | newDragX = -0.5*density*magVelocity*coeffOfDrag*s*x; 6 | newDragY = -0.5*density*magVelocity*coeffOfDrag*s*y; 7 | newDragZ = -0.5*density*magVelocity*coeffOfDrag*s*z; 8 | 9 | drag = [newDragX, newDragY, newDragZ]; 10 | 11 | 12 | end 13 | 14 | -------------------------------------------------------------------------------- /integrationFunction.m: -------------------------------------------------------------------------------- 1 | function [ xDot, drag, thrust, mass, acceleration] = integrationFunction( time, x ) 2 | gravity = 9.8067; %gravity 3 | initialAltitude = 210; 4 | rocketLength = 2.87; 5 | rocketDiameter = 0.122; 6 | thrustDuration = 1.8; 7 | thrustMagnitude = 21525; 8 | launchMass = 65.6; 9 | burnoutMass = 45.2; 10 | mass = 65.6; 11 | launchClear = initialAltitude + rocketLength; 12 | timeTable = [0 1.8 1.8001 3]; 13 | thrustTable = [21525 21525 0 0]; 14 | massTable = [65.6 45.2 45.2 45.2]; 15 | launchClearSpeed = sqrt(2*(thrustMagnitude/launchMass)*rocketLength); 16 | 17 | position = x(1:3)'; 18 | velocity = x(4:6)'; 19 | altitude = position(3); 20 | magVelocity = (velocity(1,1).^2 + velocity(1,2).^2 + velocity(1,3).^2).^.5; 21 | normVel = norm(velocity); 22 | s = ((rocketDiameter/2)^2)*pi; 23 | 24 | [temperature, pressure, density, acousticSpeed] = getConditions(altitude); 25 | 26 | machNumber = normVel/acousticSpeed; 27 | 28 | coeffDrag = getCD(machNumber, time, thrustDuration); 29 | 30 | drag = getDrag(s, density, velocity, coeffDrag, magVelocity); 31 | 32 | %mass = getMass(mass, time, launchMass, burnoutMass, thrustDuration); 33 | mass = interp1(timeTable, massTable, time, 'linear', 'extrap'); 34 | 35 | %thrust = getThrust(time, thrustDuration, thrustMagnitude, velocity, magVelocity); 36 | thrust = (velocity/norm(velocity))*interp1(timeTable,thrustTable,time,'linear','extrap'); 37 | 38 | acceleration = getAcceleration(altitude, launchClear, thrust, drag, gravity, mass, launchClearSpeed, magVelocity); 39 | 40 | xDot = [velocity'; acceleration']; 41 | 42 | end 43 | 44 | -------------------------------------------------------------------------------- /simulation.m: -------------------------------------------------------------------------------- 1 | clc 2 | clear 3 | close all 4 | clear classes 5 | 6 | time = 0; 7 | dt = 0.1; 8 | position = [0,0,210]; 9 | velocity = .001; 10 | azimuth = 45; %Degrees 11 | angleOfAttack = 45; %Degrees 12 | thrustDuration = 1.8; 13 | altitude = 210; 14 | returnData = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; 15 | theta = 90 - angleOfAttack; 16 | phi = 90 - azimuth; 17 | range = 0; 18 | velocityX = velocity*sind(theta)*cosd(phi); 19 | velocityY = velocity*sind(theta)*sind(phi); 20 | velocityZ = velocity*cosd(theta); 21 | 22 | velocityVector = [velocityX, velocityY, velocityZ]; 23 | 24 | while altitude <= 220 25 | initialState = [position';velocityVector']; 26 | [t, solution] = ode45(@integrationFunction, [time, time+dt], initialState); 27 | [xDot, drag, thrust, mass, acceleration] = integrationFunction(time+dt, initialState); 28 | newState = solution(end,:); 29 | 30 | normVelocityOutter = norm(velocityVector); 31 | normAcceleration = norm(acceleration); 32 | range = norm(position); 33 | % posH = position(1,1)/(cosd(phi)); 34 | magXY = (((velocityVector(1,1)^2)+(velocityVector(1,2)^2))^.5); 35 | flightAngle = atand(velocityVector(1,3)/magXY); 36 | % flightAngle = asind(((((9.8066)*(range)/normVelocityOutter))^0.5)+45); 37 | newIterationData = [time,position(1,1),position(1,2),position(1,3), velocityVector(1,1), velocityVector(1,2), velocityVector(1,3), normVelocityOutter, acceleration(1,1), acceleration(1,2), acceleration(1,3), normAcceleration, drag(1,1), drag(1,2), drag(1,3), mass, flightAngle]; 38 | 39 | position = newState(1:3)'; 40 | velocityVector = newState(4:6)'; 41 | time = time + dt; 42 | altitude = position(3); 43 | 44 | returnData = cat(1,returnData, newIterationData); 45 | position = position'; 46 | velocityVector = velocityVector'; 47 | % % % normVelocityOutter = norm(velocityVector); 48 | end 49 | 50 | while altitude > 220 51 | initialState = [position';velocityVector']; 52 | [t, solution] = ode45(@integrationFunction, [time, time+dt], initialState); 53 | [xDot, drag, thrust, mass, acceleration] = integrationFunction(time+dt, initialState); 54 | newState = solution(end,:); 55 | 56 | normVelocityOutter = norm(velocityVector); 57 | normAcceleration = norm(acceleration); 58 | range = norm(position); 59 | % posH = position(1,1)*(cosd(phi)); 60 | % flightAngle = asind(((((9.8066)*(range)/normVelocityOutter))^0.5)+45); 61 | magXY = (((velocityVector(1,1)^2)+(velocityVector(1,2)^2))^.5); 62 | flightAngle = atand(velocityVector(1,3)/magXY); 63 | newIterationData = [time,position(1,1),position(1,2),position(1,3), velocityVector(1,1), velocityVector(1,2), velocityVector(1,3), normVelocityOutter, acceleration(1,1), acceleration(1,2), acceleration(1,3), normAcceleration, drag(1,1), drag(1,2), drag(1,3), mass, flightAngle]; 64 | 65 | if newState(1,3) < 220 66 | newState = solution(12,:); 67 | position = newState(1:3); 68 | time = t(12,:); 69 | velocityVector = newState(4:6); 70 | newIterationData = [time,position(1,1),position(1,2),position(1,3), velocityVector(1,1), velocityVector(1,2), velocityVector(1,3), normVelocityOutter, acceleration(1,1), acceleration(1,2), acceleration(1,3), normAcceleration, drag(1,1), drag(1,2), drag(1,3), mass, flightAngle]; 71 | altitude = 219; 72 | else 73 | position = newState(1:3)'; 74 | velocityVector = newState(4:6)'; 75 | altitude = position(3); 76 | end 77 | 78 | time = time + dt; 79 | 80 | 81 | returnData = cat(1,returnData, newIterationData); 82 | position = position'; 83 | velocityVector = velocityVector'; 84 | end 85 | 86 | timeData = returnData(:,1); 87 | xdata = returnData(:,2); 88 | ydata = returnData(:,3); 89 | zdata = returnData(:,4); 90 | normVData = returnData(:,8); 91 | normAData = returnData(:,12); 92 | fPAngleData = returnData(:,17); 93 | dataTable = [timeData, xdata, ydata, zdata]; 94 | csvwrite('john_fleming_outputData.txt',dataTable); 95 | 96 | figure(1) 97 | plot(ydata,zdata) 98 | title('Z vs Y') 99 | xlabel('Y') 100 | ylabel('Z') 101 | 102 | figure(2) 103 | plot(timeData,zdata) 104 | title('Z vs Time') 105 | xlabel('Time') 106 | ylabel('Z') 107 | 108 | figure(3) 109 | plot(timeData,normVData) 110 | title('Speed vs Time') 111 | xlabel('Time') 112 | ylabel('Speed') 113 | 114 | figure(4) 115 | plot(timeData,normAData) 116 | title('Acceleration Magnitude vs Time') 117 | xlabel('Time') 118 | ylabel('Acceleration Magnitude') 119 | 120 | figure(5) 121 | plot(timeData,fPAngleData) 122 | title('Flight Path Angle vs Time') 123 | xlabel('Time') 124 | ylabel('Flight Path Angle') 125 | 126 | disp('Important Values:'); 127 | disp(''); 128 | stuff1 = ['Range: ', range]; 129 | disp('Range:'); 130 | disp(range); 131 | disp(''); 132 | disp('Time of Flight:'); 133 | disp(timeData(end,:)); 134 | disp(''); 135 | disp('Impact Coordinates:'); 136 | disp(xdata(end,:)); 137 | disp(ydata(end,:)); 138 | disp(zdata(end,:)); 139 | disp(''); 140 | disp('Max Velocity:'); 141 | disp(normVData(20,:)); 142 | disp('at time:'); 143 | disp(timeData(20,:)); 144 | 145 | 146 | 147 | --------------------------------------------------------------------------------