Matt
Matt

Reputation: 13

Simplifying expressions that include symbolic variables

I am trying to automatically simplify my MATLAB output, but so far I have only been able to achieve it manually.

After my code executes I'm presented with the following data:

((yf^2*((138454006717812460917559315575*a^2)/20282409603651670423947251286016 + 20)^2 - 20*yf^2*((19169511976216058056763775016476506759529226598462437580625*a^4)/6582018229284824168619876730229402019930943462534319453394436096 - (39872549173714278543306705994157*a^2)/40564819207303340847894502572032 + 25))^(1/2) - (- (138454006717812460917559315575*a^2)/20282409603651670423947251286016 - 20)*yf)/(10*yf^2)

where 'yf' and 'a' are symbolic variables. However, during a portion of the code, these variables are assigned specific values. Accordingly, if I copy that data above and put it back into MATLAB it then aknowledges that 'yf' and 'a' have specific values and provides me the following:

41734615734664636717495154985753713986168093218527630622251194499100^(1/2)/6788754250849186019091129738088810 + 1362181378384807202322670643292642/678875425084918601909112973808881

Which is much better, because the variables are gone. However it is still a very large piece of data to look at, and if I repeat the process of putting this answer back into MATLAB then it finally spits out the answer of 2.9581.

How can I make this process automatic, so that when I execute my code I am straight away presented with 2.9581?

Upvotes: 1

Views: 65

Answers (2)

kabdulla
kabdulla

Reputation: 5429

One option would be to use eval. This can be done in one line with just your expression as the argument passed in.

In example below eval used before yf a assigned returns the same expression. But after they're assigned gives a numeric answer:

syms yf a
expression = ((yf^2*((138454006717812460917559315575*a^2)/20282409603651670423947251286016 + 20)^2 - 20*yf^2*((19169511976216058056763775016476506759529226598462437580625*a^4)/6582018229284824168619876730229402019930943462534319453394436096 - (39872549173714278543306705994157*a^2)/40564819207303340847894502572032 + 25))^(1/2) - (- (138454006717812460917559315575*a^2)/20282409603651670423947251286016 - 20)*yf)/(10*yf^2);
eval(expression)
yf = 1.0;
a = 2.0;
eval(expression)

Upvotes: 1

AVK
AVK

Reputation: 2149

You can substitue subexpressions using subs and convert the resulting expression without variables to a numeric value using double:

s= ((yf^2*... % Your symbolic expression here
s= subs(s,yf,100) % Replace yf with value, for example, 100
s= subs(s,a,101) % Replace a with value, for example, 101
v= double(s) % Convert the symbolic expression to a numeric value

Upvotes: 0

Related Questions