Defining constraint equations for fmincon

2 views (last 30 days)
Puneeth
Puneeth on 31 Jul 2014
Hello, I've been working on the simulation of torque minimization of a 3r planar arm. However my problem is purely code based. I've created a function for the torque that has to be minimized which is as follows:(starting from function f)
if true
% code
end
function f = torque_3r(th)
global M1 M2 M3 L1 L2 L3 Lc1 Lc2 Lc3 g
M1=1;
M2=1;
M3=1;
L1=1;
L2=1;
L3=1;
Lc1=L1/2;
Lc2=L2/2;
Lc3=L3/2;
g=10;
P=[M1*g*Lc1*sin(th(1));M2*g*(Lc2*sin(th(1)+th(2))+L1*sin(th(1)));M3*g*(Lc3*sin(th(1)+th(2)+th(3))+L2*sin(th(1)+th(2))+L1*sin(th(1)))]
G=[M1*g*Lc1*cos(th(1))+M2*g*(Lc2*cos(th(1)+th(2))+L1*cos(th(1)))+M3*g*(Lc3*cos(th(1)+th(2)+th(3))+L2*cos(th(1)+th(2))+L1*cos(th(1)));M2*g*Lc2*cos(th(1)+th(2))+M3*g*(Lc3*cos(th(1)+th(2)+th(3))+L2*cos(th(1)+th(2)));M3*g*Lc3*cos(th(1)+th(2)+th(3))]
f= G.'*G
Now I've created one more function for the constraint equations which is(starts from function[c,ceq]):
if true
% code
end
function [c,ceq] = vel(th)
syms a b c d t1 t2
th=zeros(3,1)
T=[t1^3 t1^2 t1 1;t2^3 t2^2 t2 1;3*t1^2 2*t1 1 0;3*t2^2 2*t2 1 0]
coeff=[a b c d].'
T1=subs(T,t1,0)
T2=subs(T1,t2,2)
coeff=inv(T2)*[3 2 0 0].' % inv(T2)*[X1 X2 V1 V2]
coeff2=inv(T2)*[0 1 0 0].'
i=1;
for t=0:0.1:2
x3(i)=(coeff(1)*t^3+coeff(2)*t^2+coeff(3)*t+coeff(4));
y3(i)=(coeff2(1)*t^3 +coeff2(2)*t^2+coeff2(3)*t+coeff2(4));
c=[];
ceq1(i)=cos(th(1))+cos(th(1)+th(2))+cos(th(1)+th(2)+th(3))-x3(i);
ceq2(i)=sin(th(1))+sin(th(1)+th(2))+sin(th(1)+th(2)+th(3))-y3(i);
ceq=[ceq1;ceq2];
fmincon(@torque_3r,[0,0,0.5],[],[],[],[],[],[],@vel);
ans=th
x2(i) = cos(th(1))+cos(th(1)+th(2))
y2(i) = sin(th(1))+sin(th(1)+th(2))
x1(i) = cos(th(1))
y1(i) = sin(th(1))
X=[0,x1(i),x2(i),x3(i)];
Y=[0,y1(i),y2(i),y3(i)];
hold off
pause(0.05)
i=i+1;
plot(X,Y)
axis([-3,3,-3,3])
end
and the error i receive is "Maximum recursion limit of 500 reached. Use set(0,'RecursionLimit',N) to change the limit. Be aware that exceeding your available stack space can crash MATLAB and/or your computer.
Error in cell/unique>localcat
Caused by: Failure in initial user-supplied nonlinear constraint function evaluation. FMINCON cannot continue."
My aim is to get the 'th' values from fmincon (in the for loop) satisfying my constraint equations[c,ceq] in every for loop and use them in the next loop.
Now the problem is,the constraint equations contain 'x3(i)' and 'y3(i)' which are functions of 't', so equations for 'c' and 'ceq' have to be put in the same for loop as that of fmincon and 'x3(i)','y3(i)'.(Correct me if there is any other alternative).
Any sort of correction which would simplify my code would be really helpful. Thanks.

Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!