Download presentation
Presentation is loading. Please wait.
Published byAllyson Little Modified over 9 years ago
1
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.
2
Today’s Matlab Session Matlab User Interface Environment Arrays and Matrix Operations Plotting Graphs - X-Y Plots M-files
3
Running Matlab Default Matlab Opening Window looks like this:
4
Opening Window Matlab Opening Window Command window Current (File) Directory Command History Workspace window File Editor
5
Opening Window Matlab Opening Window Command Window allows you to enter Matlab commands. Current Directory Window saves files and data for use.
6
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.
7
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.
8
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.
9
Desktop icon Click Desktop icon to modify display window, Maximize Workspace, add/delete windows.
10
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.
11
Today’s Matlab Session Matlab User Interface Environment Arrays and Matrix Operations Plotting Graphs - X-Y Plots M-files
12
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)
13
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: |a 11 +b 11 a 12 +b 12 | A + B = |a 21 +b 21 a 22 +b 22 | >> A + B
14
Matrix Addition/Subtraction/Multiplication |a 11 +b 11 a 12 +b 12 | A + B = |a 21 +b 21 a 22 +b 22 | Ex.
15
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.
16
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:
17
Matrix Transpose Transpose of a matrix (A T 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
18
Matrix Power/Determinant/Inverse Matrix Power (or Exponentiation) A 2 = A*A, A 3 = A 2 * A = A*A*A, etc. >>A^2 >>A^3 etc.
19
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| = a 11 * a 22 - a 12 * a 21 Ex. >>A=[3,2;5,1] >>det(A) ans = -7
20
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.
21
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)
22
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.
23
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
24
Today’s Matlab Session Matlab User Interface Environment Arrays and Matrix Operations Plotting Graphs - X-Y Plots M-files
25
Display Format in Matlab Matlab uses a default format that shows four decimal digits. Ex. >>A = 51.1 returns A = 51.1000
26
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
27
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.
28
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.
29
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
30
Line Types for X-Y Plots Use the following commands for customized plots: - solid line -- dashed line -. dash-dot line
31
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
32
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
33
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
34
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)
35
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
36
Matlab Session Matlab User Interface Environment Arrays and Matrix Operations Plotting Graphs - X-Y Plots M-files
37
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)
38
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’)
39
Saving an M-file Then, save the script as sin_cos_plot.m: Go to File Save 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’)
40
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.
41
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.
42
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
43
Any Questions?
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.