Can I substitute a particular expression?

조회 수: 8 (최근 30일)
Valeri Aronov
Valeri Aronov 2021년 7월 23일
댓글: Valeri Aronov 2021년 7월 24일
subexpr() discovers a common subexpression and rewrites a symbolic expression in terms of the found subexpression. How can I similarly rewrite an expression in terms a given subexpression? For example, I would like to use sigma = k^(1/2) to obtain:
sigma, sigma^3, sigma^5
out of
k^(1/2), k^(3/2), k^(5/2)?
  댓글 수: 3
Valeri Aronov
Valeri Aronov 2021년 7월 23일
Hi Ajay,
There are 15 expressions I am trying to simplify. Two simplest are:
1/((C1*C2*R1*R2*w^2 - 1)^2 + w^2*(C2*R1 + C2*R2)^2)^(1/2) (A)
-(C2*R1*R2*w^2*(C1*C2*R1*R2*w^2 - 1))/((C1*C2*R1*R2*w^2 - 1)^2 + w^2*(C2*R1 + C2*R2)^2)^(3/2) (B)
Others contain (C1*C2*R1*R2*w^2 - 1)^2 + w^2*(C2*R1 + C2*R2)^2 subexpression in the power 3/2 and 5/2. I ran subexpr() 4 times in succession and it found common subexpressions but never (A).
I do not know how can I possibly help it to find what I want.
Valeri Aronov
Valeri Aronov 2021년 7월 24일
편집: Valeri Aronov 2021년 7월 24일
When I did this:
syms a b c
a = 1 + c^(1/2)
b = 1/c^(1/2)
A1 = subexpr([a, b], 'sigma')
the result was:
A1 = [c^(1/2) + 1, 1/c^(1/2)]
subexpr() does not see the opportunity for sigma to be c^(1/2) let alone in an expression with c^(3/2).
Conclusion: My intent to save on multiple sqrt() calls can't be achieved with current version of subexpr(). I still can do ...^(3/2) and ...^(5/2) once.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2021년 7월 24일
There are two scenarios:
1)
syms k sigma positive
A = [k, k^(1/2), k^(3/2), k^(5/2)]
A = 
B = subs(A, k, sigma^2)
B = 
That is, if necessary you solve() to find a way to express k in terms of sigma, and then subs() into the expression. In this scenario, every k is substituted, not just the ones that have powers. Watch out for the "positive" -- if k could hypothetically be complex or real, then collapasing the exponents might not be correct.
2)
syms k sigma positive
A = [k, k^(1/2), k^(3/2), k^(5/2)]
A = 
mapSymType(A, 'power', @(expr) piecewise(children(expr,1)==k, sigma^(2*children(expr,2)), expr))
ans = 
In this scenario, you only want to affect k that are in power relationships and leave the rest unchanged.
You might perhaps wonder why you would want to leave some unchanged. The answer to that is that your actual situation might have to do with trig being raised to powers, such as wanting to substitute (1-cos(EXPRESSION)^2) for sin(EXPRESSION)^2 while leaving the sin() that are not to powers alone.
  댓글 수: 1
Valeri Aronov
Valeri Aronov 2021년 7월 24일
Magic! Borrowing your code as it is in your 1) for my case produced:
((...)^(1/2))^3 and ((...)^(1/2))^5
but this change
subs(A, k, sigma)
rendered exactly what I wanted.
How can I buy you beer when you come to Sydney? If the country is ever opened again.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by