Got Questions? Get Answers.
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:
objects containing functions

Subject: objects containing functions

From: Simon Preston

Date: 29 Mar, 2010 17:33:09

Message: 1 of 7

Hi all,

I would like to represent a dynamical model (defined in terms of ODEs, i.e., dx_dt = f(x,t;theta)) as a Matlab object. To do so, I would like the object to contain the function f as a property.

One way to achieve this is to define f as an inline function. The trouble doing this is that solving the model with an ODE solver requires repeated evaluations of this inline function, which is extemely slow.

I could of course have f as a function separate from my model object, but this loses the nice convenient and tidy property that the model object is self contained.

Any suggestions how I could do this better - i.e. ways of defining f within the object, but so it is as fast to evaluate as if it were a separate function?

Thanks, S

Subject: objects containing functions

From: Steven Lord

Date: 29 Mar, 2010 17:53:13

Message: 2 of 7


"Simon Preston" <preston.simon+mathsworks@gmail.com> wrote in message
news:hoqo8l$4vl$1@fred.mathworks.com...
> Hi all,
>
> I would like to represent a dynamical model (defined in terms of ODEs,
> i.e., dx_dt = f(x,t;theta)) as a Matlab object. To do so, I would like
> the object to contain the function f as a property.
>
> One way to achieve this is to define f as an inline function. The trouble
> doing this is that solving the model with an ODE solver requires repeated
> evaluations of this inline function, which is extemely slow.
>
> I could of course have f as a function separate from my model object, but
> this loses the nice convenient and tidy property that the model object is
> self contained.
>
> Any suggestions how I could do this better - i.e. ways of defining f
> within the object, but so it is as fast to evaluate as if it were a
> separate function?

Use a function handle.

--
Steve Lord
slord@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ

Subject: objects containing functions

From: Rob Campbell

Date: 29 Mar, 2010 17:53:05

Message: 3 of 7


> Any suggestions how I could do this better - i.e. ways of defining f within the object, but so it is as fast to evaluate as if it were a separate function?
>
> Thanks, S

I don't know exactly what you mean by an "in-line function" in this case. You mean you created a function handle or that you've defined a sub-function within another function?

The last few releases of Matlab have made it very easy to create object properties.
www.mathworks.com/products/matlab/object_oriented_programming.html
www.mathworks.com/company/newsletters/digest/2008/sept/matlab-objects.html
I've used this for problems in the psat but haven't bench-marked it. In earlier versions you had to put your functions into a funny "@myObject" directory. Never used those.

Subject: objects containing functions

From: Simon Preston

Date: 30 Mar, 2010 09:18:02

Message: 4 of 7

"Steven Lord" <slord@mathworks.com> wrote in message <hoqpdm$nq9$1@fred.mathworks.com>...
>
> "Simon Preston" <preston.simon+mathsworks@gmail.com> wrote in message
> news:hoqo8l$4vl$1@fred.mathworks.com...

> > Any suggestions how I could do this better - i.e. ways of defining f
> > within the object, but so it is as fast to evaluate as if it were a
> > separate function?
>
> Use a function handle.

Steve and Rob, thanks for your replies.

By inline, I mean using the function "inline" which I think stores the function as a string and uses "eval" whenever called, hence being slow.

Steve's suggestion to use function handles gives a big improvement over inline. However, for my function f to be contained within the object, (I think) I need to use an anonymous function - I have tried this, and it runs slower by a factor of 2 compared with calling a function defined in the standard way. Why is this and can it be avoided?

Running the below code repeatedly shows the issue.

Thanks for your help, S



function [] = test_anon_function_speed()

fn_anon = @(t,x,theta)[-theta(1)*x(1)+theta(1)*x(2);theta(2)*x(1)-x(2)-x(1)*x(3);-theta(3)*x(3)+x(1)*x(2)];

theta = rand(3,1);
x = rand(3,1);

tic;fn([],x,theta);toc;

tic;fn_anon([],x,theta);toc;

% ---------------------------------------------------------------------

function out = fn(t,x,theta)

out = [-theta(1)*x(1)+theta(1)*x(2);theta(2)*x(1)-x(2)-x(1)*x(3);-theta(3)*x(3)+x(1)*x(2)];

Subject: objects containing functions

From: Rob Campbell

Date: 30 Mar, 2010 12:32:04

Message: 5 of 7


> Steve's suggestion to use function handles gives a big improvement over inline. However, for my function f to be contained within the object, (I think) I need to use an anonymous function - I have tried this, and it runs slower by a factor of 2 compared with calling a function defined in the standard way. Why is this and can it be avoided?

I don't know why that would be the case.
Have you tried having your "fn(t,x,theta)" be an object method?

Subject: objects containing functions

From: Simon Preston

Date: 30 Mar, 2010 15:10:25

Message: 6 of 7

"Rob Campbell" <matlab@robertREMOVEcampbell.removethis.co.uk> wrote in message <hosr04$ss2$1@fred.mathworks.com>...
>
> > Steve's suggestion to use function handles gives a big improvement over inline. However, for my function f to be contained within the object, (I think) I need to use an anonymous function - I have tried this, and it runs slower by a factor of 2 compared with calling a function defined in the standard way. Why is this and can it be avoided?
>
> I don't know why that would be the case.
> Have you tried having your "fn(t,x,theta)" be an object method?

Rob, thanks for the suggestion - though the f really needs to be a property of the object, so can't be hard-coded as a method.

Interestingly there doesn't seem to be such difference in run times when the function output is scalar (rather than vector). Not sure if maybe this gives a clue as to the source of slowness.

Best wishes, S

Subject: objects containing functions

From: Simon Preston

Date: 7 Apr, 2010 12:40:22

Message: 7 of 7

"Simon Preston" <preston.simon+mathsworks@gmail.com> wrote in message <hot491$brj$1@fred.mathworks.com>...
> "Rob Campbell" <matlab@robertREMOVEcampbell.removethis.co.uk> wrote in message <hosr04$ss2$1@fred.mathworks.com>...
> >
> > > Steve's suggestion to use function handles gives a big improvement over inline. However, for my function f to be contained within the object, (I think) I need to use an anonymous function - I have tried this, and it runs slower by a factor of 2 compared with calling a function defined in the standard way. Why is this and can it be avoided?
> >
> > I don't know why that would be the case.
> > Have you tried having your "fn(t,x,theta)" be an object method?
>
> Rob, thanks for the suggestion - though the f really needs to be a property of the object, so can't be hard-coded as a method.
>
> Interestingly there doesn't seem to be such difference in run times when the function output is scalar (rather than vector). Not sure if maybe this gives a clue as to the source of slowness.
>
> Best wishes, S

Sorry for the bump, but can anybody (Steve maybe?) explain the reasons for anonymous functions being slower compare with a standard function defined in an m-file. I.e. what are the relative overheads, what factors do they depend on and and how do the overheads scale with these factors?

Many thanks, S

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