CSCE 155N Fall 2012 Homework Assignment 4: Gravitational Force

CSCE 155N
Fall 2012
Homework Assignment 4: Gravitational Force between Planets
Assigned: October 18, 2012
Due: November 6. 2012
Note: This assignment is to be completed individually - collaboration is strictly prohibited.
Points: 100 points
Memo To: The programmer
Memo From: Your boss
Objectives
The objectives of this homework assignment:
1. Master the use of standard I/O in Matlab.
2. Master the use of control statements in Matlab.
3. Master the use of File Input/Output (I/O) in Matlab.
4. Mater the use of functions in Matlab.
5. Master with the use of cell arrays in Matlab.
6. Mater with the concept of control statements (selection and
loop) in solution design.
7. Be exposed to the use of structures.
8. Appreciate and understand the need of functions and subfunctions.
9. Be exposed to the concept of problem decomposition and
the design-implementation-test cycle.
10. Appreciate and understand the application of computational
thinking in solving engineering problems
Physics and astronomy
department want us to
develop a program that helps
them with their astronomical
calculations.
The program mainly computes
the gravitational force
between different objects in
our solar system (i.e. the sun, 8
planets, and 1 dwarf planet). I
am sorry to note that there are
only 8 official planets now … 8(
…
Have fun!
Your boss
Problem Description
Fact: In 2006, the planet Pluto was downgraded to a dwarf planet from being the ninth planet in
our solar system.
This is really an interesting fact. However, a more interesting one is that, in 1680s, Newton
formulated what is so called Newton's law of universal gravitation. This law simply states that
every pair of masses in the universe has an attraction force in between that is proportional to
masses of the pair and inversely proportional to the square of the distance between them [see
Figure 1]. This law can be applied for masses as small as electrons in an atom or for giant objects
like the planets in the space.
In mathematical form, the Newton's law of universal
gravitation expresses the force
between two masses
and
by:
Figure 1: Gravitational force between
two masses M1 and M2
1
where:
is the force between the two masses (note that
) measured in newtons (N)
is the gravitational constant which is approximately equal to
are the masses of the first and the second objects, respectively, in kilograms (kg)
is the distance between the centers of the masses in meters.
Example:
Given the following information about the moon and earth:
Mass of the Earth
Mass of the Moon
The average 1 distance from the center of earth to the center of the moon is
What is the gravitational force between the earth and the moon? By substituting directly in
Equation 1, we get:
Using the similar approach, we can find the gravitational force between any two planets, or any
planet and the sun.
The following table shows some of the parameters of the Sun and each planet in our solar
system.
Planet
Sun
Mercury
Venus
Earth
Mars
Jupiter
Saturn
Uranus
Neptune
Pluto (a dwarf planet)
Mass (1024 kg)
1.989*106
0.330
4.87
5.97
0.642
1899
568
86.8
102
0.0125
Diameter (km)
1390000
4879
12104
12756
6792
142984
120536
51118
49528
2390
Distance from the Sun (AU)
0.000
0.390
0.723
1.000
1.523
5.203
9.539
19.180
30.060
39.530
Distances in the solar system are very large. Therefore using miles or kilometers to express the
distances is not practical because it will need more analysis to interpret and compare planet's
information. As a result, in astronomy, astronomical unit (AU) is usually used to represent the
distances. AU represents the distances normalized with respect to the Earth's distance from the
sun; hence, the distance of the earth from the sun is 1 AU. In other words, we know that the
average distance from the earth to the sun is approximately 149,600,000 km, hence:
1
Orbits of planets and moons are not perfectly circular, they are elliptical instead, so there is a point in which the
moon is closest to the earth, and a point furthest from the earth. Hence we use the average distance. This also applies
to planets around the sun.
2
If we know that the average distance from the sun to Mars is
of Mars in AU units is:
, then the distance
The physics and astronomy department asked you to write a program that helps the students
easily find the information, and gravitational forces between different planets and sun in our
solar system. Your tasks are as follows:
1. Load the file "SolarSystem.dat" which is a cell array that contains the table shown above.
Note: We will provide the input file on the course website at:
http://www.cse.unl.edu/~lksoh/Classes/CSCE155N_Fall12/homeworks.html
2. Create and arrange the data given in the cell array into a vector of structures called "Solar".
Each object in this structure has the following fields: (a) Name, (b) Mass, (c) Diameter, and
(d) Distance from the Sun. For example: Solar(1).Name = ‘Sun’, Solar(1).Mass =
1.989*1030, Solar(1).Diameter = 1390000*103 , and Solar(1).Distance = 0.
3. Create a function (Gravitational_Force_Fields.m) that computes the gravitational force
between any two objects using Equation 1. The input arguments of this function should be
the two masses (in kilograms), and the distance between the two objects (in meters). The
output argument should be the gravitational force (in newtons).
a. Create another function similar to the above (Gravitational_Force_Struct.m) that returns
the same gravitational force, but with the two input arguments being two Solar structures.
b. Indicate in your README file which function is a better solution and provide reasons.
4. You will have to also create a sub-function that will convert the distance from the AU to
meters (using Equation 2), because the distance used in the function you created in Step 3
should be in meters.
Note: You can't make this conversion as part of the function in Step 3, because this is a
conversion that is specifically used in astronomy, on the other hand, the function you created
in Step 3 can be used with different applications.
5. In your main program (that will make use of the above functions), the user should be
prompted for a pair of objects in the solar system which s/he wishes to compute the
gravitational force in between.
6. Now, it might happen that the user enters a wrong name of the object or does not know the
full correct name of it. You can create a smarter program that can predict what the user want
to input. In this case, for the sake of simplicity, we will assume that the user always input the
first three letters of the object's name correctly. (This means that the user will never input the
"Sun" incorrectly!) Your program should be able to handle cases like these in the following
examples.
a. Assume that the user enters "Earht". The only other object that starts with letter "E" is the
"Earth".
b. Assume that the user enters "Satrn", in this case, we will need to compare more than the
first letter, because we have Saturn and Sun.
c. Similar to the 2nd case, we can have ambiguity between Mercury and Mars.
3
One way to handle this, is that we guess the name based on the first one, two or three letters
in the input (i.e., comparing the first few letters to the list of objects we have), and then we
can ask the user if the user meant to input the name that our program guessed. (Important
Note: As a programmer, you should predict the different mistakes that a user can make while
inputting the information required from him. The above is one of them; the more errors your
program can handle, the more points you will receive.)
7. Your program should do the following:
a. Display the information of each object, distance between these two objects, and the
gravitational force between these two objects.
b. Save these displayed data to an output file that carries the name of the two objects.
A sample run of the required program is shown below.
Sample Output 1 (PGCalc)
Input:
Welcome to PGCalc:
Please enter the name of the first object:
Earht
Did you mean "Earth"?
1
[1- Yes
2- No]
Please enter the name of the second object:
Sun
Output:
The Mass of the Sun is: 1.989*1030 kg
The Diameter of the Sun is: 1390000 km
The Mass of the Earth is: 5.97*1024
The Diameter of the Earth is: 12756 km
The distance from the center of the Sun and the center of the Earth is:
149,600,000 km
The Gravitational force between the Sun and the Earth is: 3.5410*1022
The output data is saved in the file: EarthSun.dat
Thank you for using PGCalc.
Here the user input the first three letters correctly, and your program did guess the name of the
object correctly. However, the user used the question as a second chance to change the his/her
mind and entered a completely different input.
Sample Output 2 (PGCalc)
Input:
Welcome to PGCalc:
Please enter the name of the first object:
Earht
Did you mean "Earth"?
[1- Yes
2- No]
4
2
Please enter the name of the first object:
Earth
Please enter the name of the second object:
Sun
Output:
The Mass of the Sun is: 1.989*1030 kg
The Diameter of the Sun is: 1390000 km
The Mass of the Earth is: 5.97*1024
The Diameter of the Earth is: 12756 km
The distance from the center of the Sun and the center of the Earth is:
149,600,000 km
The Gravitational force between the Sun and the Earth is: 3.5410*1022
The output data is saved in the file: EarthSun.dat
Thank you for using PGCalc.
Challenge - Extra Credit (10 points)
Astronomers might be interested in a single planet, and the relation between this planet and all
other objects in the solar system. The way the data is presented affects the speed and clarity of
interpretation the program's user need to understand these relations. For example, sorting the data
in an ascending or descending way can help the user to easily interpret which object in the solar
system has the highest effect and which one is the least affective. In this part you are asked to
create a program that performs the following functions:
1. Prompt the user to enter the name of the object of interest.
2. Using the function (Gravitational_Force_Fields.m), calculate the gravitational force between
the object entered in Step 1, and all other objects in the solar system. Store these values in a
vector that carries the name of the planet entered in Step 1.
3. Sort the gravitational forces stored in the vector you created in Step 2 in a descending order.
You can perform sorting using any sorting algorithm. However you need to explain in detail
your sorting algorithm used in the README.
Important Note: You are not allowed to use any built-in sorting function in Matlab. You will
need to implement your own sorting algorithm. Also, you need to clearly explain it for credit in
this part.
A sample run of the required program is shown below.
Sample Output 1 (Sorting_ PGCalc)
Input:
Welcome to Sorting_PGCalc:
Please enter the name of the object of interest:
Venus
5
Output:
The gravitational forces between Venus and all other objects in the solar
system (in a descending order) are:
Object's Name
'Sun'
'Jupiter'
'Saturn'
'Earth'
'Uranus'
'Neptune'
'Mercury'
'Mars'
'Pluto'
Gravitational Force
[5.9770e+033]
[9.2094e+029]
[1.3998e+029]
[4.6825e+028]
[1.0217e+028]
[7.5538e+027]
[2.1530e+027]
[1.7435e+027]
[6.9982e+023]
Thank you for using Sorting_PGCalc
Submission Procedure
This assignment is due at the start of class (2:00 PM). Assignments five minutes late will
NOT be accepted. It is highly recommended that you read the grading policy and grading
guidelines on the course website for a complete explanation of how the assignments will be
graded. Remember, your program should follow a good programming style, include plenty of
comments – both inline documentation and Matlabdoc documentation, and perform all of the
functionality outlined above. Also, in the welcome message of your program, state whether
you are implementing the extra credit functionality or not.
There are two submission steps:
(1) You must “handin” the following files on-line:

Source files: PGCalc.m , Gravitational_Force_Fields.m, Gravitional_Force_Structs.m,
Sorting_PGCalc.m (if you decide to do the extra credit part), and other m-files and m-file
functions


Readme file: README
Testing file: TEST (Also, if you complete the Challenge, please submit your input text
file, TEST_Challenge.)
 A word documentation "PG.doc" that shows how did you think of different errors that a
user can perform and how does your program handle these errors.
(2) You must submit a stapled paper copy of a coversheet, your source files, the README file,
documentation and the TEST file.
Both of these steps must be done by the start of the class on the day the assignment is due.
Please download this coversheet from the instructor’s course website, under the Homework
Assignments link. This coversheet allows the grader to give comments and categorize the points
for your homework.
6