Bar Graphing the results of two Switch statements
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Edwin J Santiago Alvarez
il 9 Mar 2015
Commentato: Guillaume
il 10 Mar 2015
Hello,
I am making a program that involves two switch statements and the entire periodic table. The program goes from switch statement #1, Pick a group from the periodic table, pick an element within that group, enter mass of the element and then it will calculate the number of atoms in the said amount of grams of the element. Then it goes directly to switch statement #2 and the same process happens all over again. My objective is to let the user pick two elements, set a mass for both of them, find how many atoms are in the grams of the selected elements and show a bar graph. I was hoping if someone knows how I can take those two results and put them in a bar graph so I could compare them vertically on a Y-axis. Here is what I have so far.
Thanks Edwin.
0 Commenti
Risposta accettata
Guillaume
il 10 Mar 2015
Common practice for switch statements like you have done is to use the same output variable for all the cases. So instead of:
case 1
massofH = input('Enter mass of Hydrogen in grams:');
numofHatoms = (massofH/H)*Avosnum;
fprintf('There are %g atoms in %g grams of Hydrogen \n;',numofHatoms,massofH);
case 2
massofLi = input('Enter mass of Lithium in grams:');
numofLiatoms = (massofLi/Li)*Avosnum;
fprintf('There are %g atoms in %g grams of Lithium \n;',numofLiatoms,massofLi);
%etc.
you would have
case 1
massofatoms = input('Enter mass of Hydrogen in grams:');
numofatoms = (massofatoms/H)*Avosnum;
fprintf('There are %g atoms in %g grams of Hydrogen \n;',numofatoms,massofatoms);
case 2
massofatoms = input('Enter mass of Lithium in grams:');
numofatoms = (massofLi/Li)*Avosnum;
fprintf('There are %g atoms in %g grams of Lithium \n;',numofatoms ,massofatoms);
%etc.
Because you have the same variables for all cases, after the switch statement you can then do something useful with it.
One of the rule of programming: if you write the same code more than once, time to make it into a function. So instead of the two switch statements which are identical, extract the code into a function:
function [numofatoms, massofatoms] = atompicker
%Periodic Table: Element's Atomic Weight
%Group 1A's (Alkaline Metals and Hydrogen) Atomic Weight
H = 1.01; Li = 6.94; Na = 22.99; K = 39.09; Rb = 85.47; Cs = 132.9; Fr = 223;
%Group 2A's (Alkali Earth Metals) Atomic Weight
Be = 9.01; Mg = 24.31; Ca = 40.08; Sr = 87.62; Ba = 137.33; Ra = 226;
%etc.
choice = menu('Pick a group within the Periodic Table:','1A','2A','3B','Lanthanides','Actinides','4B','5B','6B','7B','8B','9B','10B','11B','12B','3A','4A','5A','6A','7A','8A');
switch choice
case 1
element = menu('Pick element from the group:','Hydrogen','Lithium','Sodium','Potassium','Rubidium','Cesium','Francium');
switch element
case 1
massofatoms = input('Enter mass of Hydrogen in grams:');
numofatoms = (massofatoms/H)*Avosnum;
fprintf('There are %g atoms in %g grams of Hydrogen \n;',numofatoms,massofatoms);
case 2
massofatoms = input('Enter mass of Lithium in grams:');
numofatoms = (massofLi/Li)*Avosnum;
fprintf('There are %g atoms in %g grams of Lithium \n;',numofatoms ,massofatoms);
%etc.
end
end
end
In you script then it's simply
[numofatoms(1), massofatoms(1)] = atompicker
[numofatoms(2), massofatoms(2)] = atompicker
bar(numofatoms);
5 Commenti
Guillaume
il 10 Mar 2015
I'm not sure I understand your question. Can you rephrase or show an example of what you mean?
Più risposte (1)
Christiaan
il 10 Mar 2015
Hello Edwin,
What you could do is to add a line for each case, where the calculated amount of atoms is defined in only one variable. Repeat this for choice 2.
Then, since it is good to know which element you have chosen in the end, you could also add for each case a string, that you know which element is chosen.
An example how it could look like is this:
switch elegr1
case 1
massofH = input('Enter mass of Hydrogen in grams:');
numofHatoms = (massofH/H)*Avosnum;
fprintf('There are %g atoms in %g grams of Hydrogen \n;',numofHatoms,massofH);
numofatoms_choice1 = numofHatoms; label_choise1='Hydrogen';
case 2
If you have done this, in the end of the m-file you will have for choice 1 and 2, not only the amount of atoms but also which atom it is.
Then you can easily use the bar plot, to plot the results. (and use a legend which atom it is)
Good luck! Christiaan
0 Commenti
Vedere anche
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!