# Convolution of function handles

4 views (last 30 days)
qfn on 16 Jul 2021
Commented: qfn on 16 Jul 2021
I'm doing some nonlinear fitting in MATLAB.
With most details stripped away, I have two function handles, f = @(x) and g = @(A,x) , where A is some parameter vector.
I'd really like to make a new function handle C = @(A,x), which is the convolution of f with g using parameter A.
Naively, that would be..
c = @(A,x) integral ( @(s) f(s).*g(A,x-s),-inf,inf);
This works for single values--e.g. c(1,1), but c isn't a well behaved function handle -- it throws errors for c(1,[1,2])*, which renders c unusable in non-linear fit algorithms. I think that's because the integral function is vectorized, so when x is a singleton, it can put in whatever size array it wishes for s, but when x is a vector, it snags on that step. In theory I could use some kind of repmat to fix this problem, but that seems awfully sloppy.
For a few more details:
f = @(x) is a piecewise combination of a few different exponentials and input functions.
g is actually just an exponential decay -- g = @(A,x) exp(-A*x);
I could probably compute these things explicitely and that might end up being fastest anyways but f may not always be such a nice function in practice.
*error here:
>> c(1,[1, 1])
Matrix dimensions must agree.
Error in @(s)f(s).*g(A,x-s)
Error in integralCalc/iterateScalarValued (line 314)
fx = FUN(t);
Error in integralCalc/vadapt (line 132)
[q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 75)
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in @(A,x)integral(@(s)f(s).*g(A,x-s),0,180)

Steven Lord on 16 Jul 2021
Use the 'ArrayValued', true name-value pair argument as shown in the "Vector-Valued Function" example on the documentation page for the integral function.
##### 1 CommentShowHide None
qfn on 16 Jul 2021
This fixes the problems stated. Thank you!

R2018a

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by