Simulating a rocket in Matlab

2.4k Views Asked by At

I want to simulate a rocket. I found this code in a book. For the past two days I have been trying to understand it. For instance there is a line: M=5.98e24/m0; %massive body mass in units of m0. What does the author mean by massive body mass in units of m0? In this case the massive body is earth. Why is he dividing with m0? Continuing he does the same for a number of variables. Tau for example is another one. Tau is equal to 805 s. He uses then tau for v0 and F0. Why? He also computes ff=0.96; mp=mi*ff; %fuel fraction, and fuel mass. What does he mean with fuel fraction?

Please help me understand. I thought this would be a cool project to do in my spare time...

 %rocket.m
    %program to solve the variable mass rocket equation in the
    %presence of a gravitational force from a massive body
    clear;
    G=6.67e-11;                    %universal gravitational constant (Nm^2/kg^2)
    R=6.37e6;                      %unit of distance (m) - massive body radius
    m0=5.98e24;                    %unit of mass (kg) - could be the massive body
    M=5.98e24/m0;                  %massive body mass in units of m0
    tau=sqrt(R^3/(G*m0));          %unit of time in seconds
    v0=R/tau; F0=m0*R/tau^2;       %unit of speed and unit of force
    mi=2.8e6/m0;                   %initial payload+fuel mass in units of m0
    ff=0.96; mp=mi*ff;             %fuel fraction, and fuel mass
    Thrust=1.5*(G*mi*M/R^2)*m0^2;  %Let the Thrust be # times initial mass weight
    Thrust=Thrust/F0;              %Thrust in units of force
    u=0.35;                        %gas exhaust velocity in units of v0
    an=55; th=an*2*pi/360;         %angle of burn determines launch angle
    ux=u*cos(th); uy=u*sin(th);    %exhaust velocity components in units of v0
    alpha=(Thrust/u);              %alpha in units of m0/tau
    mf=mi-mp;                      %final mass is payload mass (after fuel burnout)
    tfmax=(mi-mf)/alpha;           %fuel burnout time in units of tau
    tmax=50*tfmax;                 %simulation run time
    x0=0;y0=1;vx0=0;vy0=0;         %initial positions, speeds
    ic1=[x0;vx0;y0;vy0;mi];        %initial conditions: position, velocity, rocket mass

[t,w]=ode45('rocket_der',[0.0,tmax],ic1,[],alpha,ux,uy,M,tfmax);%default tolerance

L=2.5*sqrt(x0^2+y0^2);         %window size
h=[0:0.025:2*pi];
x=cos(h);y=sin(h);             %massive body perimeter
%plot(x,y,'g'); hold on        %massive body plot if needed here
%plot(w(:,1),w(:,3))           %use this to plot all the x,y points calculated
n=length(t);                   %size of the time array
for i=1:n                      %Loop to pick the points that lie above ground
  if sqrt(w(i,1)^2+w(i,3)^2) >= 0.99 %ground is 1.0 R, include points slightly below
     nn=i;
     t1(i)=t(i);
     x1(i)=w(i,1); y1(i)=w(i,3);
     vx1(i)=w(i,2); vy1(i)=w(i,4);
  else
      break;
  end
end

figure
for i=1:nn                     
  clf;
  hold on
  plot(x1(i),y1(i),'k.')       %rocket position
  plot(x,y,'b');               %draw the massive body
  axis ([-L L -L L])           %windows size
  axis equal                   %square window
  pause(0.0125)
end
plot(x1,y1,'r:')               %trace the rocket path

Book link : https://books.google.se/books?id=JevPH1zc8XMC&pg=PA349&lpg=PA349&dq=runge+kutta+matlab+position+and+speed&source=bl&ots=b5ym2csX1w&sig=FdnZvOXVvqV9fNeaspxPGlRfils&hl=en&sa=X&ved=0ahUKEwiqo8DpubrLAhUmQpoKHVW-DrQQ6AEIMDAD#v=onepage&q&f=false

1

There are 1 best solutions below

2
On

The author is just normalizing the variables with some "characteristic" sizes, so that the variables used in the script are dimensionless and easy to compare with each other.

This is done in physics all the time. For instance, when I and my fellow students had to solve the Schrödinger-equation numerically (in a Quantum Mechanics course), we divided the energy $E$ with some characteristic energy $E_0$, to get a unit-less energy $\widetilde{E}=E/E_0$. "Characteristic" just means that the size of the energy is typical for the system that you're working with; we were working with electrons and other such particles, so here the energies were typically very small!

It is just a matter of convention, but working with unit-less variables typically makes the scripts cleaner, as you don't have to drag around conversion-factors and such.

As a side note, do you know the theory underlying this simulation? If not, I'd strongly recommend trying to simulate something that you do understand instead; working with a subject you don't really understand can be very frustrating and probably won't be fruitful.

Either way, good luck with the project(s)!