Difference between revisions of "Getting started with MATLAB or Python"
Line 140: | Line 140: | ||
x = linspace(0, 18, 10) % easiest method | x = linspace(0, 18, 10) % easiest method | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Now create the corresponding \(y\)-vector using MATLAB's ability to do vector operations. We can write | |||
<syntaxhighlight lang="matlab"> | |||
y = (x-8).^2 - 40 | |||
</syntaxhighlight> | |||
'''Note''': that we must use <tt>.^</tt> to calculate the exponent. If you just write <tt>^</tt> by itself, MATLAB will assume you want to calculate the matrix exponent, which is defined quite differently. In fact, any '''element-by-element''' calculation must use the "." notation to tell MATLAB ''not'' to use its default '''matrix''' calculations. | |||
For example, use <tt>.*</tt> for element-by-element matrix multiplication. | |||
| width="50%" valign="top" | | | width="50%" valign="top" | | ||
Line 151: | Line 159: | ||
x = np.linspace(0, 18, 10) # easiest method | x = np.linspace(0, 18, 10) # easiest method | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Now create the corresponding \(y\)-vector: | |||
<syntaxhighlight lang="python"> | |||
y = (x-8) ** 2 - 40 | |||
</syntaxhighlight> | |||
We raise a variable to a power using the <tt>**</tt> notation; the <tt>^</tt> operator means something else in Python. | |||
|} | |} | ||
'''Key difference''' | |||
* In MATLAB, everything is a matrix calculation, by default. | |||
* In Python's NumPy library (<tt>numpy</tt>), everything is an element-by-element calculation, by default. | |||
'''Every''' Python script in this course must start with the following two lines: | <!-- '''Every''' Python script in this course must start with the following two lines: | ||
<syntaxhighlight lang="python"> | <syntaxhighlight lang="python"> | ||
import numpy as np | import numpy as np | ||
from matlabplotlib.pylab import * | from matlabplotlib.pylab import * | ||
</syntaxhighlight> | </syntaxhighlight> | ||
''Advanced topic:''The reason for <tt>import</tt> is due to a technical issue related to [http://bytebaker.com/2008/07/30/python-namespaces/ Python namespaces] | ''Advanced topic:''The reason for <tt>import</tt> is due to a technical issue related to [http://bytebaker.com/2008/07/30/python-namespaces/ Python namespaces] --> | ||
=Introduction to the Command Line= | =Introduction to the Command Line= |
Revision as of 18:32, 12 September 2010
Introduction
MATLAB | Python |
---|---|
MATLAB (MATrix LABoratory) is a high level computer language/ interactive software package developed and distributed by MathWorks™. Matlab was first developed in the 1970s by Cleve Molar. Cleve was later joined by John N. Little and Steve Bangert and the three went on to found MathWorks™. MATLAB excels at performing matrix operations and can handle large data sets (stored as matrices) very easily. MATLAB was originally designed as a user friendly interface for LINPACK and EISPACK and so was intended for linear algebra application. Since then MATLAB has greatly expanded it's core abilities to encompass a large array of graphic and numeric applications. These core abilities may in turn be expanded further through the addition of specialized "tool boxes". |
From Wikipedia: Python is a high-level programming language whose design philosophy emphasizes code readability. Python aims to combine "remarkable power with very clear syntax", and its standard library of built-in functions is large and comprehensive. We will use the NumPy and SciPy modules (the equivalent of a MATLAB toolbox), to provide scientific computing capabilities to Python. These modules, like MATLAB, allow you to handle large data arrays with little effort. They provide all the tools we require for this course. We will also use the matplotlib module, which provides Python with plotting capabilities similar to MATLAB. |
Access / Installation
MATLAB | Python |
---|---|
MATLAB is installed on all computers in the John Hodgins Engineering Student Technology Centre (JHE 233A / 234) as well as the Burke Science Building Labs (BSB 241 / 242 / 244 / 249). The most up to date release of MATLAB is version R2010b. While there are some backward compatibility issues when it comes to older versions of MATLAB, for the level of code that will be encountered in this class students should not encounter issues with portability of code. A MATLAB/SIMULINK student package (version R2010A, good for Windows, Mac, and Linux) is available at the University Book Store for $112.95 (for those students who would like a copy of MATLAB on their laptop or home PC). The student versions comes with the following features:
I personally have a student copy on my home PC. It's a good buy if you have the extra cash. |
Python is freely available. The latest stable version that we recommend for the course is version 2.6, because it is compatible with the external libraries that we will use. Installation instructions for Python are available on this website. |
Getting started
These descriptions are specific to Windows-users. Mac and Linux users will have a similar display.
MATLAB | Python |
---|---|
|
When starting Python(x,y) you will be presented with the following window. Spyder (Scientific PYthon Development EnviRonment) is the name of the development environment we will be using.
You can have one or multiple files open at any time.
|
Your first script
In this script we will create a plot with 10 points (a vector), consisting of the numbers \(x = [0, 2, 4, ... 18] \). Then we will also create a corresponding vector \(y = (x-8)^2 - 40\) and plot these \( (x,y)\) points, pairwise, on a plot.
MATLAB | Python |
---|---|
There are several ways we can create our vector \(x\) in MATLAB. x = [0 2 4 6 8 10 12 14 16 18]
x = [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]
x = linspace(0, 18, 10) % easiest method
Now create the corresponding \(y\)-vector using MATLAB's ability to do vector operations. We can write y = (x-8).^2 - 40
Note: that we must use .^ to calculate the exponent. If you just write ^ by itself, MATLAB will assume you want to calculate the matrix exponent, which is defined quite differently. In fact, any element-by-element calculation must use the "." notation to tell MATLAB not to use its default matrix calculations. For example, use .* for element-by-element matrix multiplication. |
There are several ways we can create our vector \(x\) in Python. import numpy as np
from matlabplotlib.pylab import *
x = np.array([0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
x = np.linspace(0, 18, 10) # easiest method
Now create the corresponding \(y\)-vector: y = (x-8) ** 2 - 40
We raise a variable to a power using the ** notation; the ^ operator means something else in Python. |
Key difference
- In MATLAB, everything is a matrix calculation, by default.
- In Python's NumPy library (numpy), everything is an element-by-element calculation, by default.
Introduction to the Command Line
|
|
Scripts and Functions
|
|
Code Structures
|
|
Plotting Data
|
|
Additional Resources
|
|