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:
Help needed for anonymous function

Subject: Help needed for anonymous function

From: John

Date: 24 Feb, 2009 09:50:18

Message: 1 of 12

Hi all,

I need some help for using anonymous function.
I would like to solve for x in a system of 2 equations. However, in the 2 by 2 matrix, I need to perform numerical integration where the integrand is a function of t and of x.
a portion of the code:
D{1} = @(x) (quad(@(t) (t.^(lambda-1).*exp(-kappa*(b-x(1))*t)),0,10));
D{2} = @(x) (quad(@(t) (t.^(lambda-1).*exp(kappa*(b-x(1))*t)),0,10));
etc
However, when I want to perform matrix multiplication, I can't do that as anonymous functions do not support multiplication of matrices. (eg. inv(D)*B-C)
Any help would be appreciated.

Subject: Help needed for anonymous function

From: Steven Lord

Date: 24 Feb, 2009 14:53:17

Message: 2 of 12


"John " <onlylf2@hotmail.com> wrote in message
news:go0fsq$dld$1@fred.mathworks.com...
> Hi all,
>
> I need some help for using anonymous function.
> I would like to solve for x in a system of 2 equations. However, in the 2
> by 2 matrix, I need to perform numerical integration where the integrand
> is a function of t and of x.
> a portion of the code:
> D{1} = @(x) (quad(@(t) (t.^(lambda-1).*exp(-kappa*(b-x(1))*t)),0,10));
> D{2} = @(x) (quad(@(t) (t.^(lambda-1).*exp(kappa*(b-x(1))*t)),0,10));
> etc
> However, when I want to perform matrix multiplication, I can't do that as
> anonymous functions do not support multiplication of matrices. (eg.
> inv(D)*B-C)

Performing operations on D, or on a regular anonymous function handle,
doesn't make sense. Performing operations on _the result returned by the
function handle_ does. Define one more function handle:

 % assuming D has 4 elements
M = @(x) [D{1}(x) D{2}(x); D{3}(x) D{4}(x)];
% or
M = @(x) reshape(cellfun(@(z) z(x), D), [2 2]);

and use M(x) in your calculations.

As a side note, you probably shouldn't be inverting your matrix unless you
absolutely need to and you know your matrix is not ill-conditioned. Use
backslash instead.

--
Steve Lord
slord@mathworks.com

Subject: Help needed for anonymous function

From: John

Date: 24 Feb, 2009 17:17:02

Message: 3 of 12

Hi,

I tried ur approach but i got an error message.
Below is a portion of my code with the error message:
M = @(x) [D{1}(x) D{2}(x); D{3}(x) D{4}(x)];
N = @(x) [E{1}(x); E{2}(x)];
O = @(x) [G{1}(x) G{2}(x); G{3}(x) G{4}(x)];
P = @(x) [H{1}(x); H{2}(x)];
f = O*M\N-P;
??? Function 'mtimes' is not defined for values of class 'function_handle'.


"Steven Lord" <slord@mathworks.com> wrote in message <go11kt$1or$1@fred.mathworks.com>...
> Performing operations on D, or on a regular anonymous function handle,
> doesn't make sense. Performing operations on _the result returned by the
> function handle_ does. Define one more function handle:
>
> % assuming D has 4 elements
> M = @(x) [D{1}(x) D{2}(x); D{3}(x) D{4}(x)];
> % or
> M = @(x) reshape(cellfun(@(z) z(x), D), [2 2]);
>
> and use M(x) in your calculations.
>
> As a side note, you probably shouldn't be inverting your matrix unless you
> absolutely need to and you know your matrix is not ill-conditioned. Use
> backslash instead.
>
> --
> Steve Lord
> slord@mathworks.com
>

Subject: Help needed for anonymous function

From: Steven Lord

Date: 24 Feb, 2009 18:06:39

Message: 4 of 12


