BTL - Group 5 - CC07 (General Physics 1)
BTL - Group 5 - CC07 (General Physics 1)
BTL - Group 5 - CC07 (General Physics 1)
GENERAL PHYSICS 1
20/12/2021
2|Page
Class PH1003 – CC07
Group 5
Group Members
Cao Nhật Quang 2152902
Nguyễn Đức Khải 2152661
Nguyễn Đức Trí 2153066
3|Page
Table of content
I. THEORETICAL BASIS 5
1. DEFINITION 5
2. THE EQUATION USED 5
1. THE CONTENT 5
2. REQUIREMENT 5
3. TASK 5
4. THE ORIENTATION OF THE SOLUTION 6
III. MATLAB 6
1. CODE 6
2. CODE EXPLANATION 7
3. EXAMPLE 8
IV. CONCLUSION 9
V. REFERENCES 10
4|Page
I. THEORETICAL BASIS
1. Definition
- The electric field is defined as the electric force per unit charge. The direction of the field is
taken to be the direction of the force it would exert on a positive test charge. The electric field
is radially outward from a positive charge and radially in toward a negative point charge.
- When a test charge q0 is placed in an electric field, the charge–field system has an electric
potential energy U. The potential energy per unit charge U/q0 is independent of the test charge
and has a value at every point in an electric field. It is called the electric potential V (or
simply the potential) at that point.
2. The equation used
The electric field vector is determined by the potential in the Oxy plane by the following
expression:
V V
E gradV i j
x y
→ If we know the function V(x, y, z) for all points in the region around a charge distribution,
we can find the components of 𝐸⃗ at any point by taking partial derivatives of V(x, y, z).
Note: Students can use other non-symbolic approaches. Submitting report has to contain
text explaining the content of the program and the entire code verified to run properly in
MATLAB.
5|Page
4. The orientation of the solution
- Firstly, we have the equation of V depend on the variables x and y
For example: V(x,y) = 3x2 +5y3
V V
- According to the equation: E gradV i j
x y
III. MATLAB
1. Code
function E_and_V_Group_5
fprintf('......................................... \n')
fprintf('- The equation used: E= -gradV \n')
fprintf('- Enter potential V(x,y) as an equation \n');
fprintf('- For example: x^2 + y^2 \n');
syms x y real
V = input('- Potential equation V(x,y)=');
NGrid = 30;
xMax = 10;
yMax = xMax;
for i=1:NGrid
xPlot(i) = -xMax + (i-1)/(NGrid-1)*(2*xMax);
yPlot(i) = -yMax + (i-1)/(NGrid-1)*(2*yMax);
end
Ex = -1*diff(V,x);
Ey = -1*diff(V,y);
fprintf('......................................... \n')
fprintf('- Electric field components are \n');
disp(['Ex component = ', char(Ex)]);
disp(['Ey component = ', char(Ey)]);
fprintf('>> Strike "Enter" to see the graph <<')
for i=1:NGrid
y = yPlot(i);
for j=1:NGrid
x = xPlot(j);
VPlot(i,j) = subs( V );
ExPlot(i,j) = subs( Ex );
EyPlot(i,j) = subs( Ey );
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
clf; figure(gcf);
meshc(xPlot,yPlot,VPlot);
6|Page
xlabel('x'); ylabel('y'); zlabel('Potential');
title('Potential surface (Strike "Enter" to continue)');
pause;
axis([-xMax xMax -yMax yMax]);
cs = contour(xPlot,yPlot,VPlot);
clabel(cs);
hold on;
quiver(xPlot,yPlot,ExPlot,EyPlot);
title('Potential surface and Electric field direction');
xlabel('x'); ylabel('y');
hold off;
2. Code explanation
- fprintf(formatSpec,A1,...,An): formats data and displays the results on the screen.
- syms var1 ... varN set: sets an assumption that the created symbolic variables belong to a set.
- str = input(prompt,'s'): returns the entered text, without evaluating the input as an
expression.
- diff(V,x): differentiating V with respect to x (x can be replaced by y or z in different cases).
- for index = values, statements - end: executes a group of statements in a loop for a specified
number of times. End terminates for, while, switch, try, if, and parfor statements. Without an
end statement, for, while, switch, try, if, and parfor wait for further input.
- disp(X): displays the value of variable X without printing the variable name. Another way to
display a variable is to type its name, which displays a leading “X =” before the value.
- subs(s): returns a copy of s, replacing symbolic variables in s, with their values obtained from
the calling function and the MATLAB® Workspace, and then evaluates s. Variables with no
assigned values remain as variables.
- B = sqrt(X): returns the square root of each element of the arrayX. For the elements of X that
are negative or complex, sqrt(X)produces complex results.
- clf: deletes from the current figure all graphics objects whose handles are not hidden (i.e., their
HandleVisibility property is set to on).
- fig = gcf: returns the current figure handle. If a figure does not exist, then gcf creates a figure
and returns its handle. You can use the figure handle to query and modify figure properties.
- meshc(X,Y,Z): draws a wireframe mesh and a contour plot under it with a color determined
by Z, so the color is proportional to surface height.
- title(txt): adds the specified title to the axes or chart returned by the gca command. Reissuing
the title command causes the new title to replace the old title.
- pause: temporarily stops MATLAB® execution and waits for the user to press any key. The
pause function also temporarily stops the execution of Simulink® models but does not pause
their repainting.
- axis(limits): specifies the limits for the current axes. Specify the limits as a vector of four, six,
or eight elements
- contour(X,Y,Z), contour(X,Y,Z,n), and contour(X,Y,Z,v): draw contour plots of Z using X
and Y to determine the x and y values.
7|Page
- clabel(C,h): labels the current contour plot with rotated text inserted into each contour line.
The contour lines must be long enough to fit the label, otherwise clabel does not insert a label.
If you do not have the contour matrix C, then replace C with [].
- quiver(x,y,u,v): plots vectors as arrows at the coordinates specified in each corresponding
pair of elements in x and y. The matrices x, y, u, and v must all be the same size and contain
corresponding position and velocity components. However, x and y can also be vectors, as
explained in the next section. By default, the arrows are scaled to just not overlap, but you can
scale them to be longer or shorter if you want.
- hold on: retains plots in the current axes so that new plots added to the axes do not delete
existing plots. New plots use the next colors and line styles based on the ColorOrder
andLineStyleOrder properties of the axes. MATLAB® adjusts axes limits, tick marks, and tick
labels to display the full range of data. If axes do not exist, then the hold command creates them.
- hold off: sets the hold state off so that new plots added to the axes clear existing plots and
reset all axes properties. The next plot added to the axes uses the first color and line style based
on the ColorOrder and LineStyleOrder properties of the axes. This option is the default
behavior.
3. Example
- The equation: V(x,y) = 3x2 +5y3 = 3*x^2+5*y^3
- Running the program:
8|Page
Image 3. Potential surface
IV. CONCLUSION
In conclusion, it is possible to code a function to determine the electric field vectors from the
electric potential function and plot both the potential surface and electric field vectors in
MATLAB. It can save time and increase accuracy in calculating and drawing the graph.
9|Page
V. REFERENCES
- A. L. Garcia and C. Penland, MATLAB Projects for Scientists and Engineers, Prentice Hall,
Upper Saddle River, NJ, 1996. http://www.algarcia.org/fishbane/fishbane.html. Or
https://www.mathworks.com/matlabcentral/fileexchange/2268-projects-for-scientists-and-
engineers
10 | P a g e