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

后端 未结 2 1447
感情败类
感情败类 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.

    0 讨论(0)
  • 2021-01-24 12:39

    You need to give some more information in your question: What is the problem you are having? What have you tried?

    However, assuming that z(i,j) is your electric field calculated on a 2d grid, just call contourf after your for loops as

    contourf(z)
    
    0 讨论(0)
提交回复
热议问题