Input arguments to function include colon operator. To input the colon character, use ':' instead.
9 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello guys,
I've looked online for a solution for my problem, but can't seem to find one. I am trying to fit a force curve to a predefined one.
The curve is geometry dependent, where I used an anonymous function handle @(y) for one of the lengths.
To further have a realizable geometry, I want to make sure that the angle theta2a (in closed state) which is equal to the angle theta2_0 (same angle in closed state, but different approach). I use this different approach to make sure the gripper in closed state is actually closed. This constraint is embedded in the nonlinear constraints function. Basically the important lines are:
theta2a= @(y) acos((x(6).^2+s(1).^2-y.^2)./(2.*x(6).*s(1)))+ acos((k(1).^2+s(1).^2-x(3).^2)./(2.*k(1).*s(1))) -atan(x(2)./(a(1)-x(1)));
theta2_0=@(y) atan2(J2(2)-J3(2), J2(1)-J3(1));
ceq(1)=@(y) theta2a(y)- theta2_0(y);
For matters of completeness: this is my full code:
clc; clear all; close all
global theta2a theta2_0
%
%***************************************************************************************************************************
% Force curve for new gripping system
%***************************************************************************************************************************
%************************************ Part 1
%------------------------------------- Activation mechanism with transmission
%handle length : x1
%------------------------------------- tooth number of gears
z=[25; 40; 80; 63; 21; 21; 40]; % for example: gear 1 has 25 teeth, gear 2 has 40 teeth...
% rotatory angle (t = psi)
t1=0:pi/360:pi/15;
t3= t1.*z(1)./z(3);
t5= t3.*z(4)./z(5);
t7= t1.*z(1)./z(3).*z(4)./z(5);
%gear radius =tooth_number/2 * 10^-3 [m]
r3=z(1)/2*0.001;
r4=z(4)/2*0.001;
r5=z(5)/2*0.001;
r7=z(7)/2*0.001;
da= r7.*t7;
%------------------------------------- Definition of lengths [m]
x=[0.038;0.008;0.015;0.012;0.008;0.006;0.036];
lb=0.008; %lower bound
ub=0.015; %upper bound
x4p=[];
r=0.0095; %radius
a0= 0.04893;
a= a0+da;
F0=1;
%------------------------------------- definition of joints
J1s=[a(end); x(3)-r];
J1=[a0;(x(3)-r)];
J3=[x(1);x(2)-r];
%assessing the valid area of values
%------------------------------------- find intersections for constraints
%--------------- running variables
t_1 = linspace(0,2*pi);
t_2 = linspace(0,2*pi);
t_2s = linspace(0,2*pi);
%--------------- definition of circle around Joint3
r1 = x(6);
x1 = r1*cos(t_1) + J3(1);
y1 = r1*sin(t_1) + J3(2);
for x4=lb:0.0005:ub
%--------------- definition of circle around Joint1
r2 = x4;
x2 = r2*cos(t_2) + J1(1);
y2 = r2*sin(t_2) + J1(2);
%--------------- definition of circle around Joint1 in rearmost position
x2s = r2*cos(t_2s) + J1s(1);
y2s = r2*sin(t_2s) + J1s(2);
%--------------- find intersections
[xout,yout]=intersections(x1,y1,x2,y2,1);
[xout2,yout2]=intersections(x1,y1,x2s,y2s,1);
%--------------- check if solution is valid for both positions of J1
%and enter valid solutions in x4p
if length(xout)-length(xout2)==0
x4p=[x4p;x4];
end
end
%------------------------------------- set valid area of x4 between lower
%and upper bound
lb=min(x4p);
ub=max(x4p);
%% ************************************ Part 2: optimization of x(4)=y
clc
% global y
%-------------------------- calculation of auxiliary lengths
l= sqrt((x(1)-0.001).^2+(x(2)-r).^2);
k= sqrt(x(2).^2+(a-x(1)).^2);
s= sqrt((x(3)-x(2)).^2+(a-x(1)).^2);
%-------------------------- calculation of angles, depending on y
theta1= @(y) acos((s.^2+y.^2-x(6).^2)./(2.*s.*y))+ acos((s.^2+x(3).^2-k.^2)./(2.*s.*x(3)));
theta2= @(y) acos((x(6).^2+s.^2-y.^2)./(2.*x(6).*s))+ acos((k.^2+s.^2-x(3).^2)./(2.*k.*s)) -atan(x(2)./(a-x(1)));
theta2a= @(y) acos((x(6).^2+s(1).^2-y.^2)./(2.*x(6).*s(1)))+ acos((k(1).^2+s(1).^2-x(3).^2)./(2.*k(1).*s(1))) -atan(x(2)./(a(1)-x(1)));
phi_1= atan(x(7)/x(5));
%-------------------------- calculation of Forces
F1= F0*x(1)./r7*r5/r4;
F4=@(y) F1.*x(6)./l.*(sin(theta1(y)).*cos(theta1(y)+theta2(y)))./(1-2.*(sin(theta2(y))).^2);
%--------------- definition of circle around Joint1, depending on y
r2 =@(y) y;
x2 =@(y) r2(y).*cos(t_2) + J1(1);
y2 =@(y) r2(y).*sin(t_2) + J1(2);
%--------------- definition of circle around Joint1 in rearmost position, depending on y
x2s =@(y) r2(y).*cos(t_2s) + J1s(1);
y2s =@(y) r2(y).*sin(t_2s) + J1s(2);
%--------------- find intersections
U=@(y) intersections(x1,y1,x2(y),y2(y),1);
V=@(y) intersections(x1,y1,x2s(y),y2s(y),1);
%------------------------------------- define line segments between joints
u_b=[J2(1)-J3(1);J2(2)-J3(2);0]; % vector from J3 to J2
v_b=[1;0;0];
u_a=[J2(1)-J1(1);J2(2)-J1(2);0]; % vector from J1 to J2
v_a=[0;-1;0];
%................. use instead of
%[M,I] = max(yout);
%J2=[xout(I);yout(I)];
W =@(y) max(U(2));
u1=@(y) U(1);
u2=@(y) U(2);
wi=@(y) find(U(2)==W);
J2=@(y) [u1(wi);u2(wi)];
%.................
%------------------------------------- calculate initial angles theta
theta2_0=@(y) atan2(J2(2)-J3(2), J2(1)-J3(1));
%................. used instead of
% theta2_0=@(y) atan2(norm(cross(@(y) u_b(y),v_b)), dot(@(y) u_b(y),v_b));
%.................
%************************** objective Function
t1g= t1.*180./pi; %theta1 in degrees
OF= 1/15.*t1g+0.5;
%**************************
A = [];
b = [];
Aeq = [];
beq = [];
%x = [ x4 ];
% lb = [ 0.008]; %lower bound was already defined in part1
x0 = [0.012 ];
% ub = [ 0.015]; %upper bound was already defined in part1
nonlcon = @mycon3;
options = optimoptions('fmincon','Display','iter','Algorithm','sqp');
fun= @(y) dot(abs(F4(y)-OF).^2,ones(length(OF),1));
%
% fsurf(fun,'ShowContours','on')
y = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
With mycon3(y):
function [c,ceq] = mycon3(y)
global theta2a theta2_0
% Compute nonlinear inequalities at x.
c=[];
% Compute nonlinear equalities at x.
% Make sure for every y, theta2 for initial position has to be equal to
% theta2_0, which represents theta2 for the closed gripper
ceq(1)=@(y) theta2a(y)- theta2_0(y);
% ceq=[];
end
The error popping up every time is:
Input arguments to function include colon operator. To input the colon character, use ':' instead.
Error in fmincon (line 651)
initVals.nceq = ceqtmp(:);
Error in Test3 (line 203)
y = fmincon(fun,x0,A,b,Aeq,beq,lb,ub,nonlcon,options);
What can I do, to surpass this error? Thanks for any help!
0 commentaires
Réponse acceptée
Stephen23
le 25 Août 2020
Modifié(e) : Stephen23
le 25 Août 2020
The function mycon3 is not correctly defined.
The fmincon documentation states that its optional nonlcon input argument should be "...a function that accepts a vector or array x and returns two arrays, c(x) and ceq(x)." In contrast the function mycon3 returns one empty array and one anonymous function, thus clearly does not meet the requirements.
Replace that anonymous function with an actual array, e.g:
ceq = theta2a(y)- theta2_0(y);
Plus de réponses (0)
Voir également
Catégories
En savoir plus sur Assembly dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!