introduction to matlab matlab is a software package for technical computation. matlab allows you...

43
Introduction to Matlab Matlab is a software package for technical computation. Matlab allows you to solve many numerical problems including - arrays and matrix operations - data analysis and graphics - numerical integration - statistical analysis - etc.

Upload: allyson-little

Post on 18-Jan-2016

226 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Introduction to Matlab

Matlab is a software package for technical computation.

Matlab allows you to solve many numerical problems including

- arrays and matrix operations - data analysis and graphics - numerical integration - statistical analysis - etc.

Page 2: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Today’s Matlab Session

Matlab User Interface Environment

Arrays and Matrix Operations Plotting Graphs - X-Y Plots

M-files

Page 3: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Running Matlab

Default Matlab Opening Window looks like this:

Page 4: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Opening Window Matlab Opening Window

Command window Current (File) Directory

Command History

Workspacewindow

File Editor

Page 5: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Opening Window

Matlab Opening Window Command Window

allows you to enter Matlab commands.

Current Directory Window saves files and data for use.

Page 6: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Opening Window

Matlab Opening Window Command Window

allows you to enter Matlab commands.

Current Directory Window saves files and data for use.

Command History Window records the Matlab commands you

issued.

Page 7: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Opening Window

Matlab Opening Window Command Window allows

you to enter Matlab commands.

Current Directory Window saves files and data for use.

Command History Window records the Matlab commands

you issued. Workspace Window keeps

track of the variables you have defined as you execute them in the command window.

double (x) returns double precision value for x.

Page 8: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Opening Window

Matlab Opening Window Command Window allows you to

enter Matlab commands. Current Directory Window saves

files and data for use. Command History Window

records the Matlab commands you issued.

Workspace Window keeps track of the variables you have defined as you execute them in the command window.

File Edit Window allows you to open existing files including M-files and edit them, create a new file, and save workspace.

Page 9: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Desktop icon

Click Desktop icon to modify display window,

Maximize Workspace, add/delete windows .

Page 10: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Desktop icon

Click Desktop icon to modify display window. Note: Clicking Desktop to Desktop Layout to

Default will allow you to get back to the default layout anytime.

Page 11: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Today’s Matlab Session

Matlab User Interface Environment Arrays and Matrix Operations Plotting Graphs - X-Y Plots

M-files

Page 12: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Built-In Matrices in Matlab

Square matrix Equal number of rows

and columns. Ex. >>A=[1,2,3;2,3,4;3,4,5]

;

Zero matrix >>A = zeros(3,3)

Page 13: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Matrix Addition/Subtraction/Multiplication

Matrix addition/subtraction is done by adding/subtracting element by element

e.g., Adding two 2x2 matrices are done as follows: |a11+b11 a12+b12| A + B = |a21+b21 a22+b22|

>> A + B

Page 14: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Matrix Addition/Subtraction/Multiplication

|a11+b11 a12+b12| A + B = |a21+b21 a22+b22|

Ex.

Page 15: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Matrix Multiplication

Two matrices, A and B, can be multiplied if and only if the number of columns in A is equal to the number of rows in B These matrices are then called conformable For example, (2x3) times (3x2) = 2x2 matrix

Note: Matrix multiplication results in an array where each element is a dot product.

Page 16: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Matrix Multiplication

Two matrices, A and B, can be multiplied if and only if the number of columns in A is equal to the number of rows in B:

Page 17: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Matrix Transpose

Transpose of a matrix (AT or A’ in Matlab) The transpose of a matrix is a new matrix where the

rows of the original matrix form the columns of the new matrix

Ex. 1 2 3 A= 4 5 6 7 8 9 >>A’ Ans = 1 4 7 2 5 8 3 6 9

Page 18: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Matrix Power/Determinant/Inverse

Matrix Power (or Exponentiation) A2 = A*A, A3 = A2 * A = A*A*A, etc. >>A^2 >>A^3 etc.

Page 19: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Determinant

A determinant of a matrix is a scalar computed from the elements of the matrix For a 2 X 2 matrix, the determinant of A (or

det(A) ) is |A| = a11* a22 - a12* a21

Ex. >>A=[3,2;5,1]>>det(A)ans = -7

Page 20: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Determinant

A determinant of a matrix is a scalar computed from the elements of the matrix For a 2 X 2 matrix, the determinant of A (or

det(A)) is |A| = a11 a22 - a12 a21 For a 3 X 3 matrix, the determinant of A is

|A| = a11 a22 a33 + a12 a23 a31 + a13 a21 a32 - a31 a22 a13 - a32 a23 a11 - a33 a21 a12

etc.

Page 21: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Matrix singularity

The inverse of a square matrix, if exists, was defined to be a square matrix such that A*A -1 = I where I is the identity matrix.

Def. A square matrix is singular if and only if (iff) its determinant is zero, i.e. det(A) = 0.

=> A square matrix A has an inverse iff

det(A) 0.(An important condition to solve simultaneous

equations)

Page 22: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Ex. Solving Simultaneous Equations

A square matrix A has an inverse iff det(A) 0. Ex.

Then C*x=r, where x=

=> Solution: x=C’*r iff det(C) 0.

Page 23: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Ex. Solving Simultaneous Equations

A square matrix A has an inverse iff det(A) 0. Ex.

Then C*x=r, where x= => x=C’*r iff det(C) 0.

