summing functions handles in matlab

Posted by user552231 on Stack Overflow See other posts from Stack Overflow or by user552231
Published on 2011-01-16T09:37:56Z Indexed on 2011/01/16 21:53 UTC
Read the original article Hit count: 204

Filed under:
|
|

Hi
I am trying to sum two function handles, but it doesn't work. for example:

y1=@(x)(x*x);
y2=@(x)(x*x+3*x);
y3=y1+y2

The error I receive is "??? Undefined function or method 'plus' for input arguments of type 'function_handle'."

This is just a small example, in reality I actually need to iteratively sum about 500 functions that are dependent on each other.

EDIT
The solution by Clement J. indeed works but I couldn't manage to generalize this into a loop and ran into a problem. I have the function s=@(x,y,z)((1-exp(-x*y)-z)*exp(-x*y)); And I have a vector v that contains 536 data points and another vector w that also contains 536 data points. My goal is to sum up s(v(i),y,w(i)) for i=1...536 Thus getting one function in the variable y which is the sum of 536 functions. The syntax I tried in order to do this is:

sum=@(y)(s(v(1),y,z2(1))); 
for i=2:536 
  sum=@(y)(sum+s(v(i),y,z2(i))) 
end

© Stack Overflow or respective owner

Related posts about function

Related posts about math