Scientific Computation and
Programming
PYTHON, R, MATLAB, FREEMAT
What is Python?
A programming language with strong similarities to
PERL (Practical Extraction and Report Language),
but with powerful typing and object oriented
features.
Commonly used for producing HTML (HyperText Markup
Language) content on websites. Great for text files.
Useful built-in types (lists, dictionaries).
Clean syntax, powerful extensions.
Why Python?
Natural Language ToolKit
Ease of use; interpreter
The scientist‟s need:
- Get data (simulation, experiment control)
- Manipulate and process data
- Visualize results (to understand what we are
doing)
- Communicate results: produce figures for reports
or publications, write presentations
Advantages of python
Very rich scientific computing libraries (a bit less
than Matlab, though)
Well thought out language, allowing to write very
readable and well structured code: we “code what we
think”.
Many libraries for other tasks than scientific
computing (web server management, serial port
access, etc.)
Free and open-source software, widely spread, with a
vibrant community
Drawbacks of Python
less pleasant development environment than, for
example, Matlab. (More geek-oriented).
Not all the algorithms that can be found in more
specialized software or toolboxes.
Python Readings
Things to read through
“Dive into Python” (Chapters 2 to 4)
http://diveintopython.org/
Python 101 – Beginning Python
http://www.rexx.com/~dkuhlman/python_101/python_101.htm
l
Things to refer to
The Official Python Tutorial
http://www.python.org/doc/current/tut/tut.html
The Python Quick Reference
http://rgruet.free.fr/PQR2.3.html
R
A programming environment for data analysis and
graphics
Software for Statistical Data Analysis
R is a software that provides a programming
environment for doing statistical data analysis. This
software was written by Robert Gentleman and Ross
Ihaka and the name of the software bear the name of
the creators. R can be effectively used for data
storage, data analysis and a variety of graphing
functions. R is distributed free and it is an open
source software.
The ‘R’ Environment
R is an integrated suite of software facilities for data
manipulation, calculation and graphical display.
an effective data handling and storage facility
a suite of operators for calculations on arrays, in particular
matrices
a large, coherent, integrated collection of intermediate tools
for data analysis
graphical facilities for data analysis and display either directly
at the computer or on hardcopy
a well developed, simple and effective programming language
(called „S‟) which includes
conditionals, loops, user defined recursive functions and input
and output facilities. (Indeed
most of the system supplied functions are themselves written
in the S language.)
R Window
Scalars, Vectors and Matrices
Like in many other programs, R
organizes numbers in scalars (a
single number { 0-dimensional),
vectors (a row of numbers, also
called arrays {1-dimensional) and
matrices (like a table { 2-
dimensional).
Strengths and Weaknesses of R
Strengths
Free and Open Source
Strong User Community
Highly extensible, flexible
Implementation of high end statistical
methods
Flexible graphics and intelligent defaults
Weakness
Steep learning curve
Slow for large datasets
Matlab
Matlab is basically a high level language which has
many specialized toolboxes for making things easier
for us
How high?
Matlab
High Level
Languages such as
C, Pascal etc.
Assembly
What is MATLAB?
High level language for technical computing
Stands for MATrix LABoratory
Everything is a matrix - easy to do linear algebra
MATLAB is a powerful, general-purpose
system or environment for doing
mathematics, scientific and engineering
calculations.
The MATLAB System
Development Environment
Mathematical Function Library
MATLAB language
Application Programming
Language
Matlab Screen
Command Window
type commands
Current Directory
View folders and m-files
Workspace
View program variables
Double click on a variable
to see it in the Array Editor
Command History
view past commands
save a whole session
using diary
Arrays, Vectors, and Matrices
Array :
A collection of data elements
arranged in rows and columns
Vector:
A one-dimensional array (single >> A=[1:9] row or column?
row or single column of data
elements) >> B=[1:9]‟ column vector
Matrix :
>> C=[1:3; 4:6; 7:9]
A two-dimensional array (more
than one row and/or column of
C=
data elements) 1 2 3
4 5 6
7 8 9
Variables
No need for types. i.e.,
int a;
double b;
float c;
All variables are created with double precision unless
specified and they are matrices.
Example:
>>x=5;
>>x1=2;
After these statements, the variables are 1x1 matrices
with double precision
Display Facilities
0.7
plot(.) 0.6
0.5
0.4
Example: 0.3
>>x=linspace(0,4*pi,100); 0.2
>>y=sin(x); 0.1
>>plot(y)
0
-0.1
>>plot(x,y) -0.2
-0.3
0 10 20 30 40 50 60 70 80 90 100
stem(.)
0.7
0.6
0.5
0.4
Example:
0.3
0.2
>>stem(y) 0.1
>>stem(x,y) 0
-0.1
-0.2
-0.3
0 10 20 30 40 50 60 70 80 90 100
Display Facilities
title(.)
>>title(‘This is the sinus function’)
This is the sinus function
1
0.8
0.6
xlabel(.) 0.4
0.2
sin(x)
0
-0.2
>>xlabel(‘x (secs)’) -0.4
-0.6
ylabel(.) -0.8
-1
0 10 20 30 40 50 60 70 80 90 100
x (secs)
>>ylabel(‘sin(x)’)
MATLAB WORKSHOP
WORKSHOP
WebPages
www.csee.wvu.edu/~jalali
Advantages of MATLAB
This is a powerful and versatile simulation software,
very popular in industry and academia
due to its exibility in allowing direct programming
and linking to FORTRAN and C routines, the
package has grown immensely, with many added
routines to allow numerical simulations of nonlinear
systems as well.
Drawbacks_MATLAB
Its size and relative complexity; it takes some time to
become familiar with its language and become
familiar with several of the main routines needed for
basic simulations.
Equations must be handled in certain form and
sequence, requiring the user to be familiar with the
phenomena being analyzed, making it somewhat
complex for inexperienced users
Good comprehension of numerical analysis, linear
algebra and linear systems is required, which makes
it somewhat inappropriate for learners or beginners
Freemat
FreeMat is an open software package that provides
all the basic functions of MATLAB
It lacks the sophisticated toolboxes provided by the
MATLAB, but it works perfectly if you use basic
functions
Freemat Window
Freemat Info
FreeMat is a numerical computational package designed
to be compatible with other numerical packages such as
Matlab and Octave. The supported operating systems for
FreeMat include Windows, Linux, and Mac OS X. Samit
Basu created this program with the hope of constructing
a free numerical computational package that is Matlab
friendly. The web page of FreeMat at www.freemat.org.
states that some of features for FreeMat include
eigenvalue and singular value decompositions, 2D/3D
plotting, parallel processing with MPI, handle-based
graphics, function pointers, etc. To download FreeMat,
go to http: //sourceforge.net/projects/freemat.