Matlab codes:>>C=[3 2 4;2 5 3;7 2 2]>>CI=inv(C)>>r=[5; 17; 11]>>x=CI*r or >>x=C’*r

Page 24: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Today’s Matlab Session

Matlab User Interface Environment Arrays and Matrix Operations Plotting Graphs - X-Y Plots

M-files

Page 25: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Display Format in Matlab

Matlab uses a default format that shows four decimal digits.

Ex. >>A = 51.1 returns A = 51.1000

Page 26: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Matlab ‘plot’ Command

Example: >>x=[1 2 3 4]; >>y=[10 20 20 40] >>plot (x,y,’o’) %This creates an x-y

scatter plot with an ‘o’

marking each point

Page 27: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

X-Y Plots

>>x=[1 2 3 4]; >>y=[10 20 25 35] >>plot (x,y,’o’) %This creates an x-y scatter

plot with an ‘o’ marking each point

>>grid on % This will add a grid over the

plot >>grid off % This will remove a grid.

Page 28: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

X-Y Plots

>>x=[1 2 3 4] >>y=[10 20 25 35] >>plot (x,y,’o’) This does an x-y scatter plot

with an ‘o’ marking each point

If >>plot (x,y) has been used, Matlab would have plotted y vs. x in a line plot.

Page 29: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

X-Y Plots

Another Example: >>x = sin(0:0.1:10); >>y = cos(0:0.1:10); >>plot (x,y)

Starting point

End point

Increment

Page 30: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Line Types for X-Y Plots

Use the following commands for customized plots:

- solid line -- dashed line -. dash-dot line

Page 31: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Line Types for X-Y Plots

Use the following commands for customized plots: - solid line -- dashed line -. dash-dot line

Example: >>x=[1 2 3 4] >>y=[10 20 30 40]

>>plot (x,y,’--’) %This plots an x-y plot with a dashed line

>>plot(x,y,’-.’) %This plots an x-y plot with a dash-dot line

Page 32: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Multiple Plots Use the following commands for customized plots: - solid line; -- dashed line; -. Dash-dot line

Example: >>x=[0:0.1:5]; >>y1=sin(x); >>y2=cos(x); >>plot(x,y1,’--’); >>hold on %This will keep the plot of x

vs. y1

Page 33: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Multiple Plots Use the following commands for customized plots: - solid line; -- dashed line; -. Dash-dot line

Example: >>x=[0:0.1:5]; >>y1=sin(x); >>y2=cos(x); >>plot(x,y1,’--’); >>hold on %This will retain the first plot

>>plot(x,y2,’-.’) % This will plot a cosine curve wit a

dash-dot line >>grid on

Page 34: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

More Examples of X-Y Plots

Example >>y=sin(0:0.4 : pi*4); >>plot (x,y)Note: You may enter y=sin(0 : 0.4 : 4*pi)

Page 35: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Plotting Graphs in Matlab

Title and Label (exercise!) Below is a list of commands you will

need for labeling graphs in Matlab. >>title (‘whatever you want’) %Titles the

graph

>>xlabel (‘the x-axis label’) %Labels the x-axis

>>ylabel (‘the y-axis label’) %Labels the y-axis

Page 36: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Matlab Session

Matlab User Interface Environment Arrays and Matrix Operations Plotting Graphs - X-Y Plots

M-files

Page 37: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

M-files

M-files allow you to store programming codes for later use. M-files are ASCII text files similar to C or FORTRAN source codes.

They are called M-files since the filename has the form filename.m.

M-files can be created from the Edit Window (File New M-file)

Page 38: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Creating an M-file

M-files allow you to store programming codes for later use. M-files are ASCII text files similar to C or FORTRAN source codes.

They are called M-files since the filename has the form filename.m.

Example: First enter the following command sequence in the File Editor Window: File->New->M-file to open the M-file Editor:

Ex. First, enter following commands into the M-file Window: x=[0:0.1:4]; y1=sin(x); y2=cos(x); plot(x,y1) hold on plot(x,y2,’o’)

Page 39: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Saving an M-file

Then, save the script as sin_cos_plot.m:Go to FileSave Workspace as -> name the

file as sin_cos_plot.m and click save: x=[0:0.1:5]; y1=sin(x); y2=cos(x); plot(x,y1,’- -’) hold on plot(x,y2,’o’)

Page 40: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Editing M-files

They are called M-files since the filename has the form filename.m.

Example: sin_cos_plot.m We can save the above M-file using the Save

and Run icon in the File Editor Window as follows:

1. Save as sin_cos_plot.m. 2. Click on the Save and Run icon.

Page 41: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Ex. Plotdata.m

% This is an M-file (or a script) to plot the function

% y=sin(a*t)

%

t=[0:0.1:1]; a=2;

y=sin(a*t)

plot(t,y)

xlabel(‘Time (sec)’)

ylabel(‘y(t)=sin(2t)’)

grid on

Let’s spend 4-5 minutes to work on this, i.e. open File Editor and go to M-File. Type the commands on the left and click on the Save and Run icon. Name the file and run.

Page 42: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Ex. plotdata.m

% This is an M-file (or a script) to plot the function

% y=sin(a*t)

%

t=[0:0.1:1]; a=2;

y=sin(a*t)

plot(t,y)

xlabel(‘Time (sec)’)

ylabel(‘y(t)=sin(2t)’)

grid on

Let’s spend 4-5 minutes to work on this.

Let’s run the m-file:

>> plotdata

Page 43: Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays

Any Questions?