electron(1) = struct('number', 1, 'name', 'test', 'charge', 1,
'position', [0 (1.1*closeenough) 0]);
electron(2) = struct('number', 2, 'name', 'plus', 'charge', 1,
'position', [0 0 0]);
electron(3) = struct('number', 3, 'name', 'minus', 'charge', -1,
'position', [xpos3 0 0]);
nmax = 3;
disp(' k x y z');
disp('--------------------');
k = 1;
continueloop = 1;
while (continueloop)
%fieldline(k) = electron(1).position;
netforce = [0 0 0];
for i=2:nmax
r(i) = electron(i).position - electron(1).position;
rmag(i) = sqrt(r(i)*r(i)');
if rmag(i) < closeenough | rmag(i) > faraway
continueloop = 0;
end
unit(i) = [rx(i) ry(i) rz(i)]./rmag(i);
forcemag(i) = electron(i).charge/(rmag(i)^2);
forcevec(i) = forcemag(i).*unit(i);
netforce = netforce + forcemag(i)*sign(electron
(i).charge).*forcevec(i);
end
unitforce = netforce / sqrt(netforce*netforce');
electron(1).position = electron(1).position + unitforce*increment;
if k == maxint
continueloop = 0;
end
disp(' %d ', k);
disp(' %g ', electron(1).position);
k = k + 1;
end
%plot3(
I keep getting the error:
??? In an assignment A(I) = B, the number of elements in B and
I must be the same.
Error in ==> test at 24
r(i) = electron(i).position - electron(1).position;
>>
I would like to know why I keep getting this and I would like to know
how to fix it if at all possible.
As you can see I commented out the line: %fieldline(k) = electron
(1).position;
That is because if it is not commented, the same error will come up
for that line. Eventually I want to do a 3D plot of this, and I will
plot fieldline(k).
Thanks in advance for any help you may be able to give me, it is
greatly appreciated!
John
*snip*
> I keep getting the error:
> ??? In an assignment A(I) = B, the number of elements in B and
> I must be the same.
>
> Error in ==> test at 24
> r(i) = electron(i).position - electron(1).position;
The variable "electron" is a 3-element struct array, and the position field
of each element of the struct array is a 1-by-3 vector.
> I would like to know why I keep getting this and I would like to know
> how to fix it if at all possible.
r(i) is a 1-by-1 element of r.
electron(i).position - electron(1).position results in a 1-by-3 vector.
Can you fit a vector with 3 elements into one element of another matrix?
No.
You could store the vector in a row of a matrix:
r(i, 1:3) = electron(i).position - electron(1).position;
or if you've preallocated r to have 3 columns:
r(i, :) = electron(i).position - electron(1).position;
or as an element in a cell array.
r{i} = electron(i).position - electron(1).position;
--
Steve Lord
sl...@mathworks.com
comp.soft-sys.matlab (CSSM) FAQ: http://matlabwiki.mathworks.com/MATLAB_FAQ
Thank you very much for your help!
It seems to have fixed the problem for me. :)