http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818
MATLAB Central Newsreader  plz helpme/adviseme on my algorithm!
Feed for thread: plz helpme/adviseme on my algorithm!
enus
©19942015 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Fri, 02 Jan 2009 16:12:01 +0000
plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619576
Sid Hayes
Hi<br>
<br>
Im having problems implementing my algorithm. All i want to implement is the formula:<br>
<br>
% q_zero is just a scalar number, assume scalars q(i)'s all equal to one.<br>
% c=constant scalar.<br>
% R is a vector. i.e, the random charges on a rod, where the force is to be computed % between the cahrges.<br>
<br>
F=q_zero*E<br>
<br>
where E= sum(for i=1:N) q(i)*(RR(i))/c*(abs(RR(i)).^3)<br>
<br>
Can someone please help me, or advise me on a better way to implement this?<br>
<br>
Warm Regards<br>
Sid<br>
<br>
p.s. my previous algorithm was:<br>
my algorithm is:<br>
clear all;<br>
% User to enter number of charges (N) to be placed on rod.<br>
N=input('Enter number of charges: '); <br>
<br>
% Position N charges on the rod randomly.<br>
intN=int32(N);<br>
% Get the random numbers, and sort them.<br>
R = sort(2 * rand(intN, 1)  1);<br>
<br>
R(1)=1; % Assign boundary charges.<br>
R(intN)=1;<br>
<br>
% Calculate the charges of N forces using Coulomb's Total Force.<br>
q_zero = input('Enter charge amount: ');<br>
constant=1;<br>
En=zeros(N,N);<br>
q=0.0;<br>
for i = 1:N<br>
En = sum((q(i)*(RR(i)))/(constant*(abs(RR(i)).^3)));<br>
end<br>
Fx=q_zero*En;

Fri, 02 Jan 2009 17:23:02 +0000
Re: plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619586
Matt
"Sid Hayes" <sid.hayes@hotmail.com> wrote in message <gjlech$v6$1@fred.mathworks.com>...<br>
> Hi<br>
> <br>
> Im having problems implementing my algorithm. All i want to implement is the formula:<br>
> <br>
> % q_zero is just a scalar number, assume scalars q(i)'s all equal to one.<br>
> % c=constant scalar.<br>
> % R is a vector. i.e, the random charges on a rod, where the force is to be computed % between the cahrges.<br>
> <br>
> F=q_zero*E<br>
> <br>
> where E= sum(for i=1:N) q(i)*(RR(i))/c*(abs(RR(i)).^3)<br>
<br>
This formula for E doesn't agree with the one in your forloop. In the above, you are multiplying by (abs(RR(i)).^3). Conversely, in the forloop, you are dividing by it.<br>
<br>
<br>
> % Calculate the charges of N forces using Coulomb's Total Force.<br>
> q_zero = input('Enter charge amount: ');<br>
> constant=1;<br>
> En=zeros(N,N);<br>
> q=0.0;<br>
> for i = 1:N<br>
> En = sum((q(i)*(RR(i)))/(constant*(abs(RR(i)).^3)));<br>
> end<br>
> Fx=q_zero*En;<br>
<br>
A few problems. <br>
<br>
(1)You said that q is a vector withh all q(i)=1, but in the above, it is a scalar and it is equal to zero. <br>
<br>
(2) You initializae En as an NxN matrix for some reason and then replace it by an Nvector.<br>
<br>
(3) You are overwriting En at each pass through the loop. I think what you want to do is accumulate the results in each loop. <br>
<br>
So I'm guessing it should look like this:<br>
<br>
En=zeros(size(R));<br>
q=ones(1,N);<br>
for i = 1:N<br>
En = En + sum( q(i)* (RR(i)) / (constant*(abs(RR(i)).^3) ) );<br>
end<br>
<br>
Fx=q_zero*En;

