Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added lecture_05
  • Loading branch information
rcc02007 committed Jan 26, 2017
1 parent a623138 commit e5b6204
Show file tree
Hide file tree
Showing 5 changed files with 323 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lecture_05/.ipynb_checkpoints/lecture_05-checkpoint.ipynb
@@ -0,0 +1,6 @@
{
"cells": [],
"metadata": {},
"nbformat": 4,
"nbformat_minor": 2
}
208 changes: 208 additions & 0 deletions lecture_05/lecture_05.ipynb
@@ -0,0 +1,208 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Good coding habits\n",
"## naming folders and files"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"[Stanford file naming best practices](https://library.stanford.edu/research/data-management-services/data-best-practices/best-practices-file-naming)\n",
"\n",
"1. Include information to distinguish file name e.g. project name, objective of function, name/initials, type of data, conditions, version of file, \n",
"2. if using dates, use YYYYMMDD, so the computer organizes by year, then month, then day\n",
"3. avoid special characters e.g. !, #, \\$, ...\n",
"4. avoid using spaces if not necessary, some programs consider a space as a break in code use dashes `-` or underscores `_` or CamelCase"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Commenting your code"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Its important to comment your code to mention what a variable's units are, what the function is supposed to do, etc. \n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"function i=code(j)\n",
" % Example of bad variable names and bad function name\n",
" for w=1:j\n",
" i(w)=w;\n",
" end\n",
"end"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'code' is a command-line function\n",
"\n",
" Example of bad variable names and bad function name\n",
"\n",
"\n",
"Additional help for built-in functions and operators is\n",
"available in the online version of the manual. Use the command\n",
"'doc <topic>' to search the manual index.\n",
"\n",
"Help and information about Octave is also available on the WWW\n",
"at http://www.octave.org and via the help@octave.org\n",
"mailing list.\n"
]
}
],
"source": [
"help code"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Choose variable names that describe the variable"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"function count_vector=counting_function(max_value)\n",
" % Good variable names and better help documentation\n",
" % \n",
" % counting function creates a vector from 1 to max_value where each index, i, is \n",
" % stored in each vector spot\n",
" for i=1:max_value\n",
" count_vector(i)=i; % set each element in count_vector to i\n",
" end\n",
"end "
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"'counting_function' is a command-line function\n",
"\n",
" Good variable names and better help documentation\n",
" \n",
" counting function creates a vector from 1 to max_value where each index, i, is \n",
" stored in each vector spot\n",
"\n",
"\n",
"Additional help for built-in functions and operators is\n",
"available in the online version of the manual. Use the command\n",
"'doc <topic>' to search the manual index.\n",
"\n",
"Help and information about Octave is also available on the WWW\n",
"at http://www.octave.org and via the help@octave.org\n",
"mailing list.\n"
]
}
],
"source": [
"help counting_function"
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"## Putting it all together"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1. Clone your homework_1 to your computer\n",
"2. open Matlab (cli, jupyter or gui)\n",
"3. Change working directory to homework_1 *e.g.* Windows:`cd('C:\\Users\\rcc02007\\Documents\\Github\\homework_1')`, Mac: `cd('/Users/rcc02007/Documents/Github/homework_1')`\n",
"4. You have already created your first script `myscript.m` (if not see lecture_4)\n",
"5. Run `>> my_script.m`\n",
"6. Create a new m-file called nitrogen_pressure.m\n",
"7. Create a function based upon the ideal gas law for nitrogen, Pv=RT\n",
" 1. R=0.2968 kJ/(kg-K)\n",
" 2. inputs to function are v (specific volume m^3/kg), and T, temperature (K)\n",
" 3. output is P, pressure (kPa)\n",
"8. Once the function works, commit the change to the repository (add a message, like 'added file nitrogen_pressure.m'\n",
"9. After file is 'committed', 'push' the changes to your github account\n",
"\n",
"for the command-line git user, this is steps 8 and 9:\n",
"1. `$ git add *`\n",
"2. `$ git commit -m 'added file nitrogen_pressure.m'`\n",
"3. `$ git push -u origin master\n",
" Username for 'https://github.uconn.edu':rcc02007 <enter>\n",
" Password for 'https://rcc02007@github.uconn.edu': `\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Octave",
"language": "octave",
"name": "octave"
},
"language_info": {
"file_extension": ".m",
"help_links": [
{
"text": "MetaKernel Magics",
"url": "https://github.com/calysto/metakernel/blob/master/metakernel/magics/README.md"
}
],
"mimetype": "text/x-octave",
"name": "octave",
"version": "0.19.14"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
109 changes: 109 additions & 0 deletions lecture_05/lecture_05.md
@@ -0,0 +1,109 @@

# Good coding habits
## naming folders and files

[Stanford file naming best practices](https://library.stanford.edu/research/data-management-services/data-best-practices/best-practices-file-naming)

1. Include information to distinguish file name e.g. project name, objective of function, name/initials, type of data, conditions, version of file,
2. if using dates, use YYYYMMDD, so the computer organizes by year, then month, then day
3. avoid special characters e.g. !, #, \$, ...
4. avoid using spaces if not necessary, some programs consider a space as a break in code use dashes `-` or underscores `_` or CamelCase

## Commenting your code

Its important to comment your code to mention what a variable's units are, what the function is supposed to do, etc.



```octave
function i=code(j)
% Example of bad variable names and bad function name
for w=1:j
i(w)=w;
end
end
```


```octave
help code
```

'code' is a command-line function

Example of bad variable names and bad function name


Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at http://www.octave.org and via the help@octave.org
mailing list.


## Choose variable names that describe the variable


```octave
function count_vector=counting_function(max_value)
% Good variable names and better help documentation
%
% counting function creates a vector from 1 to max_value where each index, i, is
% stored in each vector spot
for i=1:max_value
count_vector(i)=i; % set each element in count_vector to i
end
end
```


```octave
help counting_function
```

'counting_function' is a command-line function

Good variable names and better help documentation
counting function creates a vector from 1 to max_value where each index, i, is
stored in each vector spot


Additional help for built-in functions and operators is
available in the online version of the manual. Use the command
'doc <topic>' to search the manual index.

Help and information about Octave is also available on the WWW
at http://www.octave.org and via the help@octave.org
mailing list.


## Putting it all together

1. Clone your homework_1 to your computer
2. open Matlab (cli, jupyter or gui)
3. Change working directory to homework_1 *e.g.* Windows:`cd('C:\Users\rcc02007\Documents\Github\homework_1')`, Mac: `cd('/Users/rcc02007/Documents/Github/homework_1')`
4. You have already created your first script `myscript.m` (if not see lecture_4)
5. Run `>> my_script.m`
6. Create a new m-file called nitrogen_pressure.m
7. Create a function based upon the ideal gas law for nitrogen, Pv=RT
1. R=0.2968 kJ/(kg-K)
2. inputs to function are v (specific volume m^3/kg), and T, temperature (K)
3. output is P, pressure (kPa)
8. Once the function works, commit the change to the repository (add a message, like 'added file nitrogen_pressure.m'
9. After file is 'committed', 'push' the changes to your github account

for the command-line git user, this is steps 8 and 9:
1. `$ git add *`
2. `$ git commit -m 'added file nitrogen_pressure.m'`
3. `$ git push -u origin master
Username for 'https://github.uconn.edu':rcc02007 <enter>
`



```octave
```
Binary file added lecture_05/lecture_05.pdf
Binary file not shown.
Binary file added lecture_05/octave-workspace
Binary file not shown.

0 comments on commit e5b6204

Please sign in to comment.