Asked by upinderjeet
on 26 Mar 2013

I am trying to create a while loop as follows:

while (abs(C(X,U)-gamma)>=eps1)||(abs(Lbar_u(X,U)))

if abs(Lbar_u(X,U))>=eps2

k=k+1;

U=U-K*Lbar_u(X,U);

if abs(C(X,U)-gamma)>=eps1

X=X-inv(Cx(X,U))*(C(X,U)-gamma)

end

end

elseif abs(C(X,U)-gamma)>=eps1

X=X-inv(Cx(X,U))*(C(X,U)-gamma)

end

end

THE ERROR MESSAGE WHICH I GET IS AS FOLLOWS:

"Conversion to logical from sym is not possible"

what can I do??

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

Learn moreOpportunities for recent engineering grads.

Apply Today
## 11 Comments

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/68616-how-can-i-use-the-symbolic-expression-with-a-logical-or-operator-both-being-parts-of-the-conditio#comment_138946

Which of your variable was created by sym() or syms ?

Also, I recommend you recheck the second part of your "while" condition. At the moment it is testing whether abs(Lbar_u(X,U)) is non-zero.

## upinderjeet (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/68616-how-can-i-use-the-symbolic-expression-with-a-logical-or-operator-both-being-parts-of-the-conditio#comment_138961

eps1,eps2,X,U and C were declared syms and all other were declared global. Here is the corrected version of the while condition and thanks for pointing that typing error out. so here is the code again:

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/68616-how-can-i-use-the-symbolic-expression-with-a-logical-or-operator-both-being-parts-of-the-conditio#comment_138974

That "while" looks exactly the same as the previous one, and still is checking whether Lbar_u(X,U) is non-zero.

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/68616-how-can-i-use-the-symbolic-expression-with-a-logical-or-operator-both-being-parts-of-the-conditio#comment_138976

If eps1, eps2, X, U, and C are all syms, then the first clause in your "while"

is trying to access a symbolic variable at a pair of symbolic indexes, and subtract a known value from the symbolic result, which is of course going to give a symbolic result since almost all of the expression was symbolic. Then you take the abs() of that symbolic result, giving a symbolic result. You then try to use >= to compare that symbolic result to the symbolic value eps1. And you are expecting a "true" or "false" answer ???

## upinderjeet (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/68616-how-can-i-use-the-symbolic-expression-with-a-logical-or-operator-both-being-parts-of-the-conditio#comment_138988

i just changed the while loop....please check....i had forgotten to put >=eps2 in the second part of the while condition. I am expecting the loop to just carry on until both the conditions turn false and hence the loop is terminated. I had assigned gamma=1.

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/68616-how-can-i-use-the-symbolic-expression-with-a-logical-or-operator-both-being-parts-of-the-conditio#comment_138993

Okay, I see the second part of the loop condition changed now. But the first part of it still has the same analysis: that you are ending up trying to use ">" to compare a symbolic expression to a symbolic variable that has no obvious algebraic relationship. How can you expect to get a true/false answer?

## upinderjeet (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/68616-how-can-i-use-the-symbolic-expression-with-a-logical-or-operator-both-being-parts-of-the-conditio#comment_138999

okey....lets consider first part...abs(C(X,U)-gamma)>=eps1 .....now actually when I write C(X,U)..i am expecting to call the earlier defined function as C(X,U)=X+U: now within the loop i am expecting to see the changes in the value of X and U so that when I write C(X,U)in the while condition, it is evaluated at the updated value of X and U and then the comparison happens. And I want to continue the loop until both the comparisons are dissatisifed and hence the loop is no longer executed. That is why I used OR operator.

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/68616-how-can-i-use-the-symbolic-expression-with-a-logical-or-operator-both-being-parts-of-the-conditio#comment_139000

You indicated, though, that X and U and eps1 are symbolic. With X and U symbolic, X+U would be symbolic, and abs(X+U-1) would be symbolic, so you would be comparing a symbolic expression to eps1 which you also indicated is symbolic.

## upinderjeet (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/68616-how-can-i-use-the-symbolic-expression-with-a-logical-or-operator-both-being-parts-of-the-conditio#comment_139003

yes....so is there anyhting wrong in doing so......as far as i believe we cudnt use this logical operator for these symbolic expressions....and i beleive that due to the error message I got when I ran the code. "Conversion to logical from sym is not possible"....but I a still new to matlab....so please correct me wheere I am wrong...

## Walter Roberson (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/68616-how-can-i-use-the-symbolic-expression-with-a-logical-or-operator-both-being-parts-of-the-conditio#comment_139005

There are circumstances under which it makes sense to use logical operators with symbolic expressions, but a "while" condition is not suitable.

In some cases what would be suitable would be constructing a piecewise expression. I do not think your while loop is suitable for that, though.

If you had actual values to work with for X and U and eps1 and eps2, then you could use subs() to create symbolic numeric values that could be compared, something like

though usually there are more efficient ways to code, such as using matlabFunction()

## upinderjeet (view profile)

Direct link to this comment:https://www.mathworks.com/matlabcentral/answers/68616-how-can-i-use-the-symbolic-expression-with-a-logical-or-operator-both-being-parts-of-the-conditio#comment_139188

I got the point you are trying to make here. My next biggest challenge is now to achieve the same goal(i.e execution of all the statements following the while condition, as long as those 2 inequalities in the condition part are hold true). Now, how can I achieve that without using the while loop.

Log in to comment.