Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to sum up array of function handles

340 views
Skip to first unread message

Suranita

unread,
Feb 2, 2011, 2:42:06 PM2/2/11
to
Hi,
I have faced a function handle stated below:

s2=@(n,f,phi0) ((n+1-i)/(1-i))*phi0^(1-i)*(f^(1-i)-1);
Now I want to sum up s2 over i=2:n and want to get a resultant sum function as z=@(n,f,phi0) sum(s2(2:n,f,phi0)). Because, I need to call z in several places of my program with different inputs. Can anybody please help me to resolve the problem? I have tried with cellfun, funarray commands following several topic discussed in the matlab forum but none of them really worked!
Looking forward to your response(s),
Regards,
Kanjilal

Steven_Lord

unread,
Feb 2, 2011, 3:44:18 PM2/2/11
to

"Suranita " <kanj...@geophysik.uni-frankfurt.de> wrote in message
news:iicc2e$ou9$1...@fred.mathworks.com...


> Hi,
> I have faced a function handle stated below:
>
> s2=@(n,f,phi0) ((n+1-i)/(1-i))*phi0^(1-i)*(f^(1-i)-1);

Unless i was defined before this function call, MATLAB will treat your use
of it as a call to the built-in function.

http://www.mathworks.com/help/techdoc/ref/i.html

If it was defined before, that value will be "memorized' by the function
handle s2 and you will NOT be able to change it without redefining s2, as it
is not an input argument for this anonymous function.

> Now I want to sum up s2 over i=2:n and want to get a resultant sum
> function as z=@(n,f,phi0) sum(s2(2:n,f,phi0)). Because, I need to call z
> in several places of my program with different inputs. Can anybody please
> help me to resolve the problem? I have tried with cellfun, funarray
> commands following several topic discussed in the matlab forum but none
> of them really worked!

You cannot add function handles.

You CAN add the values obtained by _evaluating_ function handles, however.

--
Steve Lord
sl...@mathworks.com
To contact Technical Support use the Contact Us link on
http://www.mathworks.com

Matt J

unread,
Feb 2, 2011, 4:00:05 PM2/2/11
to
"Suranita " <kanj...@geophysik.uni-frankfurt.de> wrote in message <iicc2e$ou9$1...@fred.mathworks.com>...

> Hi,
> I have faced a function handle stated below:
>
> s2=@(n,f,phi0) ((n+1-i)/(1-i))*phi0^(1-i)*(f^(1-i)-1);
> Now I want to sum up s2 over i=2:n and want to get a resultant sum function as z=@(n,f,phi0) sum(s2(2:n,f,phi0)).
>
>Because, I need to call z in several places of my program with different inputs.
===============

why not use z in the form that you've already written it above. I.e., by defining

z=@(n,f,phi0) sum(s2(2:n,f,phi0)).

you can now make function calls of the form z(n,f,phi0) to obtain the n-term sum.

Suranita

