Enforcing a rule in a symbolic expression

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?

 採用された回答

Michael Haderlein
Michael Haderlein 2014 年 8 月 18 日

0 投票

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 件のコメント

Omar Shehab
Omar Shehab 2014 年 8 月 18 日
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 2014 年 8 月 19 日
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.

サインインしてコメントする。

その他の回答 (1 件)

Star Strider
Star Strider 2014 年 8 月 18 日

2 投票

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 件のコメント

Omar Shehab
Omar Shehab 2014 年 8 月 18 日
Shouldn't it affect all sij's? I want only the squared terms to be replaces by 1. When I tried to run your code I got following error.
Undefined function or variable 's11'.
Star Strider
Star Strider 2014 年 8 月 18 日
編集済み: Star Strider 2014 年 8 月 18 日
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
Omar Shehab 2014 年 8 月 18 日
Okay, I have created them like this.
syms s11 s12 s13 s14 s15
Then, I wrote the assumptions:
>> 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)
Finally I expanded the expression:
expand(sym('(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'))
Still I am getting the same expression:
s11^2/4 + (s11*s12)/2 + (s11*s13)/2 + (s11*s14)/2 + (s11*s15)/2 + (3*s11)/2 + s12^2/4 + (s12*s13)/2 + (s12*s14)/2 + (s12*s15)/2 + (3*s12)/2 + s13^2/4 + (s13*s14)/2 + (s13*s15)/2 + (3*s13)/2 + s14^2/4 + (s14*s15)/2 + (3*s14)/2 + s15^2/4 + (3*s15)/2 + 9/4
:(
Star Strider
Star Strider 2014 年 8 月 18 日
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
Omar Shehab 2014 年 8 月 18 日
I don't want to evaluate my expression. I just want to enforce the rule sij^2 = 1. This will remove all the second order terms i.e. squared terms giving me a simpler expression. So, in the original expression, s11^2/4 will become 1/4 and so on.
Star Strider
Star Strider 2014 年 8 月 18 日
編集済み: Star Strider 2014 年 8 月 18 日
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
Omar Shehab 2014 年 8 月 18 日
Thanks. It worked.
Omar Shehab
Omar Shehab 2014 年 8 月 18 日
I wanted to accept this answer but mistakenly clicked on the other one. Don't know how to fix it.
Star Strider
Star Strider 2014 年 8 月 18 日
編集済み: Star Strider 2014 年 8 月 18 日
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.

サインインしてコメントする。

製品

Community Treasure Hunt

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

Start Hunting!

Translated by