Center of mass problem

Imagine a matrix as if you're looking at a ship from above.
I have a ship and I want to store 21 containers on it, as close as possible to its center of mass.
The center of mass of the ship is (2,3) (line 2 and column 3)
The 21 containers and respective Kg's are:
containers=[100,70,30,70,10,10,100,30,70,100,10,30,30,70,100,10,70,30,70,10,10]
What code do I use in order to generate a matrix (3x7) where the cointainers are distributed in a way that the center of mass remains as close as possible to the original one (2,3).
Thanks

10 Comments

What do you have so far to start this problem? I'm sure we could write a code for you, but it will be easier for you to understand and adapt as needed if you tell us what you're thinking.
Hi Bob, this is what i've done so far:
clc
clear all
cargovector=randi(5,1,21);
cargovector(cargovector==1)=10;
cargovector(cargovector==2)=30;
cargovector(cargovector==3)=70;
cargovector(cargovector==4)=100;
cargomatrix=vec2mat(cargovector,7);
next step would be reorganize the containers as I explained.. but i'm really struggling to do it
Can you describe to me how you would like to solve this without code?
Also, are you just using the code to double check something you already know, or do you need to consider all possible configurations of the crates?
The center of mass of the empty ship is (2,3)
Now I have to add the containers in a way that the new center of mass remains as close as possible to (2,3)
Neither double check or consider all possible configurations. I need to find the best configuration
Thank you for you help
I would probably start by determining the 'CG' of the configuration based on a weighted average of the containers. Once you have that you can do a distance calculation to find how far you are from the ideal center.
As for placement you know you want to have even distribution left and right, as well as forward and back. They don't need to be dependent on each other, so you can write conditional checks to look for each value separately. A good starting place is to find the total weight, and then divide by two, for front and aft, and three, for side to side. This will give you an idea about how much weight you should expect to have to keep it balanced.
Understood everything except the last part where
you begin with “A good starting place..”
Would apricciate if you could explain it better
Thank you very much for your help
If you calculate the center of gravity between two equal mass objects then the CG will be halfway between them. Using this idea, if you want to have a CG in a specific location, and you have two equal size objects, you know that you need to place them in a line, each with equal distance to the CM.
Since you have a specific location you're trying to place the CG you can begin relatively similarly. Sum up all of your mass, 1030, and then divide it by two, 515, then you know that you want to get as close as you can to having 515 in front of the CG, and half in back. The real problem is more complicated than that because you have differing distances and placement that is directly in line with the ideal CG, but it's a place to start.
Similarly, for the side to side CG, you have three rows. so divide the total by three, ~343, and lay out the containers so that each row has approximately that much weight. If one of the rows has more, it could be best if it is the center one, because that one is in line with the ideal CG, and will not throw your CG off laterally.
Please, keep in mind that this is very much an engineering method of thinking, and will not truly consider ALL possible options, just allow you to think logically about how to place the containers in a reasonably efficient way.
The closest I got was having a moment arm of 10 about the ideal CG.
Thank you very much Bob
Image Analyst
Image Analyst on 30 Nov 2018
Edited: Image Analyst on 30 Nov 2018
What is the size and shape of the objects? Obviously if they're dense circular lead rods then they can be packed closer to the center than if they were rectangular mattresses of the same weight.

Sign in to comment.

Answers (1)

Jim Riggs
Jim Riggs on 29 Nov 2018
Edited: Jim Riggs on 30 Nov 2018

0 votes

I wrote a function to compute the CG in X and Y. Then I used this function to find a solution by inspection (i.e. I manualy adjusted the positions until I found an answer )
function [Xcg, Ycg] = moment(D);
[row,col]=size(D);
Wtot = sum(sum(D)); % total weight of all containers
Xsum=0;
Ysum=0;
for i=1:row
for j=1:col
Xmom = D(i,j)*j; % X moment of container i,j
Ymom = D(i,j)*i; % Y moment of container i,j
Xsum = Xsum + Xmom; % total X moment
Ysum = Ysum + Ymom; % total Y moment
end
end
Xcg = Xmom/Wtot;
Ycg = Ysum/Wtot;
end
I use this function to verify that the following matrix has a CG at 3,2: (column 3, row 2):
D = [100, 70, 70, 10, 30, 10, 30;...
100, 100, 30, 70, 10, 10, 70;...
100, 70, 70, 10, 30, 10, 30]

6 Comments