Fri, 02 Jan 2009 17:37:01 +0000
Re: plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619589
Matt
"Matt " <mjacobson.removethis@xorantech.com> wrote in message <gjlihm$rh4$1@fred.mathworks.com>...<br>
> "Sid Hayes" <sid.hayes@hotmail.com> wrote in message <gjlech$v6$1@fred.mathworks.com>...<br>
> > Hi<br>
> > <br>
> > Im having problems implementing my algorithm. All i want to implement is the formula:<br>
> > <br>
> > % q_zero is just a scalar number, assume scalars q(i)'s all equal to one.<br>
> > % c=constant scalar.<br>
> > % R is a vector. i.e, the random charges on a rod, where the force is to be computed % between the cahrges.<br>
> > <br>
> > F=q_zero*E<br>
> > <br>
> > where E= sum(for i=1:N) q(i)*(RR(i))/c*(abs(RR(i)).^3)<br>
> <br>
> This formula for E doesn't agree with the one in your forloop. In the above, you are multiplying by (abs(RR(i)).^3). Conversely, in the forloop, you are dividing by it.<br>
> <br>
> <br>
> > % Calculate the charges of N forces using Coulomb's Total Force.<br>
> > q_zero = input('Enter charge amount: ');<br>
> > constant=1;<br>
> > En=zeros(N,N);<br>
> > q=0.0;<br>
> > for i = 1:N<br>
> > En = sum((q(i)*(RR(i)))/(constant*(abs(RR(i)).^3)));<br>
> > end<br>
> > Fx=q_zero*En;<br>
> <br>
> A few problems. <br>
> <br>
> (1)You said that q is a vector withh all q(i)=1, but in the above, it is a scalar and it is equal to zero. <br>
> <br>
> (2) You initializae En as an NxN matrix for some reason and then replace it by an Nvector.<br>
> <br>
> (3) You are overwriting En at each pass through the loop. I think what you want to do is accumulate the results in each loop. <br>
> <br>
> So I'm guessing it should look like this:<br>
> <br>
> En=zeros(size(R));<br>
> q=ones(1,N);<br>
> for i = 1:N<br>
> En = En + sum( q(i)* (RR(i)) / (constant*(abs(RR(i)).^3) ) );<br>
> end<br>
> <br>
> Fx=q_zero*En;<br>
> <br>
<br>
Or maybe as follows<br>
<br>
<br>
En=0; %En is a scalar quantitiy<br>
q=ones(1,N);<br>
for i = 1:N<br>
En = En + sum( q(i)* (RR(i)) / (constant*(abs(RR(i)).^3) ) );<br>
end<br>
<br>
Fx=q_zero*En;<br>

Fri, 02 Jan 2009 18:08:02 +0000
Re: plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619591
Roger Stafford
"Sid Hayes" <sid.hayes@hotmail.com> wrote in message <gjlech$v6$1@fred.mathworks.com>...<br>
> Hi<br>
> <br>
> Im having problems implementing my algorithm. All i want to implement is the formula:<br>
> <br>
> % q_zero is just a scalar number, assume scalars q(i)'s all equal to one.<br>
> % c=constant scalar.<br>
> % R is a vector. i.e, the random charges on a rod, where the force is to be computed % between the cahrges.<br>
> <br>
> F=q_zero*E<br>
> <br>
> where E= sum(for i=1:N) q(i)*(RR(i))/c*(abs(RR(i)).^3)<br>
> <br>
> Can someone please help me, or advise me on a better way to implement this?<br>
> <br>
> Warm Regards<br>
> Sid<br>
> <br>
> p.s. my previous algorithm was:<br>
> my algorithm is:<br>
> clear all;<br>
> % User to enter number of charges (N) to be placed on rod.<br>
> N=input('Enter number of charges: '); <br>
> <br>
> % Position N charges on the rod randomly.<br>
> intN=int32(N);<br>
> % Get the random numbers, and sort them.<br>
> R = sort(2 * rand(intN, 1)  1);<br>
> <br>
> R(1)=1; % Assign boundary charges.<br>
> R(intN)=1;<br>
> <br>
> % Calculate the charges of N forces using Coulomb's Total Force.<br>
> q_zero = input('Enter charge amount: ');<br>
> constant=1;<br>
> En=zeros(N,N);<br>
> q=0.0;<br>
> for i = 1:N<br>
> En = sum((q(i)*(RR(i)))/(constant*(abs(RR(i)).^3)));<br>
> end<br>
> Fx=q_zero*En;<br>
<br>
In addition to Matt's remarks I will add this. Your stuff only makes sense if you are calculating the field E at some arbitrary point P = (x,y) or (x,y,z), in space. This had better not be one of the locations of your N charges because the field there would be infinite. You are using 'R' to designate this vector P but matlab would be confused by that because you are also using R to denote the positions of the charges. Use some other letter. Also for all of this to make sense, the positions of the charges along the rod should each be threedimensional vectors (in the spatial sense, not matlab's threedimensonal sense,) not scalars, to allow vector subtraction from P. For that you need to decide what is the orientation and location of the rod in space.<br>
<br>
By the way I don't see the need of sorting your charges in order along the rod. Coulomb's law holds independent of their ordering in R.<br>
<br>
Roger Stafford

