Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %% Coulombs Law for TWO POINT charges
- %{
- This program is able to calculate the force, distance, or an unknown charge
- when given the other variables. Allows users to calculate solutions by
- inputting manual charge quantities or by using elementary charges i.e.
- protons and electrons. Comments and clarifications are available at the end
- of the script. New edits are: force and distance can now be calculated with
- two proton groups or two electron groups, warning messages pop up for distance
- calculations if there is only one negative value input.
- %}
- %% Code start
- clc,clear
- ElecCharge = -1.6022*10^-19; %[C] Charge on an electron
- ProCharge = 1.6022*10^-19; %[C] Charge on a proton
- k = 9*10^9; %[N*m^2/C^2] Coulomb's law constant
- % Start of the selection statements
- % Does the equation require the use the a number of protons and
- % electrons or are the values of the charges altogether different?
- c = input('Does the equation call for protons and electrons or different charges?\n');
- switch c
- case 1 % Protons and electrons
- % Does the force between a number of protons and electrons need to
- % be calculated?
- b = input('Calculate the force, the distance, or unknown charge?\n');
- switch b
- % The force is the unknown value and needs to be calculated.
- case 1 % force
- disp('Okay beginning to calculate the force of elementary charge(s)');
- % Does the problem require you to use protons, electrons,
- % or both elementary charges? The number of elementary
- % charges will asked later. This question only needs to
- % know which elementary charge(s) is/are involved.
- e = input('\nDoes the problem deal with protons, electrons, or both?\n');
- switch e
- case 1 % Protons only
- % Is there another charge that needs to be
- % input or does the other charge contain
- % protons as well. The second option would be
- % used in a situation where two nuceli have a force on each other.
- w = input('Do protons contribute to one or both of the point charges?\n');
- if w == 1 % Protons are in 1 object
- % How many protons can now be entered
- % below.
- f = input('How many protons are there?\n');
- TotalPCharge = f * ProCharge; % [C] The total amount of charge in the protons.
- OtherCharge = input('What is the charge of the other object in Coulombs?\n');
- d = input('What is the distance between the two point charges in meters?\n');
- d2 = d^2;
- F = (k*TotalPCharge*OtherCharge)/d2;
- fprintf('The force is %e N\n', F)
- elseif w == 2 % Protons are in 2 objects
- % Do both charges have the same amount of
- % protons, i.e. do they have the same
- % magnitude
- y = input('Are these charges the same');
- if y == 0 % Indentical groups
- % Two similar groups of protons are
- % be used in this calculation
- z = input('How many protons are there in each group?\n');
- TotalPCharge = z * ProCharge; % [C] The total amount of charge in the protons.
- d = input('What is the distance between the two point charges in meters?\n');
- d2 = d^2;
- F = (k*TotalPCharge^2)/d2;
- fprintf('The force is %e N\n', F)
- elseif y ==1 % Different groups
- aa = input('How many protons are there in the first object?\n');
- TotalPCharge1 = aa * ProCharge; % [C] The total amount of charge in the protons.
- ab = input('How many protons are there in the second object?\n');
- TotalPCharge2 = ab * ProCharge;
- d = input('What is the distance between the two point charges in meters?\n');
- d2 = d^2;
- F = (k*TotalPCharge1*TotalPCharge2)/d2;
- fprintf('The force is %e N\n', F)
- end
- end
- case 2 % Electrons only
- % Is there only one point charge with electrons
- % or two? The latter option may not be used
- % very often.
- ac = input('Do electrons contribute to one of both of the point charges?\n');
- if ac == 1
- % There is another point charge besides the
- % group of electrons that needs to be
- % accounted for.
- h = input('How many electrons\n');
- TotalECharge = h * ElecCharge; % [C] The total charge from the electrons
- Othercharge = input('What is the charge of the other object in coulombs\n');
- d = input('What is the distance between the two point charges in meters?\n');
- d2 = d^2;
- F = (k*TotalECharge*Othercharge)/d2;
- fprintf('The force is %e N\n',F )
- % Electrons account for both of the point
- % charges
- elseif ac == 2
- % Do both of the point charges have the
- % same number of electrons and magnitude?
- ad = input('Are the charges the same in magnitude?\n');
- % Yes there are two indentical point
- % charges
- if ad == 0
- ae = input('How many electrons are in each point object?\n');
- totalecharge = ae * ElecCharge;
- d = input('What is the distance between the point charges in meters?\n');
- d2 = d^2;
- F = (k*totalecharge^2)/d2;
- fprintf('The force is %e N\n',F )
- % No they are not the same in magnitude and another set
- % of electrons must be accounteds for
- elseif ad == 1
- af = input('How many electrons are in the first object?\n');
- TotalECharge1 = af * ElecCharge;
- ag = input('How many electrons are in the second object?\n');
- TotalECharge2 = ag * ElecCharge;
- d = input('What is the distance between the point charges in meters?\n');
- d2 = d^2;
- F = (k*TotalECharge1*TotalECharge2)/d2;
- fprintf('The Force is %e N\n',F)
- end
- end
- case 3 % Protons and electrons
- % How many protons are there
- p = input('The number of protons\n');
- totalPCharge = ProCharge * p;
- l = input('The number of electrons\n');
- totalECharge = ElecCharge * l;
- d = input('What is the distance between the two point charges in meters?\n');
- d2 = d^2;
- F = (k*totalECharge*totalPCharge)/d2;
- fprintf('The force is %e N\n',F )
- otherwise
- disp('Action could not be completed')
- end % switch for force
- % The distance is the unknown value and needs to be
- % calculated
- case 2 % Distance
- m = input('Does the problem involve protons, electrons, or both?\n');
- switch m
- % The problem only involves protons as the
- % elementary charge
- case 1 % Protons only
- ah = input('Do protons count for one or two of the point charges?\n');
- if ah == 1 % Protons are in 1 object
- n = input('How many protons?\n');
- TotalpCharge = n * ProCharge; % [C] The total amount of charge in the protons.
- otherCharge = input('What is the charge of the other object in Coulombs?\n');
- Force = input('What is the force between the point charges in Newtons');
- d2 = (k*TotalpCharge*otherCharge)/Force;
- d = sqrt(d2);
- fprintf('The distance between the point charges is %e m\n', d)
- elseif ah == 2 % Protons are in 2 objects
- % Do both of the point charges have the
- % same number of electrons and magnitude?
- ai = input('Are the charges the same in magnitude?\n');
- % Yes there are two indentical point
- % charges
- if ai == 0
- aj = input('How many protons are in each point object?\n');
- totalPcharge = aj * ProCharge;
- Force = input('What is the force between the point charges in Newtons');
- d2 = (k*TotalpCharge^2)/Force;
- d = sqrt(d2);
- fprintf('The distance between the point charges is %e m\n',Force )
- % No they are not the same in magnitude and another set
- % of protons must be accounted for
- elseif ai == 1
- ak = input('How many protons are in the first object?\n');
- TotalPCharge1 = ak * ProCharge;
- al = input('How many protrons are in the second object?\n');
- TotalPCharge2 = al * ProCharge;
- Force = input('What is the force between the point charges in Newtons');
- d2 = (k*TotalpCharge1* TotalPCharge2)/Force;
- d = sqrt(d2);
- fprintf('The distance between the point charges is %e m\n',Force )
- end
- end
- % The problem only involves electrons as the
- % elementary charge.
- case 2 % Electrons only
- am = input('Do electrons count for one or two of the charges?\n');
- if am == 1
- o = input('How many electrons?\n');
- TotaleCharge = o * ElecCharge; % [C] The total amount of charge in the protons.
- if TotaleCharge < 0
- disp('You have entered a negative charge. Make sure either the force or other charge is negative or the answer will not be accurate')
- end
- othercharge = input('What is the charge of the other object in Coulombs?\n');
- force = input('What is the force between the point charges in Newtons\n');
- d2 = (k*TotaleCharge*othercharge)/force;
- d = sqrt(d2);
- fprintf('The distance between the point charges is %e m\n', d)
- elseif am == 2
- an = input('Do both charges have the same amount of electrons?\n');
- if an == 0
- ao = input('How many electrons are in each point oject?\n');
- totalEcharge = ao * ElecCharge;
- Force = input('What is the force between the point charges in Newtons');
- d2 = (k*totalEcharge^2)/Force;
- d = sqrt(d2);
- fprintf('The distance between the point charges is %e m\n', d)
- elseif an == 1
- ap = input('How many electrons are in the first point object?\n');
- TotalECharge1 = ap * ElecCharge;
- aq = ipnut('How many electrons are in the second point object?\n');
- TotalECharge2 = aq * ElecCharge;
- Force = input('What is the force between the point charges in Newtons');
- d2 = (k*totalEcharge^2)/Force;
- d = sqrt(d2);
- fprintf('The distance between the point charges is %e m\n', d);
- end
- end
- % The problem requires both elementary charges
- case 3 % Protons and electrons
- r = input('Number of protons?\n');
- totalpCharge = r * ProCharge;
- s = input('Number of electrons?\n');
- totaleCharge = s * ElecCharge;
- if totaleCharge < 0
- disp('You have entered a negative charge. Make sure the force negative or the answer will not be accurate')
- end
- force = input('What is the force between the point charges in Newtons\n');
- d2 = (k*TotaleCharge*othercharge)/force;
- d = sqrt(d2);
- fprintf('The distance between the point charges is %e m\n', d)
- otherwise
- disp('Action could not be completed')
- end % switch for distance
- % Either Protons or electrons are being used, BUT there is
- % an unknown charge that needs to be calculated
- case 3 % missing charge
- % Determining whether the known charge is from protons
- % or electrons
- t = input('Does the problem use protons or electrons as the given charge?\n');
- if t == 1
- % If the charge is from protons
- u = input('Enter the number of protons\n');
- KnownCharge = u * ProCharge;
- elseif t == 2
- % If the charge is from electrons
- v = input('Enter the number of electrons\n');
- KnownCharge = v * ElecCharge;
- end % If statement
- F = input('Force in Newtons\n');
- d = input('Distance between objects in meters\n');
- d2 = d^2;
- UnknownCharge = (F*d2)/(k*KnownCharge);
- fprintf('The unknown charge is %e\n Coulombs', UnknownCharge)
- otherwise
- disp('Action could not be completed')
- end % switch
- % No it does not require the use of protons and electrons and requires
- % manual input of the charge quantities
- case 2 % specific charges
- % Should the end result be the distance between charges, the force
- % or magnitude of the charges on each other, or do you need to find
- % a specific charge given everything else?
- a = input('Calculating distance, force, or missing charge?\n');
- switch a
- % The distance between the two point charges needs to
- % calculated. The answer will come out in meters.
- case 1 % distance
- F = input('Force in Newtons\n');
- Q1 = input('Charge on object 1 in C\n');
- Q2 = input('Charge on object 2 in C\n');
- if (F < 0) || (Q1 < 0) || (Q2 < 0)
- disp('One of your values were negative. Checking to see if an error will occur ')
- if (F<0) && (Q1<0) || (F<0) && (Q2<0) || (Q1<0) && (Q2<0)
- while (F<0) && (Q1<0) && (Q2<0)
- disp('An error will occur please reevaluate the input values ')
- F = input('Force in Newtons\n');
- Q1 = input('Charge on object 1 in C\n');
- Q2 = input('Charge on object 2 in C\n');
- end
- disp('No error will occur. Calculating.')
- else
- disp('An error will occur please reevaluate the input values ')
- F = input('Force in Newtons\n');
- Q1 = input('Charge on object 1 in C\n');
- Q2 = input('Charge on object 2 in C\n');
- end
- end
- d2 = (k*Q1*Q2)/F;
- d = sqrt(d2);
- fprintf('The distance is %e\n m', d)
- % The force or the magnitude from one charge to another needs
- % to be calculated. The answer will come out in Newtons.
- case 2 % force
- Q1 = input('Charge on object 1 in Coulombs\n');
- Q2 = input('Charge on object 2 in Coulombs\n');
- d = input('Distance between objects in meters\n');
- d2 = d^2;
- F = (k*Q1*Q2)/d2;
- fprintf('The force is equal to %e\n N', F)
- % The charge of another point object needs to be calculated.
- % The answer will come out in Coulombs.
- case 3 % missing charge
- F = input('Force in Newtons\n');
- Q = input('Known charge on object in Coulombs\n');
- d = input('Distance between objects in meters\n');
- d2 = d^2;
- q = (F*d2)/(k*Q);
- fprintf('The unknown charge is %e\n Coulombs', q)
- end % switch
- end % if statement
- %{
- % Comments are made before each selection statement and before some input
- % commands to clarify what is going on in that part of the code. Comments
- % are lined up with specific operations and have a line space between
- % operations.
- % It should be noted that for questions with multiple options (e.g. a,b,c)
- % the corresponding number should be input to choose that option (i.e. 1 is
- % a, b is 2). Questions that are yes or no, should be answered 0 or 1 as
- % true or false.
- % Variables from outputs that control the branches are generally
- % insignificant and thus have no important name. Variables that are
- % meaningful, like the ones used in the actual equations, have names that
- % represent their values. d and d2 are specific variables whose value
- % changes depending on which branch is used, but its name is the same for
- % simplicity reasons.
- % Common conversion factors:
- % cm is 10^-2 m
- % mm is 10^-3 m
- % Angstrom is 10^-10 m
- % fermi is 10^-15 m
- % MN is 10^3 N
- % GN is 10^6 N
- % nC is 10^-9 C
- %}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement