Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
plz help-me/advise-me on my algorithm!

Subject: plz help-me/advise-me on my algorithm!

From: Sid Hayes

Date: 2 Jan, 2009 16:12:01

Message: 1 of 15

Hi

Im having problems implementing my algorithm. All i want to implement is the formula:

% q_zero is just a scalar number, assume scalars q(i)'s all equal to one.
% c=constant scalar.
% R is a vector. i.e, the random charges on a rod, where the force is to be computed % between the cahrges.

F=q_zero*E

where E= sum(for i=1:N) q(i)*(R-R(i))/c*(abs(R-R(i)).^3)

Can someone please help me, or advise me on a better way to implement this?

Warm Regards
Sid

p.s. my previous algorithm was:
my algorithm is:
clear all;
% User to enter number of charges (N) to be placed on rod.
    N=input('Enter number of charges: ');

% Position N charges on the rod randomly.
    intN=int32(N);
    % Get the random numbers, and sort them.
    R = sort(2 * rand(intN, 1) - 1);
    
    R(1)=-1; % Assign boundary charges.
    R(intN)=1;
   
% Calculate the charges of N forces using Coulomb's Total Force.
    q_zero = input('Enter charge amount: ');
    constant=1;
    En=zeros(N,N);
    q=0.0;
    for i = 1:N
        En = sum((q(i)*(R-R(i)))/(constant*(abs(R-R(i)).^3)));
    end
    Fx=q_zero*En;

Subject: plz help-me/advise-me on my algorithm!

From: Matt

Date: 2 Jan, 2009 17:23:02

Message: 2 of 15

"Sid Hayes" <sid.hayes@hotmail.com> wrote in message <gjlech$v6$1@fred.mathworks.com>...
> Hi
>
> Im having problems implementing my algorithm. All i want to implement is the formula:
>
> % q_zero is just a scalar number, assume scalars q(i)'s all equal to one.
> % c=constant scalar.
> % R is a vector. i.e, the random charges on a rod, where the force is to be computed % between the cahrges.
>
> F=q_zero*E
>
> where E= sum(for i=1:N) q(i)*(R-R(i))/c*(abs(R-R(i)).^3)

This formula for E doesn't agree with the one in your for-loop. In the above, you are multiplying by (abs(R-R(i)).^3). Conversely, in the for-loop, you are dividing by it.


> % Calculate the charges of N forces using Coulomb's Total Force.
> q_zero = input('Enter charge amount: ');
> constant=1;
> En=zeros(N,N);
> q=0.0;
> for i = 1:N
> En = sum((q(i)*(R-R(i)))/(constant*(abs(R-R(i)).^3)));
> end
> Fx=q_zero*En;

A few problems.

