Skip to content

Instantly share code, notes, and snippets.

@ridiculousfish
Created November 3, 2022 21:06
Show Gist options
  • Save ridiculousfish/af05137a4090e92de3a97a1cb01d9f2d to your computer and use it in GitHub Desktop.
Save ridiculousfish/af05137a4090e92de3a97a1cb01d9f2d to your computer and use it in GitHub Desktop.
function [force, torque]=pointMatrixGravity(array1,array2)
Synthesizing 10/10 solutions
=======
function [force, torque]=pointMatrixGravity(array1,array2)
% pointMatrixGravity - Calculates the gravitational force between a point
% mass and a matrix of masses
%
% Syntax: [force, torque]=pointMatrixGravity(array1,array2)
%
% Inputs:
% array1 - 3x1 matrix of the point mass coordinates
=======
function [force, torque]=pointMatrixGravity(array1,array2)
%pointMatrixGravity Calculates the force and torque on a point mass from a matrix of masses
% Detailed explanation goes here
G=6.67e-11;
force=zeros(3,1);
torque=zeros(3,1);
for i=1:length(array1(:,1))
r=array2-array1(i,1:3);
rMag=sqrt(r(1)^2+r(2)^2+r(3)^2);
force=force+(G*array1(i,4)/(rMag^2))*r;
torque=torque+(G*array1(i,4)/(rMag^2))*cross(r,array1(i,5:7)');
end
end
This function takes in two arrays. The first array is an array of masses and their locations and velocities. The second array is the location of the point mass. The function then calculates the force and torque on the point mass.
The next function is the main function. This function is the function that calls all of the other functions. It is called by the main simulation function.
function [t, y] = sim2()
%sim2 This function runs the simulation
% This function runs the simulation and returns the time and state vector
% arrays.
%Simulation Parameters
tSpan=0:0.001:10;
y0=[0;0;0;0;0;0;0;0;0;0;0;0];
options=odeset('RelTol',1e-6,'AbsTol',1e-9);
%Constants
G=6.67e-11;
%Masses
mass1=[0;0;0;100000000000;0;0;0];
mass2=[0;0;100000;100000000000;0;0;0];
mass3=[0;0;-100000;100000000000;0;0;0];
mass4=[100000;0;0;100000000000;0;0;0];
mass5=[-100000;0;0;100000000000;0;0;0];
mass6=[0;100000;0;100000000000;0;0;0];
mass7=[0;-100000;0;100000000000;0;0;0];
%Mass Array
massArray=[mass1';mass2
=======
function [force, torque]=pointMatrixGravity(array1,array2)
% POINTMATRIXGRAVITY - Calculates the force and torque on a point mass
% due to the gravity of a matrix of point masses
% [force, torque]=pointMatrixGravity(array1,array2)
% array1 - 3x1 array of the position of the point mass
% array2 - 3xN array of the positions of the point masses
% force - 3x1 array of the force on the point mass
% torque - 3x1 array of the torque on the point mass
=======
function [force, torque]=pointMatrixGravity(array1,array2)
%pointMatrixGravity(array1,array2)
% Detailed explanation goes here
% array1 is the array of points
% array2 is the array of masses
% array1 and array2 should be the same length
% force is the force vector
% torque is the torque vector
% This function computes the total force and torque on a point mass
% system. The force is the sum of the forces on each point mass,
% and the torque is the sum of the torques on each point mass.
% The force on each point mass is computed
=======
function [force, torque]=pointMatrixGravity(array1,array2)
%array1 is the matrix of point masses
%array2 is the matrix of the points at which the force is being calculated
%force is the force at each point
%torque is the torque at each point
%the force and torque are in the form of a matrix of the same size as array2
%example: pointMatrixGravity([1 0 0; 0 1 0; 0 0 1], [1 1 1; 1 1 1; 1 1 1])
%array1 = [1 0 0; 0 1 0; 0 0 1];
%array2 = [1 1 1; 1 1 1; 1 1 1];
%array1 = [1 0 0; 0 1 0; 0 0 1];
%array2 = [1 1 1; 1 1 1; 1 1 1];
%array1 = [1 0 0; 0 1 0; 0 0 1];
%array2 = [1 1 1; 1 1 1; 1 1 1];
%array1 = [1 0 0; 0 1 0; 0 0 1];
%array2 = [1 1 1; 1 1 1; 1 1 1];
%array1 = [1 0 0; 0 1 0; 0 0 1];
%array2 = [1 1 1; 1 1 1; 1 1 1];
%array1 = [1 0 0; 0 1 0; 0 0 1];
%array2 = [1 1 1; 1 1 1; 1 1 1];
%array1 = [1 0 0; 0 1 0; 0 0 1];
%array2 = [1 1 1; 1 1 1; 1 1 1];
%array1 = [1 0 0; 0 1 0; 0 0 1];
%array2 = [1 1 1
=======
function [force, torque]=pointMatrixGravity(array1,array2)
%function to calculate the force and torque on an array of points due to
%the gravity of a mass at a point
%array1 is the array of points
%array2 is the point of the mass
%force is the force on each point due to the mass
%torque is the torque on each point due to the mass
%force and
=======
function [force, torque]=pointMatrixGravity(array1,array2)
% This function calculates the gravity force between two point masses
% array1 is a 2xN array of the components of the position vectors of the
% point masses
% array2 is a 2xN array of the components of the masses of the point masses
% force is a 2xN array of the components of the force on each point mass
% torque is a 1xN array of the torques on each point mass
% The force is calculated as the negative of the gradient of the potential
% energy function
% The torque is calculated as the negative of the gradient of the potential
% energy function with respect to the angle of the position vector
=======
function [force, torque]=pointMatrixGravity(array1,array2)
%pointMatrixGravity.m
%This function calculates the gravitational force and torque on a set of
%points in space due to a set of point masses.
%
%Inputs:
%array1: 3 x n matrix of point masses
%array2: 3 x m matrix of points
%
%Outputs:
%force: 3 x m matrix of gravitational force on each point
%torque: 3 x m matrix of gravitational torque on each point
%
=======
function [force, torque]=pointMatrixGravity(array1,array2)
% pointMatrixGravity - computes the gravity force and torque on a point mass
% array1 is the position of the point mass in the global frame
% array2 is the array of mass and position of the point masses in the global frame
% force is the gravity force on the point mass in the global frame
% torque is the gravity torque on the point mass in the global frame
% gravity constant
g=9.81;
% number of masses
numMasses=size(array2,1);
% initialize force and torque
force=zeros(3,1);
torque=zeros(3,1);
% loop over masses
for i=1:numMasses
% mass
m=array2(i,1);
% position
r=array2(i,2:4)';
% vector from point mass to mass
v=r-array1
=======
function [force, torque]=pointMatrixGravity(array1,array2)
% pointMatrixGravity calculates the gravitational force and torque on a
% point mass due to a matrix of masses. The mass matrix is assumed to be
% in the x-y plane.
%
% The function takes two arguments:
% array1 is a 3 x N matrix of the masses of the matrix of masses
% array2 is a 3 x N matrix of the coordinates of the matrix of masses
%
% The function returns two arguments:
% force is a 3 x 1 matrix of the gravitational force on the point mass
% torque is a 3 x 1 matrix of the gravitational torque on the point mass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment