Rikki-Tikki-Tavi
Rikki-Tikki-Tavi

Reputation: 203

Use Subexpressions to Simplify Long Symbolic Equations in Matlab

My question is essentially the same as this one:

Simplifying a very long symbolic expression by automatically introducing temporal variables or in any other way

However, I don't have Mathematica and the question was not answered for Matlab, further, this is 2 years old and maybe people have had some ideas since then.

Like Andrey, I have an extremely large equation (about 13000 characters) as a result of a multiple-chain-rule derivate of an already long function. The derivate contains numerous instances of the subfunctions in the original equation and their derivates. I believe it's possible to compress the function to at least a 10th with suitable replacements. I am wondering if there is a way to have Matlab make these replacements automatically for me.

This is also relevant from a performance standpoint, because I have thens of these equations that are evaluated for a Jacobi matrix to solve a complicated system of nonlinear equations.

Thank you for your time.

Upvotes: 4

Views: 2624

Answers (2)

Rikki-Tikki-Tavi
Rikki-Tikki-Tavi

Reputation: 203

I managed to help myself with two things (but thank you for the quick answer, EJG89!):

a) instead of inserting an expression for each subfunction, I leave them as unspecified symbolic functions like so:

%M_g(a,b,c) = F(a,b,c)/G(a,b,c)^2*...
syms M_g(a,b,c)

Now when I have a function

F_a(a,b,c) = .../M_g(a,b,c)*...

And derive by c for example. I get an answer in the form of D([3], M_g) wherever the derivate appears.

b) the Matlab function subs can be used to replace find and replace expressions in a symbolic function, but I prefer the first solution, because I think it carries less risk of making errors because of inattentiveness.

Any more ideas?

Upvotes: 0

EJG89
EJG89

Reputation: 1189

The closest thing MatLab has is the subexpr() function. For instance you have a formula:

% Declare symnbolic    
syms x
% Define equation
eq1 = x^2 + 3*x^2 + 6*x^2 + x^2/57*x + sqrt(x^2)

% Simplify by substitution
subexpr(eq1)

With the output:

sigma = 

x^2

ans =

10*sigma + (sigma*x)/57 + sigma^(1/2)

Source: http://www.mathworks.nl/help/symbolic/subexpr.html

Upvotes: 3

Related Questions