如何将中的解决方案转换为基本公式

  • 本文关键字:转换 解决方案 matlab
  • 更新时间 :
  • 英文 :


我正试图将我的问题转化为基本公式,这样我就可以用它来解决更大的问题,但我无法思考。我的主要问题是对大量序列使用相同的策略

这里1、2、3是连接在一起的对象。例如,如果我得到的输出是序列(来自排列):-2-1-3;2-3-1现在我必须根据它质心的距离来比较这两个序列

对于2-1-3,首先我计算了2和1的质心,然后测量了它与2质心的距离(d21),然后我计算了所有物体的质心(1,2,3)以及它与2和1质心的距离;最后dx1=d21+d213

类似地,我对序列2-3-1进行了计算,并计算出dx2=d23+d231

换句话说,我想要dx1,dx2值作为输出,我为此编码了程序,如图所示,

m1=input('the value of m1 is')
m2=input('the value of m2 is')
m3=input('the value of m3 is')
x1=input('the value of x1 is')
x2=input('the value of x2 is')
y1=input('the value of y1 is')
y2=input('the value of y2 is')
y3=input('the value of y3 is')
x3=input('the value of x3 is')
% here m is the mass and x and y are the center of mass of an respective object
% X11 and Y11 are the center of mass of all three objects connected together
Y11=input('the value of Y11 is')
X11=input('the value of X11 is')
[X21,Y21]= Centerofmass(m1,m2,x1,x2,y1,y2)
[X213,Y213]= Centerofmassy(m1,m2,m3,x3,X21,Y21,y3)
[X23,Y23]= Centerofmassyy(m3,m2,x3,x2,y3,y2)
[X231,Y231]= Centerofmassyyy(m1,m2,m3,x1,X23,Y23,y1)
X = [x2,y2;X21,Y21];
d21 = pdist(X,'euclidean');
X = [X21,Y21;X213,Y213];
d213 = pdist(X,'euclidean');
dx1=d21+d213
X = [x2,y2;X23,Y23];
d23 = pdist(X,'euclidean');
X = [X23,Y23;X231,Y231];
d231 = pdist(X,'euclidean');
dx2=d23+d231
% Where functions are
% X21 and Y21 are the center of mass of 2 and 1 connected together
function[X21,Y21]= Centerofmass(m1,m2,x1,x2,y1,y2)
X21=((m1*x1)+(m2*x2))/(m1+m2);
Y21=((m1*y1)+(m2*y2))/(m1+m2);
end
% X213 and Y213 are the center of mass of all objects connected together
function [X213,Y213]= Centerofmassy(m1,m2,m3,x3,X21,Y21,y3)
X213=(((m1+m2)*X21)+(m3*x3))/(m1+m2+m3);
Y213=(((m1+m2)*Y21)+(m3*y3))/(m1+m2+m3); 
end
% X23 and Y23 are the center of mass of 2 and 3 connected together
function[X23,Y23]= Centerofmassyy(m3,m2,x3,x2,y3,y2)
X23=((m3*x3)+(m2*x2))/(m3+m2);
Y23=((m3*y3)+(m2*y2))/(m3+m2);
end
% X231 and Y231 are the center of mass of all three objects connected together
function [X231,Y231]= Centerofmassyyy(m1,m2,m3,x1,X23,Y23,y1)
X231=(((m3+m2)*X23)+(m1*x1))/(m1+m2+m3);
Y231=(((m3+m2)*Y23)+(m1*y1))/(m1+m2+m3); 
end

我如何对更大的问题使用相同的策略,我指的是有超过7个对象和超过60个输出或序列来计算距离的问题????

下面的代码应该会让您朝着正确的方向前进。这将创建一个名为data的数组,其中行表示对象,列(1、2和3)分别表示m、x和y。这将允许您拥有数量可变的对象,具有不同的质量和X、Y位置。

objects = input('How many objects: ');
data = zeros(objects,3);
for k = 1:objects
% INPUT OBJECT VALUES
data(k,1) = input(strcat('Enter mass of object #',int2str(k),': '));
data(k,2) = input(strcat('Enter X-value of object #',int2str(k),': '));
data(k,3) = input(strcat('Enter Y-value of object #',int2str(k),': '));
end

代码的第二部分将涉及一些简单的组合数学。您将需要另一个类似的for循环,该循环将遍历每个可能的组合(取决于有多少对象),并计算(并存储在另一个变量中)得到的质心计算。

特别是,您将使用一个名为"组合"的概念。

for k = 1:objects
combinations = combnk(1:objects,k)
for c = size(combinations,1)
% combinations(c,:) here will be the indices of each
% possible combination. This is where you will calculate
% the center of mass
end
end

上述for循环中的每一行combinations都将是对象的所有可能组合。然后,您将希望在其中的另一个for循环中循环每个组合并计算结果。我把那部分留给你。

相关内容

最新更新