From: Raymond's Group Lee on
I am wondering there is anyway we can add 2 functions, f(x) and g(x), together.


f = @(x) x ;
g = @(x) x^2 ;
h = @(x) f + g ;

This does not work.

If this is possible, I can use it to define function like

f(x) = x + x^2 + x^3 + ...+ x^100 + ... + x^1000 ;

Thanks.
From: Steven Lord on

"Raymond's Group Lee" <jh52lee(a)uwaterloo.ca> wrote in message
news:hq9sl5$5aq$1(a)fred.mathworks.com...
>I am wondering there is anyway we can add 2 functions, f(x) and g(x),
>together.
>
>
> f = @(x) x ;
> g = @(x) x^2 ;
> h = @(x) f + g ;

Addition is not defined for function handles. However, addition can be
defined for _the values returned by two function handles_.

f = @(x) x;
g = @(x) x.^2;
h = @(x) f(x) + g(x);

> This does not work.
>
> If this is possible, I can use it to define function like
>
> f(x) = x + x^2 + x^3 + ...+ x^100 + ... + x^1000 ;

Technically yes, although creating 1001 individual function handles to do so
seems excessive, and I'd question the use of such a high degree polynomial
on general principles. I'd use POLYVAL instead.

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


From: James Allison on
Try:

f = @(x) x ;
g = @(x) x^2 ;
h = @(x) f(x) + g(x) ;

-James

Raymond's Group Lee wrote:
> I am wondering there is anyway we can add 2 functions, f(x) and g(x),
> together.
>
>
> f = @(x) x ;
> g = @(x) x^2 ;
> h = @(x) f + g ;
> This does not work.
>
> If this is possible, I can use it to define function like
>
> f(x) = x + x^2 + x^3 + ...+ x^100 + ... + x^1000 ;
>
> Thanks.
From: Raymond's Group Lee on
"Steven Lord" <slord(a)mathworks.com> wrote in message <hq9u3s$40i$1(a)fred.mathworks.com>...
>
> "Raymond's Group Lee" <jh52lee(a)uwaterloo.ca> wrote in message
> news:hq9sl5$5aq$1(a)fred.mathworks.com...
> >I am wondering there is anyway we can add 2 functions, f(x) and g(x),
> >together.
> >
> >
> > f = @(x) x ;
> > g = @(x) x^2 ;
> > h = @(x) f + g ;
>
> Addition is not defined for function handles. However, addition can be
> defined for _the values returned by two function handles_.
>
> f = @(x) x;
> g = @(x) x.^2;
> h = @(x) f(x) + g(x);
>
> > This does not work.
> >
> > If this is possible, I can use it to define function like
> >
> > f(x) = x + x^2 + x^3 + ...+ x^100 + ... + x^1000 ;
>
> Technically yes, although creating 1001 individual function handles to do so
> seems excessive, and I'd question the use of such a high degree polynomial
> on general principles. I'd use POLYVAL instead.
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Thanks, Steve. That's a good way.
From: Raymond's Group Lee on
"Steven Lord" <slord(a)mathworks.com> wrote in message <hq9u3s$40i$1(a)fred.mathworks.com>...
>
> "Raymond's Group Lee" <jh52lee(a)uwaterloo.ca> wrote in message
> news:hq9sl5$5aq$1(a)fred.mathworks.com...
> >I am wondering there is anyway we can add 2 functions, f(x) and g(x),
> >together.
> >
> >
> > f = @(x) x ;
> > g = @(x) x^2 ;
> > h = @(x) f + g ;
>
> Addition is not defined for function handles. However, addition can be
> defined for _the values returned by two function handles_.
>
> f = @(x) x;
> g = @(x) x.^2;
> h = @(x) f(x) + g(x);
>
> > This does not work.
> >
> > If this is possible, I can use it to define function like
> >
> > f(x) = x + x^2 + x^3 + ...+ x^100 + ... + x^1000 ;
>
> Technically yes, although creating 1001 individual function handles to do so
> seems excessive, and I'd question the use of such a high degree polynomial
> on general principles. I'd use POLYVAL instead.
>
> --
> Steve Lord
> slord(a)mathworks.com
> comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
>

Thanks, Steve. That's a good way.