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:
Need Help Guys. Very simple problem

Subject: Need Help Guys. Very simple problem

From: Sid Hayes

Date: 15 Jan, 2009 22:25:03

Message: 1 of 6

Hi Guys

I need to minimise the total force by moving the i th charge with respect to lamda which i will be calling 0.001.

Its nearly there, but a simple problem is stopping me.

Warm Regards
Sid

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.
    N=int32(N);
    % Get the random numbers, and sort them.
    R = sort(2 * rand(N, 1) - 1);
    
    R(1)=-1; % Assign boundary charges.
    R(N)=1;
   
% Calculate the charges of N forces using C.. o.. u.. l..o.. m.. b..'s T.o,t.a,l F,o.r,c.e.
    q_zero = input('Enter charge amount: ');
    %epsilon0=8.85e-12; % permittivity of free space (Nm^2/C^-2)
    %constant=1/4*pi*epsilon0;
    constant=1;
    %En=zeros(size(R));
    En=zeros(N,N);
    %Fx=zeros(N,N);
    %En=0.0;
    q=ones(1,N);
     for i = 2:N-1
         for j = 2:N-1
             if(i ~= j) % i charge Not equal to j charge
                
                %z=((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) );
                %Z(isnan(Z))=0.0;
                %En = En + sum(Z);
         
                Rij=(R(i) - R(j));
                En(i,j) = En(i,j) + sum( ((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) ) );
                %surf(En); % plot statement to visualize En at each charge
                %En %return En values to screen
                
                Fx=q_zero.*En;
                
                % Displace every charge a little bit (as value lamda) in direction of
                % force. Use: Rij(t+1)=Rij(t)+lamda*Fx(t).
                lamda = 0.001;
                t=1;
                while (lamda<2 || Fx == 0) %need condition to be until Fx=0; an equlibrium state.
                    R(1)=R(1)+lamda;
                    %Rij(i+1) = Rij(i) + lamda*Fx(i);
                    %t=t+1;
                    Rij=(R(i) - R(j));
                    En(i,j) = En(i,j) + sum( ((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) ) );
                    lamda=lamda+0.001;
                    Fx=q_zero.*En;
                    t=+1;
                end
                plot(i,Fx)
             end
         end
     end
     fprintf('\n');

Subject: Need Help Guys. Very simple problem

From: Sid Hayes

Date: 16 Jan, 2009 00:33:01

Message: 2 of 6

> Hi Guys
>
> I need to minimise the total force by moving the i th charge with respect to lamda which i will be calling 0.001.
>
> Its nearly there, but a simple problem is stopping me.
>
> Warm Regards
> Sid
>
> 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.
> N=int32(N);
> % Get the random numbers, and sort them.
> R = sort(2 * rand(N, 1) - 1);
>
> R(1)=-1; % Assign boundary charges.
> R(N)=1;
>
> % Calculate the charges of N forces using C.. o.. u.. l..o.. m.. b..'s T.o,t.a,l F,o.r,c.e.
> q_zero = input('Enter charge amount: ');
> %epsilon0=8.85e-12; % permittivity of free space (Nm^2/C^-2)
> %constant=1/4*pi*epsilon0;
> constant=1;
> %En=zeros(size(R));
> En=zeros(N,N);
> %Fx=zeros(N,N);
> %En=0.0;
> q=ones(1,N);
> for i = 2:N-1
> for j = 2:N-1
> if(i ~= j) % i charge Not equal to j charge
>
> %z=((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) );
> %Z(isnan(Z))=0.0;
> %En = En + sum(Z);
>
> Rij=(R(i) - R(j));
> En(i,j) = En(i,j) + sum( ((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) ) );
> %surf(En); % plot statement to visualize En at each charge
> %En %return En values to screen
>
> Fx=q_zero.*En;
>
> % Displace every charge a little bit (as value lamda) in direction of
> % force. Use: Rij(t+1)=Rij(t)+lamda*Fx(t).
> lamda = 0.001;
> t=1;
> while (lamda<2 || Fx == 0) %need condition to be until Fx=0; an equlibrium state.
> R(1)=R(1)+lamda;
> %Rij(i+1) = Rij(i) + lamda*Fx(i);
> %t=t+1;
> Rij=(R(i) - R(j));
> En(i,j) = En(i,j) + sum( ((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) ) );
> lamda=lamda+0.001;
> Fx=q_zero.*En;
> t=+1;
> end
> plot(i,Fx)
> end
> end
> end
> fprintf('\n');

