can some one explain the syntax of this code please
Show older comments
x=[3 8];
y=[5 5];
q=[-3 3];
% Coulumb's number
ke = 8.9875517873681764e9;
xi=linspace(0,10,20);
yi=linspace(0,10,20);
[XI YI] = meshgrid(xi,yi);
zi = complex(XI,YI);
z = complex(x,y);
[ZI Z]=ndgrid(zi(:),z(:));
dZ = ZI-Z;
Zn = abs(dZ);
E = (dZ./Zn.^3)*(q(:)*ke);
E = reshape(E,size(XI));
En = abs(E);
Ex = real(E);
Ey = imag(E);
figure;
quiver(XI,YI,Ex./En,Ey./En);
hold on;
plot(x,y,'Or');
axis equal;
Answers (2)
Steven Lord
on 30 Dec 2022
1 vote
I suggest you start with the free MATLAB Onramp tutorial to quickly learn the essentials of MATLAB. Once you've gone through that you should be able to understand much of that code. If you encounter a function with which you're not familiar read through its documentation page using the doc function.
7 Comments
Moaz salah
on 30 Dec 2022
Image Analyst
on 30 Dec 2022
I suggest you take it one line at a time. First look up the function on the line in the help, and see what it does. Then make a comment before that line with a short description of what the following line does. Do that for every line in the program.
Moaz salah
on 30 Dec 2022
Image Analyst
on 30 Dec 2022
Edited: Image Analyst
on 30 Dec 2022
% Compute E from this mathematical formula.
E = (dZ./Zn.^3)*(q(:)*ke);
% reshape makes E the same size (rows and columns) as XI.
E = reshape(E,size(XI));
% abs takes the absolute value.
En = abs(E);
% real takes the real component of a complex number
Ex = real(E);
% imag takes the imaginary component of a complex number.
Ey = imag(E);
% figure brings up a new blank window.
figure;
% quiver plots a field of a bunch of little arrows.
quiver(XI,YI,Ex./En,Ey./En);
Moaz salah
on 30 Dec 2022
Walter Roberson
on 30 Dec 2022
Be careful there, the * operation is algebraic matrix multiplication (inner product) not element by element multiplication. Element by element is the .* operator
Moaz salah
on 30 Dec 2022
Image Analyst
on 30 Dec 2022
1 vote
To learn fundamental concepts, such as syntax, invest 2 hours of your time here:
Categories
Find more on Vector Fields in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!