|
|
Line 1: |
Line 1: |
| =Introduction=
| |
|
| |
| {| class="wikitable"
| |
| |-
| |
| ! MATLAB
| |
| ! Python
| |
| |-
| |
| | width="50%" valign="top" |
| |
|
| |
| 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".
| |
|
| |
| | width="50%" valign="top" |
| |
|
| |
| ''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 [http://numpy.scipy.org/ 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 [http://matplotlib.sourceforge.net/index.html matplotlib] module, which provides Python with plotting capabilities similar to MATLAB.
| |
| |}
| |
|
| |
| =Access / Installation=
| |
|
| |
| {| class="wikitable"
| |
| |-
| |
| ! MATLAB
| |
| ! Python
| |
| |-
| |
| | width="50%" valign="top" |
| |
| 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:
| |
|
| |
| * MATLAB version R2010a (Everything you need to get started)
| |
| * Simulink (Will be useful in your control classes)
| |
| * Control System Toolbox
| |
| * Signal Processing Toolbox
| |
| * Signal Processing Blockset
| |
| * Statistics Toolbox
| |
| * Optimization Toolbox
| |
| * Symbolic Math Toolbox
| |
| * Image Processing Toolbox
| |
| * Product Demos
| |
|
| |
| I personally have a student copy on my home PC. It's a good buy if you have the extra cash.
| |
|
| |
| | width="50%" valign="top" |
| |
|
| |
| 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.
| |
|
| |
| [[Python_installation_instructions | 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.
| |
| {| class="wikitable"
| |
| |-
| |
| ! MATLAB
| |
| ! Python
| |
| |-
| |
| | width="50%" valign="top" |
| |
|
| |
| *When you launch MATLAB the following window will open:
| |
|
| |
| [[Image:MATLAB_start.PNG|center|400px]]
| |
|
| |
| *Do not worry if MATLAB does not open exactly like this. You can easily modify what windows are visible through the '''Desktop''' drop down menu. You can select the windows you want either by toggling them individually (<span style="color:red">Region A</span>) or by selecting a template (<span style="color:blue">Region B</span>). Selecting the ''default'' template should give you the window layout you saw above.
| |
|
| |
| [[File:MATLAB_Desktop_Dropdown.PNG|center|400px]]
| |
|
| |
|
| |
| *The core MATLAB setup has 6 main sections of interest that you will become very familiar with by the end of this course:
| |
|
| |
| [[File:MATLAB_Windows_Labeled.PNG|center|400px]]
| |
|
| |
| ;1. Command Window
| |
| :The command window (or "command line") provides your main means of interacting with the MATLAB software. The command line allows you to enter simple codes that are processed immediately. All program outputs also appear in the command window. While it is technically possible to write an entire program at the command line, this is not recommended. For true programs, scripts and functions will be used.
| |
|
| |
| ;2. Current Directory
| |
| :The "current directory" panel lists all of the files (MATLAB or other) that are located in the directory MATLAB is currently accessing. This directory may be changed via the drop down address bar at the top of the main program window. For a program to run properly, ALL CODES required must be in the same directory (for example, if you write a main script file that calls a separate function file, both files must be in the same directory).
| |
|
| |
| ;3. Command History
| |
| :The command history records all commands entered in the command window (for possible future reference).
| |
|
| |
| ;4. Workspace
| |
| :The workspace lists all variables, matrices, and function handles currently being stored by MATLAB. The workspace also provides basic information on the values being stored, such as size, max, min, etc. Perhaps most useful is the ability to double click variables and matrices in the workspace to open up the '''Variable Editor''' window. The variable editor is essentially a spread sheet representation of your variables that allows for easy manipulation (especially useful for large matrices).
| |
|
| |
| ;5. Editor Window
| |
| :The editor window(s) does not immediately open when you launch MATLAB. The editor window is where you write scripts and function files that can be compiled and run in the command window (to be discussed in detail later). To open a new editor window you can either go to <tt>File</tt>><tt>New</tt>><tt>Blank M-File</tt>, hit Ctrl+N, type "edit" at the command line, or hit the "New M-File" icon at the top left of the main screen (it looks like the little piece of paper with the explosion in the top left corner). To open a pre-existing M-File you need to switch the current directory to the location of the file (as mentioned above) and simply double click on the M-File name in the "current directory" sidebar.
| |
|
| |
| ;6. Plot Window
| |
| :When the MATLAB plot tools are used the resulting graphics are displayed in separate plot windows (more on this later).
| |
|
| |
| | width="50%" valign="top" |
| |
|
| |
| When starting Python(x,y) you will be presented with the following window.
| |
|
| |
| [[Image:start-pythonxy.jpg|center|300px]]
| |
| Spyder ('''S'''cientific '''PY'''thon '''D'''evelopment '''E'''nvi'''R'''onment) is the name of the development environment we will be using.
| |
| # Select the <tt>--pylab</tt> option
| |
| # Then launch the Spyder environment.
| |
|
| |
| * Python(x,y) has a layout similar to that shown below [[Image:Layout-pythonxy-low.jpg|borderless|center|500px]]
| |
| ;1. Command Window
| |
| :The command window is where you interact with Python. Commands entered here are processed immediately. While it is technically possible to write an entire program at the command line, this is not recommended. For true programs, you should write a script in region 5 of the software.
| |
|
| |
| ;2. Current Directory
| |
| :The "current directory" panel lists all of the files that are located in the directory Python is currently accessing. This directory may be changed by navigating to a new directory and clicking on its name.
| |
|
| |
| ;3. Command History
| |
| :The command history records all commands entered in the command window (for possible future reference).
| |
|
| |
| ;4. Workspace
| |
| :The workspace lists all variables (e.g. vectors and matrices) currently available to you. The workspace also provides the size and "type" of variable. You can double click on a variable and change its value: e.g. edit a specific entry in a vector, in a spreadsheet-like interface.
| |
|
| |
| ;5. Editor Window
| |
| :The editor window is where you write scripts and function files. After saving the file to disk, you can run it by pressing <tt>F9</tt> (or click on <tt>Source</tt> in the top menu and select <tt>Run in interactive console</tt>)
| |
|
| |
| You can have one or multiple files open at any time.
| |
|
| |
| ;6. Plot Window
| |
| :Python plots are opened in new separate windows, but can be "docked" (placed) anywhere inside the main Python(x,y) window.
| |
|
| |
| |}
| |
|
| |
| = 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.
| |
|
| |
| {| class="wikitable"
| |
| |-
| |
| ! MATLAB
| |
| ! Python
| |
| |-
| |
| | width="50%" valign="top" |
| |
| There are several ways we can create our vector \(x\) in MATLAB.
| |
| <syntaxhighlight lang="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
| |
| >> x
| |
|
| |
| x =
| |
|
| |
| 0 2 4 6 8 10 12 14 16 18
| |
| </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;
| |
| >> y
| |
|
| |
| y =
| |
|
| |
| 24 -4 -24 -36 -40 -36 -24 -4 24 60
| |
| </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. If you are bit rusty on this concept, we recommend you read page 4 and 5 of [[Media:MATLAB_primer.pdf | Dr. Mhaskar's Python primer]].
| |
|
| |
| | width="50%" valign="top" |
| |
|
| |
| There are several ways we can create our vector \(x\) in Python.
| |
| <syntaxhighlight lang="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
| |
| >>> x
| |
| array([ 0, 2, 4, 6, 8, 10, 12, 14, 16, 18])
| |
| </syntaxhighlight>
| |
|
| |
| Now create the corresponding \(y\)-vector:
| |
| <syntaxhighlight lang="python">
| |
| >>> y = (x-8) ** 2 - 40
| |
| >>> y
| |
| array([ 24., -4., -24., -36., -40., -36., -24., -4., 24., 60.])
| |
| </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.
| |
|
| |
|
| |
| Finally, we are ready to plot these \( (x,y)\) points. Notice that the code is ''nearly'' identical.
| |
| {| class="wikitable"
| |
| |-
| |
| ! MATLAB
| |
| ! Python
| |
| |-
| |
| | width="50%" valign="top" |
| |
| <syntaxhighlight lang="matlab">
| |
| plot(x, y, '.-')
| |
| grid on
| |
| xlabel('x')
| |
| ylabel('y')
| |
| title('A plot of y = (x-8)^2 - 40')
| |
| </syntaxhighlight>
| |
| [[Image:Matlab-first-plot.jpg|borderless|center|400px]]
| |
|
| |
| | width="50%" valign="top" |
| |
|
| |
| <syntaxhighlight lang="python">
| |
| plot(x, y, '.-')
| |
| grid('on') # <--- it is a function in Python, so add ()
| |
| xlabel('x')
| |
| ylabel('y')
| |
| title('A plot of y = (x-8)^2 - 40')
| |
| </syntaxhighlight>
| |
| [[Image:Python-first-plot.jpg|borderless|center|400px]]
| |
| |}
| |
|
| |
| <!-- '''Every''' Python script in this course must start with the following two lines:
| |
| <syntaxhighlight lang="python">
| |
| import numpy as np
| |
| from matlabplotlib.pylab import *
| |
| </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] -->
| |
|
| |
| =Introduction to the Command Line= | | =Introduction to the Command Line= |
| {| border="1" width="100%" cellspacing="10" | | {| border="1" width="100%" cellspacing="10" |