Author Topic: Simulate orbit using Matlab  (Read 109100 times)

Offline thundergod

  • Member
  • Posts: 8
  • Liked: 1
  • Likes Given: 0
Re: Simulate orbit using Matlab
« Reply #40 on: 04/25/2009 11:51 pm »
So,  after looking at the files where should I place the if statement exactly?  I have my function but not sure where to place it or how it should be written. If everyone would prefer I can just paste the code instead of uploading the files.

Offline thundergod

  • Member
  • Posts: 8
  • Liked: 1
  • Likes Given: 0
Re: Simulate orbit using Matlab
« Reply #41 on: 04/26/2009 10:10 pm »
driverss3matt.m

------
% Script Solution for 3-body problem (earth, moon, spaceship)
% 2-D
% Spaceship thruster is fired for a certain period of time from a...
% geostationary orbit
% Figure eight solution

clear;
close all;
clc;
global me mm ms G xm thrust thrustduration

% -----Constants------%
            me = 59742e24;  % mass of the earth [kg]
            mm = 7.35e22;   % mass of the moon [kg]
            ms = 1.5e5;     % mass of the spaceship [kg]
            G  = 6.673e-11; % Gravitational Constant [m^3 kg^-1 s^-2]
            xm = 384e6;     % distance from the earth to the moon [m]

% -----Parameters------%
thrustduration = 452;   
        tfinal = 5e4;   
thrust = 888e5; % amount of thrust [lb]

            x0 =  -6.578e6 ; % initial position in x
            y0 =  0        ; % initial position in y
            v0 = sqrt((G*me)/(6.4e6+2e5));    % initial velocity
           vx0 = 0; % initial velocity in x
           vy0 = sqrt((G*me)/(6.4e6+2e5))+(thrustduration*thrust)/ms; % initial velocity in y

%              t =  ; % time
%       t_begin =  0; % time for initiate thrust?
%       t_end   =  10;
%       t_travel =  ; % time for spaceship to travel

         tspan =  [0,tfinal] ; % time span, entire?
%          tsize =  size(tspan); % time size?
         
%----If Statement for Thrust----%
       

            y0 = [x0,y0,vx0,vy0]; % not sure what all needs to be here

         [t,y] = ode45('mattscode', tspan, y0);

             x = y(:,1) ; % position in x after time t
             y = y(:,2) ; % position in y after time t
%           vx1 = y(:,3) ; % velocity in x after time t
 %          vy1 = y(:,4) ; % velocity in y after time t

%----Plotting Velocity Analysis----%
% figure(1);
% subplot(2,1,1), plot(t,vx1);
% subplot(2,1,2), plot(t,vy1);
% xlabel('Time (sec.)');
% ylabel('Velocity (m/sec.)');


%----Plotting Earth and Moon----%

%Earth%
figure(2);
h=0; k=0; r=6.4e6; N=256;
t=(0:N)*2*pi/N;
fill( r*cos(t)+h, r*sin(t)+k,'r');
text(0,0,'E')
axis('equal')
hold on

%Moon%
h=xm; k=0; r=1.75e6; N=256;
t=(0:N)*2*pi/N;
fill( r*cos(t)+h, r*sin(t)+k,'b');
text(xm,0,'M')
axis('equal')

%----Plotting Orbit----%
plot(x,y,'k');
title('Spaceship Orbit');
ylabel('y(t)');
xlabel('x(t)');
axis('equal')
hold off

-------

mattscode.m

--------function ydot = mattscode(t,var)

global me mm ms G xm thrust thrustduration

x  = var(1);    % x position [m]
y  = var(2);    % y position [m]
vx = var(3);    % x velocity [m/s]
vy = var(4);    % y velocity [m/s]

% -----2-D problem => 4 ODE's------%
if t <= thrustduration
   
