I need help writing this function in MATLAB: Function Name: spaceTime Inputs: 1.
ID: 3876143 • Letter: I
Question
I need help writing this function in MATLAB:
Function Name: spaceTime Inputs: 1. (double) Number of Earth years 2. (double) Semi-major axis of the other planet's orbit (in m) 3. (double) Mass of the other planet (in kg) Outputs 1. (double) Number of years on the other planet Background While watching the movie The Martian, you begin to wonder about how years on other planets relate to years on Earth. You find yourself curious about how time would pass differently on Mars, and on other planets. Since a year on Earth is how long it takes the Earth to complete an orbit around the sun, how would this compare to a year on Mars, or how long it would take Mars to make a complete orbit around the sun? Since you're an engineer, you decide to generalize this idea to work for any number of Earth years and any other planet, given readily available orbital and physical data. Function Description: Newton's law of gravitation gives the orbital period T of a body of mass Mi around another body of mass M with semi-major axis of orbit a as T=2 G (M M2) where G = 6.674 × 10-11 m3 kg-1 s-2 (the gravitational constant), a is in meters, T is in seconds. and M1 and M2 are both in kilograms. Notes: A year is the orbital period of a planet, or the time it takes for the planet to make a complete orbit around the Sun. The semi-major axis of a planet's (elliptical) orbit is half the longest diameter of its orbit. The Sun's mass is 1.989 x 1030 kg. All planets given for this function will be within the Solar System, so none will be more . massive than the Sun. Round the output to three decimal places. Hints All these worlds are ours, but there is one on which we should attempt no landing... try using the parameters of that world as the inputs to the solution function.Explanation / Answer
Save the function in a file named spaceTime.m
function planetYears = spaceTime(earthYears, a, M1)
M2 = 1.989E30; #mass of Sun
G = 6.674E-11; #gravitational constant
term = a^3/(G*(M1+M2));
T = 2*pi*sqrt(term); #in seconds
secsInAYear = 365*24*60*60;
T = T/secsInAYear; #convert to years
planetYears = earthYears * T;
end
Now in another script file/ command window , copy and run the following code
AU = 149597871000; # 1 astronomical unit in meters
MarsAxis = 1.524 * AU; # semi major axis of mars
MarsMass = 0.642E24; #mass in kg
spaceTime(1, MarsAxis, MarsMass) #calculate how many years is it on mars for 1 year on earth
output
ans = 1.8825