what is matlab? matlab is one of a number of commercially available, sophisticated mathematical...

40
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Upload: dwight-byrd

Post on 13-Jan-2016

223 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

What is MATLAB?• MATLAB is one of a number of

commercially available, sophisticated mathematical computation tools.

• Others include• Maple• Mathematica• MathCad

Page 2: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

MATLAB Excels at

• Numerical calculations• Especially involving matrices

• Graphics• MATLAB stands for

Matrix Laboratory

Page 3: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Why MATLAB

• Easy to use• Versatile• Built in programming language• Not a general purpose language like C/C++ or Java• MATLAB was written in C

Medical imaging Fluid Dynamics

Page 4: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Getting Started

• Type in

matlab

at the shell prompt in your terminal.• MATLAB opens to a default window configuration

Page 5: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

MATLAB uses a standard windows menu bar

To exit MATLAB use the close icon or from the menu: File->Exit MATLAB

Page 6: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Command WindowEnter commands at the promptCurrent Directory

MATLAB Windows

Command HistoryWorkspace Window

Page 7: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

You can use the command window much like you’d use a calculator

The standard order of operation rules apply

Command Window

Page 8: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Workspace Window

Workspace Window

Page 9: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Workspace Window

Scalar

Vector

2-D Matrix

Page 10: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Document Window

If you double click on any variable in the workspace window MATLAB launches a document window containing the array editor

You can edit variables in the array editor

Page 11: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Document Window

Page 12: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Figure Window

• When Figures are created a new window opens• It’s extremely easy to create graphs in MATLAB• Try plotting

The semicolon suppresses the output from each command

Page 13: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Editing Window

• This window allows you to type and save a series of commands without executing them

• There are several ways to open an editing window– From the file menu– With the new file icon

Page 14: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Open an editing window from the file menu or with the new file icon

Page 15: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Save and Run

Write your code in the editing window, then run it using the Save and Run icon

Page 16: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Solving Problems with MATLAB

Page 17: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Naming Variables

All names must start with a letter

They may contain letters, numbers and the underscore ( _ )

Names are case sensitive

There are certain keywords you can’t use (iskeyword)

Page 18: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

The Basic Data Type in MATLAB: Matrices

Group of numbers arranged into rows and columns

Single Value (Scalar) Matrix with one row and one column

Vector (One dimensional matrix) One row or one column

Matrix (Two dimensional)

Page 19: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Array OperationsUsing MATLAB as a glorified calculator is OK, but its real strength is in matrix manipulations

Page 20: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

To create a row vector, enclose a list of values in brackets

Page 21: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

You may use either a space or a comma as a “delimiter” in a row vector

Page 22: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Use a semicolon as a delimiter to create a new row

Page 23: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Use a semicolon as a delimiter to create a new row

Page 24: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Shortcuts• While a complicated matrix might have to be

entered by hand, evenly spaced matrices can be entered much more readily. The command 

b= 1:5

or the command

b = [1:5]

both return a row matrix 

Page 25: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

The default increment is 1, but if you want to use a different increment put it between the first and final values

Page 26: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

To calculate spacing between elements use linspace

initial value in the array

final value in the array

number of elements in the array

Page 27: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Calculations between scalars and arrays

Page 28: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Calculations between arrays: addition and subtraction

Addition between arrays is performed on corresponding elements

Subtraction between arrays is performed on corresponding elements

Page 29: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

MATLAB interprets * to mean matrix multiplication. The arrays a and b are not the correct size for matrix multiplication in this example

Multiplication between arrays is performed on corresponding elements if the .* operator is used

Page 30: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Array Operations

Array multiplication .*

Array division ./

Array exponentiation .^

In each case the size of the arrays must match

Page 31: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Transpose

The transpose operator changes rows to columns or vice versa.

Page 32: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

The transpose operator makes it easy to create tables

Page 33: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

table =[degrees;radians]’ would have given the same result

Page 34: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

The transpose operator works on both one dimensional and two dimensional arrays

Page 35: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Saving Your Work• If you save a MATLAB session, all that is

saved are the values of the variables you have named

Page 36: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Save either by using the file menu or...

Save with a command in the command window

MATLAB automatically saves to a .mat file

Page 37: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Script M-files• If you want to save your work, you need to

create an M-file• File->New->M-file• Type your commands in the edit window

that opens• The file is saved into the current directory• It runs in the command window

Page 38: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Comments• The % sign identifies comments

• You need one on each line

Page 39: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

• Create a .m file that tabulates and plots the aerodynamic drag on a sphere of radius (r) of 5m as a function of velocity(V) (with V going from 0 to 100m/s). Assume the drag coeficient is 1.

• The drag on a sphere is given by:

• Drag = Cd*0.5*air_density*V2*πr2

• Cd - drag coeficient

• Air_density = 1x10^-6

Problem we can do together:

Page 40: What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad

Drag problem continued:• Cd = 1;

• V = [0:1:100];

• r=5;

• air_density=1e-6;

• drag = Cd*air_density*(V.^2)*pi*r^2;

• table = [V',drag']

• plot (V,drag)

Do the results make sense?