Can I substitute a particular expression?

5 次查看(过去 30 天)
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 个)

类别

Help CenterFile Exchange 中查找有关 Formula Manipulation and Simplification 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by