^^

Subject: Need Help Guys. Very simple problem

From: Image Analyst

Date: 16 Jan, 2009 00:38:02

Message: 3 of 6

sid hayes:
Well Fx is an array, because En is a 2D array. Perhaps you meant
Fx=q_zero*En(i, j);
instead?

Subject: Need Help Guys. Very simple problem

From: Sid Hayes

Date: 16 Jan, 2009 09:31:03

Message: 4 of 6

"sid hayes" <sid.hayes@hotmail.com> wrote in message <gkod3v$8fu$1@fred.mathworks.com>...
> Hi Guys
>
> I need to minimise the total force by moving the i th charge with respect to lamda which i will be calling 0.001.
>
> Its nearly there, but a simple problem is stopping me.
>
> Warm Regards
> Sid
>
> 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.
> N=int32(N);
> % Get the random numbers, and sort them.
> R = sort(2 * rand(N, 1) - 1);
>
> R(1)=-1; % Assign boundary charges.
> R(N)=1;
>
> % Calculate the charges of N forces using C.. o.. u.. l..o.. m.. b..'s T.o,t.a,l F,o.r,c.e.
> q_zero = input('Enter charge amount: ');
> %epsilon0=8.85e-12; % permittivity of free space (Nm^2/C^-2)
> %constant=1/4*pi*epsilon0;
> constant=1;
> %En=zeros(size(R));
> En=zeros(N,N);
> %Fx=zeros(N,N);
> %En=0.0;
> q=ones(1,N);
> for i = 2:N-1
> for j = 2:N-1
> if(i ~= j) % i charge Not equal to j charge
>
> %z=((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) );
> %Z(isnan(Z))=0.0;
> %En = En + sum(Z);
>
> Rij=(R(i) - R(j));
> En(i,j) = En(i,j) + sum( ((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) ) );
> %surf(En); % plot statement to visualize En at each charge
> %En %return En values to screen
>
> Fx=q_zero.*En;
>
> % Displace every charge a little bit (as value lamda) in direction of
> % force. Use: Rij(t+1)=Rij(t)+lamda*Fx(t).
> lamda = 0.001;
> t=1;
> while (lamda<2 || Fx == 0) %need condition to be until Fx=0; an equlibrium state.
> R(1)=R(1)+lamda;
> %Rij(i+1) = Rij(i) + lamda*Fx(i);
> %t=t+1;
> Rij=(R(i) - R(j));
> En(i,j) = En(i,j) + sum( ((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) ) );
> lamda=lamda+0.001;
> Fx=q_zero.*En;
> t=+1;
> end
> plot(i,Fx)
> end
> end
> end
> fprintf('\n');

Hi

If I change the plot function to surf(Fx) which is what I want to see, It gives the message:

>> Enter number of charges: 10
>> Enter charge amount: 0.01

Warning: Z must not be a scalar or vector, not rendering surface.
Warning: Z must not be a scalar or vector, not rendering surface.
Warning: Z must not be a scalar or vector, not rendering surface.
Warning: Z must not be a scalar or vector, not rendering surface.

Warm Regards
Sid

Subject: Need Help Guys. Very simple problem

From: Sid Hayes

Date: 16 Jan, 2009 12:04:02

Message: 5 of 6

