Make a loop to calculate molecular weight

조회 수: 4 (최근 30일)
Mike
Mike 2013년 11월 15일
편집: Walter Roberson 2016년 10월 9일
"Use a while loop to ask the user to enter the elemental symbols of the elements in the compound and store these in a padded character array. The user should be asked if there are any more elements to be added and the loop should continue as long as the user enters an upper or lower case y.
Use a second loop that will prompt the user to enter the atomic mass and number of atoms in the compound for each element. These should be stored in separate arrays."
Please help I can't get it to word.
  댓글 수: 2
dpb
dpb 2013년 11월 15일
Well, haven't even seen your start at what doesn't work...show that and a specific question may elicit hints. Full solutions are rarely, if ever, forthcoming.
Mike
Mike 2013년 11월 17일
편집: Walter Roberson 2013년 11월 17일
disp('Enter atomic symbols in single quotesEnter atomic symbols in single quotes.')
disp('When the symbol is a single letter, precede the letter with a space.')
element=input('Enter the atomic symbol: ','s')
more=input('Are there more elements? 1 for yes 2 for no ','s')
while (1)
if more==1
element=input('Enter the atomic symbol: ')
more=input('Are there more elements? y/n ')
end
end
best i can do so far.. can't figure out how to get the loop to continue when user writes y or n... its giving me error saying y is not a variable

댓글을 달려면 로그인하십시오.

답변 (3개)

Walter Roberson
Walter Roberson 2013년 11월 17일
more = input('Are there more elements? y/n','s');
while strcmpi(more, 'y')
element{end+1} = input('Enter the atomic symbol: ', 's);
more = input('Are there more elements? y/n','s');
end
  댓글 수: 5
Mike
Mike 2013년 11월 19일
편집: Walter Roberson 2013년 11월 20일
yes I learned cell arrays... I revised my code a little bit, I got it to work, just a part of my problem that I didn't post on here is to: • Create a table containing the elemental symbols, the number of the those elements in the compound and the wt% of that element in compound (these should be in columns)
And I'm getting an error msg saying :
Error using horzcat
Dimensions of matrices being concatenated are not consistent.
Error in proj5_problem2 (line 26)
table=[done, a,b,c]
Anytime I input 3 or more elements. It stops asking for molecular weight after the second element and says that error message... Please help, I cannot figure out whats wrong. Here is my code:
more='y';
n=1;
disp('Enter atomic symbols in single quotesEnter atomic symbols in single quotes.')
disp('When the symbol is a single letter, precede the letter with a space.')
while more=='y'
element=input('Enter the atomic symbol: ','s');
element1=char(element);
theelement(n)={element1};
more=input('Are there more elements? y/n ','s');
n=n+1;
end
for x=1:length(theelement{n-1})
disp(['For the element:' theelement(x)])
weight(x)=input('Please enter the weight: ');
atoms(x)=input('Enter the number of these atoms in the compound: ');
end
totalweight=sum(weight.*atoms);
nweight=weight.*atoms;
fprintf('The total weight is %f \n',totalweight)
weightpercent=(nweight./totalweight)*100
done=char(theelement)
a=num2str(nweight')
b=num2str(atoms')
c=num2str(weightpercent')
table=[done, a,b,c]
disp('symbol weight number wt%')
disp(num2str(table))
Marc
Marc 2013년 11월 19일
I have to say that this question becomes redundant when you look at molmass() in the file exchange. The logic used in that function DOES what you are asking the user to input....
So if you want the molecular weight for C6H6 or Pt(NH3)4Cl2.... It figures out the atoms and the amounts, along with "stuff" in parenthesis...
Thus the weight percent becomes trivial...
%C in benzene = 100*6*MolMass('C')/MolMass('C6H6)

댓글을 달려면 로그인하십시오.


Marc
Marc 2013년 11월 19일
I really hate doing others homework...
But... This works really well and shows one how to use recursive programming.
Next time try google...

Ngoc Tran
Ngoc Tran 2016년 10월 8일
I just ran into this today and this is the code that I have. I don't use a loop.
Create a container map (similar to Python's dictionary):
key = {'C','H','O'};
value = [12,1,16];
atom = containers.Map(key,value);
molecule = {'C','H','H','H','H'} %CH4
MW = sum(cellfun(@(x) atom(x), molecule))
This will go through every element in the "molecule" list, get the atom mass. You will have an array of atom mass, in this case [12 1 1 1 1], and then sum them up.
To get a more extensive key/value pair you can find .csv files online. I'm just giving an example.

카테고리

Help CenterFile Exchange에서 Numerical Integration and Differentiation에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by