(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.

(2) You initializae En as an NxN matrix for some reason and then replace it by an N-vector.

(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.

So I'm guessing it should look like this:

     En=zeros(size(R));
     q=ones(1,N);
     for i = 1:N
         En = En + sum( q(i)* (R-R(i)) / (constant*(abs(R-R(i)).^3) ) );
     end

    Fx=q_zero*En;

Subject: plz help-me/advise-me on my algorithm!

From: Matt

Date: 2 Jan, 2009 17:37:01

Message: 3 of 15

"Matt " <mjacobson.removethis@xorantech.com> wrote in message <gjlihm$rh4$1@fred.mathworks.com>...
> "Sid Hayes" <sid.hayes@hotmail.com> wrote in message <gjlech$v6$1@fred.mathworks.com>...
> > Hi
> >
> > Im having problems implementing my algorithm. All i want to implement is the formula:
> >
> > % q_zero is just a scalar number, assume scalars q(i)'s all equal to one.
> > % c=constant scalar.
> > % R is a vector. i.e, the random charges on a rod, where the force is to be computed % between the cahrges.
> >
> > F=q_zero*E
> >
> > where E= sum(for i=1:N) q(i)*(R-R(i))/c*(abs(R-R(i)).^3)
>
> This formula for E doesn't agree with the one in your for-loop. In the above, you are multiplying by (abs(R-R(i)).^3). Conversely, in the for-loop, you are dividing by it.
>
>
> > % Calculate the charges of N forces using Coulomb's Total Force.
> > q_zero = input('Enter charge amount: ');
> > constant=1;
> > En=zeros(N,N);
> > q=0.0;
> > for i = 1:N
> > En = sum((q(i)*(R-R(i)))/(constant*(abs(R-R(i)).^3)));
> > end
> > Fx=q_zero*En;
>
> A few problems.
>
> (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.
>
> (2) You initializae En as an NxN matrix for some reason and then replace it by an N-vector.
>
> (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.
>
> So I'm guessing it should look like this:
>
> En=zeros(size(R));
> q=ones(1,N);
> for i = 1:N
> En = En + sum( q(i)* (R-R(i)) / (constant*(abs(R-R(i)).^3) ) );
> end
>
> Fx=q_zero*En;
>

Or maybe as follows


     En=0; %En is a scalar quantitiy
      q=ones(1,N);
      for i = 1:N
          En = En + sum( q(i)* (R-R(i)) / (constant*(abs(R-R(i)).^3) ) );
      end
 
     Fx=q_zero*En;
 

Subject: plz help-me/advise-me on my algorithm!

From: Roger Stafford

Date: 2 Jan, 2009 18:08:02

Message: 4 of 15

"Sid Hayes" <sid.hayes@hotmail.com> wrote in message <gjlech$v6$1@fred.mathworks.com>...
> Hi
>
> Im having problems implementing my algorithm. All i want to implement is the formula:
>
> % q_zero is just a scalar number, assume scalars q(i)'s all equal to one.
> % c=constant scalar.
> % R is a vector. i.e, the random charges on a rod, where the force is to be computed % between the cahrges.
>
> F=q_zero*E
>
> where E= sum(for i=1:N) q(i)*(R-R(i))/c*(abs(R-R(i)).^3)
>
> Can someone please help me, or advise me on a better way to implement this?
>
> Warm Regards
> Sid
>
> p.s. my previous algorithm was:
> my algorithm is:
> clear all;
> % User to enter number of charges (N) to be placed on rod.
> N=input('Enter number of charges: ');
>
> % Position N charges on the rod randomly.
> intN=int32(N);
> % Get the random numbers, and sort them.
> R = sort(2 * rand(intN, 1) - 1);
>
> R(1)=-1; % Assign boundary charges.
> R(intN)=1;
>
> % Calculate the charges of N forces using Coulomb's Total Force.
> q_zero = input('Enter charge amount: ');
> constant=1;
> En=zeros(N,N);
> q=0.0;
> for i = 1:N
> En = sum((q(i)*(R-R(i)))/(constant*(abs(R-R(i)).^3)));
> end
> Fx=q_zero*En;

  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 three-dimensional vectors (in the spatial sense, not matlab's three-dimensonal 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.

  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.

Roger Stafford

Subject: plz help-me/advise-me on my algorithm!

From: Sid Hayes

Date: 2 Jan, 2009 18:41:02

Message: 5 of 15

Hi Matt,

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.

the algorithm now gives an error of:
??? Error using ==> plus
Matrix dimensions must agree.

Error in ==> TFalgorithm at 23
         En = En + sum( q(i)* (R-R(i)) / (constant*(abs(R-R(i)).^3) ) );

The algorithm used:
clear all;
% User to enter number of charges (N) to be placed on rod.
    N=input('Enter number of charges: ');

% Position N charges on the rod randomly.
    intN=int32(N);
    % Get the random numbers, and sort them.
    R = sort(2 * rand(intN, 1) - 1);
    
    R(1)=-1; % Assign boundary charges.
    R(intN)=1;
   
% Calculate the charges of N forces using Coulomb's Total Force.
    q_zero = input('Enter charge amount: ');
    constant=1;
    En=zeros(size(R));
     q=ones(1,N);
     for i = 1:N
         En = En + sum( q(i)* (R-R(i)) / (constant*(abs(R-R(i)).^3) ) );
     end

    Fx=q_zero*En;

Subject: plz help-me/advise-me on my algorithm!

From: Sid Hayes

Date: 2 Jan, 2009 19:31:01

Message: 6 of 15

^

Subject: plz help-me/advise-me on my algorithm!

From: Matt

Date: 2 Jan, 2009 19:58:02

Message: 7 of 15


I think the following will work:


clear all;
% User to enter number of charges (N) to be placed on rod.
    N=input('Enter number of charges: ');

% Position N charges on the rod randomly.
    intN=int32(N);
    % Get the random numbers, and sort them.
    R = sort(2 * rand(intN, 1) - 1);
    
    R(1)=-1; % Assign boundary charges.
    R(intN)=1;
   
% Calculate the charges of N forces using Coulomb's Total Force.
    q_zero = input('Enter charge amount: ');
    P = input('Enter test location -1<=P<=1: ');
    constant=1;
    En=0;
     q=ones(1,N);
     for i = 1:N
         En = En + sum( q(i)* (P-R(i)) ./ (constant*(abs(P-R(i)).^3) ) );
     end

    Fx=q_zero*En;

Subject: plz help-me/advise-me on my algorithm!

From: Sid Hayes

Date: 2 Jan, 2009 20:09:02

Message: 8 of 15

Hi Matt

Its not what i wanted, but i will do some more work on it and get back to you.

Cheers

Subject: plz help-me/advise-me on my algorithm!

From: Matt

Date: 2 Jan, 2009 21:35:02

Message: 9 of 15

"Sid Hayes" <sid.hayes@hotmail.com> wrote in message <gjls8t$9m6$1@fred.mathworks.com>...
> Hi Matt
>
> Its not what i wanted, but i will do some more work on it and get back to you.
>
> Cheers

Perhaps this then?


clear all;
% User to enter number of charges (N) to be placed on rod.
    N=input('Enter number of charges: ');

% Position N charges on the rod randomly.
    intN=int32(N);
    % Get the random numbers, and sort them.
    R = sort(2 * rand(intN, 1) - 1);
    
    R(1)=-1; % Assign boundary charges.
    R(intN)=1;
   
% Calculate the charges of N forces using Coulomb's Total Force.
    q_zero = input('Enter charge amount: ');
    constant=1;
    En=0;
     q=ones(1,N);
     for i = 1:N
         Z=q(i)* (R-R(i)) ./ (constant*(abs(P-R(i)).^3) ) ;
         Z(isnan(Z))=0;
         En = En + sum(Z);
     end

    Fx=q_zero*En;

Subject: plz help-me/advise-me on my algorithm!

From: Sid Hayes

Date: 3 Jan, 2009 03:55:03

Message: 10 of 15

An error is printed during run-time:

??? Undefined function or variable "Z".

Error in ==> TFalgorithm at 27
     Z(isnan(Z))=0; %error is printed during run time, as z is undefined.

Subject: plz help-me/advise-me on my algorithm!

From: Image Analyst

Date: 3 Jan, 2009 04:43:01

Message: 11 of 15

Sid:
Try this. Just copy and paste. It will work (assuming no lines got broken at weird places).

clc;
clear all;
% User to enter number of charges (N) to be placed on rod.
numCharges = int32(input('Enter number of charges: '));

% Position charges on the rod randomly.
% Get the random numbers, and sort them.
R = sort(2 * rand(numCharges, 1) - 1);

R(1)=-1; % Assign boundary charges.
R(numCharges)=1;

% Calculate the charges of N forces using Coulomb's Total Force.
q_zero = input('Enter charge amount: ');
constant=1;
% Calculate how many forces between ion pairs there will be.
numInteractions = numCharges * (numCharges - 1) / 2;
% Pre-allocate an array to hold all these forces.
forceBetweenPair = zeros(numInteractions, 1);
for charge1 = 1 : numCharges
for charge2 = 1 : numCharges
if charge1 ~= charge2
% If it's not the same ion
% Calculate force between the two ions.
distanceSquared = abs((R(charge1) - R(charge2))^2);
forceBetweenPair = constant * ...
q_zero * q_zero / distanceSquared;
end
end
end
% Display sum
totalSum = sum(forceBetweenPair)


Regards,
ImageAnalyst

Subject: plz help-me/advise-me on my algorithm!

From: Image Analyst

Date: 3 Jan, 2009 05:06:02

Message: 12 of 15

Sorry - two errorw in the above code I just posted.
Corrected code below:

clc;
clear all;
% User to enter number of charges (N) to be placed on rod.
numCharges = int32(input('Enter number of charges: '));

% Position charges on the rod randomly.
% Get the random numbers, and sort them.
R = sort(2 * rand(numCharges, 1) - 1);

R(1)=-1; % Assign boundary charges.
R(numCharges)=1;

% Calculate the charges of N forces using Coulomb's Total Force.
q_zero = input('Enter charge amount: ');
constant=1;
% Calculate how many forces between ion pairs there will be.
numInteractions = numCharges * (numCharges - 1) / 2;
% Pre-allocate an array to hold all these forces.
forceBetweenPair = zeros(numInteractions, 1);
for charge1 = 1 : numCharges
for charge2 = 1 : numCharges
if charge1 ~= charge2
% If it's not the same ion
% Calculate force between the two ions.
distanceSquared = abs((R(charge1) - R(charge2))^2);
forceBetweenPair(charge1) = constant * ...
q_zero * q_zero / distanceSquared;
end
end
end
% Sum up and divide by 2 to eliminate double counting.
% For example double-counting force between 2 and 5, and also 5 and 2.
% Display sum
totalSum = sum(forceBetweenPair / 2)

Subject: plz help-me/advise-me on my algorithm!

From: Matt

Date: 3 Jan, 2009 18:44:02

Message: 13 of 15

"Sid Hayes" <sid.hayes@hotmail.com> wrote in message <gjlech$v6$1@fred.mathworks.com>...
> Hi
>
> Im having problems implementing my algorithm. All i want to implement is the formula:
>
> % q_zero is just a scalar number, assume scalars q(i)'s all equal to one.
> % c=constant scalar.
> % R is a vector. i.e, the random charges on a rod, where the force is to be computed % between the cahrges.
>
> F=q_zero*E
>
> where E= sum(for i=1:N) q(i)*(R-R(i))/c*(abs(R-R(i)).^3)


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.

Subject: plz help-me/advise-me on my algorithm!

From: Roger Stafford

Date: 3 Jan, 2009 22:01:41

Message: 14 of 15

"Matt " <mjacobson.removethis@xorantech.com> wrote in message <gjobli$41j$1@fred.mathworks.com>...
> .......
> 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.

  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.

  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 (R-R(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.

  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?

Roger Stafford

Subject: plz help-me/advise-me on my algorithm!

From: Matt

Date: 3 Jan, 2009 22:58:02

Message: 15 of 15

"Roger Stafford" <ellieandrogerxyzzy@mindspring.com.invalid> wrote in message <gjon85$rcc$1@fred.mathworks.com>...
 I think the intent is clearly that the R in (R-R(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.
>
> There has been no response from Sid on this point even though I mentioned it yesterday.
-------------------------------------------------------------------

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.

But I agree that it's the only thing that makes sense as an exercise.

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us