Fri, 02 Jan 2009 18:41:02 +0000
Re: plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619596
Sid Hayes
Hi Matt,<br>
<br>
q is a scalar quantity, and En is meant to be a vector quantity. Also the final force F=q_zero*En will also be a vector quantity. Also, in reference to Roger comment, it will be calculated in two and three dimensions as different algorithms, but i thought first it would be great having an algorithm which works.<br>
<br>
the algorithm now gives an error of:<br>
??? Error using ==> plus<br>
Matrix dimensions must agree.<br>
<br>
Error in ==> TFalgorithm at 23<br>
En = En + sum( q(i)* (RR(i)) / (constant*(abs(RR(i)).^3) ) );<br>
<br>
The algorithm used:<br>
clear all;<br>
% User to enter number of charges (N) to be placed on rod.<br>
N=input('Enter number of charges: '); <br>
<br>
% Position N charges on the rod randomly.<br>
intN=int32(N);<br>
% Get the random numbers, and sort them.<br>
R = sort(2 * rand(intN, 1)  1);<br>
<br>
R(1)=1; % Assign boundary charges.<br>
R(intN)=1;<br>
<br>
% Calculate the charges of N forces using Coulomb's Total Force.<br>
q_zero = input('Enter charge amount: ');<br>
constant=1;<br>
En=zeros(size(R));<br>
q=ones(1,N);<br>
for i = 1:N<br>
En = En + sum( q(i)* (RR(i)) / (constant*(abs(RR(i)).^3) ) );<br>
end<br>
<br>
Fx=q_zero*En;

Fri, 02 Jan 2009 19:31:01 +0000
Re: plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619603
Sid Hayes
^

Fri, 02 Jan 2009 19:58:02 +0000
Re: plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619610
Matt
<br>
I think the following will work:<br>
<br>
<br>
clear all;<br>
% User to enter number of charges (N) to be placed on rod.<br>
N=input('Enter number of charges: '); <br>
<br>
% Position N charges on the rod randomly.<br>
intN=int32(N);<br>
% Get the random numbers, and sort them.<br>
R = sort(2 * rand(intN, 1)  1);<br>
<br>
R(1)=1; % Assign boundary charges.<br>
R(intN)=1;<br>
<br>
% Calculate the charges of N forces using Coulomb's Total Force.<br>
q_zero = input('Enter charge amount: ');<br>
P = input('Enter test location 1<=P<=1: ');<br>
constant=1;<br>
En=0;<br>
q=ones(1,N);<br>
for i = 1:N<br>
En = En + sum( q(i)* (PR(i)) ./ (constant*(abs(PR(i)).^3) ) );<br>
end<br>
<br>
Fx=q_zero*En;

Fri, 02 Jan 2009 20:09:02 +0000
Re: plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619611
Sid Hayes
Hi Matt<br>
<br>
Its not what i wanted, but i will do some more work on it and get back to you.<br>
<br>
Cheers