unread,
Feb 2, 2011, 7:27:03 PM2/2/11
to
Hi,
Thanks a lot for your reply. I understand your point that summing up function handles is not possible. In that case at least there should be some way to satisfy my purpose of calling the function in the form sum(s2(2:n1,f1,phi0)) instead of the form i said in my earlier message i.e. z=@(n,f,phi0) sum(s2(2:n,f,phi0). Here n1 and f1 are the inputs at the other places where I need the sum function. Is it possible to do it?
Thanks in advance,
Best regards,
Kanjilal
"Steven_Lord" <sl...@mathworks.com> wrote in message <iicfn2$rac$1...@fred.mathworks.com>...

Suranita

unread,
Feb 2, 2011, 7:28:04 PM2/2/11
to
Hi,
Thanks a lot for your suggestion. But I already have tried to do it. It did not work :(
Best regards,
Kanjilal
"Matt J" wrote in message <iicgkl$qov$1...@fred.mathworks.com>...

Matt J

unread,
Feb 2, 2011, 9:20:06 PM2/2/11
to
"Suranita " <kanj...@geophysik.uni-frankfurt.de> wrote in message <iicsqj$m1r$1...@fred.mathworks.com>...

> Hi,
> Thanks a lot for your suggestion. But I already have tried to do it. It did not work :(
=====

Well, you should consider investigating that, because I'm having absolutely no difficulty when I do it:

>> i=3; s2=@(n,f,phi0) ((n+1-i)/(1-i))*phi0^(1-i)*(f^(1-i)-1);

>> z=@(n,f,phi0) sum(s2(2:n,f,phi0));


>> z(3,4,2)

ans =

0.1172

Suranita

unread,
Feb 3, 2011, 3:39:03 AM2/3/11
to
Hi,
Yes but if you calculate z(3,4,2) manually then you'll have answer as 0.8671875 which is not same as the computed result obtained from function handle and that was the reason why I said it didn't work. Because for me also it was not giving the same result as with the one obtained by manual computation and so finally I was not getting the result I expect :(.
Thanks and regards,
Kanjilal

"Matt J" wrote in message <iid3cm$o1r$1...@fred.mathworks.com>...

waku

unread,
Feb 3, 2011, 10:01:39 AM2/3/11
to
On Feb 3, 3:39 am, "Suranita " <kanji...@geophysik.uni-frankfurt.de>
wrote:

> Hi,
>  Yes but if you calculate z(3,4,2) manually then you'll have answer as 0.8671875 which is not same as the computed result obtained from function handle and that was the reason why I said it didn't work. Because for me also it was not giving the same result as with the one obtained by manual computation and so finally I was not getting the result  I expect :(.

If you get a different answer, it's most likely because the value of i
is different from the one used in the evaluation above. You've got a
hint earlier in this thread about what might be wrong -- the value of
i seen by s2 is the one i had at the time s2 was defined, not the one
i might have at the time s2 is called:

i = 0; foo = @() i;

foo()
% 0

i = 1; foo()
% 0

If i in your formula for s2 is supposed to be fixed (you do want it to
be a constant throughout all calls), then you may want to use that
constant value to avoid confusion. However, if you expect i to be
different from call to call, you need to include it in the function's
parameters. As you can see above, the approach of setting i
externally to s2 does not work. Try redefining s2 (and possibly z) as

s2 = @(n, f, phi0, i) ...

and see what happens.

vQ

Matt J

unread,
Feb 3, 2011, 10:36:03 AM2/3/11
to
"Suranita " <kanj...@geophysik.uni-frankfurt.de> wrote in message <iidpj7$ni7$1...@fred.mathworks.com>...

> Hi,
> Yes but if you calculate z(3,4,2) manually then you'll have answer as 0.8671875 which is not same as the computed result obtained from function handle and that was the reason why I said it didn't work. Because for me also it was not giving the same result as with the one obtained by manual computation and so finally I was not getting the result I expect :(.
======

I'm seeing absolutely no difference between the computation that I showed you and the same computation done manually.

My guess is the same as waku and Steve about why you're not getting what you expect.


Suranita

unread,
Feb 8, 2011, 5:22:07 AM2/8/11
to
Hi All,
Thanks a lot for all of your help. But probably I was not succeeded to convey the message the way I want. Because, redefining s2 again and again with respect to different i or j whatever was only my problem. I wanted to create a sum function z(n,f,phi0) such that z(n,f,phi0)=Sum_i=2:n [((n+1-i)/(1-i))*phi0^(1-i)*(f^(1-i)-1)]. I do not want to define i every time. I just want to define z in this way so that whenever I will call z(n+1,f,phi0), it will call automatically Sum_i=2:n1 [((n1+1-i)/(1-i))*phi0^(1-i)*(f^(1-i)-1)] , where n1=n+1 and so on.... Is it possible anyway to create the function in such a way?
Thanks and regards,
Sincerely,
Suranita

"Matt J" wrote in message <iiei13$gk$1...@fred.mathworks.com>...

Bruno Luong

unread,
Feb 8, 2011, 7:25:05 AM2/8/11
to
"Suranita " <kanj...@geophysik.uni-frankfurt.de> wrote in message <iir5gf$eik$1...@fred.mathworks.com>...

> Hi All,
> Thanks a lot for all of your help. But probably I was not succeeded to convey the message the way I want. Because, redefining s2 again and again with respect to different i or j whatever was only my problem. I wanted to create a sum function z(n,f,phi0) such that z(n,f,phi0)=Sum_i=2:n [((n+1-i)/(1-i))*phi0^(1-i)*(f^(1-i)-1)]. I do not want to define i every time. I just want to define z in this way so that whenever I will call z(n+1,f,phi0), it will call automatically Sum_i=2:n1 [((n1+1-i)/(1-i))*phi0^(1-i)*(f^(1-i)-1)] , where n1=n+1 and so on.... Is it possible anyway to create the function in such a way?
> Thanks and regards,
> Sincerely,
> Suranita

Do you know how to use the command SUM?

sum_i = 2:n [ sin(i) ]

can be written in Matlab as

>> n = 10
>> sum(sin(2:n))

Bruno

0 new messages