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;

Réponses (2)

Steven Lord
Steven Lord le 30 Déc 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 commentaires

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
Image Analyst
Image Analyst le 30 Déc 2022
Modifié(e) : Image Analyst le 30 Déc 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);
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

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 30 Déc 2022

1 vote

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

Catégories

En savoir plus sur Vector Fields dans Centre d'aide et File Exchange

Question posée :

le 30 Déc 2022

Commenté :

le 30 Déc 2022

Community Treasure Hunt

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

Start Hunting!

Translated by