Advertisement
hiddenGem

Coulomb's Law

May 22nd, 2020
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 21.89 KB | None | 0 0
  1. %% Coulombs Law for TWO POINT charges
  2. %{
  3. This program is able to calculate the force, distance, or an unknown charge
  4. when given the other variables. Allows users to calculate solutions by
  5. inputting manual charge quantities or by using elementary charges i.e.
  6. protons and electrons. Comments and clarifications are available at the end
  7. of the script. New edits are: force and distance can now be calculated with
  8. two proton groups or two electron groups, warning messages pop up for distance
  9. calculations if there is only one negative value input.
  10. %}
  11. %% Code start
  12. clc,clear
  13.  
  14. ElecCharge = -1.6022*10^-19;          %[C] Charge on an electron
  15. ProCharge = 1.6022*10^-19;            %[C] Charge on a proton
  16. k = 9*10^9;                           %[N*m^2/C^2] Coulomb's law constant
  17.  
  18. % Start of the selection statements
  19.  
  20.     % Does the equation require the use the a number of protons and
  21.     %   electrons or are the values of the charges altogether different?
  22. c = input('Does the equation call for protons and electrons or different charges?\n');
  23. switch c
  24.     case 1 % Protons and electrons
  25.         % Does the force between a number of protons and electrons need to
  26.         %   be calculated?
  27.         b = input('Calculate the force, the distance, or unknown charge?\n');
  28.             switch b
  29.                
  30.                 % The force is the unknown value and needs to be calculated.
  31.                 case 1 % force
  32.                     disp('Okay beginning to calculate the force of elementary charge(s)');
  33.                    
  34.                     % Does the problem require you to use protons, electrons,
  35.                     %   or both elementary charges? The number of elementary
  36.                     %   charges will asked later. This question only needs to
  37.                     %   know which elementary charge(s) is/are involved.
  38.                     e = input('\nDoes the problem deal with protons, electrons, or both?\n');
  39.                     switch e
  40.                        
  41.                         case 1 % Protons only
  42.                            
  43.                             % Is there another charge that needs to be
  44.                             %   input or does the other charge contain
  45.                             %   protons as well. The second option would be
  46.                             %   used in a situation where two nuceli have a force on each other.
  47.                                
  48.                             w = input('Do protons contribute to one or both of the point charges?\n');
  49.                             if w == 1 % Protons are in 1 object
  50.                                
  51.                                 % How many protons can now be entered
  52.                                 %   below.
  53.                                 f = input('How many protons are there?\n');
  54.                                 TotalPCharge = f * ProCharge;         % [C] The total amount of charge in the protons.
  55.                                 OtherCharge = input('What is the charge of the other object in Coulombs?\n');
  56.                                 d = input('What is the distance between the two point charges in meters?\n');
  57.                                 d2 = d^2;
  58.                                 F = (k*TotalPCharge*OtherCharge)/d2;
  59.                                 fprintf('The force is %e N\n', F)
  60.                             elseif w == 2 % Protons are in 2 objects
  61.                                
  62.                                 % Do both charges have the same amount of
  63.                                 %   protons, i.e. do they have the same
  64.                                 %   magnitude
  65.                                 y = input('Are these charges the same');
  66.                                     if y == 0 % Indentical groups
  67.                                        
  68.                                         % Two similar groups of protons are
  69.                                         %   be used in this calculation
  70.                                         z = input('How many protons are there in each group?\n');
  71.                                         TotalPCharge = z * ProCharge;         % [C] The total amount of charge in the protons.
  72.                                         d = input('What is the distance between the two point charges in meters?\n');
  73.                                         d2 = d^2;
  74.                                         F = (k*TotalPCharge^2)/d2;
  75.                                         fprintf('The force is %e N\n', F)  
  76.                                     elseif y ==1 % Different groups
  77.                                         aa = input('How many protons are there in the first object?\n');
  78.                                         TotalPCharge1 = aa * ProCharge;         % [C] The total amount of charge in the protons.
  79.                                         ab = input('How many protons are there in the second object?\n');
  80.                                         TotalPCharge2 = ab * ProCharge;  
  81.                                         d = input('What is the distance between the two point charges in meters?\n');
  82.                                         d2 = d^2;
  83.                                         F = (k*TotalPCharge1*TotalPCharge2)/d2;
  84.                                         fprintf('The force is %e N\n', F)
  85.                                     end
  86.                             end
  87.                         case 2 % Electrons only
  88.                            
  89.                             % Is there only one point charge with electrons
  90.                             %   or two? The latter option may not be used
  91.                             %   very often.
  92.                             ac = input('Do electrons contribute to one of both of the point charges?\n');
  93.                             if ac == 1
  94.                                
  95.                                 % There is another point charge besides the
  96.                                 %   group of electrons that needs to be
  97.                                 %   accounted for.
  98.                                 h = input('How many electrons\n');
  99.                                 TotalECharge = h * ElecCharge;       % [C] The total charge from the electrons
  100.                                 Othercharge = input('What is the charge of the other object in coulombs\n');
  101.                                 d = input('What is the distance between the two point charges in meters?\n');
  102.                                 d2 = d^2;
  103.                                 F = (k*TotalECharge*Othercharge)/d2;
  104.                                 fprintf('The force is %e N\n',F )
  105.                            
  106.                             % Electrons account for both of the point
  107.                             %   charges
  108.                             elseif ac == 2
  109.                                
  110.                                 % Do both of the point charges have the
  111.                                 % same number of electrons and magnitude?
  112.                                 ad = input('Are the charges the same in magnitude?\n');
  113.                                
  114.                                 % Yes there are two indentical point
  115.                                 %    charges
  116.                                 if ad == 0
  117.                                     ae = input('How many electrons are in each point object?\n');
  118.                                     totalecharge = ae * ElecCharge;
  119.                                     d = input('What is the distance between the point charges in meters?\n');
  120.                                     d2 = d^2;
  121.                                     F = (k*totalecharge^2)/d2;
  122.                                     fprintf('The force is %e N\n',F )                                    
  123.                                    
  124.                                 % No they are not the same in magnitude and another set
  125.                                 %   of electrons must be accounteds for  
  126.                                 elseif ad == 1
  127.                                     af = input('How many electrons are in the first object?\n');
  128.                                     TotalECharge1 = af * ElecCharge;
  129.                                     ag = input('How many electrons are in the second object?\n');
  130.                                     TotalECharge2 = ag * ElecCharge;
  131.                                     d = input('What is the distance between the point charges in meters?\n');
  132.                                     d2 = d^2;
  133.                                     F = (k*TotalECharge1*TotalECharge2)/d2;
  134.                                     fprintf('The Force is %e N\n',F)
  135.                                 end
  136.                             end
  137.                         case 3 % Protons and electrons
  138.                            
  139.                             % How many protons are there
  140.                             p = input('The number of protons\n');
  141.                             totalPCharge = ProCharge * p;
  142.                             l = input('The number of electrons\n');
  143.                             totalECharge = ElecCharge * l;
  144.                             d = input('What is the distance between the two point charges in meters?\n');
  145.                             d2 = d^2;
  146.                             F = (k*totalECharge*totalPCharge)/d2;
  147.                             fprintf('The force is %e N\n',F )
  148.                         otherwise
  149.                             disp('Action could not be completed')
  150.                     end     % switch for force
  151.            
  152.                 % The distance is the unknown value and needs to be
  153.                 %   calculated
  154.                 case 2 % Distance
  155.                     m = input('Does the problem involve protons, electrons, or both?\n');
  156.                     switch m
  157.                        
  158.                         % The problem only involves protons as the
  159.                         %   elementary charge
  160.                         case 1 % Protons only
  161.                             ah = input('Do protons count for one or two of the point charges?\n');
  162.                             if ah == 1 % Protons are in 1 object
  163.                                 n = input('How many protons?\n');
  164.                                 TotalpCharge = n * ProCharge;         % [C] The total amount of charge in the protons.
  165.                                 otherCharge = input('What is the charge of the other object in Coulombs?\n');
  166.                                 Force = input('What is the force between the point charges in Newtons');
  167.                                 d2 = (k*TotalpCharge*otherCharge)/Force;
  168.                                 d = sqrt(d2);
  169.                                 fprintf('The distance between the point charges is %e m\n', d)
  170.                             elseif ah == 2 % Protons are in 2 objects
  171.                                
  172.                                 % Do both of the point charges have the
  173.                                 %   same number of electrons and magnitude?
  174.                                 ai = input('Are the charges the same in magnitude?\n');
  175.                                
  176.                                 % Yes there are two indentical point
  177.                                 %    charges
  178.                                 if ai == 0
  179.                                     aj = input('How many protons are in each point object?\n');
  180.                                     totalPcharge = aj * ProCharge;
  181.                                     Force = input('What is the force between the point charges in Newtons');
  182.                                     d2 = (k*TotalpCharge^2)/Force;
  183.                                     d = sqrt(d2);
  184.                                     fprintf('The distance between the point charges is %e m\n',Force )                                    
  185.                                    
  186.                                 % No they are not the same in magnitude and another set
  187.                                 %   of protons must be accounted for  
  188.                                 elseif ai == 1
  189.                                     ak = input('How many protons are in the first object?\n');
  190.                                     TotalPCharge1 = ak * ProCharge;
  191.                                     al = input('How many protrons are in the second object?\n');
  192.                                     TotalPCharge2 = al * ProCharge;
  193.                                     Force = input('What is the force between the point charges in Newtons');
  194.                                     d2 = (k*TotalpCharge1* TotalPCharge2)/Force;
  195.                                     d = sqrt(d2);
  196.                                     fprintf('The distance between the point charges is %e m\n',Force )                                      
  197.                                 end
  198.                             end
  199.                            
  200.                         % The problem only involves electrons as the
  201.                         %   elementary charge.                        
  202.                         case 2 % Electrons only
  203.                             am = input('Do electrons count for one or two of the charges?\n');
  204.                             if am == 1
  205.                                 o = input('How many electrons?\n');
  206.                                 TotaleCharge = o * ElecCharge;         % [C] The total amount of charge in the protons.
  207.                                     if TotaleCharge < 0
  208.                                         disp('You have entered a negative charge. Make sure either the force or other charge is negative or the answer will not be accurate')
  209.                                     end
  210.                                 othercharge = input('What is the charge of the other object in Coulombs?\n');
  211.                                 force = input('What is the force between the point charges in Newtons\n');
  212.                                 d2 = (k*TotaleCharge*othercharge)/force;
  213.                                 d = sqrt(d2);
  214.                                 fprintf('The distance between the point charges is %e m\n', d)
  215.                              elseif am == 2
  216.                                 an = input('Do both charges have the same amount of electrons?\n');
  217.                                     if an == 0
  218.                                         ao = input('How many electrons are in each point oject?\n');
  219.                                         totalEcharge = ao * ElecCharge;
  220.                                         Force = input('What is the force between the point charges in Newtons');
  221.                                         d2 = (k*totalEcharge^2)/Force;
  222.                                         d = sqrt(d2);
  223.                                         fprintf('The distance between the point charges is %e m\n', d)
  224.                                     elseif an == 1
  225.                                         ap = input('How many electrons are in the first point object?\n');
  226.                                         TotalECharge1 = ap * ElecCharge;    
  227.                                         aq = ipnut('How many electrons are in the second point object?\n');
  228.                                         TotalECharge2 = aq * ElecCharge;
  229.                                         Force = input('What is the force between the point charges in Newtons');
  230.                                         d2 = (k*totalEcharge^2)/Force;
  231.                                         d = sqrt(d2);
  232.                                         fprintf('The distance between the point charges is %e m\n', d);
  233.                                      end
  234.                              end
  235.                         % The problem requires both elementary charges    
  236.                         case 3 % Protons and electrons
  237.                              r = input('Number of protons?\n');
  238.                              totalpCharge = r * ProCharge;
  239.                              s = input('Number of electrons?\n');
  240.                              totaleCharge = s * ElecCharge;
  241.                                 if totaleCharge < 0
  242.                                     disp('You have entered a negative charge. Make sure the force negative or the answer will not be accurate')
  243.                                 end
  244.                              force = input('What is the force between the point charges in Newtons\n');
  245.                              d2 = (k*TotaleCharge*othercharge)/force;
  246.                              d = sqrt(d2);
  247.                              fprintf('The distance between the point charges is %e m\n', d)                            
  248.                         otherwise
  249.                             disp('Action could not be completed')
  250.                     end         % switch for distance  
  251.                    
  252.                 % Either Protons or electrons are being used, BUT there is
  253.                 %   an unknown charge that needs to be calculated    
  254.                 case 3 % missing charge
  255.                    
  256.                     % Determining whether the known charge is from protons
  257.                     %   or electrons
  258.                     t = input('Does the problem use protons or electrons as the given charge?\n');
  259.                     if t == 1
  260.                        
  261.                         % If the charge is from protons
  262.                         u = input('Enter the number of protons\n');
  263.                         KnownCharge = u * ProCharge;
  264.                     elseif t == 2
  265.                        
  266.                         % If the charge is from electrons
  267.                         v = input('Enter the number of electrons\n');
  268.                         KnownCharge = v * ElecCharge;
  269.                     end % If statement
  270.                     F = input('Force in Newtons\n');
  271.                     d = input('Distance between objects in meters\n');
  272.                     d2 = d^2;
  273.                     UnknownCharge = (F*d2)/(k*KnownCharge);
  274.                     fprintf('The unknown charge is %e\n Coulombs', UnknownCharge)
  275.                 otherwise
  276.                         disp('Action could not be completed')
  277.             end     % switch
  278.     % No it does not require the use of protons and electrons and requires
  279.     %   manual input of the charge quantities
  280.     case 2 % specific charges
  281.        
  282.         % Should the end result be the distance between charges, the force
  283.         %    or magnitude of the charges on each other, or do you need to find
  284.         %   a specific charge given everything else?
  285.         a = input('Calculating distance, force, or missing charge?\n');
  286.         switch a
  287.            
  288.             % The distance between the two point charges needs to
  289.             %   calculated. The answer will come out in meters.
  290.             case 1      % distance
  291.                 F = input('Force in Newtons\n');
  292.                 Q1 = input('Charge on object 1 in C\n');
  293.                 Q2 = input('Charge on object 2 in C\n');
  294.                 if (F < 0) || (Q1 < 0) || (Q2 < 0)
  295.                     disp('One of your values were negative. Checking to see if an error will occur ')
  296.                     if (F<0) && (Q1<0) || (F<0) && (Q2<0) || (Q1<0) && (Q2<0)
  297.                         while (F<0) && (Q1<0) && (Q2<0)
  298.                             disp('An error will occur please reevaluate the input values ')
  299.                             F = input('Force in Newtons\n');
  300.                             Q1 = input('Charge on object 1 in C\n');
  301.                             Q2 = input('Charge on object 2 in C\n');
  302.                         end
  303.                         disp('No error will occur. Calculating.')
  304.                     else
  305.                         disp('An error will occur please reevaluate the input values ')
  306.                             F = input('Force in Newtons\n');
  307.                             Q1 = input('Charge on object 1 in C\n');
  308.                             Q2 = input('Charge on object 2 in C\n');
  309.                     end
  310.                 end
  311.                 d2 = (k*Q1*Q2)/F;
  312.                 d = sqrt(d2);
  313.                 fprintf('The distance is %e\n m', d)
  314.                
  315.             % The force or the magnitude from one charge to another needs
  316.             %    to be calculated. The answer will come out in Newtons.
  317.             case 2      % force
  318.                 Q1 = input('Charge on object 1 in Coulombs\n');
  319.                 Q2 = input('Charge on object 2 in Coulombs\n');
  320.                 d = input('Distance between objects in meters\n');
  321.                 d2 = d^2;
  322.                 F = (k*Q1*Q2)/d2;
  323.                 fprintf('The force is equal to %e\n N', F)
  324.                
  325.             % The charge of another point object needs to be calculated.
  326.             %   The answer will come out in Coulombs.
  327.             case 3      % missing charge
  328.                 F = input('Force in Newtons\n');
  329.                 Q = input('Known charge on object in Coulombs\n');
  330.                 d = input('Distance between objects in meters\n');
  331.                 d2 = d^2;
  332.                 q = (F*d2)/(k*Q);
  333.                 fprintf('The unknown charge is %e\n Coulombs', q)
  334.         end     % switch
  335. end     % if statement
  336.  
  337. %{
  338. % Comments are made before each selection statement and before some input
  339. %    commands to clarify what is going on in that part of the code. Comments
  340. %    are lined up with specific operations and have a line space between
  341. %    operations.
  342.  
  343. % It should be noted that for questions with multiple options (e.g. a,b,c)
  344. %    the corresponding number should be input to choose that option (i.e. 1 is
  345. %    a, b is 2). Questions that are yes or no, should be answered 0 or 1 as
  346. %    true or false.
  347.  
  348. % Variables from outputs that control the branches are generally
  349. %   insignificant and thus have no important name. Variables that are
  350. %   meaningful, like the ones used in the actual equations, have names that
  351. %   represent their values. d and d2 are specific variables whose value
  352. %   changes depending on which branch is used, but its name is the same for
  353. %   simplicity reasons.
  354.  
  355. % Common conversion factors:
  356. %   cm is 10^-2 m
  357. %   mm is 10^-3 m
  358. %   Angstrom is 10^-10 m
  359. %   fermi is 10^-15 m
  360. %   MN is 10^3 N
  361. %   GN is 10^6 N
  362. %   nC is 10^-9 C
  363. %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement