MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn moreOpportunities for recent engineering grads.

Apply Today
Asked by upinderjeet on 4 Apr 2013

**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**

*No products are associated with this question.*

Answer by Carlos on 4 Apr 2013

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.

Show 1 older comment

Carlos on 4 Apr 2013

Something I don´t uderstand about the code is what upinderjeet tries to achieve by doing

subs(C)

Exactly Walter it is not possible,in fact you taught me in past questions how to use symbolic variables.

upinderjeet on 4 Apr 2013

Walter and Carlos

Thanks first of all for the quick reply. Actually If you notice the while loop the value of u1,u2 and u3 is getting changed if the certain conditions are met. So I want "C" to acquire the latest value of itself according to the updated values of u1,u2 and u3. So by using "double(subs())" command I want to accomplish that and actually everything worked fine at the end for my previous issues on which walter was helping me but this is similar and related problem but the only differnce is that X and U now are vector parameters unlike the scalar case of the past Walter.

Walter Roberson on 5 Apr 2013

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()

## 0 Comments