Fri, 02 Jan 2009 21:35:02 +0000
Re: plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619622
Matt
"Sid Hayes" <sid.hayes@hotmail.com> wrote in message <gjls8t$9m6$1@fred.mathworks.com>...<br>
> Hi Matt<br>
> <br>
> Its not what i wanted, but i will do some more work on it and get back to you.<br>
> <br>
> Cheers<br>
<br>
Perhaps this then?<br>
<br>
<br>
clear all;<br>
% User to enter number of charges (N) to be placed on rod.<br>
N=input('Enter number of charges: '); <br>
<br>
% Position N charges on the rod randomly.<br>
intN=int32(N);<br>
% Get the random numbers, and sort them.<br>
R = sort(2 * rand(intN, 1)  1);<br>
<br>
R(1)=1; % Assign boundary charges.<br>
R(intN)=1;<br>
<br>
% Calculate the charges of N forces using Coulomb's Total Force.<br>
q_zero = input('Enter charge amount: ');<br>
constant=1;<br>
En=0;<br>
q=ones(1,N);<br>
for i = 1:N<br>
Z=q(i)* (RR(i)) ./ (constant*(abs(PR(i)).^3) ) ;<br>
Z(isnan(Z))=0;<br>
En = En + sum(Z);<br>
end<br>
<br>
Fx=q_zero*En;

Sat, 03 Jan 2009 03:55:03 +0000
Re: plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619652
Sid Hayes
An error is printed during runtime:<br>
<br>
??? Undefined function or variable "Z".<br>
<br>
Error in ==> TFalgorithm at 27<br>
Z(isnan(Z))=0; %error is printed during run time, as z is undefined.

Sat, 03 Jan 2009 04:43:01 +0000
Re: plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619662
Image Analyst
Sid:<br>
Try this. Just copy and paste. It will work (assuming no lines got broken at weird places).<br>
<br>
clc;<br>
clear all;<br>
% User to enter number of charges (N) to be placed on rod. <br>
numCharges = int32(input('Enter number of charges: ')); <br>
<br>
% Position charges on the rod randomly. <br>
% Get the random numbers, and sort them. <br>
R = sort(2 * rand(numCharges, 1)  1); <br>
<br>
R(1)=1; % Assign boundary charges. <br>
R(numCharges)=1; <br>
<br>
% Calculate the charges of N forces using Coulomb's Total Force. <br>
q_zero = input('Enter charge amount: '); <br>
constant=1; <br>
% Calculate how many forces between ion pairs there will be.<br>
numInteractions = numCharges * (numCharges  1) / 2;<br>
% Preallocate an array to hold all these forces.<br>
forceBetweenPair = zeros(numInteractions, 1);<br>
for charge1 = 1 : numCharges<br>
for charge2 = 1 : numCharges<br>
if charge1 ~= charge2<br>
% If it's not the same ion<br>
% Calculate force between the two ions.<br>
distanceSquared = abs((R(charge1)  R(charge2))^2);<br>
forceBetweenPair = constant * ...<br>
q_zero * q_zero / distanceSquared;<br>
end<br>
end<br>
end <br>
% Display sum<br>
totalSum = sum(forceBetweenPair)<br>
<br>
<br>
Regards,<br>
ImageAnalyst

Sat, 03 Jan 2009 05:06:02 +0000
Re: plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619666
Image Analyst
Sorry  two errorw in the above code I just posted. <br>
Corrected code below:<br>
<br>
clc;<br>
clear all;<br>
% User to enter number of charges (N) to be placed on rod. <br>
numCharges = int32(input('Enter number of charges: ')); <br>
<br>
% Position charges on the rod randomly. <br>
% Get the random numbers, and sort them. <br>
R = sort(2 * rand(numCharges, 1)  1); <br>
<br>
R(1)=1; % Assign boundary charges. <br>
R(numCharges)=1; <br>
<br>
% Calculate the charges of N forces using Coulomb's Total Force. <br>
q_zero = input('Enter charge amount: '); <br>
constant=1; <br>
% Calculate how many forces between ion pairs there will be.<br>
numInteractions = numCharges * (numCharges  1) / 2;<br>
% Preallocate an array to hold all these forces.<br>
forceBetweenPair = zeros(numInteractions, 1);<br>
for charge1 = 1 : numCharges<br>
for charge2 = 1 : numCharges<br>
if charge1 ~= charge2<br>
% If it's not the same ion<br>
% Calculate force between the two ions.<br>
distanceSquared = abs((R(charge1)  R(charge2))^2);<br>
forceBetweenPair(charge1) = constant * ...<br>
q_zero * q_zero / distanceSquared;<br>
end<br>
end<br>
end <br>
% Sum up and divide by 2 to eliminate double counting.<br>
% For example doublecounting force between 2 and 5, and also 5 and 2.<br>
% Display sum<br>
totalSum = sum(forceBetweenPair / 2)