"sid hayes" <sid.hayes@hotmail.com> wrote in message <gkod3v$8fu$1@fred.mathworks.com>...
> Hi Guys
>
> I need to minimise the total force by moving the i th charge with respect to lamda which i will be calling 0.001.
>
> Its nearly there, but a simple problem is stopping me.
>
> Warm Regards
> Sid
>
> 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.
> N=int32(N);
> % Get the random numbers, and sort them.
> R = sort(2 * rand(N, 1) - 1);
>
> R(1)=-1; % Assign boundary charges.
> R(N)=1;
>
> % Calculate the charges of N forces using C.. o.. u.. l..o.. m.. b..'s T.o,t.a,l F,o.r,c.e.
> q_zero = input('Enter charge amount: ');
> %epsilon0=8.85e-12; % permittivity of free space (Nm^2/C^-2)
> %constant=1/4*pi*epsilon0;
> constant=1;
> %En=zeros(size(R));
> En=zeros(N,N);
> %Fx=zeros(N,N);
> %En=0.0;
> q=ones(1,N);
> for i = 2:N-1
> for j = 2:N-1
> if(i ~= j) % i charge Not equal to j charge
>
> %z=((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) );
> %Z(isnan(Z))=0.0;
> %En = En + sum(Z);
>
> Rij=(R(i) - R(j));
> En(i,j) = En(i,j) + sum( ((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) ) );
> %surf(En); % plot statement to visualize En at each charge
> %En %return En values to screen
>
> Fx=q_zero.*En;
>
> % Displace every charge a little bit (as value lamda) in direction of
> % force. Use: Rij(t+1)=Rij(t)+lamda*Fx(t).
> lamda = 0.001;
> t=1;
> while (lamda<2 || Fx == 0) %need condition to be until Fx=0; an equlibrium state.
> R(1)=R(1)+lamda;
> %Rij(i+1) = Rij(i) + lamda*Fx(i);
> %t=t+1;
> Rij=(R(i) - R(j));
> En(i,j) = En(i,j) + sum( ((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) ) );
> lamda=lamda+0.001;
> Fx=q_zero.*En;
> t=+1;
> end
> plot(i,Fx)
> end
> end
> end
> fprintf('\n');

By the way when this warning is displayed Fx was changed to Fx=q_zero.*En(i,j)

Cheers
Sid

Subject: Need Help Guys. Very simple problem

From: Sid Hayes

Date: 18 Jan, 2009 14:31:02

Message: 6 of 6

"sid hayes" <sid.hayes@hotmail.com> wrote in message <gkpt3i$ekc$1@fred.mathworks.com>...
> "sid hayes" <sid.hayes@hotmail.com> wrote in message <gkod3v$8fu$1@fred.mathworks.com>...
> > Hi Guys
> >
> > I need to minimise the total force by moving the i th charge with respect to lamda which i will be calling 0.001.
> >
> > Its nearly there, but a simple problem is stopping me.
> >
> > Warm Regards
> > Sid
> >
> > 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.
> > N=int32(N);
> > % Get the random numbers, and sort them.
> > R = sort(2 * rand(N, 1) - 1);
> >
> > R(1)=-1; % Assign boundary charges.
> > R(N)=1;
> >
> > % Calculate the charges of N forces using C.. o.. u.. l..o.. m.. b..'s T.o,t.a,l F,o.r,c.e.
> > q_zero = input('Enter charge amount: ');
> > %epsilon0=8.85e-12; % permittivity of free space (Nm^2/C^-2)
> > %constant=1/4*pi*epsilon0;
> > constant=1;
> > %En=zeros(size(R));
> > En=zeros(N,N);
> > %Fx=zeros(N,N);
> > %En=0.0;
> > q=ones(1,N);
> > for i = 2:N-1
> > for j = 2:N-1
> > if(i ~= j) % i charge Not equal to j charge
> >
> > %z=((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) );
> > %Z(isnan(Z))=0.0;
> > %En = En + sum(Z);
> >
> > Rij=(R(i) - R(j));
> > En(i,j) = En(i,j) + sum( ((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) ) );
> > %surf(En); % plot statement to visualize En at each charge
> > %En %return En values to screen
> >
> > Fx=q_zero.*En;
> >
> > % Displace every charge a little bit (as value lamda) in direction of
> > % force. Use: Rij(t+1)=Rij(t)+lamda*Fx(t).
> > lamda = 0.001;
> > t=1;
> > while (lamda<2 || Fx == 0) %need condition to be until Fx=0; an equlibrium state.
> > R(1)=R(1)+lamda;
> > %Rij(i+1) = Rij(i) + lamda*Fx(i);
> > %t=t+1;
> > Rij=(R(i) - R(j));
> > En(i,j) = En(i,j) + sum( ((q(i)*q(j))*(Rij))./ (constant*(abs(Rij).^3) ) );
> > lamda=lamda+0.001;
> > Fx=q_zero.*En;
> > t=+1;
> > end
> > plot(i,Fx)
> > end
> > end
> > end
> > fprintf('\n');
>
> By the way when this warning is displayed Fx was changed to Fx=q_zero.*En(i,j)
>
> Cheers
> Sid

Hi

Im still stuck on this?

Could you help me please.

Sid

Tags for this Thread

No tags are associated with 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