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:
Symbolic Object -> Function Handle

Subject: Symbolic Object -> Function Handle

From: amulder

Date: 3 Jul, 2008 20:09:15

Message: 1 of 5

I have a symbolic function, f, of 4 variables:

syms x y a b real
v = [x y];
w = [a b];
f = w * v'

I would like to pass the resultant expression "a*x+b*y" in terms of the 4 variables to an optimization routine.

My only solution is to copy-paste "a*x+b*y" to a function handle, and then call the optimization routine like this:

clear a b x y;
g = @(a,b,x,y) a*x+b*y ; % copy-paste here
loss = @(p) g(p(1),p(2),p(3),p(4));
optimization(loss);

How do I get MATLAB to evaluate w*v' symbolically and then pass it to optimization as if it were a function handle?

Subject: Symbolic Object -> Function Handle

From: roberson@ibd.nrc-cnrc.gc.ca (Walter Roberson)

Date: 3 Jul, 2008 20:59:57

Message: 2 of 5

In article <15441583.1215115785775.JavaMail.jakarta@nitrogen.mathforum.org>,
amulder <team46_andrew@hotmail.com> wrote:
>I have a symbolic function, f, of 4 variables:

>syms x y a b real
>v = [x y];
>w = [a b];
>f = w * v'

>I would like to pass the resultant expression "a*x+b*y" in terms of the 4 variables to an optimization routine.

>My only solution is to copy-paste "a*x+b*y" to a function handle, and then call the optimization routine like this:

You could probably use inline() -- though you would need to identify
the variables involved and pass those as the additional parameters to
inline() (otherwise it will think the only parameter to the function
is 'x'.) In maple, identifying the names could be done with
indents(Expression,'name') but I don't know whether that maple call
is available through the Matlab interface or what type it would return.
--
  "To all, to each! a fair good-night,
   And pleasing dreams, and slumbers light" -- Sir Walter Scott

Subject: Symbolic Object -> Function Handle

From: amulder

Date: 3 Jul, 2008 22:35:57

Message: 3 of 5

> You could probably use inline() -- though you would
> need to identify
> the variables involved and pass those as the
> additional parameters to
> inline() (otherwise it will think the only parameter
> to the function
> is 'x'.)

Excellent. The inline command works when I convert the symbolic f to a string like this: g = inline(char(f)).

Thanks!

Subject: Symbolic Object -> Function Handle

From: Steven Lord

Date: 8 Jul, 2008 15:05:12

Message: 4 of 5


"amulder" <team46_andrew@hotmail.com> wrote in message
news:15441583.1215115785775.JavaMail.jakarta@nitrogen.mathforum.org...
>I have a symbolic function, f, of 4 variables:
>
> syms x y a b real
> v = [x y];
> w = [a b];
> f = w * v'
>
> I would like to pass the resultant expression "a*x+b*y" in terms of the 4
> variables to an optimization routine.
>
> My only solution is to copy-paste "a*x+b*y" to a function handle, and then
> call the optimization routine like this:
>
> clear a b x y;
> g = @(a,b,x,y) a*x+b*y ; % copy-paste here
> loss = @(p) g(p(1),p(2),p(3),p(4));
> optimization(loss);
>
> How do I get MATLAB to evaluate w*v' symbolically and then pass it to
> optimization as if it were a function handle?

Use FINDSYM to find the symbolic variables in your expression, then write an
anonymous function that calls SUBS on your symbolic expression. Note that
you will need to know in which order FINDSYM returns the variables (or if
you know which variables are included in your expression, list them in the
order you want rather than using FINDSYM):

syms x y a b real
v = [x y];
w = [a b];
f = w * v'
g = @(vec) subs(f, findsym(f), vec)

As I mentioned above, since findsym(f) returns 'a, b, x, y' that's the order
in which values will be substituted into your expression.

--
Steve Lord
slord@mathworks.com

Subject: Symbolic Object -> Function Handle

From: amulder

Date: 9 Jul, 2008 15:09:01

Message: 5 of 5

>
> Use FINDSYM to find the symbolic variables in your
> expression, then write an
> anonymous function that calls SUBS on your symbolic
> expression. Note that
> you will need to know in which order FINDSYM returns
> the variables (or if
> you know which variables are included in your
> expression, list them in the
> order you want rather than using FINDSYM):
>
> syms x y a b real
> v = [x y];
> w = [a b];
> f = w * v'
> g = @(vec) subs(f, findsym(f), vec)
>
> As I mentioned above, since findsym(f) returns 'a, b,
> x, y' that's the order
> in which values will be substituted into your
> expression.
>
> --
> Steve Lord
> slord@mathworks.com
>
>

The findsym() method works, but when I call an optimization routine it is about 10 times as slow as copying and pasting the symbolic expression into an anonymous function.

Fast code with copy-paste:

g = @(a,b,x,y) a*x+b*y ; % copy-paste here
loss = @(p) g(p(1),p(2),p(3),p(4));
optimization(loss);

Slower code without copy-paste:

loss = @(vec) subs(f, findsym(f), vec)
optimization(loss);

Both 'loss' functions give the same values at trial values of a,b,x,y (when the parameters are in the same order), and both require 16 bytes as class 'function_handle array'.

Where does the speed discrepancy come from?

Tags for this Thread

No tags are associated with 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