yd1 = vx;
yd2 = vy;
yd3 = 1/ms*(-G*me*ms/(x^2+y^2)*x/sqrt(x^2+y^2) + G*mm*ms/((xm-x)^2+y^2)*x/sqrt((xm-x)^2+y^2) + thrust*x/sqrt(x^2+y^2)); % unit vectors instead of angles
yd4 = 1/ms*(-G*me*ms/(x^2+y^2)*y/sqrt(x^2+y^2) - G*mm*ms/((xm-x)^2+y^2)*y/sqrt((xm-x)^2+y^2) + thrust*y/sqrt(x^2+y^2)); % unit vectors instead of angles
ydot = [yd1;yd2;yd3;yd4];
else
yd1 = vx;
yd2 = vy;
yd3 = 1/ms*(-G*me*ms/(x^2+y^2)*x/sqrt(x^2+y^2) + G*mm*ms/((xm-x)^2+y^2)*x/sqrt((xm-x)^2+y^2)); % unit vectors instead of angles
yd4 = 1/ms*(-G*me*ms/(x^2+y^2)*y/sqrt(x^2+y^2) - G*mm*ms/((xm-x)^2+y^2)*y/sqrt((xm-x)^2+y^2)); % unit vectors instead of angles

ydot = [yd1;yd2;yd3;yd4];
end



end
----------

if you run this as is, you will see that a spaceship goes around the earth and hits the moon.

there is something not taking into account in the equations so that gravity of moon affects spaceship.

Also, I think something about the parameters may be wrong.  Can anyone help or spot the problem.

**From last post, my teacher said to put the thrust as a tangential vector, so how do I update that from what I have now**

Sorry for the strikethough, some of it is commented out, but some of it is a comment made or not finished.
« Last Edit: 04/26/2009 10:15 pm by thundergod »

Offline thundergod

  • Member
  • Posts: 8
  • Liked: 1
  • Likes Given: 0
Re: Simulate orbit using Matlab
« Reply #42 on: 04/27/2009 02:42 am »
I am so close now...if and when i get it I will upload.

The last thing is that dang thrust.

My teacher told me: You need a vector which always points in the tangential direction. Then you need to build a unit vector from this ...

I am assuming its the n-t coordinate system, but I am unsure how to formulate this into what I need for matlab. 

Any help?

Offline thundergod

  • Member
  • Posts: 8
  • Liked: 1
  • Likes Given: 0
Re: Simulate orbit using Matlab
« Reply #43 on: 07/08/2009 06:24 pm »
So I'm back.

I managed to finish and get most of the credit for my project.  Thanks everyone for your help.

My new personal project is to make this a 3d simulation such as an earlier member was doing.

Anyone want to help?

Right now I only have the figure and earth/moon plotted, but I am working on the code for the orbit.

I have included my final 2d version--it still crashes into the moon before making the figure eight, maybe someone could figure out why for me

Also the beginning of my 3d version.

Offline ersin2323

  • Member
  • Posts: 1
  • Liked: 0
  • Likes Given: 0
Re: Simulate orbit using Matlab
« Reply #44 on: 11/02/2009 03:31 pm »
any one has standart LEO Matlab Codes? I need a simulation...

Offline Art_UA

  • Member
  • Posts: 2
  • Liked: 0
  • Likes Given: 0
Re: Simulate orbit using Matlab
« Reply #45 on: 07/03/2010 03:37 pm »
Hello!
Sorry for bumping such old topic. I REALLY need some help.
I'm trying to make a 2D model of restricted three-body problem, so my problem is relative to problems being discussed in this topic and I decided to reply here because of this.
My goal is to make a model of figure eight transfer between the Earth and the Moon. First at all, I decided to make a model of the static Earth and Moon moving around and then add a spaceship to this model. I have changed the first C5C6's program with Jorge's correctives, but what I got doen't actually looks like Moon's orbit. If someone will help me with this I could be very much obliged.
I've added a model of figure eight transfer between the Earth and the Moon with static Earth and Moon (from www.mathworks.com). Maybe modifying this model to make Moon move will be easier than step-by-step C5C6's program modifying?
« Last Edit: 07/03/2010 08:33 pm by Art_UA »

Offline Art_UA

  • Member
  • Posts: 2
  • Liked: 0
  • Likes Given: 0
Re: Simulate orbit using Matlab
« Reply #46 on: 07/04/2010 12:54 pm »
I've added a satellite to my model, but it does't work correctly. There are some troubles witn calculating of the gravitational acceleration I think. I calculate it like this:

