12.010 Computational Methods of Scientific Programming


Instructors: T. Herring (, Chris Hill (


This course introduces programming languages and techniques used by physical scientists: FORTRAN, C, C++, Matlab, and Mathematica.  Emphasis is placed on program design, algorithm development and verification, and comparative advantages and disadvantages of different languages.  Students first learn the basic usage of each language, common types of problems encountered, and techniques for solving a variety of problems encountered in contemporary research: examination of data with visualization techniques, numerical analysis, and methods of dissemination and verification.  No prior programming experience is required.


Prerequisite: 18.01, 18.02, 8.01


Aim of course:

The aim of this course is to give students an introduction to the uses of computer languages in the analysis of contemporary scientific problems.  The course covers both conceptual areas of converting a problem to be solved into a computer-based solution, and specific aspects of individual languages and the types of problems they are best suited to solve.   Emphasis is placed on the importance of structure, documentation, and levels of "user-hostility" of program interface.  For each language, the basic syntax and structure of the language is covered with examples drawn from real applications.  The major toolboxes and libraries, interfacing techniques and platform specific issues are also addressed. 

Homework will be assigned for each of the languages and there will be a common project developed during the course that students will solve using the language of their choice.   The final class will be used to compare the different solutions developed.  Homework grading will be based not only on completing the assigned task but also on solution structure, robustness, human interface, documentation and transportability. 


This class has no final exam.

Class grading: 80% homework, 20% final project


Office Hours for Prof. Herring; Tuesday 1:00-2:00 pm


There is an open courseware version of this course.


Course Outline:



Date 2011






Introduction: Problem formulation, algorithm development, algorithm implementation, and algorithm verification. Structure and documentation







Fortran: "Formula Translation".  Program creation, compilation and linking, variables and parameters, flow control, subroutines and functions.  Structure and documentation.  Use of libraries, internal and external communication, and interaction with other languages. Fortran: Implementation issues: Compilation errors, segmentation violations, Not-a-Number (NaN), Input/Output (IOSTAT) errors, Runtime errors.  Transportable code, standard extensions. Fortran 90 differences and similarities to Fortran 77
















C for Scientific uses. Representation of data through arrays, pointers, and data structures.  Function calls, argument passing and scoping rules, IO, profiling, system calls, and signals.

C++ objects. Encapsulation and inheritance, polymorphic operators.
















Mathematica. What it is and what it can do; structure of Mathematica; symbols, exact numbers, and machine numbers; lists, vectors, and matrices

Working with Mathematica: numerical calculations, symbolic calculations, and graphics.  Importing and exporting information







MatLab. "Matrix Laboratory". Matlab syntax, workspace, variables.  Script M-files, IO, control flow, debugging, and profiling tools.  Object-oriented programming

Matlab applications, polynomials, interpolation, integration, differentiation, ODE.  Graphics, 2-D, 3-D, Graphical User Interface (GUI)













Python scripting language program







Advanced graphics in Matlab, 3-D representation and exportable animations.




Ordinary differential equation (ODE) solutions used Matlab and Mathematica




Advanced Topics: Parallel computing with large memory and large numbers of CPUs.

Advanced topics: Parallel MATLAB.

Discussion of final projects.

Order of the presentations will be decided in the last class.










Final Project Presentations



Instructors: TH: T. Herring,; CH Chris Hill



Lecture Notes 2010 (shaded) and 2009 (non-shaded)


The lecture notes are converted from Microsoft Power Point documents portable document format (PDF).  The most complete version of the documents is the power point files, which we make available for each lecture.  Lecture notes are added as the class is taught.   Check the date on the lecture notes to make sure you get this year’s version.


Lecture 01: Class introduction; overview of languages; Program development

 Lec 01 PDF        Lec01 Power Point       Thursday, September 09, 2010.


Lecture 02: Discussion of aspects of computers and their operation.  An example is given of program development for a simple case of computing the area of a figure.  In this lecture there are notes that appear in the power point document.  (These notes are often important for doing the homework and are not in the PDF file).

Lec 02 PDF         Lec 02 Power Point      Tuesday, September 14, 2010

In the lecture notes the poly_area.f Fortran program is referred to.  This program implements the area determination algorithm discussed in the lecture. Links that are in the notes for this lecture:


Lecture 03: Started FORTRAN (Formula Translation).  Went through the basic elements that make up this language.  An on-line version of a Fortan77 manual can be found at the  In reading these notes, you should think about operations you want to do and what command or commands do you use to do that.  The list on Fortran intrinsic functions can be found at - sh-15.10

Other links to look at:

Lec 03 PDF         Lec 03 PowerPoint       Thursday, September 16, 2010


Lecture 04: Continued with showing the elements of the Fortran and more detail on the typically encountered features of the language. Topics covered: subroutines and functions; intrinsic functions; constants and variables; input output with open/close, read/write, formats; character strings.

Lec 04 PDF         Lec 04 PowerPoint       Tuesday, September 21, 2010

The links in this lecture were to fortran and intrinsic functions above. 

The following programs give examples of different Fortran codes:

loops.f                               demonstrates loops

ifs.f                 demonstrates if statements

inout.f                        demonstrates file input and output

subs.f                         demonstrates subroutines and functions.

To download programs, right click on link and "save link target as".   With one-button mouse use <ctrl><click> on link.


The programs poly_area.f and vars.f are also used.


On Athena to use these programs:

ssh –X

% add gcc-4.0  # This is probably not needed

% gfortran poly_area.f –o poly_area

% poly_area

(Note: do not type %, it is meant to be the prompt).


PC Instructions

If you are familiar with installing software on your PC and know lots about the Windows system you can try to install g77 from the link below.  (We don’t know if this will work on Vista).   There is a “click here” for instructions button that you should read first to see if you understand the instructions.

Below is a link to Fortran (g77) for PC

Link to PC Fortran 77

Below are alternative instructions (these require about the same amount of knowledge as the instructions above).

Alternative is to add cygwin to your PC (linux running on PC).  Download cygwin from

Use cygwin_setup to get gcc, g77 and nedit

Also needed is

which is the Windows X-window system.  (Needed for nedit)

When using X-windows from cygwin you need to add at the terminal prompt

setenv DISPLAY :0.0

(this allows programs such as nedit and xterm to open windows).


GFORTRAN (f90) instructions gives instructions for installing gfortran on a PC.  They link to this location (we have not checked this for viruses.


If you have not used your PC for things other than web browsing, word, excel, power point and mail, you will probably have an easier time using the Athena system for the Fortran and C homework and activities.

You have several choices here.  The basic concept is to compile and run your code on an Athena machine and to create/edit your code either on Athena or your PC.

If you create your code on your PC using Wordpad, for example, then you need to be able to copy the code files from your PC to the Athena. The link below is to MIT software for the PC

The link to SecureCRT is

SecureCRT allows you log into Athena machines remotely from your laptop and SecureFX allows you to transfer files between your PC and Athena and visa versa.  A quick start guide is

SecureFX allows you to move files back and forth between your PC and Athena.  The quick start guide is

An alternative to using SecureCRT is use the Athena dialup ssh terminal.

This will create a window that looks like SecureCRT terminal window.  (You will still need SecureFX to moves files back and forth).


When you login into Athena with SecureCRT you will be connected to (the same connection when used the http connection above.   You can probably add the server to SecureCRT and connect to this machine as well.  On athena you:

add gcc-4.0

gfortran loops.f -o loops


./loops  # if the above does not work.


On, you can run gedit to edit your programs when using SecureCRT (I think: There may be an issue with X-windows forwarding.

On all systems you can use jedit remotely.  (This can be slow).

add jedit

jedit loops.f


For editing files, you can also edit on your laptop and use SecureFX to copy the files to athena.

Windows may want to add a .txt extent to your .f files because they are text files.  Once the files have been transferred to athena you use the mv to change the name (mv = move)

mv loops.f.txt loops.f



Intel Mac Instructions

Nedit can be downloaded from

gfortran downloaded from (see links with gfortran in name).

(Fink commander is a great utility to install to get lots of linux packages for the Mac


If you want to run fortan on athena then use terminal under Applications/Utilities or use X in from the same directory.  Once terminal runs use:

ssh -X <user name>

add gcc-4.0

gfortran loops.f –o loops



You can use TextEdit on your Mac (or nedit when you run X, also under Applications/Utilities) to edit your files and

scp <filename> <user name>

to copy files to athena.  If no folder information is given in file name, then scp needs to be run from the folder with the file.  (cd allows you change directory and pwd tells you which directory you are in.).

If you get a message, command not found when you try to run your program, e.g. loops, try




Other sources of information: O'Reilly series of books on programming (available with MIT certificate) Useful link with lots of information on Fortran in its various forms.


Lecture 05: Continued with FORTRAN.  Character strings, Control statements if and do; other commands such as include, common, parameter.  More programs will be developed in class including simple output and computing root-mean-square scatter of randomly generated numbers.  Program developed in class: rms.f

Lec 05 PDF      Lec 05 PowerPoint    Thursday, September 23, 2010


Lecture 06: Finish up FORTRAN.  Compile, linking and runtime errors and miscellaneous topics.  Practice with using the language.

Lec 06 PDF      Lec 06 PowerPoint    Tuesday, September 28, 2010


Lecture 07: Examines the changes that were made in fortran90 with the introductions on more modern concepts in programming languages.

Lec 07 PDF      Lec 07 PowerPoint    Thursday, September 30, 2010



Lecture 08: Start of c-language programming. History, variables and executable statements. Additional links for this class

Lec 08 PDF      Lec 08 PowerPoint    Tuesday October 5, 2010


Lecture 09: Continuation of C. Covering Examined C-pointer; File Input/Output and the routines for formatted reads and write; Compiling C routines; The C preprocessor cpp; Structures in C; Memory management

Lec09_pnt.c is demonstration of pointers

Lec 09 PDF      Lec 09 PowerPoint    Thursday October 7, 2010


Lecture 10: Finish structures and memory management in C.  Start of C++. Inheritance and overloading in C++.

C and C++ routines used in class launch.c.

Ball.h and

C++ folders ustring launch coord

Lec 10 PDF      Lec 10 PowerPoint    Thursday October 12, 2010


Lecture 11: Finish up of C++ looking at classes, inheritance and overloading.  We will look more carefully at the code linked in Lecture 10.  Homework number 3 has been set.

Lec 11 PDF      Lec 11 PowerPoint    Tuesday October 14, 2010

Example pieces of code for C and comparison to Fortran

C_Basics.html           C_Fortan_compare.html   C_Pointers.html


Lecture 12: Start of Mathematica.  These lectures are accompanied by a Mathematica Notebook that shows example of concepts presented in the notes

Lec 12 PDF      Lec 12 PowerPoint    Lec 12 NB       Tuesday October 19, 2010

Introductory screen casts from Wolfram Introduction (20 minutes)  Making models (15 minutes)  Use of the demonstrations site


Lecture 13: Continuation of Mathematica.  These lectures are accompanied by a Mathematica Notebook that shows example of concepts presented in the notes

Lec 13 PDF      Lec 13 PowerPoint    Lec 13 NB       Thursday October 21, 2010


Lecture 14: MATLAB: Introductory lecture on Matlab introducing system, variable types, control and functions.  The following Matlab M-files are used in the lecture: Lec01_01.m Lec01_02.m

Lec 14 PDF      Lec 14 Powerpoint    Tuesday October 26, 2010


Lecture 15: MATLAB: path command, variables, file IO and dialog boxes.  The following M-files are used Lec02_01_file.m Lec02_02_db.m Lec03_01_file.m (The last file will be used in the next lecture as well). A tar file with the data is Tseries.tar

Lec 15 PDF      Lec 15 Powerpoint    Thursday October 28, 2010


Lecture 16: MATLAB: Graphics handles and animation of figures.  The following M-files are used in class Lec03_movie.m and per_func.m

Lec 16 PDF      Lec 16 Powerpoint    Tuesday November 2, 2010


Lecture 17: MATLAB: Final class on GUI construction and use. A tar file with the data is Tseries.tar

Basic GUI layout: GPSanal.m gpsguio.m

Lec 17 PDF      Lec 17 Powerpoint    Thursday November 4, 2010


Lecture S1: Python  I

LecS1 PDF       Lec S1 PowerPoint    Tuesday November 9, 2010


Lecture S2: Python  

LecS2 PDF       Lec S2 PowerPoint    Tuesday November 16, 2010



Lecture 18: 3-D graphics in matlab.

Examples are: Lec18_3D.m                Lec18_TotalANC.avi           Lec18_RateANC.avi    Dif_1006_0407.fig    

Lec 18 PDF      Lec 18 Powerpoint    Thursday November 18, 2010


Lecture 18: Solution to differential equations in Mathematica and Matlab.

Mathematic notebook Lec19_NDsolve.nb

Matlab solutions are Lec19_ODE.m          Lec19_animate.m    Lec19_hit.m              Lec19_bacc.m

Lec 19 PDF      Lec 19 Powerpoint    Tuesday November 23, 2010




Lecture 23: Introduction of Class Project.  Examination of Q03 results. Graphics formats and issues about vector and pixel based graphics. Random number generators  GMT graphics package for mapping applications

The order of the class presentations will be decided in this class and posted after the class.

Matlab script to compare Q3 results Q3_compare.m

Data files matlab.dat fortrana.dat fortranb.dat C.dat mathematicf.dat


Lec 23 PDF      Lec 23 Powerpoint    Tuesday December 7, 2010


Homework Schedule for 2010


Homework will be set here and due on dates shown

Homework #1             PDF file          Due Thursday, September 30, 2010


Homework #2             PDF file          Q1_text.txt     Due Thursday, October 21, 2010


Homework #3             PDF file          Q1_text.txt     Due Thursday, November 04, 2010.


Homework #4             PDF file          Q1_text.txt     Due Thursday, November 18, 2010.


Project: Group pairs and project descriptions    Due Thursday, November 18, 2010.


Homework #5             PDF file          Q1_text.txt     Due Thursday, December 02, 2010.



Final Project    Demonstrated Thursday December 9, 2010.  Final are submissions due that day.


Each Presentation should take about 7-10 minutes, which includes a description of the project and a demonstration of the program.  The presentation, project description and program source code should be emailed to by Thursday December 09, 2010.




Project Presentation:  Thursday December 9, 2010

Project Presentation should be a total of 7-10 minutes long include (this is not very long):  Groups with two people will have 10-minutes and single person groups will have 10 minutes.  Some of the time should be left for questions.

1. Brief description of project

2. Role played by each member of team when more than one.

3. Demonstration of program.


All members of team should participate in the presentation.


Each project submission should include:

1.  Written document that outlines the algorithms used in the project.  The document should include:
Role of each person in the project
Methods that were used to verify that the code generated correct answers,

2.  Program design and layout discussion

3.  Program source code and instructions on installation

4.  Examples of output of program and instructions for using the program.


Final electronic submissions are due Thursday December 9,  2010.


Project Groups 12.010 2010






Susan Flowers

Sudoku solver (Mathematica)


Shaena Berlin,
Anna Merrifield

Predicting Run-up on the North Shore of O’ahu



Kirrah Jones,
Ciara Sivels

Neuton Diffusion



Benjamin Nield

Checkers Game


Martina Coccia,
Sedar Sahin

Expansion of Universe



Curran Oi

Tracking High-Energy Electrons





Kimberly Huppert

Slope Stability analysis


Charles Hallam

Sudoku solver


Stephanie Sallum, Harriet Lau




Lauren Jozwiak

Flexure models