Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
what's the error in this code ? it should return the position of the minimum electric field but it return zeros ,whyyyyy T~T
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
clc
clear all
N = input('Numbers of charges=');
prompt = {'x-coor:','y-coor:','charge-value'};
name = 'Input coordinates and charges value';
numlines = 1;
answer = inputdlg(prompt,name,numlines);
Q = [str2num(answer{1}); str2num(answer{2});str2num(answer{3})]';
Q
E_min=0;
Ex_min=0;
Ey_min=0;
F=10000000000000000000000000;
for k=1:N
for y=0:0.01:10
x1= Q(k,1);
y1=Q(k,2);
for x=0:0.01:10
Ex=(Q(k,3)*(x-x1))/(4*pi*8.854*10^-12 * ((x-x1)^2+(y-y1)^2)^(3/2));
Ey=(Q(k,3)*(y-y1))/(4*pi*8.854*10^-12 * ((x-x1)^2+(y-y1)^2)^(3/2));
Ex_min=Ex_min+Ex;
Ey_min=Ey_min+Ey;
E=sqrt( (Ex_min)^2 + (Ey_min)^2 );
E_min=E_min+E;
if E_min < F
F=E_min;
X_min=x;
Y_min=y;
end
end
end
Ex_min=0;
Ey_min=0;
end
F
X_min
Y_min
0 comentarios
Respuestas (1)
Roger Stafford
el 29 de Dic. de 2014
Instead of computing the total electric field for all charges at each separate point, you appear to be summing the field components over all the 101-by-101 points in your x-y grid, separately for each charge. That makes no sense. You need to rearrange the order of your for-loops so that the outer loops vary x and y and the innermost loop scans through all the charges to get total field at each separate point so as to find the correct minimum.
2 comentarios
Roger Stafford
el 31 de Dic. de 2014
Editada: Roger Stafford
el 31 de Dic. de 2014
Your code still looks faulty.
1) In the line
E = sqrt( (Ex_tot)^2 + (Ey_tot)^2 );
you are computing the magnitude too soon. You need to wait until the innermost loop is done summing the two components Ex_tot and Ey_tot for all N charges before you compute the magnitude. Remember, the magnitude of the sum of field vectors is the magnitude of the sum of their respective components, not the sum of their magnitudes.
2) The line
E_min = E_min+E;
should not be done at all. After you compute E upon exiting the inner loop, this E itself is what you are trying to find the minimum of, not its sum over all grid points. The latter would make no sense.
3) The lines in the 'if-end' section are also misplaced. They should be executed only after the summation over the charges is completed and they should be:
if E < F
F=E;
X_min=x;
Y_min=y;
end
4) Similarly the two lines
Ex_tot=0;
Ey_tot=0;
are misplaced, and should be done only after exiting that inner loop (or better still just before entering it.)
5) The initial value given F should have been 'inf' to be sure of a proper result in all circumstances.
You should also be aware that this code could be greatly simplified by using the matlab 'sum' and 'min' functions in the proper way.
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!