MATLAB: A point charge of Q = 5 nC is located at (2,2,8). how to find the distance of the electric fields generated by the charge over the range of 0 < x < 4 m, 0 < y < 4 m, and 0 < z < 4 m due to the point charge

electric_field

x = 0:0.1:4;
y = 0:0.1:4;
z = 0:0.1:4;
%distance from Q to position of Efield
r = (((x-2).^2)+((y-2).^2)+((z-8).^2)).^0.5;

Best Answer

>> [xA,yA,zA] = ndgrid(x,y,z);
>> rA = sqrt( (xA-2).^2 + (yA-2).^2 + (zA-8).^2 );
Of course rA will actually have size numel(x)*numel(y)*numel(z)