Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Faster symbolic substitution

Subject: Faster symbolic substitution

From: Dennis

Date: 5 Dec, 2008 17:15:03

Message: 1 of 8

Hi,

I have a large symbolic Jacobian matrix (400 elements 30variables). I am using "subs" to evaluate it at every iteration (of my algorithm). However, this is taking too long. Any tricks to make it swifter?

Thanks,
Dennis

Subject: Faster symbolic substitution

From: Alan Weiss

Date: 6 Dec, 2008 01:47:10

Message: 2 of 8

Dennis wrote:
> Hi,
>
> I have a large symbolic Jacobian matrix (400 elements 30variables). I am using "subs" to evaluate it at every iteration (of my algorithm). However, this is taking too long. Any tricks to make it swifter?
>
> Thanks,
> Dennis

Try using matlabFunction. It takes a long time to generate a file, but
after that things will be swift.
http://www.mathworks.com/access/helpdesk/help/toolbox/symbolic/brt7nuv.html#brt7nyh

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: Faster symbolic substitution

From: emre

Date: 5 Dec, 2011 21:48:08

Message: 3 of 8

Alan Weiss <aweiss@mathworks.com> wrote in message <ghcliu$1am$1@fred.mathworks.com>...
> Dennis wrote:
> > Hi,
> >
> > I have a large symbolic Jacobian matrix (400 elements 30variables). I am using "subs" to evaluate it at every iteration (of my algorithm). However, this is taking too long. Any tricks to make it swifter?
> >
> > Thanks,
> > Dennis
>
> Try using matlabFunction. It takes a long time to generate a file, but
> after that things will be swift.
> http://www.mathworks.com/access/helpdesk/help/toolbox/symbolic/brt7nuv.html#brt7nyh
>
> Alan Weiss
> MATLAB mathematical toolbox documentation

Even if i didnt ask the question for fast substitution, this comment has been really life saver for me. I was working on some online intelligent learning algorithm and it needed to be fast at each iteration. Your comment helped a lot to reduce a very complicated symbolic matrix substitution time from hours to seconds! Thanks very much..

Subject: Faster symbolic substitution

From: Alan Weiss

Date: 5 Dec, 2011 22:04:26

Message: 4 of 8

On 12/5/2011 4:48 PM, emre wrote:
> Alan Weiss <aweiss@mathworks.com> wrote in message
> <ghcliu$1am$1@fred.mathworks.com>...
>> Dennis wrote:
>> > Hi,
>> > > I have a large symbolic Jacobian matrix (400 elements
>> 30variables). I am using "subs" to evaluate it at every iteration (of
>> my algorithm). However, this is taking too long. Any tricks to make it
>> swifter?
>> > > Thanks,
>> > Dennis
>>
>> Try using matlabFunction. It takes a long time to generate a file, but
>> after that things will be swift.
>> http://www.mathworks.com/access/helpdesk/help/toolbox/symbolic/brt7nuv.html#brt7nyh
>>
>>
>> Alan Weiss
>> MATLAB mathematical toolbox documentation
>
> Even if i didnt ask the question for fast substitution, this comment has
> been really life saver for me. I was working on some online intelligent
> learning algorithm and it needed to be fast at each iteration. Your
> comment helped a lot to reduce a very complicated symbolic matrix
> substitution time from hours to seconds! Thanks very much..

I'm glad the suggestion proved valuable. Thanks you for letting me know
that it helped.

Alan Weiss
MATLAB mathematical toolbox documentation

Subject: Faster symbolic substitution

From: Carlos

Date: 21 Nov, 2012 12:35:08

Message: 5 of 8

Hi, I have the same problem with large 20x20 symbolic jacobian and I can´t figure out how to use Matlabfunction to solve my problem.
The first is to build the handle G
G = matlabFunction(Jacobian)
My jacobian has 20 variables x1..x20.
So if I did G(0,0,0...0) Would I obtain the a numerical matrix result of substituting the 20 variables by zero.
Thank you

Subject: Faster symbolic substitution

From: Christopher Creutzig

Date: 22 Nov, 2012 09:33:05

Message: 6 of 8

On 21.11.12 13:35, Carlos wrote:
> Hi, I have the same problem with large 20x20 symbolic jacobian and I can´t figure out how to use Matlabfunction to solve my problem.
> The first is to build the handle G
> G = matlabFunction(Jacobian)
> My jacobian has 20 variables x1..x20.
> So if I did G(0,0,0...0) Would I obtain the a numerical matrix result of substituting the 20 variables by zero.

Basically, yes. It probably wouldn't hurt to explicitly tell
matlabFunction the order of variables you expect, though.

If this doesn't work as expected, please create a small complete example
showing what you see and saying what you expected to see instead.


Christopher

Subject: Faster symbolic substitution

From: Jan

Date: 7 Jun, 2014 15:11:07

Message: 7 of 8

Hello,

 I have got similar problem as guys above.
I am working on description of robotic arm. It is composed of six serial transformations - result of previous transformation is one of inputs of the next transformation. And every transformation is composed of few "subtransformations". The problem is, that creation of last tranformation is extremely slow. It took more than one half of a day and it still was not finished. I have to use 33 symbolic variables and the final equations are really complex. I have already found out, that I can rapidly decrease computing time (I used this in case, which was not so complex) by substituting inputs dependent on previous result by some general vector and than use "subs" to create final expression (see exapmle). But in this case, I have got so complex expressions, that either with this help computing simply takes too long.
I have already found out, that I can create function handle via matlabFunction and accelerate substitution of numbers, but is there any way of making the process of creating the "general expression" faster, please?

Example
Suppose I am interested in final vector R2 (dim 3), but following sequence takes too long

R0=transformation(a1,a2,a3...,initial vector);
R1=transformation(b1,b2,b3....,R0);
R2=transformation(c1,c2,c3....,R1);

So i used something like

R0=transformation(a1,a2,a3...,initial vector);
R1=transformation(b1,b2,b3....,S);
R2=transformation(c1,c2,c3....,R1);

R2=subs(R2,S,R0)

what increased the speed of the code.

(But now I have got six transformations and subs does not help a lot, do you know, how implement matlabFunction into this example? And would it be useful?)

Thank you very much!

Jan

Subject: Faster symbolic substitution

From: Christopher Creutzig

Date: 11 Jun, 2014 12:39:42

Message: 8 of 8

On 6/7/14 5:11 PM, Jan wrote:

> I have already found out, that I can create function handle via matlabFunction and accelerate substitution of numbers, but is there any way of making the process of creating the "general expression" faster, please?

Without seeing the actual formulas, this is just stabbing in the dark,
but you may find it useful to call simplify on your intermediate
results. Also, if you are not actually interested in the symbolic
result, but only want it to plug in numbers, using a sequence of
functions generated by matlabFunction may be a better option.

> So i used something like
>
> R0=transformation(a1,a2,a3...,initial vector);
> R1=transformation(b1,b2,b3....,S);
> R2=transformation(c1,c2,c3....,R1);
>
> R2=subs(R2,S,R0)
>
> what increased the speed of the code.
>
> (But now I have got six transformations and subs does not help a lot, do you know, how implement matlabFunction into this example? And would it be useful?)

fR0 = matlabFunction(R0, ...);
fR2 = matlabFunction(R2, ...);
value = fR2(c1,c2,...,fR0(a1,a2,...))


HTH,

Christopher

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us