MATLAB: Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function to return an output with the same size and shape as the input arguments.

besselmatrix

I don't understand the problem in this code:
Roi=750;
Row=1000;
R=Row/Roi;
r=50;
d=50;
H0=50;
l=5;
b=5;
a=(2*l*H0/pi)^0.5;
Area=pi*a^2;
Vol=Area*H0;
Omega=1;
va=(3*(pi)^(3/2))/(16*sqrt(2))*(Omega*sqrt(l*H0))/(1+pi/4*R*(l/b));
J=@(x,y)(besselj(0,x.*y*a/d)).*sqrt(1-y.^2).*y;
B=@(x)integral(@(y)J(x,y),0,1)
J0=@(x)besselj(0,x*r/d);
F=@(t)integral(@(x)J0(x).*B(x).*sqrt(x.*tanh(x))*sin(sqrt(9.81/d*t.^2).*x.*tanh(x)).*x,0,10)
e=@(t)-2*(a/d)^3*(sqrt(2*H0/d-(a/d*b/a)))/(1+2/3*R*a/b).*F(t)
fplot(e)
Command window
Warning: Function behaves unexpectedly on array inputs. To improve performance, properly vectorize your function
to return an output with the same size and shape as the input arguments.
> In matlab.graphics.function.FunctionLine>getFunction
In matlab.graphics.function.FunctionLine/updateFunction
In matlab.graphics.function.FunctionLine/set.Function_I
In matlab.graphics.function.FunctionLine/set.Function
In matlab.graphics.function.FunctionLine
In fplot>singleFplot (line 232)
In fplot>@(f)singleFplot(cax,{f},limits,extraOpts,args) (line 191)
In fplot>vectorizeFplot (line 191)
In fplot (line 161)
In Case41 (line 44)
Warning: Error updating FunctionLine.
The following error was reported evaluating the function in FunctionLine update: Inner matrix dimensions must
agree.

Best Answer

fplot prefers to pass a vector to the function for efficiency, but checks whether that works and gives a warning if it doesn't work. So e() would be called with a vector which your code does not expect.
integral() always passes a vector as its argument. You have nested integrals and each of those tries to pass a vector. You are going to end up returning the wrong size to be integrated.
For 1d integration you can sometimes use the arrayvalued option to integral() but you need to be careful about multiple levels. More robust is to put in arrayfun calls around all integral() calls that might be invoked on a nonscalar.