"John " <onlylf2@hotmail.com> wrote in message
news:go1a2e$p8o$1@fred.mathworks.com...
> Hi,
>
> I tried ur approach but i got an error message.
> Below is a portion of my code with the error message:
> M = @(x) [D{1}(x) D{2}(x); D{3}(x) D{4}(x)];
> N = @(x) [E{1}(x); E{2}(x)];
> O = @(x) [G{1}(x) G{2}(x); G{3}(x) G{4}(x)];
> P = @(x) [H{1}(x); H{2}(x)];
> f = O*M\N-P;
> ??? Function 'mtimes' is not defined for values of class
> 'function_handle'.

As I said:

> "Steven Lord" <slord@mathworks.com> wrote in message
> <go11kt$1or$1@fred.mathworks.com>...
>> Performing operations on D, or on a regular anonymous function handle,
>> doesn't make sense. Performing operations on _the result returned by the
>> function handle_ does. Define one more function handle:

You're still trying to operate on the function handles, not the values you
receive when you evaluate the function handles.

x = 5;
f = O(x)*M(x)\N(x)-P(x)

% or

fh = @(x) O(x)*M(x)\N(x)-P(x);
f = fh(5)

should work.

If you really want to operate symbolically (i.e. have f as a function of x)
and all the functions you use in D, E, G, and H support sym objects from
Symbolic Math Toolbox:

x = sym('x');
f = O(x)*M(x)\N(x)-P(x) % Evaluating the function handles for the sym
variable x

--
Steve Lord
slord@mathworks.com

Subject: Help needed for anonymous function

From: John

Date: 25 Feb, 2009 06:07:02

Message: 5 of 12

I understand that f(5) would work.
But x(1), x(2) is the solution that I am looking for. That's why I have to get a function f in terms of x and perform fsolve on the function.
However, I seem to be facing some problems with x(1) and x(2).
when i write x=sym('x') it seems like matlab does not take into account that x can be a vector.
partial code:
D{1} = @(x) (quad(@(t) (t.^(lambda-1).*exp(-kappa*(b-x(1))*t)),0,ulimit));
D{2} = @(x) -(quad(@(t) (t.^(lambda-1).*exp(kappa*(b-x(1))*t)),0,ulimit));
...

E{1} = @(x) (exp(x(1))*(1+K));
E{2} = @(x) (exp(x(1))*(1+K)/kappa);

G{1} = @(x) (quad(@(t) (t.^(lambda-1).*exp(-kappa*(b-x(2))*t)),0,ulimit));
G{2} = @(x) -(quad(@(t) (t.^(lambda-1).*exp(kappa*(b-x(2))*t)),0,ulimit));
...

H{1} = @(x) (exp(x(2))*(1-K));
H{2} = @(x) (exp(x(2))*(1-K)/kappa);

So basically, my D (2 by 2 matrix) and E (2 by 1) are functions of x(1) and G (2 by 2 matrix) and H (2 by 1) are functions of x(2)

M = @(x) [D{1}(x) D{2}(x); D{3}(x) D{4}(x)];
N = @(x) [E{1}(x); E{2}(x)];
O = @(x) [G{1}(x) G{2}(x); G{3}(x) G{4}(x)];
P = @(x) [H{1}(x); H{2}(x)];

x = sym('x');
f = O(x)*M(x)\N(x)-P(x)

gives the following error
??? Index exceeds matrix dimensions.
Error in ==> @(t) (t.^(lambda-1).*exp(-kappa*(b-x(2))*t))
and some quad error as well.

Thanks.

