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

4 visualizzazioni (ultimi 30 giorni)
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 Commenti
Cristobal Meza
Cristobal Meza il 27 Apr 2022
Well, the thing is that its supposed to create an electromagnetic field, it stil doesn't occur :(
Jan
Jan il 27 Apr 2022
@Cristobal Meza: I've formatted your code to improve the readibility. By the way, the overkill of parentheses reduced the clarity.

Accedi per commentare.

Risposta accettata

Jan
Jan il 27 Apr 2022
Modificato: Jan il 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 Commenti
Cristobal Meza
Cristobal Meza il 27 Apr 2022
I was blind and now I see.
Thank you very much, now I also understand that my teachers are kinda dated about matlab, thank you!
Jan
Jan il 28 Apr 2022
You are welcome. It is the nature of teachers to tend to outdated information. :-)

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Vector Fields in Help Center e File Exchange

Prodotti


Release

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by