This is a simple tutorial to get you started with Matlab. Matlab makes it easy to perform scientific computations without having to learn a programming language such as Fortran, C, or C++. This tutorial is developed keeping assignment_0.pdf for AM205 in mind. It can be used for other classes or self-learning as well.
- Matlab Basics: start up, simple commands, Matlab as calculator
- Matlab Variables: Vectors and Matrices
- Linear Equations
- Simple programming: control of flow, functions, m-files
You should make use of online search tools such as google to search for relevant matlab material. There is a lot of material on the web. Additional resources include:
- Matlab online tutorial at: http://www.mathworks.com/academia/student_center/tutorials/mltutorial_launchpad.html?s_tid=int_tut
- Comprehensive online documentation at the Matlab site: http://www.mathworks.com/help/techdoc/index.html
- Another site: http://en.wikibooks.org/wiki/MATLAB_Programming
- Numerical Computing with MATLAB by Cleve Moler
- A Non-matlab, but very useful book: Numerical Recipes (2007). William H. Press, Saul A. Teukolsky, William T. Vetterling, and Brian P. Flannery. Cambridge University Press
This depends on your operating system. On Mac, it is usually installed in /Applications and you should see a Matlab icon with a MATLAB_<version number>. Double clicking the icon should open it. On Windows, there should either be a shortcut on the desktop or it can be found under programs in the start-up.
Matlab Development Environment (Matlab Desktop):
This can change with new releases. The following video gives a quick introduction:
The key items are "Command Window" (where you type commands in this tutorial), "Current Folder", and the "Workspace".
Note: In Matlab, any line beginning with % is a comment (not executed by matlab). Lines without % are commands that are executed by matlab. You can copy and paste the matlab statements in this tutorial into matlab
Open matlab and in the command window, type:
This will open a separate window with documentation and a search box. There are other ways! For example, if you want to find out more about a specific command, you can do:
For space reasons, the results are not shown in the code block unless necessary.
All matlab variables are arrays. Even single numbers are arrays of dimension 1. Below, we start experimenting with some arrays.
Element-wise operations on an array
More on plotting later.
Example – Dot product, angle between vectors:
Matrix example (Linear Equations)
We now solve the linear equation:
We will later consider the control of flow within a loop under functions.
More advanced topics
complex plots (plot annotations, axis labels, etc.)
Some 3D plotting
Sinc function (https://en.wikipedia.org/wiki/Sinc_function)
m-FILES (SCRIPTS AND FUNCTIONS)
A collection of matlab commands in a file named <name>.m.
To run the script, save the script file (say example.m) in a directory accessible by matlab and just type the script name (without .m) in the command window. In this case it will be 'example'.
Matlab functions are callable sub-procedures that are
put in a file named <function_name>.m
Example function (stat.m from matlab documentation) is as follows:
Put the above function in a file called stat.m in a directory which is in matlab's path. You can use this function as below:
The following are some functions relevant to assignment_0.pdf of AM205.
They are meant to get you started with the problems, but they are not "complete solutions".They can be useful for learning functions in matlab for other classes as well.