Enforcing a rule in a symbolic expression

1 view (last 30 days)
I have the following symbolic expression:
(3*s11)/2 + (3*s12)/2 + (3*s13)/2 + (3*s14)/2 + (3*s15)/2 + (s11*s12)/2 + (s11*s13)/2 + (s11*s14)/2 + (s12*s13)/2 + (s11*s15)/2 + (s12*s14)/2 + (s12*s15)/2 + (s13*s14)/2 + (s13*s15)/2 + (s14*s15)/2 + s11^2/4 + s12^2/4 + s13^2/4 + s14^2/4 + s15^2/4 + 9/4
It is stored as a symbolic expression variable. I would like to enforce the rule sij^2 = 1 i.e. the variables can be either -1 or +1. If I enforce the rule in the expression mentioned above, the expression will be as follows.
(3*s11)/2 + (3*s12)/2 + (3*s13)/2 + (3*s14)/2 + (3*s15)/2 + (s11*s12)/2 + (s11*s13)/2 + (s11*s14)/2 + (s12*s13)/2 + (s11*s15)/2 + (s12*s14)/2 + (s12*s15)/2 + (s13*s14)/2 + (s13*s15)/2 + (s14*s15)/2 + 1/4 + 1/4 + 1/4 + 1/4 + 1/4 + 9/4
How can I do this in Matlab?

Accepted Answer

Michael Haderlein
Michael Haderlein on 18 Aug 2014
I suppose you want to solve the equation?
You can give additional arguments. In this case, I think, you must use strings:
solve('(3*s11)/2 + (3*s12)/2 + (3*s13)/2 + (3*s14)/2 + (3*s15)/2 + (s11*s12)/2 + (s11*s13)/2 + (s11*s14)/2 + (s12*s13)/2 + (s11*s15)/2 + (s12*s14)/2 + (s12*s15)/2 + (s13*s14)/2 + (s13*s15)/2 + (s14*s15)/2 + s11^2/4 + s12^2/4 + s13^2/4 + s14^2/4 + s15^2/4 + 9/4',...
's11^2=1','s12^2=1','s13^2=1','s14^2=1','s15^2=1' )
  2 Comments
Omar Shehab
Omar Shehab on 18 Aug 2014
When I copy paste your code it give me following output, not the one I want.
Warning: 6 equations in 5 variables.
> In C:\Program Files\MATLAB\R2014a\toolbox\symbolic\symbolic\symengine.p>symengine at 56
In mupadengine.mupadengine>mupadengine.evalin at 97
In mupadengine.mupadengine>mupadengine.feval at 150
In solve at 170
ans =
s11: [5x1 sym]
s12: [5x1 sym]
s13: [5x1 sym]
s14: [5x1 sym]
s15: [5x1 sym]
Michael Haderlein
Michael Haderlein on 19 Aug 2014
That's strange, I don't get a warning. Directly copied from the command window:
>> solve('(3*s11)/2 + (3*s12)/2 + (3*s13)/2 + (3*s14)/2 + (3*s15)/2 + (s11*s12)/2 + (s11*s13)/2 + (s11*s14)/2 + (s12*s13)/2 + (s11*s15)/2 + (s12*s14)/2 + (s12*s15)/2 + (s13*s14)/2 + (s13*s15)/2 + (s14*s15)/2 + s11^2/4 + s12^2/4 + s13^2/4 + s14^2/4 + s15^2/4 + 9/4','s11^2=1','s12^2=1','s13^2=1','s14^2=1','s15^2=1')
ans =
s11: [5x1 sym]
s12: [5x1 sym]
s13: [5x1 sym]
s14: [5x1 sym]
s15: [5x1 sym]
Anyway, I read that solving the equation was not your target. Sorry for misunderstanding your question.

Sign in to comment.

More Answers (1)

Star Strider
Star Strider on 18 Aug 2014
I’m not certain what you’re doing, so I can’t test your code with this, but you could use assume:
assume( s11 == -1 | s11 == 1 )
assume( s12 == -1 | s12 == 1 )
assume( s13 == -1 | s13 == 1 )
assume( s14 == -1 | s14 == 1 )
assume( s15 == -1 | s15 == 1 )
Expr = (3*s11)/2 + (3*s12)/2 + (3*s13)/2 + (3*s14)/2 + (3*s15)/2 + (s11*s12)/2 + (s11*s13)/2 + (s11*s14)/2 + (s12*s13)/2 + (s11*s15)/2 + (s12*s14)/2 + (s12*s15)/2 + (s13*s14)/2 + (s13*s15)/2 + (s14*s15)/2 + 1/4 + 1/4 + 1/4 + 1/4 + 1/4 + 9/4;
I can only claim that the Symbolic Math Toolbox accepts it. I have no idea how you would use it or evaluate it. (I created your statement as ‘Expr’ for my convenience.)
  9 Comments
Omar Shehab
Omar Shehab on 18 Aug 2014
I wanted to accept this answer but mistakenly clicked on the other one. Don't know how to fix it.
Star Strider
Star Strider on 18 Aug 2014
Edited: Star Strider on 18 Aug 2014
My pleasure!
I will get half-credit if you Vote for it. And since you wanted to Accept it, I’ll keep it up rather than deleting it (as I usually do with my Answers that aren’t accepted).
Unfortunately, it is impossible to ‘un-accept’ an Answer once accepted. That happens more often that you would imagine, and probably means that MathWorks needs to re-design MATLAB Answers with the help of a good human factors engineer.

Sign in to comment.

Products

Community Treasure Hunt

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

Start Hunting!