MATLAB: Anonymous function representation using integral

anonymous function representationdebug

n = [1, 2]';
g = @(t, x) sum( n .* t .* x(n));
f = @(x) integral(@(t) g(t, x), 0, 1);
I think g works well. e.g. g(1,[1,2]') gives the correct answer. However, when I tried f([1,1]'), I got the following message:
Error using .*
Matrix dimensions must agree.
Error in @(t,x)sum(n.*t.*x(n))
Error in @(t)g(t,x)
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)
[q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 88)
Q = integralCalc(fun,a,b,opstruct);
Error in @(x)integral(@(t)g(t,x),0,1)
What's wrong with my code?

Best Answer

integral() requires that the anonymous function be vectorized in its argument.