"DOUBLE cannot convert the input expression into a double array. If the input expression contains a symbolic variable, use the VPA function instead."
4 次查看(过去 30 天)
显示 更早的评论
My command window code is:
syms L C gamma lambda k K X U FOG SOG u1 u2 u3 eps1 eps2;
C=u1^2+3*u2^2+2*u3^2;
L = u1*u2+2*u1*u3+3*u2*u3;
U=[u1 u2];X=[u3];
C=u1^2+3*u2^2+2*u3^2;
gamma=4;
Cx=jacobian(C,X);
Cu=jacobian(C,U);
Lu=jacobian(L,U);
Lx=jacobian(L,X);
lambda_T=-Lx*inv(Cx);
Lbar_u=Lu+lambda_T*Cu;
Lbar_x=Lx+lambda_T*Cx;
Lbar_ux=jacobian(Lbar_u,X);
Lbar_xx=jacobian(Lbar_x,X);
Lbar_xu=jacobian(Lbar_x,U);
Lbar_uu=jacobian(Lbar_u,U);
Lxu=jacobian(Lx,U);
Lux=jacobian(Lu,X);
Lbar_uu_star=Lbar_uu-Lbar_ux*inv(Cx)*Cu-(Cu)'*((Cx)')^(-1)*Lbar_xu+(Cu)'*((Cx)')^(-1)*Lbar_xx*inv(Cx)*Cu;
u3=-1;u1=1;u2=1;
k=1;K=.500330;
FOG=0;SOG=1;
Opt(0,K,1,Lbar_uu_star,L,X,U,C,k,gamma,lambda_T,Cx,Cu,Lx,Lu,Lxu,Lux,Lbar_u,Lbar_uu,Lbar_x,Lbar_xu, Lbar_xx, Lbar_ux,eps1,eps2);
My M-file Opt.m is:
function[] = Opt(FOG,K,SOG,Lbar_uu_star,L,X,U,C,k,gamma,lambda_T,Cx,Cu,Lx,Lu,Lxu,Lux,Lbar_u,Lbar_uu,Lbar_x,Lbar_xu, Lbar_xx, Lbar_ux,eps1,eps2)
while abs(double(subs(C))-double(subs(gamma)))>=(double(subs(eps1)))
X=(double(subs(X)))-inv(double(subs(Cx)))*(double(subs(C))-(double(subs(gamma))));
if abs(double(subs(C))-(double(subs(gamma))))<(double(subs(eps1)))
if abs(double(subs(Lbar_u)))<(double(subs(eps2)))
break;
else
k=k+1;
if FOG == 1
U=(double(subs(U)))-K*(double(subs((Lbar_u)')));
elseif SOG == 1
U=(double(subs(U)))-inv(double(subs(Lbar_uu_star)))*(double(subs(Lbar_u)));
end
end
end
end
L_star=((double(subs(X^2/2))))+(double(subs((U^2)/2)))-2*(double(subs(X)))-2*(double(subs(U)));
X_star=double(subs(X));
U_star=double(subs(U));
if FOG == 1
display('First order gradient method')
elseif SOG == 1
display('Second order gradient method')
end
display(X_star);display(U_star);display(L_star);display(k);
end
errors I get after running the code are:
Error using mupadmex Error in MuPAD command: DOUBLE cannot convert the input expression into a double array.
If the input expression contains a symbolic variable, use the VPA function instead.
Error in sym/double (line 702) Xstr = mupadmex('symobj::double', S.s, 0);
Error in Opt (line 3) while abs(double(subs(C))-double(subs(gamma)))>=(double(subs(eps1)))
I have no Idea what is going on. Any help would be highly appreciated
0 个评论
回答(1 个)
Carlos
2013-4-4
The problem comes in
double(subs(C))
From your code
C=u1^2+3*u2^2+2*u3^2;
so when you do
subs(C)
ans =
u1^2 + 3*u2^2 + 2*u3^2
C remains unchanged, so you cannot convert a symbol to a number.
If you did
subs(C,{u1,u2,u3},{1,1,1})
ans =
6
So in conclusion you cannot convert a symbolic expression containing variables into a number.
4 个评论
Walter Roberson
2013-4-5
When you subs() and expect symbolic variables to be replaced by what is in the workspace, the substitution is limited to variables existing in the current workspace. In your code, u1 and so on are defined in the caller of the routine that does the subs()
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!