# Matlab Fundamentals

### Matlab Script Files (or M-Files)

We can simplify repetitive tasks by storing a series of Matlab commands
in a script file (later we will use these to write powerful programs)

- Use "New, M-File" under file menu or use any text editor
- Type in a series of commands, such as
`n=1;`
`R=0.0821;`
`T=300;`
`V=10:.5:30;`
`P300 = n*R*T ./ V;`
`T=200;`
`P200 = n*R*T ./ V;`
`plot(V,P200,'r-',V,P300,'g:')`
`legend('T=200 Kelvin','T=300 Kelvin')`
`xlabel('Volume (Liters)')`
`ylabel('Pressure (Atmosphere)')`

- Save as file name
`plotgas.m`
- In the Matlab window, type in
`plotgas` and it will run
your series of commands
- You can ask the user to enter input by using the
`input`
command. Change the definition of `n` to read
`n = input('Enter Number of Moles of Gas ');`

- Any text following a percent sign (%) is treated as a comment by Matlab
- Look at the definition of the M-File
`carnot.m
`to see a more extensive script file

Electronic Copy: `http://physics.gac.edu/~huber/matlab/mtlabmfl.htm`

Revised: 28-JAN-97 by Tom
Huber, Physics Department, Gustavus
Adolphus College.