Info

此问题已关闭。 请重新打开它进行编辑或回答。

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 次查看(过去 30 天)
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

回答(1 个)

Roger Stafford
Roger Stafford 2014-12-29
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 个评论
Hikaru
Hikaru 2014-12-30
i did that but the problem wasn't solved the code keeps returning zeroes here's the new code
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_tot=0; Ey_tot=0; F=10000000000000000000000000;
for y=0:0.01:10
for x=0:0.01:10
for k=1:N
x1= Q(k,1);
y1=Q(k,2);
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_tot=Ex_tot+Ex;
Ey_tot=Ey_tot+Ey;
E=sqrt( (Ex_tot)^2 + (Ey_tot)^2 );
E_min=E_min+E;
if E_min < F
F=E_min;
X_min=x;
Y_min=y;
end
Ex_tot=0;
Ey_tot=0;
end
end
end F X_min Y_min
Roger Stafford
Roger Stafford 2014-12-31
编辑:Roger Stafford 2014-12-31
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.

此问题已关闭。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by