Optimize an objective function for values plus or minus near zero.
Show older comments
I am trying to optimize a function that I have defined. The problem is that the function defines acceleration at a revolute joint and of course must take on negative and positive values. I am having a hard time coming up with a way to constrain my objective function so that I can minimize it near zero. Basically, I don’t care how large the value is, but I want the output to be as close to zero as it possibly can, given my constraints. Please forgive if I have implemented any bad practices but I have attached my objective function (using the ub,lb, and while loop addition was my attempt at limiting the objective) and constraints. All recommendations are welcome. Please let me know if any more information is needed.
First is my objective function. Also, note that I have cut the A = .... way down since it may not be necesary, but if you need to see it all let me know.
function A = Objective(x)
%Objective Function
% Objective function to minimize accelerations for 6DOF arm
th1 = x(1);
th2 = x(2);
th3 = x(3);
th4 = x(4);
thdot1 = x(5);
thddot1 = x(6);
thdot4 = x(7);
thddot4 = x(8);
tau1f = x(9);
tau2f = x(10);
tau3f = x(11);
T = x(12);
ub = x(13); % Upper bounds of objective function
lb = x(14); % Lower bounds of objective function
A = 0;
flag = 0;
while (A >= lb) || (A <= ub)
if ((T >= 0) && (T <= tau1f)) % First segment of Acceleration.
A = thddot1+(T^2*1.0/tau1f^4*(tau2f^3*tau3f*th1*1.8e1-tau2f^3*tau3f*th2*1.8e1+tau1f^2*tau
elseif ((T > tau1f) && (T <= tau1f+tau2f)) % Second segment of Acceleration.
T = T - tau1f;
A = (tau1f*tau2f^2*th3*1.2e1-tau1f*tau3f^2*th2*6.0+tau2f*tau3f^2*th1*1.2e1+tau2f^2*tau3f*
else
T = T - (tau1f + tau2f); % Third segment of Acceleration.
A = -(tau1f*tau2f^2*th3*2.4e1+tau1f^2*tau2f*th3*1.2e1-tau1f^2*tau3f*th2*6.0+tau2f^2*tau3f
end
flag = flag + 1;
if flag == 1000
break
end
end
end
And for my constraints:
% x = [th1;th2;th3;th4;thdot1;thddot1;thdot4;thddot4;tau1f;tau2f;tau3f;T;ub;lb];
A1eq = [1 0 0 0 0 0 0 0 0 0 0 0 0 0;... % Linear equality
0 0 0 1 0 0 0 0 0 0 0 0 0 0;...
0 0 0 0 1 0 0 0 0 0 0 0 0 0;...
0 0 0 0 0 1 0 0 0 0 0 0 0 0;...
0 0 0 0 0 0 1 0 0 0 0 0 0 0;...
0 0 0 0 0 0 0 1 0 0 0 0 0 0;...
0 0 0 0 0 0 0 0 0 0 0 1 0 0;...
0 0 0 0 0 0 0 0 1 1 1 0 0 0;...
0 0 0 0 0 0 0 0 0 0 0 0 1 0;...
0 0 0 0 0 0 0 0 0 0 0 0 0 1];
b1eq = [0;85;0;0;0;0;9;9;50;-50]; % Linear equality
A1 = [0 -1 0 0 0 0 0 0 0 0 0 0 0 0;... % Linear inequality
0 1 0 0 0 0 0 0 0 0 0 0 0 0;...
0 0 -1 0 0 0 0 0 0 0 0 0 0 0;...
0 0 1 0 0 0 0 0 0 0 0 0 0 0;...
0 0 0 0 0 0 0 0 -1 0 0 0 0 0;...
0 0 0 0 0 0 0 0 1 1 1 0 0 0;...
0 0 0 0 0 0 0 0 0 -1 0 0 0 0;...
0 0 0 0 0 0 0 0 0 0 -1 0 0 0;...
0 0 0 0 0 0 0 0 -1 -1 -1 0 0 0];
b1 = [0;85;0;85;0;9;0;0;0]; % Linear inequality
x10 = [0;40;40;85;0;0;0;0;2;2;2;9;50;-50]; % Start point
Thanks in advance for any help
8 Comments
Walter Roberson
on 31 Mar 2019
If you do not care about sign but want as close to zero as possible,then minimize the square of the function.
Sugs
on 31 Mar 2019
Sugs
on 31 Mar 2019
Walter Roberson
on 31 Mar 2019
Can you provide everything we would need to run the code?
Matt J
on 31 Mar 2019
The solver will never produce a result that satisfies the inequality constraints exactly, but they should satisfy them within the ConstraintTolerance input parameter.
Accepted Answer
More Answers (1)
Using linear inequalities like these
A1 = [0 -1 0 0 0 0 0 0 0 0 0 0 0 0;... % Linear inequality
0 1 0 0 0 0 0 0 0 0 0 0 0 0;...
to implement simple bounds is less effective than using the lb,ub input arguments to fmincon.
8 Comments
Sugs
on 31 Mar 2019
Matt J
on 31 Mar 2019
@Matt I would still appreciate if you wouldn't mind showing a way to bound the objective function using lb an ub please.
Sugs
on 31 Mar 2019
Sugs
on 31 Mar 2019
function [c,ceq]=nonlcon(x, lb,ub)
A=Objective(x);
c(2)=A-ub;
c(1)=lb-A;
ceq=[];
end
Sugs
on 31 Mar 2019
Categories
Find more on Choose a Solver in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!