"Steven Lord" <slord@mathworks.com> wrote in message <go1cvf$hle$1@fred.mathworks.com>...
>
> "John " <onlylf2@hotmail.com> wrote in message
> news:go1a2e$p8o$1@fred.mathworks.com...
> > Hi,
> >
> > I tried ur approach but i got an error message.
> > Below is a portion of my code with the error message:
> > M = @(x) [D{1}(x) D{2}(x); D{3}(x) D{4}(x)];
> > N = @(x) [E{1}(x); E{2}(x)];
> > O = @(x) [G{1}(x) G{2}(x); G{3}(x) G{4}(x)];
> > P = @(x) [H{1}(x); H{2}(x)];
> > f = O*M\N-P;
> > ??? Function 'mtimes' is not defined for values of class
> > 'function_handle'.
>
> As I said:
>
> > "Steven Lord" <slord@mathworks.com> wrote in message
> > <go11kt$1or$1@fred.mathworks.com>...
> >> Performing operations on D, or on a regular anonymous function handle,
> >> doesn't make sense. Performing operations on _the result returned by the
> >> function handle_ does. Define one more function handle:
>
> You're still trying to operate on the function handles, not the values you
> receive when you evaluate the function handles.
>
> x = 5;
> f = O(x)*M(x)\N(x)-P(x)
>
> % or
>
> fh = @(x) O(x)*M(x)\N(x)-P(x);
> f = fh(5)
>
> should work.
>
> If you really want to operate symbolically (i.e. have f as a function of x)
> and all the functions you use in D, E, G, and H support sym objects from
> Symbolic Math Toolbox:
>
> x = sym('x');
> f = O(x)*M(x)\N(x)-P(x) % Evaluating the function handles for the sym
> variable x
>
> --
> Steve Lord
> slord@mathworks.com
>

Subject: Help needed for anonymous function

From: John

Date: 25 Feb, 2009 10:54:01

Message: 6 of 12

Hi,

Now I tried the following but i can't perform fsolve on it.
D{1} = @(x) (quad(@(t) (t.^(lambda-1).*exp(-kappa*(b-x)*t)),0,ulimit));
D{2} = @(x) -(quad(@(t) (t.^(lambda-1).*exp(kappa*(b-x)*t)),0,ulimit));
...

E{1} = @(x) (exp(x)*(1+K));
E{2} = @(x) (exp(x)*(1+K)/kappa);

G{1} = @(y) (quad(@(t) (t.^(lambda-1).*exp(-kappa*(b-y)*t)),0,ulimit));
G{2} = @(y) -(quad(@(t) (t.^(lambda-1).*exp(kappa*(b-y)*t)),0,ulimit));
...

H{1} = @(y) (exp(y)*(1-K));
H{2} = @(y) (exp(y)*(1-K)/kappa);

M = @(x) [D{1}(x) D{2}(x); D{3}(x) D{4}(x)];
N = @(x) [E{1}(x); E{2}(x)];
O = @(y) [G{1}(y) G{2}(y); G{3}(y) G{4}(y)];
P = @(y) [H{1}(y); H{2}(y)];

f = @(x,y) (O*M\N-P);
z = fsolve(@(x,y) f, [0,0])

and i am faced with the following error:
Warning: To support parenthesis notation for invocation, "indexing" a scalar
 function handle by ":" will continue to work in R2007a, but may be invalid or may
 work differently in future releases of MATLAB. To avoid this warning and
 possible future errors, use cell arrays of function handles instead of arrays.
 For more information, type 'help function_handle' and see the section at the end
 entitled "Note on Backward Compatibility."
> In fsolve at 196
Warning: Default trust-region dogleg method of FSOLVE cannot
 handle non-square systems; using Gauss-Newton method instead.
> In fsolve at 250
??? Undefined function or method 'full' for input arguments of type 'function_handle'.

Error in ==> nlsq at 87
CostFunction = full(CostFunction);

Error in ==> fsolve at 302
    [x,FVAL,JACOB,EXITFLAG,OUTPUT,msg] = ...

