How to simulate and draw electric fields in Matlab with contourf?

后端 未结 2 1448
感情败类
感情败类 2021-01-24 11:53

I\'d like to know how to draw a graph with 2 electric charges Q and -Q and their total E, for a specific point (eg (4,5)), with contour f.. My M-file actually scans the area (fr

2条回答
  •  无人及你
    2021-01-24 12:34

    Based on Coulomb's law, the electric field created by a single, discrete charge q at a distance r is given by:

    E=q/(4*pi*e0*r.^2);

    If you have several charges you can use the superposition principle and add the contribution of each charge.

    The only thing left to do is to create a grid to compute the electrical field. For this you can use tha Matlab function meshgrid.

    A simple example in Matlab would be:

    k=1/4/pi/8.854e-12;
    d=2;
    q=[-1 1];
    x=[-d/2 d/2];
    y=[0 0];
    
    dx=0.01;
    X=(-d:dx:d);
    Y=(-d:dx:d);
    [X Y]=meshgrid(X,Y);
    
    E=zeros(size(X));
    
    for i=1:numel(q)
        r=sqrt((X-x(i)).^2+(Y-y(i)).^2);
        E=E+k*q(i)./r.^2;
    end
    
    E(isinf(E))=NaN;
    
    figure;
    contourf(X,Y,E);
    axis image;
    

    Hope it helps you. You can read the documentation of contourf to tweak the plot to your needs.

提交回复
热议问题