How to use the If function to find and replace symbols in an equation?
2 views (last 30 days)
Show older comments
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?
0 Comments
Answers (2)
Azzi Abdelmalek
on 1 Mar 2013
Edited: Azzi Abdelmalek
on 1 Mar 2013
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
on 1 Mar 2013
Edited: Matt Kindig
on 1 Mar 2013
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.
0 Comments
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!