Intelligent symbolic variable substitution

EDIT: I created an example to specify my problem in more detail.
I want to substitute variables in an equation system. Here an example to illustrate my idea:
syms f(x) z(x)
fun1 = f == (x+4)^2 + 4; % equals (x+4)^2
fun2 = f == x^2+8*x+20; % equals (x+4)^2
% fun1 === fun2
transform = z == (x+4)^2;
subs(fun1, rhs(transform), lhs(transform)) % Results in z(x) + 4
subs(fun2, rhs(transform), lhs(transform)) % Results in x^2 + 8x + 20
Obviously, fun1 and fun2 are the same. However Matlab is not able to replace the expression for z correctly when given in the form of fun2. Is there a way to achieve the same result in both notations?
Thank you for your help in advance.

답변 (1개)

Stephan
Stephan 2019년 11월 22일
편집: Stephan 2019년 11월 22일

0 개 추천

syms f(x) z(x)
fun = f == x^2 + 8*x + 16; % equals (x+4)^2
transform = z == (x+4)^2;
subs(fun, rhs(fun), lhs(transform))

댓글 수: 3

Arjun Sarin
Arjun Sarin 2019년 11월 22일
Thank you for your answer! Could you please elaborate what's the difference in the code? You are using symbolic functions which I didn't but I don't get my mistake...
Stephan
Stephan 2019년 11월 22일
편집: Stephan 2019년 11월 22일
You could also use:
syms f x z
fun = f == x^2 + 8*x + 16; % equals (x+4)^2
transform = z == (x+4)^2;
subs(fun, rhs(fun), lhs(transform))
The idea is declaring f the same way like transform - which you did not do. Thats all.
Arjun Sarin
Arjun Sarin 2019년 11월 25일
Thank you again. I tried the different notations in depth but actually it doesn't work for me. I updated the questions as the above.

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

카테고리

도움말 센터File Exchange에서 Programming에 대해 자세히 알아보기

제품

릴리스

R2019a

질문:

2019년 11월 22일

댓글:

2019년 11월 25일

Community Treasure Hunt

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

Start Hunting!

Translated by