km = (pos_sat_y - pos_Moon_y)/(pos_sat_x - pos_Moon_x); % angle coefficient of vector between moon & satellite
    ke = (pos_sat_y)/(pos_sat_x); % angle coefficient of vector between earth & satellite
    cosalpha = abs(km*ke+1)/((1+km^2)^1/2)*(1+ke)^(1/2); %cos of angle between vectors form satellite to moon & from satellite to earth
    se = (G*Earth_mass)/(pos_sat_x^2+pos_sat_y^2)^(1/2); %vector between satellit & earth
    sm = ((G*mass_Moon)/((pos_sat_x - pos_Moon_x)^2 + (pos_sat_y - pos_Moon_y)^2)^(1/2)); %vect between satellite & moon
    grav_acc_sat = se^2 + sm^2 + 2*se*sm*cosalpha;
    anglesat = atan(pos_sat_y/pos_sat_x);
   sat_acc_y = grav_acc_sat*abs(sin(anglesat));
   sat_acc_x = grav_acc_sat*abs(cos(anglesat));

How do you think is it correct?
Thanks for any help
« Last Edit: 07/04/2010 02:27 pm by Art_UA »

Offline Thiha Kyaw

Re: Simulate orbit using Matlab
« Reply #47 on: 06/29/2014 11:44 am »
Hi all friend.
 I am a student studying space system engineering at Myanmar Aerospace Engineering University. I am interested in orbit. ACE orbit is special. The line of apsides ( line between apogee and perigee)rotates 360 degree per year. I want know how this happen. If you know, share me some information or link of free book.
And I am also trying to simulate an orbit and ground track for an nanosatellite of eccentricity(0.65) , inclination(19 deg), semimajor axis ( 40600km). I want know what  you consider to simulate an orbit( 2D or 3D). How to consider about rotation of earth. If you have some m-files for molniya. Thank you all.

Offline Thiha Kyaw

Re: Simulate orbit using Matlab
« Reply #48 on: 07/01/2014 11:58 am »
The following attachment M-file is to simulate the ground track of a satellite operating any orbit using Matlab. You can change classical orbital elements. These are semi-major axis (a), eccentricity (e), inclination (iota), argument of perigee (ome), right ascension of as ascending node (omega). You can also change number of period (lab) to get successive ground track per orbit.   You can run your orbit to simulate ground track of satellite. The M-file is written for an ellipse orbit to pass over Myanmar country as much duration as possible.  If you change the inputs as following, it will provide ground track of polar orbit.   a = 6878e3; e=0; iota=97.404*rad; omega=75.308*rad; ome=0*rad; lab=15.29;
« Last Edit: 07/01/2014 12:04 pm by Thiha Kyaw »

Offline Thiha Kyaw

Re: Simulate orbit using Matlab
« Reply #49 on: 07/01/2014 12:08 pm »
polar ground track simulated by my upper post

Offline Andrewwski

  • Parrothead
  • Full Member
  • ****
  • Posts: 1543
  • Buffalo, NY
  • Liked: 13
  • Likes Given: 0
Re: Simulate orbit using Matlab
« Reply #50 on: 07/02/2014 03:33 am »
Hi all friend.
 I am a student studying space system engineering at Myanmar Aerospace Engineering University. I am interested in orbit. ACE orbit is special. The line of apsides ( line between apogee and perigee)rotates 360 degree per year. I want know how this happen. If you know, share me some information or link of free book.

I assume from your description you're talking about an Apogee at Constant time of day Equatorial orbit (that's the only orbit that fits your description that ACE makes sense in).  This is just due to nodal precession.  Wikipedia explains it decently: http://en.wikipedia.org/wiki/Nodal_precession

Since you want one rotation of the line of apsides per year, you can calculate the rate of procession and plug that in, along with your other known variables, to solve for whatever you're looking for.

Quote
And I am also trying to simulate an orbit and ground track for an nanosatellite of eccentricity(0.65) , inclination(19 deg), semimajor axis ( 40600km). I want know what  you consider to simulate an orbit( 2D or 3D). How to consider about rotation of earth. If you have some m-files for molniya. Thank you all.

The code you've posted above does that.  If you want to create a ground track of a Molniya orbit, put in those parameters.

