필터 지우기
필터 지우기

"Conversion to logical from sym is not possible." error shows up when I try to execute the following comands

조회 수: 79 (최근 30일)
X=.25;U=1;
>> eps1=.05;eps2=.05;
>> k=1;
>> while (abs(C-gamma))>=eps1
X=X-inv(Cx)*(C-gamma);
if abs(C-gamma)<eps1
if abs(Lbar_u)<eps2
break;
else
k=k+1;
U=U-K*(Lbar_u);
end
end
end
Conversion to logical from sym is not possible.
I ALSO TRIED TO USE A ALTERNATE WHILE LOOP CONDITION:
while double(subs(abs(C-gamma))) > double(eps1)
BUT THE ERROR PERSISTS
how can i fix it?
  댓글 수: 2
Walter Roberson
Walter Roberson 2013년 3월 29일
Where does the error show up? Which variables are symbols? What is the value of C and gamma and Cx and Lbar_u ?
This appears to be very similar to your previous question, which I do not think is resolved as yet. Are you sure it is not a duplicate ?
upinderjeet
upinderjeet 2013년 3월 30일
편집: Walter Roberson 2013년 3월 30일
this is not definitely a duplicate although you can call it a similar problem..i just removed the logical OR operator in the while condition to simplify things.
I declared all the variables as syms. I might be wrong in doing that as I am still a new user to matlab. I assigned gamma=1 at the beginnning of the code and C and Cx are just the function I declared in the command line as follows:(I am including the entire code below)
syms L C gamma lambda k K X U;
C=(X+U);
L=(X^2)/2+(U^2)/2-2*X-2*U;
Cx=gradient(C,X);
gamma=1;
Cu=gradient(C,U);
Lu=gradient(L,U);
Lx=gradient(L,X);
lambda_T=-Lu*inv(Cx);
Lbar_u=Lu+lambda_T*Cu;
Lbar_x=Lx+lambda_T*Cx;
Lbar_ux=gradient(Lbar_u,X);
Lbar_xx=gradient(Lbar_x,X);
Lbar_xu=gradient(Lbar_x,U);
Lbar_uu=gradient( Lbar_u,U);
eps1=.02;eps2=.02;
>> Lxu=diff(Lx,U);
>> Lux=diff(Lu,X);
>> X=.25;U=1;
>> k=1;
while (abs(C-gamma))>=eps1
X=X-inv(Cx)*(C-gamma);
if abs(C-gamma)<eps1
if abs(Lbar_u)<eps2
break;
else
k=k+1;
U=U-K*(Lbar_u);
end
end
end
Conversion to logical from sym is not possible.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2013년 3월 30일
You have
syms L C gamma lambda k K X U
C=(X+U)
because X and U are symbolic at that point, C will be symbolic. You never change the value of C, so it is going to remain symbolic.
You then have the test
while (abs(C-gamma))>=eps1
where gamma and eps1 have numeric values. But C does not have a numeric value, it has a symbolic value, so C-gamma will be symbolic, abs(C-gamma) will be symbolic, and you will be trying to use ">=" to compare a symbolic value to a numeric value.
It appears that up until the beginning of the "while", you want C to be symbolic -- for example it needs to be symbolic in order to use gradient(C,X) . But once you start the while loop, it appears that each time you write C, that you want the symbolic formula for C to be examined, and each place the formula refers to a variable that used to be symbolic but now has a numeric value, you want the numeric value to be substituted for the symbol, giving you a numeric value for C.
If that is what you want, then each place you reference C and want the substitution of numeric values to take place, you need to instead code
double(subs(C))
such as
while (abs(double(subs(C))-gamma))>=eps1
Symbolic formulas are not functions! Once they have symbolic form, any symbolic variables in them are not substituted with numeric values unless you use subs() to tell it to do the substitutions.
  댓글 수: 2
upinderjeet
upinderjeet 2013년 3월 30일
thanks alot walter.....i think now I am able to see my mistake....couple of other related doubts....
1. when do we use subs() and when do we use double(subs())?
2.In my code the reason I wrote Lxu=diff(Lx,U); and Lux=diff(Lu,X); is the fact that when I tried using :
Lxu=gradient(Lx,U);
Lux=gradient(Lu,X);
it gave me error:"first argument must be of type::Arithmetic"
so I switched to use diff() instead of gradient() as it ran error free
and my parameters are scalars for now and it wont make any difference.
What you think the reason might be?
upinderjeet
upinderjeet 2013년 3월 30일
AND..........
is there any better way to call the value of C,L,Lx,Lxu....etc...when I need them in the while loop or outside the while loop for that matter. I am asking because it wud be better to have something alternate instead of writing double(subs()) in front of every symbolic variable whenever I want their value.??

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Numbers and Precision에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by