"John " <onlylf2@hotmail.com> wrote in message <go2n66$i9v$1@fred.mathworks.com>...
> I understand that f(5) would work.
> But x(1), x(2) is the solution that I am looking for. That's why I have to get a function f in terms of x and perform fsolve on the function.
> However, I seem to be facing some problems with x(1) and x(2).
> when i write x=sym('x') it seems like matlab does not take into account that x can be a vector.
> partial code:
> D{1} = @(x) (quad(@(t) (t.^(lambda-1).*exp(-kappa*(b-x(1))*t)),0,ulimit));
> D{2} = @(x) -(quad(@(t) (t.^(lambda-1).*exp(kappa*(b-x(1))*t)),0,ulimit));
> ...
>
> E{1} = @(x) (exp(x(1))*(1+K));
> E{2} = @(x) (exp(x(1))*(1+K)/kappa);
>
> G{1} = @(x) (quad(@(t) (t.^(lambda-1).*exp(-kappa*(b-x(2))*t)),0,ulimit));
> G{2} = @(x) -(quad(@(t) (t.^(lambda-1).*exp(kappa*(b-x(2))*t)),0,ulimit));
> ...
>
> H{1} = @(x) (exp(x(2))*(1-K));
> H{2} = @(x) (exp(x(2))*(1-K)/kappa);
>
> So basically, my D (2 by 2 matrix) and E (2 by 1) are functions of x(1) and G (2 by 2 matrix) and H (2 by 1) are functions of x(2)
>
> M = @(x) [D{1}(x) D{2}(x); D{3}(x) D{4}(x)];
> N = @(x) [E{1}(x); E{2}(x)];
> O = @(x) [G{1}(x) G{2}(x); G{3}(x) G{4}(x)];
> P = @(x) [H{1}(x); H{2}(x)];
>
> x = sym('x');
> f = O(x)*M(x)\N(x)-P(x)
>
> gives the following error
> ??? Index exceeds matrix dimensions.
> Error in ==> @(t) (t.^(lambda-1).*exp(-kappa*(b-x(2))*t))
> and some quad error as well.
>
> Thanks.
> > Steve Lord
> > slord@mathworks.com
> >

Subject: Help needed for anonymous function

From: Bruno Luong

Date: 25 Feb, 2009 12:46:02

Message: 7 of 12

"John " <onlylf2@hotmail.com> wrote in message <go3809$ccd$1@fred.mathworks.com>...
> Hi,
>
> Now I tried the following but i can't perform fsolve on it.
> D{1} = @(x) (quad(@(t) (t.^(lambda-1).*exp(-kappa*(b-x)*t)),0,ulimit));
> D{2} = @(x) -(quad(@(t) (t.^(lambda-1).*exp(kappa*(b-x)*t)),0,ulimit));
> ...
>
> E{1} = @(x) (exp(x)*(1+K));
> E{2} = @(x) (exp(x)*(1+K)/kappa);
>
> G{1} = @(y) (quad(@(t) (t.^(lambda-1).*exp(-kappa*(b-y)*t)),0,ulimit));
> G{2} = @(y) -(quad(@(t) (t.^(lambda-1).*exp(kappa*(b-y)*t)),0,ulimit));
> ...
>
> H{1} = @(y) (exp(y)*(1-K));
> H{2} = @(y) (exp(y)*(1-K)/kappa);
>
> M = @(x) [D{1}(x) D{2}(x); D{3}(x) D{4}(x)];
> N = @(x) [E{1}(x); E{2}(x)];
> O = @(y) [G{1}(y) G{2}(y); G{3}(y) G{4}(y)];
> P = @(y) [H{1}(y); H{2}(y)];
>
> f = @(x,y) (O*M\N-P);
> z = fsolve(@(x,y) f, [0,0])
>

I can't help but making a little remark: a quick look tells me it's horrible way of coding. Why the need of abusing anonymous functions? I would suggest to put it on an mfile.

Bruno

Subject: Help needed for anonymous function

From: Steven Lord

Date: 25 Feb, 2009 14:55:43

Message: 8 of 12


"John " <onlylf2@hotmail.com> wrote in message
news:go3809$ccd$1@fred.mathworks.com...
> Hi,
>
> Now I tried the following but i can't perform fsolve on it.
> D{1} = @(x) (quad(@(t) (t.^(lambda-1).*exp(-kappa*(b-x)*t)),0,ulimit));
> D{2} = @(x) -(quad(@(t) (t.^(lambda-1).*exp(kappa*(b-x)*t)),0,ulimit));
> ...
>
> E{1} = @(x) (exp(x)*(1+K));
> E{2} = @(x) (exp(x)*(1+K)/kappa);
>
> G{1} = @(y) (quad(@(t) (t.^(lambda-1).*exp(-kappa*(b-y)*t)),0,ulimit));
> G{2} = @(y) -(quad(@(t) (t.^(lambda-1).*exp(kappa*(b-y)*t)),0,ulimit));
> ...
>
> H{1} = @(y) (exp(y)*(1-K));
> H{2} = @(y) (exp(y)*(1-K)/kappa);
>
> M = @(x) [D{1}(x) D{2}(x); D{3}(x) D{4}(x)];
> N = @(x) [E{1}(x); E{2}(x)];
> O = @(y) [G{1}(y) G{2}(y); G{3}(y) G{4}(y)];
> P = @(y) [H{1}(y); H{2}(y)];
>
> f = @(x,y) (O*M\N-P);

