matlab introduction

15
Matlab Introduction Daniel Moore

Upload: daniel-moore

Post on 03-Dec-2014

4.980 views

Category:

Technology


1 download

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 framework

TRANSCRIPT

Page 1: Matlab Introduction

Matlab Introduction

Daniel Moore

Page 2: Matlab Introduction

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

Page 3: Matlab Introduction

Why use Matlab?

• Huge library of mathematical functions and algorithms

• Fast implementation of vector/matrix operations

• Powerful visualizations (when used properly)

Page 4: Matlab Introduction

Sample Matlab Screen

Command Window: Insert Matlab statements at the prompt

Command History

Current Directory Script Editor

Page 5: Matlab Introduction

Useful documentation (Seriously)

For documentation on a particular command just type `doc <command>` at the prompt

…Or use the help menu bar

Page 6: Matlab Introduction

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 (.*, ./)

Page 7: Matlab Introduction

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

Page 8: Matlab Introduction

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)

Page 9: Matlab Introduction

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

Page 10: Matlab Introduction

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

Page 11: Matlab Introduction

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

Page 12: Matlab Introduction

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

Page 13: Matlab Introduction

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

Page 14: Matlab Introduction

Basic Programming

• M-files

Page 15: Matlab Introduction

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