can some one explain the syntax of this code please

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)

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

its for a project in school , the code draw the electric field lines between two charges and we will be asked to explain it and i cant really understand much of it , is reading https://www.mathworks.com/support/learn-with-matlab-tutorials.html
will be enough?
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.
i did that and i kinda understood it but the lines
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);
i dont really get them
can you type the code with comment before each line with its explaning?
i would really apreaciate it
% 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);
thank you very much.
i hope that you always find help when you need it just like you have helped me
Be careful there, the * operation is algebraic matrix multiplication (inner product) not element by element multiplication. Element by element is the .* operator
yes thank you i just noticed it

Sign in to comment.

To learn fundamental concepts, such as syntax, invest 2 hours of your time here:

Categories

Asked:

on 30 Dec 2022

Commented:

on 30 Dec 2022

Community Treasure Hunt

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

Start Hunting!