www.gusucode.com > symbolic 源码程序 matlab案例代码 > symbolic/FindElectricChargeDensityFromElectricFieldExample.m

    %% Find Electric Charge Density from Electric Field
%% 1

% Copyright 2015 The MathWorks, Inc.

syms x y ep0
E = [x^2 y^2];
rho = divergence(E,[x y])*ep0
%% 2
rho = subs(rho,ep0,1);
v = -2:0.1:2;
[xPlot,yPlot] = meshgrid(v);
Ex = subs(E(1),x,xPlot);
Ey = subs(E(2),y,yPlot);
rhoPlot = double(subs(rho,{x,y},{xPlot,yPlot}));
%% 3
quiver(xPlot,yPlot,Ex,Ey)
hold on
contour(xPlot,yPlot,rhoPlot,'ShowText','on')
title('Contour Plot of Charge Density Over Electric Field')
xlabel('x')
ylabel('y')