First, it's worth pointing out that if you call whos, symbolic variables always show up as 8 bytes. That's not because that's literally how much space they take up, but because whos doesn't really work for symbolic variables. One thing you might try is to use simplify at an earlier stage in your construction of YB. Presumably YB is the result of some intermediate calculations. If you tried to use simplify at each step of the intermediate calculations, it would "break up" the simplification task into a series of smaller ones that may help you avoid an out-of-memory problem. This isn't guaranteed to work, though.

A better option is to re-think your use of symbolic math in the first place. I can see three good reasons to use symbolic math:

- You want your eventual output to be symbolic (i.e., you're directly interested in an algebraic expression).
- You need to apply a symbolic operation somewhere (e.g., differentiation).
- You have a bunch of intermediate steps to your operations, which you expect will lead to opportunities for significant algebraic simplifications along the way, whereas if you did all those steps numerically you might accumulate unacceptable numerical inaccuracies

A contrived example of #3 would be:

a = 1e18;

x = 1;

y = x+a;

z = y-a;

a = sym(1e18);

x = sym(1);

y = x+a;

z = y-a:

Since you're making this function, I'm assuming #1 doesn't apply. If #3 applies, you're kind of stuck (though in that case using simplify at all intermediate steps as I suggested above is more likely to help).

If #2 applies, is there any way you can "isolate" that symbolic-operation step and use matlabFunction only for that part? As a somewhat contrived example, suppose you initially had

x = sym('x',[2,1]);

y = [log(x(1)+x(2));log(x(1)-x(2))];

yJac = jacobian(y,x);

A = sym('A',[2,2]);

z = A.'*yJac*A;

fz = matlabFunction(z,'Vars',{x,A});

Here, it's really only the Jacobian step that you need to do symbolically, and that step returns a relatively simple expression. However, because z ends up being a somewhat complicated symbolic expression, the resulting function fz is also pretty complicated. An alternative way to do this that would make the matlabFunction step much simpler (and therefore less likely to cause memory problems in more complicated examples) is:

x = sym('x',[2,1]);

y = [log(x(1)+x(2));log(x(1)-x(2))];

yJac = jacobian(y,x);

fyJac = matlabFunction(yJac,'Vars',{x});

fz2 = @(x2,A) A.'*fyJac(x2)*A;

Here, we've stopped using symbolics after the Jacobian step, and in particular we're now only evaluating z numerically, rather than first creating it symbolically and then converting that to a numeric function. Since yJac is a much simpler expression than the symbolic z was, fyJac is in turn much simpler than fz. Hopefully you can see how this kind of strategy can greatly reduce the computation and memory burden of the call to matlabFunction.

## 1 Comment

## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/598456-out-of-memory-when-converting-symbolic-matrix-into-function-or-by-simplifying-it#comment_1021552

⋮## Direct link to this comment

https://au.mathworks.com/matlabcentral/answers/598456-out-of-memory-when-converting-symbolic-matrix-into-function-or-by-simplifying-it#comment_1021552

Sign in to comment.