Introduction to Matlab Matlab is a software package for technical computation. Matlab allows you...

Post on 18-Jan-2016

225 views 0 download

Tags:

transcript

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.

Today’s Matlab Session

Matlab User Interface Environment

Arrays and Matrix Operations Plotting Graphs - X-Y Plots

M-files

Running Matlab

Default Matlab Opening Window looks like this:

Opening Window Matlab Opening Window

Command window Current (File) Directory

Command History

Workspacewindow

File Editor

Opening Window

Matlab Opening Window Command Window

allows you to enter Matlab commands.

Current Directory Window saves files and data for use.

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.

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.

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.

Desktop icon

Click Desktop icon to modify display window,

Maximize Workspace, add/delete windows .

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.

Today’s Matlab Session

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

M-files

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)

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

Matrix Addition/Subtraction/Multiplication

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

Ex.

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.

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:

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

Matrix Power/Determinant/Inverse

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

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

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.

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)

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.

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

Today’s Matlab Session

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

M-files

Display Format in Matlab

Matlab uses a default format that shows four decimal digits.

Ex. >>A = 51.1 returns A = 51.1000

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

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.

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.

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

Line Types for X-Y Plots

Use the following commands for customized plots:

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

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

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

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

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)

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

Matlab Session

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

M-files

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)

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’)

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’)

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.

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.

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

Any Questions?