Please help me in this Physics Project

9 views (last 30 days)
Thanh
Thanh on 21 Dec 2014
Commented: Star Strider on 21 Dec 2014
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: Calculate the energy density distribution of the electric field 1. Request The relationship between the electric field strength and power generation is represented by the expression: \ vec {E} = - \ vec {grad} \ left (V \ right) Field energy density can be calculated by the formula: u = \ frac {1} {2} \ epsilon_0 \ left | E \ right | ^ 2 This exercise requires students to use Matlab to perform graph density distribution of electrical energy according to the potential distributions on a plane already know. 2. Conditions 1) Students should have knowledge of basic programming in MATLAB. 2) Learn the Matlab command associated symbolic and graphics. 3. Duties Construction Matlab program: 1) Enter the voltage function in two variables x and y. 2) Create a matrix grid on the Oxy plane with the origin O 3) Calculate the energy density of the electric field at the nodes on the network. 4) Draw the three-dimensional graph showing the distribution and electric field energy density (z) at the point in the plane (x, y). 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:
% e_energy - Compute energy density in the electric field
clear all; help e_energy; % Clear memory; print header
%@ Initialize variables (e.g., potential V(x,y), graphics)
epsilon0 = 8.85e-12; % Permittivity of free space
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 and E
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 square magnitude of the electric field
E_Mag2 = eval( Ex )^2 + eval( Ey )^2;
%@ Compute energy density
E_Energy(i,j) = 0.5*epsilon0*E_Mag2;
end
end
%@ Plot potential and energy density
clf; figure(gcf); % Clear figure; bring figure window forward
subplot(1,2,1) % First (left) plot in 1x2 plot window
surf(xPlot,yPlot,VPlot); % Plot potential in contour/mesh plot
xlabel('x'); ylabel('y'); zlabel('Potential (V)');
title('Potential');
shading interp % Set interpolation method for drawing wiremesh
subplot(1,2,2) % Second (right) plot in 1x2 plot window
surf(xPlot,yPlot,E_Energy); % Plot energy density in contour/mesh plot
xlabel('x'); ylabel('y'); zlabel('Energy density (J/m^3)');
title('Energy density');
shading interp % Set interpolation method for drawing wiremesh
% Set color scale
colormap(bone); % Note: instead of bone, try hsv, gray, hot or cool
Here's the error:
Error using diff
Difference order N must be a positive integer scalar.
Error in e_energy1 (line 19)
Ex = symop( '-1', '*', diff(V,'x') );
I have tried everything I can think of. I've spent hours searching thru every online reference I can find, all to no avail. so if anyone out there can explain it to me in relatively simple terms, I'd really, really, really appreciate it. Thank you very much
  2 Comments
Geoff Hayes
Geoff Hayes on 21 Dec 2014
Thanh - do you have the Symbolic Math Toolbox? I get the same error if I run your code, and I don't have the toolbox. The problem is with the line
Ex = symop( '-1', '*', diff(V,'x') );
and in particular
diff(V,'x')
which is this diff function from the afore mentioned toolbox. To see if you have it, in the Command Window type
ver
What do you see? As this is a homework assignment, you may wish to start from scratch and try to understand the problem (and so solution) and not try to use someone else's code.
Star Strider
Star Strider on 21 Dec 2014
The ‘symop’ function isn’t in the online documentation.
If you have access to the function .m file (most likely ‘symop.m’, attach it (use the ‘paperclip’ icon next to the (?)Help button).

Sign in to comment.

Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!