> z = fsolve(@(x,y) f, [0,0])

You're still trying to perform operations on the function handles!

You cannot add, subtract, multiply, or divide M, N, O, or P. Period, end of
story.

For purposes of this example, all you can do to a function handle is
evaluate it or put it in a cell array. I'm guessing on the exact syntax you
want based on the way you've defined M, N, O, and P:


f = @(x, y) (O(y)*M(x)\N(x)-P(y));
z = fsolve(@(z) f(z(1), z(2)), [0 0]);


But I am starting to agree with Bruno ... rather than trying to shoehorn
anonymous functions into the framework of what you want to do, I'd start
thinking about writing an M-file function or two at this point.

--
Steve Lord
slord@mathworks.com

Subject: Help needed for anonymous function

From: John

Date: 25 Feb, 2009 16:20:03

Message: 9 of 12

Hi,

Actually I did thought of creating another m-file for the required function.
I was just thinking of lumping everything into 1 file. It seems pretty lousy.
Logically thinking, I thought that was feasible but it seems like I really can't perform any operations on function handles and that's about it. Thanks for the help.

"Steven Lord" <slord@mathworks.com> wrote in message <go3m5f$fkf$1@fred.mathworks.com>...
>
> "John " <onlylf2@hotmail.com> wrote in message
> news:go3809$ccd$1@fred.mathworks.com...
> > Hi,
> >
> > Now I tried the following but i can't perform fsolve on it.
> > D{1} = @(x) (quad(@(t) (t.^(lambda-1).*exp(-kappa*(b-x)*t)),0,ulimit));
> > D{2} = @(x) -(quad(@(t) (t.^(lambda-1).*exp(kappa*(b-x)*t)),0,ulimit));
> > ...
> >
> > E{1} = @(x) (exp(x)*(1+K));
> > E{2} = @(x) (exp(x)*(1+K)/kappa);
> >
> > G{1} = @(y) (quad(@(t) (t.^(lambda-1).*exp(-kappa*(b-y)*t)),0,ulimit));
> > G{2} = @(y) -(quad(@(t) (t.^(lambda-1).*exp(kappa*(b-y)*t)),0,ulimit));
> > ...
> >
> > H{1} = @(y) (exp(y)*(1-K));
> > H{2} = @(y) (exp(y)*(1-K)/kappa);
> >
> > M = @(x) [D{1}(x) D{2}(x); D{3}(x) D{4}(x)];
> > N = @(x) [E{1}(x); E{2}(x)];
> > O = @(y) [G{1}(y) G{2}(y); G{3}(y) G{4}(y)];
> > P = @(y) [H{1}(y); H{2}(y)];
> >
> > f = @(x,y) (O*M\N-P);
>
> > z = fsolve(@(x,y) f, [0,0])
>
> You're still trying to perform operations on the function handles!
>
> You cannot add, subtract, multiply, or divide M, N, O, or P. Period, end of
> story.
>
> For purposes of this example, all you can do to a function handle is
> evaluate it or put it in a cell array. I'm guessing on the exact syntax you
> want based on the way you've defined M, N, O, and P:
>
>
> f = @(x, y) (O(y)*M(x)\N(x)-P(y));
> z = fsolve(@(z) f(z(1), z(2)), [0 0]);
>
>
> But I am starting to agree with Bruno ... rather than trying to shoehorn
> anonymous functions into the framework of what you want to do, I'd start
> thinking about writing an M-file function or two at this point.
>
> --
> Steve Lord
> slord@mathworks.com
>

