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