How to use the If function to find and replace symbols in an equation?
Afficher commentaires plus anciens
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?
Réponses (2)
Azzi Abdelmalek
le 1 Mar 2013
Modifié(e) : Azzi Abdelmalek
le 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)
1 commentaire
Fergus
le 4 Mar 2013
Matt Kindig
le 1 Mar 2013
Modifié(e) : Matt Kindig
le 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.
Catégories
En savoir plus sur Numeric Solvers dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!