Subject: Help needed for anonymous function

From: Steven Lord

Date: 25 Feb, 2009 19:04:17

Message: 10 of 12


"John " <onlylf2@hotmail.com> wrote in message
news:go3r3j$4ev$1@fred.mathworks.com...
> Hi,
>
> Actually I did thought of creating another m-file for the required
> function.
> I was just thinking of lumping everything into 1 file. It seems pretty
> lousy.

Keep in mind that you can create multiple function in the same file, if you
need to.

> Logically thinking, I thought that was feasible but it seems like I really
> can't perform any operations on function handles and that's about it.
> Thanks for the help.

No, you can't perform arithmetic on function handles.

Let's step out of the computer world for a moment, and back into
mathematics.

Let's say I have a function f(x) = x^2. What is 4*f? If you said that 4*f
is 4*x^2, you're assuming that f is equivalent to f(x), and I'm not --
4*x^2 is _4*f(x)_, nor 4*f. The parentheses are important! 4*f(x) is
different from 4*f(y), 4*f(x+y), 4*f(x^2), or [assuming the right operators
are defined] 4*f(monkey).

You were trying to compute 4*f, and that doesn't work. My suggestions were
that you compute 4*f(x) or 4*f(y) or 4*f(monkey) instead.

This distinction is what I've been trying to explain during this thread; I
just realized that an example is worth a thousand words. Sorry I didn't
come up with this explanation earlier.

--
Steve Lord
slord@mathworks.com

Subject: Help needed for anonymous function

From: John

Date: 26 Feb, 2009 04:48:02

Message: 11 of 12

Just to double check, so i can have more than 2 functions in 1 m-file right?

Regarding the mathematics part, yup. i understand. It was like I actually do not know the 'x' in f(x) and thus i was trying to solve for it be doing some operations on f. It does make a lot of difference.

"Steven Lord" <slord@mathworks.com> wrote in message <go44nh$gfo$1@fred.mathworks.com>...
>
> "John " <onlylf2@hotmail.com> wrote in message
> news:go3r3j$4ev$1@fred.mathworks.com...
> > Hi,
> >
> > Actually I did thought of creating another m-file for the required
> > function.
> > I was just thinking of lumping everything into 1 file. It seems pretty
> > lousy.
>
> Keep in mind that you can create multiple function in the same file, if you
> need to.
>
> > Logically thinking, I thought that was feasible but it seems like I really
> > can't perform any operations on function handles and that's about it.
> > Thanks for the help.
>
> No, you can't perform arithmetic on function handles.
>
> Let's step out of the computer world for a moment, and back into
> mathematics.
>
> Let's say I have a function f(x) = x^2. What is 4*f? If you said that 4*f
> is 4*x^2, you're assuming that f is equivalent to f(x), and I'm not --
> 4*x^2 is _4*f(x)_, nor 4*f. The parentheses are important! 4*f(x) is
> different from 4*f(y), 4*f(x+y), 4*f(x^2), or [assuming the right operators
> are defined] 4*f(monkey).
>
> You were trying to compute 4*f, and that doesn't work. My suggestions were
> that you compute 4*f(x) or 4*f(y) or 4*f(monkey) instead.
>
> This distinction is what I've been trying to explain during this thread; I
> just realized that an example is worth a thousand words. Sorry I didn't
> come up with this explanation earlier.
>
> --
> Steve Lord
> slord@mathworks.com
>

Subject: Help needed for anonymous function

From: Steven Lord

Date: 26 Feb, 2009 05:07:03

Message: 12 of 12


"John " <onlylf2@hotmail.com> wrote in message
news:go56u2$4ur$1@fred.mathworks.com...
> Just to double check, so i can have more than 2 functions in 1 m-file
> right?

Yes, although only the first one in the file (the "primary function") can be
called directly by functions outside the file.

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-70666.html

http://www.mathworks.com/access/helpdesk/help/techdoc/matlab_prog/f4-39629.html

--
Steve Lord
slord@mathworks.com

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