Search code examples
vectorscilab

Vectors plot of electric field


I trying to plot vectors of electric field in scilab. But it always error :

champ: Wrong size for input arguments: Incompatible sizes.

the code:

epsilon0=1e-9/(36*%pi);
q=3e-9;
p=[-1,0,0];
x=-2:0.2:2;
y=-2:0.2:2;
[px,py]=meshgrid(x,y);

for m=1:length(x),
    for n=1:length(y),
        xp=px(m,n);
        yp=py(m,n);
        vektorr1x=xp-p(1);
        vektorr1y=yp-p(3);
        r1=sqrt(vektorr1x^2+vektorr1z^2);
        if r1~=0 then
            ar1x=vektorr1x/r1;
            ar1y=vektorr1y/r1;
            E1x=q*ar1x/(4*%pi*epsilon0*r1^2);
            E1y=q*ar1y/(4*%pi*epsilon0*r1^2);
        else
            E1x=0;
            E1y=0;
        end,
    end,
end,
pl=champ(px,py,E1x,E1y,[-2,-1,2,-1]);

Solution

  • You don't have to use loops, the following script does what you want:

    epsilon0=1e-9/(36*%pi);
    q=3e-9;
    p=[-1,0,0];
    x=-2:0.2:2;
    y=-2:0.2:2;
    [px,py]=ndgrid(x,y);
    
    vektorr1x=px-p(1);
    vektorr1y=py-p(3);
    r1=sqrt(vektorr1x.^2+vektorr1y.^2);
    ar1x=vektorr1x./r1;
    ar1y=vektorr1y./r1;
    E1x=q*ar1x./(4*%pi*epsilon0*r1.^2);
    E1y=q*ar1y./(4*%pi*epsilon0*r1.^2);
    E1x(r1==0)=0;
    E1y(r1==0)=0;
    
    clf
    champ(x,y,E1x,E1y,[-2,-1,2,-1]);
    

    To plot fields don't use meshgrid to sample the domain use ngrid instead. Moreover, don't forget to use dot-prefixed operators.