The general process would be to convert your position vector in Earth-Centered Inertial (ECI) coordinates to Earth-Centered Earth-Fixed (ECEF) coordinates.  This transformation is a function of the current time (thus accounting for the rotation of the earth).  Then, convert from ECEF to geodetic (latitude, longitude, elevation) coordinates.  MATLAB has a function for this (ecef2geodetic), although you could write your own if you want a better understanding how it works.
NEW MUSIC VIDEO:
STS-125 DREAMS in HD!

Offline Thiha Kyaw

Re: Simulate orbit using Matlab
« Reply #51 on: 07/04/2014 10:29 am »


I assume from your description you're talking about an Apogee at Constant time of day Equatorial orbit (that's the only orbit that fits your description that ACE makes sense in).  This is just due to nodal precession.  Wikipedia explains it decently: http://en.wikipedia.org/wiki/Nodal_precession

Since you want one rotation of the line of apsides per year, you can calculate the rate of procession and plug that in, along with your other known variables, to solve for whatever you're looking for.



The code you've posted above does that.  If you want to create a ground track of a Molniya orbit, put in those parameters.

The general process would be to convert your position vector in Earth-Centered Inertial (ECI) coordinates to Earth-Centered Earth-Fixed (ECEF) coordinates.  This transformation is a function of the current time (thus accounting for the rotation of the earth).  Then, convert from ECEF to geodetic (latitude, longitude, elevation) coordinates.  MATLAB has a function for this (ecef2geodetic), although you could write your own if you want a better understanding how it works.
Thank you Mr.Andrewwski. Now, I am trying to understand nodal precession  with the help of your link. And then, I am learning the build-in function (ecef2geodetic). But this is difficult to me.  If you don’t mind, and have some m-files that use the function (ecef2geodetic), please share me. Thanks for your time.

Offline Andrewwski

  • Parrothead
  • Full Member
  • ****
  • Posts: 1543
  • Buffalo, NY
  • Liked: 13
  • Likes Given: 0
Re: Simulate orbit using Matlab
« Reply #52 on: 07/07/2014 06:12 pm »
I don't have any simple files offhand, but it's really quite simple to use.  Take a look at the MATLAB documentation for the function: http://www.mathworks.com/help/map/ref/ecef2geodetic.html

The syntax for usage is:

[phi,lambda,h] = ecef2geodetic(x,y,z,ellipsoid)

The inputs x, y, and z are the coordinates in the ECEF frame.  All axes are centered at the center of the earth.  The "x" axis intersects the point where the equator and prime meridian cross, the "z" axis goes through the north pole, and the "y" axis is orthogonal to x and z to form a right-handed coordinate system (so it goes through the point where the equator intersects the 90 degree east line of longitude).

Ellipsoid is a referenceEllipsoid that defines the shape of the earth.  You can just do something like ellipsoid=referenceEllipsoid('earth'); using MATLAB's catalog - although for the purposes of this ground track you likely could just create a sphere with the radius of the earth.

The outputs phi and lambda are latitude and longitude in radians, respectively, and h is height above the surface (which isn't used in creating the ground track).

So once you have the position in ECEF coordinates, it's rather easy to plot the ground track.  The harder part will be getting your time-series of positions in ECEF coordinates.  You'll likely have them in ECI coordinates which you'll have found from some propagation of an initial state.  There's a number of sources that a quick Google search turns up on this - this one is pretty straightforward: http://earth-info.nga.mil/GandG/publications/tr8350.2/tr8350.2-a/Appendix.pdf

There's also a function in the MATLAB file exchange that I've used before and confirmed works correctly.

The m-file you posted seems to be a more generalized implementation of this by creating the latitude/longitude as theoretical rotations of the earth and projecting the current position onto this, but I'd have to dive deeper into it to figure out exactly what it's doing.  It's rather cumbersome and difficult to decipher code that isn't commented.
NEW MUSIC VIDEO:
STS-125 DREAMS in HD!

 

Advertisement NovaTech
Advertisement Northrop Grumman
Advertisement
Advertisement Margaritaville Beach Resort South Padre Island
Advertisement Brady Kenniston
Advertisement NextSpaceflight
Advertisement Nathan Barker Photography
0