Error using quiver The size of X must match the size of U or the number of columns of U.

1 view (last 30 days)
Cristobal Meza
Cristobal Meza on 26 Apr 2022
Commented: Jan on 28 Apr 2022
k = 1
q = 2
x = 3
a = 4
y = 4
i = (k.*abs(q)).*(x+(a./2)) ./ (((x+(a./2)).^2)+y.^2)^(3./2) + (k.*abs(q)).*(x-(a./2)) / (((x-(a./2)).^2)+y.^2).^(3./2)
j = (k.*abs(q).*y) ./ (((x+(a./2)).^2)+y.^2).^(3./2) + (k.*abs(q).*y) / (((x-(a./2)).^2)+y.^2).^(3./2)
Er = sqrt((i.^2)+(j.^2))
E1_x = (k.*abs(q)).*(x+(a./2)) ./ (((x+(a./2)).^2)+y.^2).^(3./2)
E1_y = (k.*abs(q).*y) ./ (((x+(a./2)).^2)+y.^2).^(3./2)
E2_x = (k.*abs(q)).*(x-(a./2)) ./ (((x-(a./2)).^2)+y.^2).^(3./2)
E2_y = (k.*abs(q).*y) ./ (((x-(a./2)).^2)+y.^2).^(3./2)
Er_x = E1_x + E2_x;
Er_y = E1_y + E2_y;
Etot = sqrt( (Er_x.^2) + (Er_y.^2) )
[x,y] = meshgrid(-10:10,-10:10);
u = (Er_x)./Etot
v = (Er_y)./Etot
quiver(x,y,u,v)
Please help, I keep getting this message and I don't know what to do:
Error using quiver The size of X must match the size of U or the number of columns of U.
  3 Comments
Jan
Jan on 27 Apr 2022
@Cristobal Meza: I've formatted your code to improve the readibility. By the way, the overkill of parentheses reduced the clarity.

Sign in to comment.

Accepted Answer

Jan
Jan on 27 Apr 2022
Edited: Jan on 27 Apr 2022
x = -10:10; % No need for meshgrid since Matlab R2016b
y = (-10:10).'; % simply use a row and a column vector
k = 1;
q = 2;
aq = abs(q); % Calculate it once only
a = 4;
% This is not used at all?! >>>>>>>>>
% i = k*abs(q) * ((x + a/2) ./ ((x + a/2).^2 + y.^2).^1.5 + ...
% (x - a/2) ./ ((x - a/2).^2 + y.^2).^1.5);
% j = k*abs(q) * (y ./ (((x+a/2).^2) + y.^2).^1.5 + ...
% y ./ (((x-a/2).^2) + y.^2).^1.5);
% Er = sqrt(i.^2 + j.^2);
% <<<<<<<<<<<
xp = ((x + a/2).^2 + y.^2).^1.5;
xm = ((x - a/2).^2 + y.^2).^1.5;
E1_x = k * aq * (x+a/2) ./ xp;
E1_y = k * aq * y ./ xp;
E2_x = k * aq * (x-a/2) ./ xm;
E2_y = k * aq * y ./ xm;
Er_x = E1_x + E2_x;
Er_y = E1_y + E2_y;
Etot = sqrt(Er_x.^2 + Er_y.^2);
u = Er_x ./ Etot;
v = Er_y ./ Etot;
quiver(x,y,u,v)
Instead of a fixed value for x and y, both coordinates are vectors of the coordinates here.
Do you see, that the code looks much cleaner? Then the symmetry of the formulas are obvious on first sight and typos would be prominent automatically. Keep the code simple.
  2 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!