# Math Help - Intro to Matlab help

1. ## Intro to Matlab help

I need to setup an M-file called nmoles that requires two vector inputs - the mass (m) and molecular weight (MW) - and returns the corresponding number of moles.

n = m/MW

can anyone help me please! thanks

2. Originally Posted by lvelazquez83
I need to setup an M-file called nmoles that requires two vector inputs - the mass (m) and molecular weight (MW) - and returns the corresponding number of moles.

n = m/MW

can anyone help me please! thanks
The general form of a function .m file is:

Code:
funtion n=nmoles(m, MW)
n=... ;
CB

3. This really didn't help me...can you please explain further.

4. Originally Posted by lvelazquez83
This really didn't help me...can you please explain further.
Replace the ... by m/MW and that is the content of the .m file.

CB

5. I typed it in as you suggested:
function n=nmoles (m, MW)
n=m/MW

But when i type it in the command window i get the following error:

??? Input argument "MW" is undefined.
Error in ==> nmoles at 2
n=m/MW

any suggestions?

6. you need to call the function from another mfile or at the command line after you finish defining the function:

eg

Code:
nmoles(1,1)

7. Originally Posted by elbarto
you need to call the function from another mfile or at the command line after you finish defining the function:

eg

Code:
nmoles(1,1)

that worked! thank you!

how about it if want to test this function out. Using masses from 1 to 10g with the coumpound benzene that has a MW of 78.115g/mol.

would the following code be correct:
nmoles (1:10, 78.115)?

8. I think that was what i was looking for. Now i need to show the MW in a 10 x 3 matrix for 3 different compounds (benzene = 78.115g/mol, ethyl alcohol = 46.07 g/mol, and r134a = 102.3 g/mol).

I only know how to put make it a 1 x 10 matrix.
ex. nmoles (1:10, 78.115), nmoles (1:10, 46.07), nmoles (1:10. 102.3).

9. I tried the following in Matlab:
Benzene = nmoles (1:10, 78.115);
Ethyl_Alcohol = nmoles (1:10, 46.07);
R134a = nmoles (1:10, 102.3);
x = [Benzene', Ethyl_Alcohol', R134a']

x =
0.0128 0.0217 0.0098
0.0256 0.0434 0.0196
0.0384 0.0651 0.0293
0.0512 0.0868 0.0391
0.0640 0.1085 0.0489
0.0768 0.1302 0.0587
0.0896 0.1519 0.0684
0.1024 0.1736 0.0782
0.1152 0.1954 0.0880
0.1280 0.2171 0.0978

Does this seem like the correct code?
Also, it is asking to put the function in meshgrid...no clue here, any suggestions?

10. Originally Posted by lvelazquez83
that worked! thank you!

how about it if want to test this function out. Using masses from 1 to 10g with the coumpound benzene that has a MW of 78.115g/mol.

would the following code be correct:
nmoles (1:10, 78.115)?
Why are you asking? Just run it and see.

CB