Write a script that computes the force of gravity between two objects given their masses and the distance between them.

One pound on Earth equals 4.4537 Newton's of force on Earth Script assumes that mass1, mass2, and distance already exist in the workspace The script should store the results of the computations in the variables gravitationalForceNewtons and gravitationalForceLbs

Answers (1)

Here's a start
function force = ComputeGravitationalForce(m1, m2, distance)
force = G * m1 * m2 / .............
You finish. You can insert a second line and finish the last line. Actually, it's extremely trivial.

7 Comments

Could you please elaborate on that? I'm very new at this and I'm still having trouble with it....
This is what I have so far, but every time I try to run it, it says there's an error on the second line...
function force= GravitationalForce(mass1, mass2, distance)
GravitationalForce= gravitationalConstant * ((mass1 * mass2)/distance)
mass1= 5.25
mass2= 16.7
distance= 3*10^-15
end
You cannot run the code by click on Run or using Run from a menu entry. You need to go down to the command line and type it, passing in the parameters you need. For example,
GravitationalForce(5.25, 16.7, 3*10^-15)
You can get rid of the assignments to mass1, mass2, and distance that you have in the code.
One thing to keep in mind is that in the great majority of programming languages, the language is "procedural". Procedural programming languages need to be understood as a sequence of steps to be executed one at a time in order. So if you have not defined a value for mass1 at a particular point, then you cannot use mass1 there: first you need to define the value and then you can use the value.
There are some "non-procedural" programming languages in which it would be valid to define an equation of state and to define particular conditions on it, with you able to mix the statement order. But you probably will never use one of those programming languages.
mass1 mass2, and distance are supposed to be passed in, so you can't set them inside the function. You need to pass them in. You can have one m-file called something like test.m and have both functions in it but don't assign mass1, mass2, and distance in the GravitationalForce function but in the main function:
function test()
mass1= 5.25
mass2= 16.7
distance= 3*10^-15
force = GravitationalForce(mass1, mass2, distance)
end % of test()
function force = GravitationalForce(mass1, mass2, distance)
gravitationalConstant = whatever.....
force = gravitationalConstant * mass1 * mass2/distance;
end % of GravitationalForce
Make sure your units are correct, like all SI. Your distance seems awfully small, unless you're computing the gravity between quarks or something.
Just out of curiosity where did you get the numbers for mass1, mass2, and distance from?
We pulled the numbers from Rosemary's code. They are arbitrary test values.
When you are calculating for real situations make sure you get the units right. For example are the masses in grams, kilograms, or pounds? Are the distances in kilometres, metres, centimetres, miles, inches?

Sign in to comment.

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!