Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.matlab

Topic: sum of function handles
Replies: 2   Last Post: Jan 16, 2013 11:46 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
Steven Lord

Posts: 17,944
Registered: 12/7/04
Re: sum of function handles
Posted: Jan 16, 2013 11:46 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply



"dert " <dert2221907@yandex.com> wrote in message
news:kd5lkh$el0$1@newscl01ah.mathworks.com...
> Hi, i want to sum two function handle. the functions that i want to sum
> are following;
>
> f1=0;
> for i=1:20
> f2=f2(x(1),x(2),x(3))
> f1=f1+f2 end
> how can i do by using function handle? the following is true?
>
> f1=@(x)(0); for i=1:20
> f2=@(x)(f2(x(1),x(2),x(3)))
> f1=@(x)(f1(x)+ f2(x));
> end
>
> Can anyone help me? Because i use this last summation function for
> fminsearch


One way to do it:

functionsTBE = cell(1, 5);
testvalue = 0;
for k = 1:5
functionsTBE{k} = @(x) sin(k*x);
testvalue = testvalue + functionsTBE{k}(3);
end
f = @(x) sum(cellfun(@(y) y(x), functionsTBE));
f(3) % Should match testvalue
abs(f(3)-testvalue)

Alternately, if your expressions aren't too complicated and you have
Symbolic Math Toolbox, write your functions as symbolic expressions, add
them, then use matlabFunction to convert the sum into a function.

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




Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.