You are on page 1of 6

%!

Electric forces in a system of particles - Coulomb's Law % University of Tennessee : EF 230 Fall, 2009 : Will Schleter function coulomb clear all; close all; clc; % Set values % q(i) - charge of particle i % qs - charge scale (10^qs) C % p(i) - position [x y z] of particle i % ps - position scale (10^ps) m [q qs p ps] = getchargedata(); % init k=9; ks=9; % constant and scale for Coulomb's Law fs = (ks+qs+qs)-(ps+ps); % scaling for the result pd = []; figure(1); hold on; axis equal title('Coulomb''s Law Example'); vs=.5; % Loop through all particles for a=1:length(q); f=[0 0 0]; % init force on particle a pa=p(a,:); % position of a as [x y z] for b=1:length(q); if a==b, continue, end; % skip if same particle pb=p(b,:); % position of b as [x y z] r=norm(pb-pa); % distance between a and b u=(pb-pa)./r; % unit vector from a to b if sign(q(a)) == sign(q(b)) u=-u; % flip direction if repelling end fab=k.*abs(q(a).*q(b))./(r.^2); % magnitude of the force between a and b f=f+fab.*u; % add b's force on a to total force on a end F(a,:)=f; % save results in F fprintf('Force on %u is (%g %g %g)x10^%d N (%.3g @ %.3g deg)\n',a,f,fs,norm(f),atan2(f(2),f(1))*180/pi); pd(end+1,:)=[pa f]; % save plot data text(pa(1)+.02,pa(2),pa(3),sprintf('%u(%d)',a,q(a))); % show charge plot3(pa(1),pa(2),pa(3),'*'); end quiver3(pd(:,1),pd(:,2),pd(:,3),pd(:,4),pd(:,5),pd(:,6),vs); function [q qs p ps] = getchargedata() % user interface for entering data or selecting a data set choices = {'User Entered','21-2','21-3','21-4'}; c=menu('Examples',choices); switch c case 1

prompt={'Number of particles','Charge magnitude (C)','Position magnitude (m)'}; ui=inputdlg(prompt); n=str2num(ui{1}); qs=str2num(ui{2}); ps=str2num(ui{3}); for i=1:n, prompt{i}=num2str(i); end; title='q,x,y,z for each particle'; ui=inputdlg(prompt,title); for i=1:n tmp = sscanf(ui{i},'%f,%f,%f,%f'); q(i)=tmp(1); p(i,:)=tmp(2:4); end case 2 q(1)=25; q(2)=-75; qs=-9; p(1,:)=[0 0 0]; p(2,:)=[3 0 0]; ps=-2; case 3 q(1)=1; q(2)=-3; q(3)=5; qs=-9; p(1,:)=[2 0 0]; p(2,:)=[4 0 0]; p(3,:)=[0 0 0]; ps=-2; case 4 q(1)=2; q(2)=2; q(3)=4; p(1,:)=[0 .3 0]; p(2,:)=[0 -.3 0]; p(3,:)=[.4 0 0]; qs=-6; ps=0; otherwise error('No choice made'); end

coulomb - Program to compute Coulomb force between charges clear all; help coulomb; % Clear memory; print header %@ Initialize variables (e.g., positions of charges, physical constants) NCharges = input('Enter the number of charges: '); for iCharge=1:NCharges fprintf('----- \n For charge #%g \n',iCharge); r_in = input('Enter position (in m) as [x y]: '); x(iCharge) = r_in(1); % x-component of position y(iCharge) = r_in(2); % y-component of position q(iCharge) = input('Enter charge (in C): '); end Epsilon0 = 8.85e-12; % Permittivity of free space (C^2/(N m^2)) Constant = 1/(4*pi*Epsilon0); % Useful constant %@ Loop over charges to compute the force on each charge fprintf('\n\n Forces are: \n\n'); for iCharge = 1:NCharges Fx = 0.0; Fy = 0.0; % Initialize components of total force to zero

%@ Loop over other charges to compute force on this charge for jCharge = 1:NCharges if( iCharge ~= jCharge ) % If iCharge NOT equal to jCharge %@ Compute the components of vector distance between two charges xij = x(iCharge) - x(jCharge); yij = y(iCharge) - y(jCharge); Rij = sqrt(xij^2 + yij^2); %@ %@ Fx Fy end end %@ Print out the total force on this charge due to the others fprintf('Force on charge #%g is: \n',iCharge); fprintf(' x-component: %g N \n',Fx); fprintf(' y-component: %g N \n',Fy); Compute the x and y components of the force between these two charges using Coulomb's law = Fx + Constant*q(iCharge)*q(jCharge)*xij/Rij^3; = Fy + Constant*q(iCharge)*q(jCharge)*yij/Rij^3;

end

CHNG TRNH MATLAB:

% Cforce - Xc nh lc tng tc tnh in gia cc in tch im trong chn khng %@ Enter your username fprintf('Enter your username (userid); for example, \n'); fprintf(' USERNAME: ABName \n'); Username = input(' USERNAME: ','s'); % Read input as a text string fprintf('\n');

%@ Initialize variables (e.g., positions of charges, physical constants) NCharges = input('Enter the number of charges: '); for iCharge=1:NCharges fprintf('----- \n For charge #%g \n',iCharge); r_in = input('Enter position (in m) as [x y]: '); x(iCharge) = r_in(1); % x-component of position y(iCharge) = r_in(2); % y-component of position q(iCharge) = input('Enter charge (in C): '); end

%@ Find xmin, xmax, ymin, and ymax xmin=min(x)-1; ymin=min(y)-1; xmax=max(x)+1; ymax=max(y)+1;

Epsilon0 = 8.85e-12; % Permittivity of free space (C^2/(N m^2)) Constant = 1/(4*pi*Epsilon0); % Useful constant

%@ Loop over charges to compute the force on each charge fprintf('\n\n Forces are: \n\n'); for iCharge = 1:NCharges

Fx = 0.0; % Initialize components of total force to zero Fy = 0.0;

%@ Loop over other charges to compute force on this charge for jCharge = 1:NCharges if( iCharge ~= jCharge ) % If iCharge NOT equal to jCharge

%@ Compute the components of vector distance between two charges xij = x(iCharge) - x(jCharge); yij = y(iCharge) - y(jCharge); Rij = sqrt(xij^2 + yij^2);

%@ Compute the x and y components of the force between %@ these two charges using Coulomb's law

Fx = Fx + Constant*q(iCharge)*q(jCharge)*xij/Rij^3; Fy = Fy + Constant*q(iCharge)*q(jCharge)*yij/Rij^3;

end end Fxnet(iCharge) = Fx;

Fynet(iCharge) = Fy; %@ Print out the total force on this charge due to the others fprintf('Force on charge #%g is: \n',iCharge); fprintf(' x-component: %g N \n',Fx); fprintf(' y-component: %g N \n',Fy); end

%@ Plot position of charges

figure(gcf); plot(x,y,'bo');

% Bring figure window forward

axis([xmin xmax ymin ymax]); for j = 1:NCharges text(x(j),y(j),sprintf(' %g',j)); end %@ Add force direction to position of charges hold on; quiver(x,y,Fxnet,Fynet,'r'); % Draw arrows for force title([Username,', ',date,', ','Cforce: Lc tng tc gia cc in tch im trong chn khng']); xlabel('x (m)'); ylabel('y (m)'); hold off;

You might also like