Sat, 03 Jan 2009 18:44:02 +0000
Re: plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619734
Matt
"Sid Hayes" <sid.hayes@hotmail.com> wrote in message <gjlech$v6$1@fred.mathworks.com>...<br>
> Hi<br>
> <br>
> Im having problems implementing my algorithm. All i want to implement is the formula:<br>
> <br>
> % q_zero is just a scalar number, assume scalars q(i)'s all equal to one.<br>
> % c=constant scalar.<br>
> % R is a vector. i.e, the random charges on a rod, where the force is to be computed % between the cahrges.<br>
> <br>
> F=q_zero*E<br>
> <br>
> where E= sum(for i=1:N) q(i)*(RR(i))/c*(abs(RR(i)).^3)<br>
<br>
<br>
By the way, don't we expect the resulting force to be zero all the time? There are no external forces on this system. All forces are interal ones between the charges.

Sat, 03 Jan 2009 22:01:41 +0000
Re: plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619758
Roger Stafford
"Matt " <mjacobson.removethis@xorantech.com> wrote in message <gjobli$41j$1@fred.mathworks.com>...<br>
> .......<br>
> By the way, don't we expect the resulting force to be zero all the time? There are no external forces on this system. All forces are interal ones between the charges.<br>
<br>
No, it's pretty clear, at least on the part of whoever assigned this problem, that it was intended for calculating the electric field at arbitrary points P = (x,y,z) different from the positions of the charges. It is just the kind of assignment I would expect in a beginning E & M physics course. In the formula F=q_zero*E the distinction that was made between q_zero and the charges in q(i) make it almost certain that q_zero is to be a test charge placed somewhere in the field E and F is the force exerted on it.<br>
<br>
You are right that the total intercharge force must be zero, courtesy of Newton's third law of motion, but Sid has taken no pains to exclude pairing the same charge with itself which would theoretically be infinite (and produce a NaN in the calculations.) I think the intent is clearly that the R in (RR(i)) was meant by somebody to be distinct from all the R(i)'s. Otherwise there is no sense to the physical problem. With the above understanding the total electric force field would not be zero.<br>
<br>
There has been no response from Sid on this point even though I mentioned it yesterday. Also there has been no response to the point that the sorting operation was totally unnecessary since the sequential ordering of charges within an array in the formula would not affect the total field. It is just a rearrangement of terms in a sum and summation is always commutative. How about it, Sid? What is your response to these two suggestions?<br>
<br>
Roger Stafford

Sat, 03 Jan 2009 22:58:02 +0000
Re: plz helpme/adviseme on my algorithm!
http://www.mathworks.com/matlabcentral/newsreader/view_thread/241818#619763
Matt
"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjon85$rcc$1@fred.mathworks.com>...<br>
I think the intent is clearly that the R in (RR(i)) was meant by somebody to be distinct from all the R(i)'s. Otherwise there is no sense to the physical problem. With the above understanding the total electric force field would not be zero.<br>
> <br>
> There has been no response from Sid on this point even though I mentioned it yesterday. <br>
<br>
<br>
On that point, at least, I think Sid did respond. I posted a version of the code that would compute the force at an arbitrary test point P and Sid said it was not what he wanted.<br>
<br>
But I agree that it's the only thing that makes sense as an exercise.