Creating new symbolic identities

1 回表示 (過去 30 日間)
Daniel Lyddy
Daniel Lyddy 2014 年 6 月 27 日
回答済み: Christopher Creutzig 2014 年 9 月 1 日
I have a matrix expression involving three real symbolic variables called x, y, and z, which are three elements of a vector v in R3. It is known that v is constrained to be a unit vector, so that x^2 + y^2 + z^2 is always equal to 1. Is there a way to tell MATLAB about this identity (x^2 + y^2 + z^2 = 1) so that whenever it encounters the expression x^2 + y^2 + z^2, it substitutes a 1 for that expression?
I tried using subs(S, x^2 + y^2 + z^2, 1), but that doesn't work ... it appears the second argument to a three-argument subs() call has to be a single variable.
I tried the following trick, inspired by the conversion from Cartesian to Spherical coordinates:
syms a b real;
s1 = subs(S, x, cos(a) * sin(b));
s2 = subs(s1, y, sin(a) * sin(b));
s3 = subs(s2, z, cos(b));
and that actually performs the x^2 + y^2 + z^2 = 1 substitution, but has the unfortunate side effect that all remaining expressions are now in terms of a and b instead of x, y, and z.
There has to be a way to do this, right? Corallary: I can't be the first person who has wanted something like this, can I?

回答 (1 件)

Christopher Creutzig
Christopher Creutzig 2014 年 9 月 1 日
Works for me:
>> syms x y z
>> S = x^2+y^2+z^2+x+y+z+1
S =
x^2 + x + y^2 + y + z^2 + z + 1
>> subs(S, x^2+y^2+z^2, 1)
ans =
x + y + z + 2
Here's something else you might want to try, since it also handles cases that are a bit more “hidden”:
>> assume(x^2+y^2+z^2==1)
>> S
S =
x^2 + x + y^2 + y + z^2 + z + 1
>> simplify(S)
ans =
x + y + z + 2
>> simplify(x^3+x*y^2-x)
ans =
-x*z^2

カテゴリ

Help Center および File ExchangeConversion Between Symbolic and Numeric についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by