matlab introduction
DESCRIPTION
The frame work that I used for my Introduction to Matlab hour long course. Most of the instruction took place on a live Matlab screen, but this provided the frameworkTRANSCRIPT
Matlab Introduction
Daniel Moore
What is Matlab?
• MATrix LABoratory– Computing environment and programming
language allowing for powerful numerical / image analysis and manipulation
– basic data element is an array that does not require dimensioning
– It allows you to solve many technical computing problems, especially those with matrix and vector formulations, fairly quickly
Why use Matlab?
• Huge library of mathematical functions and algorithms
• Fast implementation of vector/matrix operations
• Powerful visualizations (when used properly)
Sample Matlab Screen
Command Window: Insert Matlab statements at the prompt
Command History
Current Directory Script Editor
Useful documentation (Seriously)
For documentation on a particular command just type `doc <command>` at the prompt
…Or use the help menu bar
Matrix and Vector Algebra
• MATLAB is based on matrix and vector algebra– Even scalars are treated as 1x1 matrices
• This means you have to be careful when using operators on data
• Rule of thumb: functions (like sin(), cos(), etc.) act on each individual element; operators perform matrix operations
• Use dot operator notation to operate on individual elements (.*, ./)
Graphing
• Matlab can make a large variety of graphs• User has a large degree of control over what
can appear on the graph• Graph can be made from the Matlab
command line or by using the plot tools GUI• Plot, quiver, pcolor, many plot options• Many color schemes
Importing Data
• Matlab can import data from a variety of sources
• Can import using the Import Data tool or programmatically
• XLSRead is good for importing .xls files– [Numeric, txt, raw]=xlsread(File, Sheet, Range)
Basic Programming
• M-files store Matlab command lines– Can be run from the command line– When run, they execute all of the commands
contained in it as if you had typed these into a command window (like a keyboard macro)
• M-files can contain scripts or functions (or both or multiple functions)– Function variables are local to that function– Scripts only operate on variables that are hard
coded into their m-file
Some simple commands
• Some basic loops– for i=1:10 … end– while(i<10) … end– if (a>10) … elseif (a<5) … else … end– switch a case 1 … case 2 … case 3 … … otherwise
… end
• Formatting– % comments out a line– %% can help divide a section
File directory structure
• Recommend that user m-files go in a MATLAB directory within “My Documents”– Use subdirectories for various projects
• Put directories into matlab “path” allows calling functions from any directory
Working “live” on command line
• From program, go “live” with keyboard command
• “who” command lists currently defined variables
• Commands work – same as in program• Live graphs, or modifications to existing
graphs• History of command input, accessed by arrow
keys
Fitting data
• Data displayed in a plot can be fit to polynomials– Built in to plot window - tools/basic fitting
• Also can use matrix ‘backslash’ operator
Basic Programming
• M-files
Additional resources
• Matlab tutorial (pdf file)• Matlab built-in demos (in help)• Demos available on-line– http://www.mathworks.com/academia/student_c
enter/tutorials/launchpad.html
• User contribution repositories– On mathworks web site