Help me this. Why it doesnt work?
조회 수: 3 (최근 30일)
이전 댓글 표시
% 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
%@ Plot contours of constant electric potential
clf; figure(gcf); % Clear figure; bring figure window forward
meshc(xPlot,yPlot,VPlot); % Plot potential in contour/mesh plot
xlabel('x'); ylabel('y'); zlabel('Potential');
title('Strike any key to continue ...');
pause;
% Specify contour levels used in contour plot
axis([-xMax xMax -yMax yMax]); % Fix the min,max for x,y axes
cs = contour(xPlot,yPlot,VPlot); % Draw contour plot
clabel(cs); % Place contour labels on contour levels
%@ Add electric field direction to potential contour plot
hold on;
quiver(xPlot,yPlot,ExPlot,EyPlot); % Draw arrows for E field
title('Potential contours and electric field direction');
xlabel('x'); ylabel('y');
hold off;
댓글 수: 3
Image Analyst
2017년 12월 22일
Read this and then fix your post, please.
In general, it doesn't work most likely because of an error in your code. You can discover it by using this link
Star Strider
2017년 12월 22일
Please describe the problem. We cannot run your code.
What is the symop function? I cannot find it in the Symbolic Math Toolbox or muPad documentation.
채택된 답변
Walter Roberson
2017년 12월 22일
symop() is very old Symbolic Toolbox, before R14. It was part of version 1 of the Symbolic Toolbox, and was replaced by Version 2. You will need to rewrite the code.
댓글 수: 5
Walter Roberson
2017년 12월 23일
편집: Walter Roberson
2017년 12월 23일
Code enclosed. I did not alter the basic code structure, which is quite inefficient. The code could be made much faster if it were rewritten.
function e_and_v
% e_and_v - Compute electric field from potential
% and graph potential contours and E-field direction
%@ 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');
syms x y
V = input(': ','s'); % Read in V(x,y) as text string
V = sym(V);
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 = -diff(V,x);
Ey = -diff(V,y);
fprintf('Electric field components are \n');
disp(['x component : ', char(Ex)]);
disp(['y component : ', char(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) = subs( V ); % Potential V(x,y)
%@ Compute components of the electric field
ExPlot(i,j) = subs( Ex );
EyPlot(i,j) = subs( 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
%@ Plot contours of constant electric potential
clf; figure(gcf); % Clear figure; bring figure window forward
meshc(xPlot,yPlot,VPlot); % Plot potential in contour/mesh plot
xlabel('x'); ylabel('y'); zlabel('Potential');
title('Strike any key to continue ...');
pause;
% Specify contour levels used in contour plot
axis([-xMax xMax -yMax yMax]); % Fix the min,max for x,y axes
cs = contour(xPlot,yPlot,VPlot); % Draw contour plot
clabel(cs); % Place contour labels on contour levels
%@ Add electric field direction to potential contour plot
hold on;
quiver(xPlot,yPlot,ExPlot,EyPlot); % Draw arrows for E field
title('Potential contours and electric field direction');
xlabel('x'); ylabel('y');
hold off;
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!