MATLAB: Please help me in this Physics Project

elctricfieldelectricalfunctionMATLABvector

Hello everybody, I'm doing a physics project with Matlab. There are some problems and I hope you and everyone here can help me.
The project is:
Draw the electric field and the vector of electric field intensity.
The requirements are:
Initialize variables (e.g., potential V(x,y), graphics).
Evaluate electric field as Ex
= dV/ x and Ey
= dV/ y.
Loop over all grid points and evaluate V(x,y) and E(x,y) on grid.
Compute potential at the grid point.
Compute components of the electric field.
Normalize E-field vectors to unit length.
Plot contours of constant electric potential.
Add electric field direction to potential contour plot.
I have found a code for reference but I don't understand it and when I tested with Matlab, the code had a problem. Here's the code:
if true
% code
% e_and_v - Compute electric field from potential
% and graph potential contours and E-field direction
clear all; help e_and_v; % Clear memory; print header
%@ Initialize variables (e.g., potential V(x,y), graphics)
fprintf('Enter potential V(x,y) as an equation \n');
fprintf('For example: log(x^2 + y^2) \n');
V = input(': ','s'); % Read in V(x,y) as text string
NGrid = 20; % Number of grid points for plots
xMax = 5; % Values plotted from x= -xMax to x= xMax
yMax = xMax; % Values plotted from y= -yMax to y= yMax
for i=1:NGrid
xPlot(i) = -xMax + (i-1)/(NGrid-1)*(2*xMax); % x values to plot

yPlot(i) = -yMax + (i-1)/(NGrid-1)*(2*yMax); % y values to plot

end
%@ Evaluate electric field as Ex = (-1)*dV/dx and Ey = (-1)*dV/dy

% Note use of symop command to perform symbolic multiplication by -1

Ex = symop( '-1', '*', diff(V,'x') );
Ey = symop( '-1', '*', diff(V,'y') );
fprintf('Electric field components are \n');
disp(['x component : ', Ex]);
disp(['y component : ', Ey]);
%@ Loop over all grid points and evaluate V(x,y) and E(x,y) on grid
for i=1:NGrid
y = yPlot(i);
for j=1:NGrid
x = xPlot(j);
%@ Compute potential at the grid point

VPlot(i,j) = eval( V ); % Potential V(x,y)

%@ Compute components of the electric field

ExPlot(i,j) = eval( Ex );
EyPlot(i,j) = eval( Ey );
%@ Normalize E-field vectors to unit length
MagnitudeE = sqrt( ExPlot(i,j)^2 + EyPlot(i,j)^2 );
ExPlot(i,j) = ExPlot(i,j)/MagnitudeE;
EyPlot(i,j) = EyPlot(i,j)/MagnitudeE;
end
end
end
Here's the error:
??? Undefined function or method 'symop' for input arguments of
type 'sym'.
Error in ==> Untitled at 17
Ex = symop( '-1', '*', diff(V,'x') );
Can anyone explain the code and the error for me? For the code, I don't understand the thing:
if true
for i=1:NGrid
xPlot(i) = -xMax + (i-1)/(NGrid-1)*(2*xMax); % x values to plot
yPlot(i) = -yMax + (i-1)/(NGrid-1)*(2*yMax); % y values to plot
end
%@ Evaluate electric field as Ex = (-1)*dV/dx and Ey = (-1)*dV/dy
% Note use of symop command to perform symbolic multiplication by -1
Ex = symop( '-1', '*', diff(V,'x') );
Ey = symop( '-1', '*', diff(V,'y') );
end
and
if true
for i=1:NGrid
y = yPlot(i);
for j=1:NGrid
x = xPlot(j);
%@ Compute potential at the grid point
VPlot(i,j) = eval( V ); % Potential V(x,y)
%@ Compute components of the electric field
ExPlot(i,j) = eval( Ex );
EyPlot(i,j) = eval( Ey );
end
Thank you so much. Your help will be really appreciated.

Best Answer

Hi Nghia,
some functions are missing in that code, instead i wrote a version that can be useful, try :
N=100; % numbef of points
l=linspace(0,1,N); % linear spacing
[x,y]=meshgrid(l); % a plate of length 1m as example
V=100*sin(x.*y); % example of potential distriibution.
figure(1),
surf(x,y,V), title(' Potential distribution' );
[Ex,Ey]=gradient(V);
Ex=Ex*(-1);
Ey=Ey*(-1);
% Modulus
E=sqrt(Ex^2+Ey^2);
E=E/norm(E); % Normalization so that ||E(x,y)||=1.
figure(2),
contour(E);
figure(3)
contour(x,y,V);
hold on,
quiver(x,y,Ex,Ey)