Jim Riggs
Jim Riggs on 29 Nov 2018
Edited: Jim Riggs on 29 Nov 2018
Just for fun, I added a calculation of the second moment of mass about the CG, to compute the moment of inertia of the matrix of containers.
IX = 0;
IY = 0;
for i=1:col
for j=1:row
IX = IX + D(i,j)*(j-Xcg)^2;
IY = IY + D(i,j)*(i-Ycg)^2;
end
end
I found three solutions (i'm sure that there are more):
D1 = [100, 70, 70, 10, 30, 10, 30;...
100, 100, 30, 70, 10, 10, 70;...
100, 70, 70, 10, 30, 10, 30]
D1 has a CG at X=3, , Y=2, and MOI of IX = 4160, IY = 640
D2 = [ 70, 100, 70, 30, 30, 70, 10;...
100, 100, 30, 10, 10, 10, 10;...
70, 100, 70, 30, 30, 70, 10]
D2 has CG at X=3, Y=2, and IX=3440, IY=760
D3 = [ 70, 100, 70, 30, 10, 30, 10;...
30, 100, 100, 70, 70, 10, 10;...
70, 100, 70, 30, 10, 30, 10]
D3 has CG at X=3, Y=2, and IX=2580, IY = 640
Hugo Matias
Hugo Matias on 30 Nov 2018
Edited: Hugo Matias on 30 Nov 2018
Hi, Jim
My question is:
The weights of the containers are always generated randomly.
How do I transform the matrix that is generated into one like those ( the numbers would be different, obviously )
Thank you very much
Jim Riggs
Jim Riggs on 30 Nov 2018
Edited: Jim Riggs on 30 Nov 2018
I will explain how I aproach the problem. The sample data that you gave is pretty easy to work with, since there are only 4 different container weights. Writing an algorithm to do this might be tricky.
First, recignize that to obtain a CG in column 3 (where there are 7 colmns) will require significantly "front-loading" the system. I.e. you need the heavy ones in the first two columns in order to ballance the rest which will have a much longer moment arm.
Second, wherever there is an odd number of containers with the same weight, you know that one of these has to go in row #2 in order to maintain top-to-bottom symmetry.
So I started by counting the number of weight bins:
4 x 100
6 x 70
5 x 30
6 x 10
Only the 30# bin is odd, so one of the 30's has to go in row 2.
I put three of the 100's in column 1 for maximum forward leverage, and the remaining 100 must now go in row 2, so this gives an initial layout of:
100 .. .. .. .. .. .. ..
100 100 .. .. 30 .. .. ..
100 .. .. .. .. .. .. ..
The rest are allocated in pairs, maintainin symmetry in row 1 and row 3.
Again, I put the heaviest ones up front, and the lighter ones in the back. e.g.
100 70 70 30 30 10 10
100 100 70 70 30 10 10
100 70 70 30 30 10 10
After I had an initial allocation of all 21, I calculate the X,Y CG position and find that it is too far forward. (X = 2.6893) We need to move this back to X=3, so now I simply start trading lighter ones that are aft with heavier ones that are forward until I end up with X CG at X=3. As long as I keep row 1 and row 3 the same, the Y cg will always stay right at row 2.
First, I trade the70 and the 10 in the middle row:
100 70 70 30 30 10 10
100 100 10 70 30 10 70
100 70 70 30 30 10 10
This results in an X cg at 2.9223, so now we are very close.
Next, trade the other 70 and 10 in the midle row:
100 70 70 30 30 10 10
100 100 10 10 30 70 70
100 70 70 30 30 10 10
This results in Xcg = 3.0388. This was a bit too much. So now trade the 70 and 30 in the middle row:
100 70 70 30 30 10 10
100 100 10 10 70 30 70
100 70 70 30 30 10 10
This results in Xcg = 3, which is just what we want.
Hi Jim,
I already understood that! I just don't know the code in order to do it!
For complex problems like this, you need to do a lot of planning. Start with a flow chart and write in words (like I have done, above) the steos that you want to do. Then translate each step into code, breaking it down into more detail as you go.
So, it might go something like:
Step 1 make an initial allocation of the 21 containers.
Step 2 evaluate CG
Step 3 Adjust CG
Itterate step 2 and 3 until done.
Break down step 1 into sub-steps:
Make an initial alloocation:
- Sort containers into bins
- Identify bins with odd numbers
- Place odd container numbers in row 2
- Allocate remaining container pairs:
* Sort from heaviest to lighest
* Place pairs in rows 1 & 3, from heavy to light.
* Place remainder in row 2
etc..
As you go, you will identify additional functions that need to be performed, such as
- keep track of allocated/non-allocated containers
- keep track of used/available positions in the container array
Now translate each step into code and you are on your way. Obviously, this will be a whole lot of work.
The problem is I don't know how to place them! That's what i'm not understanding how I do it!

Sign in to comment.

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Asked:

on 28 Nov 2018

Commented:

on 30 Nov 2018

Community Treasure Hunt

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

Start Hunting!