finding optimum solution

4 次查看(过去 30 天)
Cora
Cora 2011-3-29
Hi everyone,
I would apprechiate help with the following matter I have the following vectors A,D,B,C,E (each size 50,1) and calculate T=-A.*((B+x)-C)+D.*E; The goal is to find constant x so that sum(T)==0. I tried fminsearch, but didn't figure out how to make it work. I would be greatful for any help. Thanks.
  2 个评论
Andrew Newell
Andrew Newell 2011-3-29
If cumsum(T)==0, then isn't T==0? Or do you mean sum(T)==0?
Cora
Cora 2011-3-29
sorry, my mistake. I mean sum(T)==0

请先登录,再进行评论。

采纳的回答

Andrew Newell
Andrew Newell 2011-3-30
I'm going to rewrite your problem in vector notation. Suppose the positions are given by vectors r_i, i=1..50, and the forces by F_i, i=1..50. The coordinates of the center are r_c. Then the torque on the system is sum_i (r_i-r_c)xF_i, where the x refers to the cross product. This can be separated into two terms: sum_i (r_i x F_i) - r_c x sum_i F_i. So you need a torque function
function T = torque(r,ri,Fi)
% Add a z component (all zeros) to allow use of CROSS.
r = [r 0];
ri = [ri zeros(length(ri),1)];
Fi = [Fi zeros(length(Fi),1)];
F = sum(Fi);
T = sum(cross(ri,Fi,2))-cross(r,F,2);
T = T(3); % The torque is only nonzero perpendicular to the plane.
To run it, save the function in a file torque.m and use these commands:
ri = [B E];
Fi = [A D];
f = @(x) torque(x,ri,Fi); % create a function f(x)
r0 = [0 0]; % initial guess for the center
rc = fsolve(f,r0)
The function f is an anonymous function, which is a way of hiding the fixed parameters ri and Fi.
  1 个评论
Cora
Cora 2011-3-31
Great, thanks for this solution. It seems to optimize both vertical and horizontal position, but based on your solution I wrote a simplified script that adjusts only the horizontal position. I really apprechiate your help.
f=@(x) torque_simple(x,A,D,B,E);
r0=0;
rc = fsolve(f,r0);
function T=torque_simple(r,A,D,B,E)
T=trapz(-A.*(B+r)+D.*E);

请先登录,再进行评论。

更多回答(1 个)

Andrew Newell
Andrew Newell 2011-3-29
As defined, your problem has an infinite number of solutions. You can expand your expression to get
T=-A.*x+D.*E-A.*B+A.*C;
Thus, for any vector T,
x = (D.*E-A.*B+A.*C-T)./A;
so you could choose any components for T so that sum(T)==0 and solve this equation.
  1 个评论
Cora
Cora 2011-3-30
thanks, however I'm not sure I understand your solution. Maybe I described the problem inadequately. I calculate torque based on force and distance in horizontal and vertical direction. I have 50 force vectors (A vertical, D horizontal) and 50 positions (B,E). The sum of torque over the 50 positions should be zero. Usually it’s not because I can’t determine the initial starting position in horizontal direction well enough, so I need to displace the 50 positions in horizontal direction (B) by a constant x in order to get the correct starting position that results in T==0. The value of x I’m looking for is the closest to x==0, so the smallest correction of my initial starting point.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by