Difference between revisions of "Getting started with MATLAB or Python"

From Process Model Formulation and Solution: 3E4
Jump to navigation Jump to search
Line 86: Line 86:


=====Matrices=====
=====Matrices=====
Matrices are arrays of values. To declare a matrix in MATLAB all one needs to do is use the "[]" matrix declaration brackets. Within a matrix declaration, the individual row elements are separated by a "," or a blank space (" ") and the rows are separated by a semicolon (thus it is possible to declare a matrix as it would appear on paper...). Note that matrices in MATLAB are declared in row dominant order (similar to C++). That is the first index refers to the row number, the second to the column number, etc. The '''Accessing Submatrices''' section below will outline how to access elements within a matrix. Also note that it is possible to create special types of "ordered" matrices using the colon (:) operator (shown in the '''Dot and Colon Operator''' section).
Matrices are arrays of values. To declare a matrix in MATLAB all one needs to do is use the "[]" matrix declaration brackets. Within a matrix declaration, the individual row elements are separated by a "," or a blank space (" ") and the rows are separated by a semicolon (thus it is possible to declare a matrix as it would appear on paper...). However, always remember that it is not a requirement to declare each new row on a separate line. As long as the semicolon is there, MATLAB will interpret it as the start of a new row. Note that matrices in MATLAB are declared in row dominant order (similar to C++). That is the first index refers to the row number, the second to the column number, etc. The '''Accessing Submatrices''' section below will outline how to access elements within a matrix. Also note that it is possible to create special types of "ordered" matrices using the colon (:) operator (shown in the '''Dot and Colon Operator''' section).
[[File:MATLAB_Matrices.PNG|center|400px]]
[[File:MATLAB_Matrices.PNG|center|400px]]



Revision as of 15:27, 13 September 2010

Introduction to the Command Line

MATLAB

  • MATLAB IS CASE SENSITIVE!!! For example, A is not the same thing as a. This is very important when working with variable and function names.

Common Command Line Commands and Functions

doc function_name

Brings up information on any built-in MATLAB function (i.e. it accesses the help directory entry).

Before After
MATLAB doc before.PNG
MATLAB doc after.PNG
edit

Opens the text editor

Before After
MATLAB edit before.PNG
MATLAB edit after.PNG


clc

Clears the screen

Before After
MATLAB clc before.PNG
MATLAB clc after.PNG


clear

Clears the variables from the workspace (and from memory)

Before After
MATLAB clear before.PNG
MATLAB clear after.PNG


Built in Functions

MATLAB has an array of simple built in functions. Basically all of the rudimentary calculation types that you can think of (sin,cos,exp,etc.) have a built in MATLAB function that may be called at the command line and coded into a script or function file (as the need arises). A few examples are shown in the screenshot below.

MATLAB Common Functions.PNG


Built in Constants

MATLAB also has predefined variables for fundamental constants (ex. pi, Inf, i). Be wary though, as these can be easily overwritten by the user. As a rule of thumb try not to name your variables after any fundamental mathematical constant.

MATLAB constants.PNG


fprintf() and disp()

fprintf() and disp() provide a means of printing text to the command window. While obviously not useful at the command line (as the example below shows), these statements will become useful when you begin writing your own scripts and functions (either as a means of displaying intermediate results or as a means of tracking down bugs). disp() is a simple display function that prints arrays or strings directly to the command window. fprintf() is a more robust function that allows formatting of the string to be printed as well as output specification (for use in printing to files). The default output is set to the command window.

MATLAB fprintf.PNG
Semicolon

Placing a semicolon at the end of a line of code will suppress any output produced by that line. This (along with fprintf() and break points) can help in debugging code. Unless you are debugging it is good practice to end all lines of code with a semi-colon to avoid a bunch of garbage intermediate values being printed out at the command line.

MATLAB semi colon.PNG
Variables

Variables declared in MATLAB are essentially 1x1 matrices. To declare a variable in MATLAB all you need to do is specify the name of the variable and then set it to a value using the "=" operator (shown below). This is the same for both MATLAB code and the command line. Note below that if you place a semi-colon after the variable declaration then the variable's value is not reprinted (the opposite is also true). Also note that if a variable is not declared for a value then the default ans variable is assigned to it. Please note that there is only one ans variable. So as soon as another undeclared value shows up the previous value will be lost. Therefore it is always a good idea to give every variable a name (to avoid plenty of headaches in the debugging stage). Also note that ans does not discriminate between variables and larger matrices, so any new undeclared variable or matrix will reassign the value of ans.

MATLAB variables.PNG
Matrices

Matrices are arrays of values. To declare a matrix in MATLAB all one needs to do is use the "[]" matrix declaration brackets. Within a matrix declaration, the individual row elements are separated by a "," or a blank space (" ") and the rows are separated by a semicolon (thus it is possible to declare a matrix as it would appear on paper...). However, always remember that it is not a requirement to declare each new row on a separate line. As long as the semicolon is there, MATLAB will interpret it as the start of a new row. Note that matrices in MATLAB are declared in row dominant order (similar to C++). That is the first index refers to the row number, the second to the column number, etc. The Accessing Submatrices section below will outline how to access elements within a matrix. Also note that it is possible to create special types of "ordered" matrices using the colon (:) operator (shown in the Dot and Colon Operator section).

MATLAB Matrices.PNG
Matrix Operations

MATLAB follows all basic linear algebra rules (obviously :P). That is to say that when you add or subtract two matrices it is done on an element by element basis, while multiplication is done one a row by column basis and division performs the reverse of this operation. Note however that if you add a "." in front of a *,/, or ^ then the operation is performed on an element by element basis. Obviously adding a dot in front of a + or - would not do anything extra (and actually results in a compilation error).

MATLAB matrix operations.PNG
Dot and Colon Operator

If you add a "." in front of a *,/, or ^ then the operation is performed on an element by element basis. Obviously adding a dot in front of a + or - would not do anything extra (and actually results in a compilation error).

MATLAB dot colon.PNG
Accessing Submatrices
MATLAB sub matrices.PNG
Concatenation
MATLAB concatenation.PNG



Python


Scripts and Functions

MATLAB


Python


Code Structures

MATLAB


Python


Plotting Data

MATLAB


Python


Additional Resources

MATLAB


Python