Enforcing a rule in a symbolic expression
Show older comments
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
More Answers (1)
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
on 18 Aug 2014
Star Strider
on 18 Aug 2014
Edited: Star Strider
on 18 Aug 2014
I thought you already had s11 ... s15 in your workspace.
Put this loop above the assume statements to create them:
for k1 = 11:15
eval(sprintf('s%d = sym(''s%d'')', k1, k1))
end
Or you could simply write them out in your syms statement, since there only five.
If sij is equal to either -1 or +1, sij^2=1.
Omar Shehab
on 18 Aug 2014
Star Strider
on 18 Aug 2014
What do you want to do?
If you want to evaluate your expression for various values of s11 ... s15 that are each -1 or +1, it is best to do that numerically.
That is easiest if you use matlabFunction to create a function file or anonymous function from it. If you decide on an anonymous function, copy the anonymous function from the Command Window to your file, and evaluate it outside of the Symbolic Math Toolbox.
Omar Shehab
on 18 Aug 2014
Star Strider
on 18 Aug 2014
Edited: Star Strider
on 18 Aug 2014
The Symbolic Math Toolbox will simplify the sum of the fractions to (7/2), as it did when I used the subs function to evaluate only the squared terms as 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 + s11^2/4 + s12^2/4 + s13^2/4 + s14^2/4 + s15^2/4 + 9/4;
Expr1 = subs(Expr, {s11^2 s12^2 s13^2 s14^2 s15^2}, {1 1 1 1 1})
produces:
Expr1 =
(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 + 7/2
Does that do what you want?
Omar Shehab
on 18 Aug 2014
Omar Shehab
on 18 Aug 2014
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.
Categories
Find more on Common Operations in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!