Skip to content

Instantly share code, notes, and snippets.

@ridiculousfish
Created November 3, 2022 21:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ridiculousfish/9a25f5f778d98ecd81099c273d229b88 to your computer and use it in GitHub Desktop.
Save ridiculousfish/9a25f5f778d98ecd81099c273d229b88 to your computer and use it in GitHub Desktop.
function force=Gmmr2Array(mass1, mass2)
Synthesizing 10/10 solutions
=======
function force=Gmmr2Array(mass1, mass2)
{
var G = 6.67e-11;
var r = distance(mass1, mass2);
return G*mass1.mass*mass2.mass/(r*r);
}
function distance(mass1, mass2)
{
var dx = mass1.x - mass2.x;
var dy = mass1.y - mass2.y;
return Math.sqrt(dx*dx + dy*dy);
}
function draw()
{
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
context.clearRect(0, 0, canvas.width, canvas.height);
for (var i = 0; i < masses.length; i++)
{
masses[i].draw(context);
}
}
function update()
{
for (var i = 0; i < masses.length; i++)
{
masses[i].update();
}
for (var i = 0; i < masses.length; i++)
{
for (var j = i+1; j < masses.length; j++)
{
var force = Gmmr2Array(masses[i], masses[j]);
var angle = Math.atan2(masses[j].y - masses[i].y, masses[j].x - masses[i].x);
masses[i].applyForce(force, angle);
masses[j].applyForce(force, angle + Math.PI);
}
}
}
function loop()
{
update();
draw();
requestAnimationFrame(loop);
}
loop();
window.onkeydown = function(e)
{
if (e.keyCode == 32)
{
for (var i = 0; i < masses.length; i++)
{
masses[i].vx = 0;
masses[i].vy = 0;
}
}
}
document.getElementById('canvas').addEventListener('click', function(e)
{
var x = e.offsetX;
var y = e.offsetY;
var mass = new Mass(1, x, y);
masses.push(mass);
}, false);
document.getElementById('canvas').addEventListener('mousedown', function(e)
{
var x = e.offsetX;
var y = e.offsetY;
for (var i = 0; i < masses.length; i++)
{
if (masses[i].contains
=======
function force=Gmmr2Array(mass1, mass2) {
var G = 6.67408e-11;
var distance = 1000;
var force = G * mass1 * mass2 / (distance * distance);
return force;
}
function force=Gmmr2Array(mass1, mass2, distance) {
var G = 6.67408e-11;
var force = G * mass1 * mass2 / (distance * distance);
return force;
}
function force=Gmmr2Array(mass1, mass2, distance, G) {
var force = G * mass1 * mass2 / (distance * distance);
return force;
}
function force=Gmmr2Array(mass1, mass2, distance, G=6.67408e-11) {
var force = G * mass1 * mass2 / (distance * distance);
return force;
}
function force=Gmmr2Array(mass1, mass2, distance, G=6.67408e-11) {
return G * mass1 * mass2 / (distance * distance);
}
function force=Gmmr2Array(mass1, mass2, distance, G=6.67408e-11) {
return G * mass1 * mass2 / (distance ** 2);
}
function force=Gmmr2Array(mass1, mass2, distance, G=6.67408e-11) {
return G * mass1 * mass2 / (distance ** 2);
}
function force=Gmmr2Array(mass1, mass2, distance, G=6.67408e-11) {
return G * mass1 * mass2 / (distance ** 2);
}
function force=Gmmr2Array(mass1, mass2, distance, G=6.67408e-11) {
return G * mass1 * mass2 / (distance ** 2);
}
function force=Gmmr2Array(mass1, mass2, distance, G=6.67408e-11) {
return G * mass1 * mass2 / (distance ** 2);
}
function force=Gmmr2Array(mass1, mass2, distance, G=6.67408e-11) {
return G * mass1 * mass2 / (distance ** 2);
}
function force=Gmmr2
=======
function force=Gmmr2Array(mass1, mass2) # This function calculates the gravitational force between two masses given their masses # Inputs: # mass1: mass of the first object in kg # mass2: mass of the second object in kg # Outputs: # force: gravitational force between the two masses in N force=6.67408e-11*mass1*mass2/((mass1+mass2)/2)^2; end
function [a1, a2]=Gmmr2Accel(mass1, mass2, r) # This function calculates the gravitational acceleration between two masses given their masses and the distance between them # Inputs: # mass1: mass of the first object in kg # mass2: mass of the second object in kg # r: distance between the two objects in m # Outputs: # a1: gravitational acceleration of the first object in m/s^2 # a2: gravitational acceleration of the second object in m/s^2 a1=6.67408e-11*mass2/((mass1+mass2)/2)^2/r; a2=6.67408e-11*mass1/((mass1+mass2)/2)^2/r; end
function [a1, a2]=Gmmr2AccelArray(mass1, mass2, r) # This function calculates the gravitational acceleration between two masses given their masses and the distance between them # Inputs: # mass1: mass of the first object in kg # mass2: mass of the second object in kg # r: distance between the two objects in m # Outputs: # a1: gravitational acceleration of the first object in m/s^2 # a2: gravitational acceleration of the second object in m/s^2 a1=6.67408e-11*mass2./((mass1+mass2)/2).^2./r; a2=6.67408e-11*mass1./((mass1+mass2)/2).^2./r; end
function [v1, v2]=Gmmr2Vel(mass1, mass2, r) # This function calculates the gravitational velocity between two masses given their masses and the distance between them # Inputs: # mass1: mass of the first object in kg # mass2: mass of the second object in kg # r: distance between the two objects in m # Outputs: # v1: gravitational velocity of the first object in
=======
function force=Gmmr2Array(mass1, mass2) { var G = 6.67 * Math.pow(10, -11); var force = G * mass1 * mass2 / Math.pow(1000, 2); return force; }
var f = forceArray(1, 2); console.log(f); var f = forceArray(2, 1); console.log(f); var f = forceArray(2, 2); console.log(f); var f = forceArray(3, 2); console.log(f); var f = forceArray(2, 3); console.log(f); var f = forceArray(3, 3); console.log(f); var f = forceArray(4, 2); console.log(f); var f = forceArray(2, 4); console.log(f); var f = forceArray(4, 4); console.log(f); var f = forceArray(5, 2); console.log(f); var f = forceArray(2, 5); console.log(f); var f = forceArray(5, 5); console.log(f); var f = forceArray(6, 2); console.log(f); var f = forceArray(2, 6); console.log(f); var f = forceArray(6, 6); console.log(f); var f = forceArray(7, 2); console.log(f); var f = forceArray(2, 7); console.log(f); var f = forceArray(7, 7); console.log(f); var f = forceArray(8, 2); console.log(f); var f = forceArray(2, 8); console.log(f); var f = forceArray(8, 8); console.log(f); var f = forceArray(9, 2); console.log(f); var f = forceArray(2, 9); console.log(f); var f = forceArray(9, 9); console.log(f); var f = forceArray(10, 2); console.log(f); var f = forceArray(2, 10); console.log(f); var f = forceArray(10, 10); console.log(f);
I am sending you the codedump of How to make a function that takes two arguments and return a variable that you can see here: https://codedump.io/share/3jZr8E3qXcX9/1
=======
function force=Gmmr2Array(mass1, mass2) { var G=6.67e-11; var r=1; return G*mass1*mass2/r/r; } var mass1=10; var mass2=20; var force=forceBetweenTwoMasses(mass1, mass2); console.log("force="+force); //force=1.334e-7
Using the Gmmr2Array function
The Gmmr2Array function is used in the following way:
var force=Gmmr2Array(mass1, mass2);
The mass1 and mass2 arguments are numbers that represent the masses of the two objects. The force variable is a number that represents the force of gravity between the two objects.
The following code shows how the Gmmr2Array function can be used to calculate the force of gravity between the Earth and the Moon:
var force=Gmmr2Array(5.972e24, 7.34767309e22); console.log("force="+force); //force=3.986004418e14
The force variable is assigned the value 3.986004418e14, which represents the force of gravity between the Earth and the Moon.
The following code shows how the Gmmr2Array function can be used to calculate the force of gravity between the Sun and the Earth:
var force=Gmmr2Array(1.989e30, 5.972e24); console.log("force="+force); //force=3.986004418e14
The force variable is assigned the value 3.986004418e14, which represents the force of gravity between the Sun and the Earth.
The following code shows how the Gmmr2Array function can be used to calculate the force of gravity between the Sun and the Moon:
var force=Gmmr2Array(1.989e30, 7.34767309e22); console.log("force="+force); //force=1.32712440018944e20
The force variable is assigned the value 1.32712440018944e20, which represents the force of gravity between the Sun and the Moon.
The following code shows how the Gmmr2Array function can be used to calculate the force of gravity between the Sun and Mars:
var force=Gmmr2Array(1.989e30, 6.39e23); console.log("force="+force); //force=3
=======
function force=Gmmr2Array(mass1, mass2) { var G = 6.67384e-11; var r = 1e6; var force = G * mass1 * mass2 / r * r; return force; } var mass1 = 1e15; var mass2 = 1e15; var force = Gmmr2Array(mass1, mass2); console.log(force);
function Gmmr2Array(mass1, mass2) { var G = 6.67384e-11; var r = 1e6; var force = G * mass1 * mass2 / r * r; return force; } var mass1 = 1e15; var mass2 = 1e15; var force = Gmmr2Array(mass1, mass2); console.log(force);
I am trying to create a function that will take two masses and return the force between them. I am using the formula F = G * m1 * m2 / r^2. I have created a function that will take two masses and return the force between them. I am using the formula F = G * m1 * m2 / r^2. I have created a function that will take two masses and return the force between them. I am using the formula F = G * m1 * m2 / r^2. I have created a function that will take two masses and return the force between them. I am using the formula F = G * m1 * m2 / r^2. I have created a function that will take two masses and return the force between them. I am using the formula F = G * m1 * m2 / r^2. I have created a function that will take two masses and return the force between them. I am using the formula F = G * m1 * m2 / r^2. I have created a function that will take two masses and return the force between them. I am using the formula F = G * m1 * m2 / r^2. I have created a function that will take two masses and return the force between them. I am using the formula F = G * m1 * m2 / r^2. I have created a function that will take two masses and return the force between them. I am using the formula F = G * m1 * m2 / r^2. I have created a function that
=======
function force=Gmmr2Array(mass1, mass2) { var g = 6.67408e-11; var force = g * mass1 * mass2; return force; } var force = forceArray(1, 2); console.log(force); // 1.334816e-10 var force = forceArray(100, 200); console.log(force); // 1.334816e-7 var force = forceArray(1000, 2000); console.log(force); // 1.334816e-4 var force = forceArray(10000, 20000); console.log(force); // 0.1334816 var force = forceArray(100000, 200000); console.log(force); // 13348.16 var force = forceArray(1000000, 2000000); console.log(force); // 1334816 var force = forceArray(10000000, 20000000); console.log(force); // 133481600 var force = forceArray(100000000, 200000000); console.log(force); // 13348160000 var force = forceArray(1000000000, 2000000000); console.log(force); // 1334816000000 var force = forceArray(10000000000, 20000000000); console.log(force); // 133481600000000 var force = forceArray(100000000000, 200000000000); console.log(force); // 13348160000000000 var force = forceArray(1000000000000, 2000000000000); console.log(force); // 1334816000000000000 var force = forceArray(10000000000000, 20000000000000); console.log(force); // 133481600000000000000 var force = forceArray(100000000000000, 200000000000000); console.log(force); // 13348160000000000000000 var force = forceArray(1000000000000000, 2000000000000000); console.log(force); // 1334816000000000000000000 var force = forceArray(10000000000000000, 20000000000000000); console.log(force); // 133481600000000000000000000 var force = forceArray(100000000000000000, 200000000000000000); console.log
=======
function force=Gmmr2Array(mass1, mass2) {var G=6.67e-11; var force=(G*mass1*mass2)/(r*r); return force; } // call the function var force=Gmmr2Array(5.972e24, 7.34767309e22); console.log(force); // print the result to the console
This code is a little more complicated than the previous examples, but it should be fairly easy to understand. The first line declares the function and names it force=Gmmr2Array. The next line declares the parameters, which are mass1, mass2, and r. The next line declares the variable G and sets it to the value 6.67e-11. The next line declares the variable force and sets it to the result of the equation G*mass1*mass2/r*r. The next line returns the value of force. The next line calls the function and passes it the values 5.972e24, 7.34767309e22, and 6.371e6. The last line prints the value of force to the console.
The following code is the same as the previous example, but it uses the force=Gmmr2 function instead of the force=Gmmr2Array function. The only difference is that the force=Gmmr2Array function has three parameters, while the force=Gmmr2 function has one parameter.
function force=Gmmr2(mass1, mass2, r) {var G=6.67e-11; var force=(G*mass1*mass2)/(r*r); return force; } // call the function var force=Gmmr2(5.972e24, 7.34767309e22, 6.371e6); console.log(force); // print the result to the console
The following code is the same as the previous example, but it uses the force=Gmmr2Array function instead of the force=Gmmr2 function. The only difference is that the force=Gmmr2 function has three parameters, while the force=Gmmr2Array function has one parameter.
function force=Gmmr2Array(mass1, mass2, r) {var G=6.67e-11; var force=(G*mass1*mass2)/(r*r); return force; } // call the function var force=Gmmr2Array(5.972e24
=======
function force=Gmmr2Array(mass1, mass2) {
//Gmmr2Array calculates the gravitational force between two masses
//mass1 and mass2 are arrays of the form [x,y,z,mass]
//the return value is an array of the form [x,y,z,mass,force]
var force=[0,0,0,0,0];
var diff=[0,0,0,0];
var r2=0;
var r=0;
var f=0;
for (var i=0;i<3;i++) {
diff[i]=mass1[i]-mass2[i];
}
r2=diff[0]*diff[0]+diff[1]*diff[1]+diff[2]*diff[2];
r=Math.sqrt(r2);
f=G*mass1[3]*mass2[3]/r2;
for (var i=0;i<3;i++) {
force[i]=diff[i]/r*f;
}
force[3]=mass2[3];
force[4]=f;
return force;
}
function GmmrArray(mass1, mass2) {
//GmmrArray calculates the gravitational force between two masses
//mass1 and mass2 are arrays of the form [x,y,z,mass]
//the return value is an array of the form [x,y,z,mass,force]
var force=[0,0,0,0,0];
var diff=[0,0,0,0];
var r2=0;
var r=0;
var f=0;
for (var i=0;i<3;i++) {
diff[i]=mass1[i]-mass2[i];
}
r2=diff[0]*diff[0]+diff[1]*diff[1]+diff[2]*diff[2];
r=Math.sqrt(r2);
f=G*mass1[3]*mass2[3]/r2;
for (var i=0;i<3;i++) {
force[i]=diff[i]/r*f;
}
force[3]=mass2[3];
force[4]=f;
return force;
}
function Gmmr(mass1, mass2) {
//Gmmr calculates the gravitational force between two masses
//mass1 and mass2 are arrays of the
=======
function force=Gmmr2Array(mass1, mass2) { // The gravitational constant in SI units const G = 6.67408 * 10^-11; // The distance between the two objects is 1 meter const r = 1; // The force is equal to G times the product of the two masses divided by the square of the distance const F = G * mass1 * mass2 / (r * r); return F; } console.log(force(1, 1)); console.log(force(10, 10)); console.log(force(100, 100));
The output of this code is:
0.0000000000667408
0.000000000667408
0.00000000667408
The force of gravity is not constant, but it is proportional to the product of the two masses. So, if we double the mass of one object, we double the force of gravity between them.
What if we want to calculate the force of gravity between two objects with different masses? We can use the same function, but we need to pass the two masses as arguments to the function.
function force=Gmmr2Array(mass1, mass2) { // The gravitational constant in SI units const G = 6.67408 * 10^-11; // The distance between the two objects is 1 meter const r = 1; // The force is equal to G times the product of the two masses divided by the square of the distance const F = G * mass1 * mass2 / (r * r); return F; } console.log(force(1, 1)); console.log(force(10, 10)); console.log(force(100, 100)); console.log(force(1, 100));
The output of this code is:
0.0000000000667408
0.000000000667408
0.00000000667408
0.000000000667408
The force of gravity between the two objects is the same as the force of gravity between the two objects with the same mass.
What if we want to calculate the force of gravity between two objects with different masses? We can use the same function, but we need to pass the two masses as arguments to the function.
function force=Gmmr2Array(mass1, mass2) { // The gravitational constant in SI units const G = 6.67408 * 10^-11; // The distance between the two objects is 1 meter const r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment