Please help me in this Physics Project
Afficher commentaires plus anciens
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.
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 2 Jan 2014
0 votes
symop() is before MATLAB 7.0 (R14). See http://www.mathworks.com/matlabcentral/newsreader/view_thread/82603
3 commentaires
Walter Roberson
le 3 Jan 2014
Vfun = str2func( ['@(x, y) ', input(': ', 's') ] );
V = Vfun(x, y);
Nghia
le 3 Jan 2014
Youssef Khmou
le 3 Jan 2014
that is efficient answer .
Catégories
En savoir plus sur Annotations dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!