MATLAB: How to plot surf (z,r,q+b)? ERROR: Z must be a matrix, not a scalar or vector.

3d plotsurf

z=linspace(-10,10,10000)
r=linspace(-10,10,10000)
fun=@(x)(1-z.*cos(x))./(1+z.^2-2.*z.*cos(x)+(r+0.5).^2).^1.5
fun2=@(x)(1-z.*cos(x))./(1+z.^2-2.*z.*cos(x)+(r-0.5).^2).^1.5
q=100./(2*3.14).*integral(fun,0,3.14,'ArrayValued',true)
b=100./(2*3.14).*integral(fun2,0,3.14,'ArrayValued',true)
surf(z, r, q+b)
ERROR: Z must be a matrix, not a scalar or vector.

Best Answer

[z,r] = meshgrid(linspace(-10,10,10000));
Note: I recommend that you replace your pair of integrals with a single integral:
fun=@(x)(1-z.*cos(x))./(1+z.^2-2.*z.*cos(x)+(r+0.5).^2).^1.5
fun2=@(x)(1-z.*cos(x))./(1+z.^2-2.*z.*cos(x)+(r-0.5).^2).^1.5
q_b = 100./(2*3.14).*integral(@(x) fun(x)+fun2(x), 0, pi, 'ArrayValued', true);
surf(z, r, q_b);
The grid integration will be considerably slower than you experienced before. I recommend reducing the 10000 by a fair bit. It probably does not make sense to integrate with a higher resolution than your display, for example.