How to use the If function to find and replace symbols in an equation?
이전 댓글 표시
I want to simplify a long equation by replacing the symbols or substitute an numerical value. For example:
syms A1 A2 A3 A1_ A2_ A3_
a= A1*A1_+A2*A2_+A3*A3_+A1*A2+A2*A3
I want to replace A1*A1_,A2*A2_ and A3*A3_to 0 and A1*A2 to A2 and A2*A3 to A3 so the ans will end up a = A2+A3.
In order to achieve this, how to use the If function for the coding or is there any function can substitute or replace those symbols?
답변 (2개)
Azzi Abdelmalek
2013년 3월 1일
편집: Azzi Abdelmalek
2013년 3월 1일
syms A1 A2 A3 A1_ A2_ A3_
a=A1*A1_+A2*A2_+A3*A3_+A1*A2+A2*A3
a=char(a)
a=regexprep(a,{'A1**A1_','A2**A2_','A3**A3_'},'0')
a=regexprep(a,{'A1**A2','A2**A3'},{'A2','A3'})
a=sym(a)
Matt Kindig
2013년 3월 1일
편집: Matt Kindig
2013년 3월 1일
I think you want to use subs() to set the zero terms:
a=subs(a, {'A1_' ,'A2_', 'A3_'}, {0,0,0});
You wouldn't be able to use this for the A1*A2 and A2*A3 terms, however, since that would mean that A2 is a symbolic variable in the first term (A1*A2) but it would have a defined value (A2=1) in the second (A2*A3) term.
카테고리
도움말 센터 및 File Exchange에서 Numeric Solvers에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!