MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB...
Transcript of MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB...
![Page 1: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/1.jpg)
Introduction to MATLAB
![Page 2: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/2.jpg)
What you will learn• The most important commands (from my view)
• Writing scripts (.m files)• Defining MATLAB variables• Matrix/array operations• Importing and extracting data• 2D plots• Intro to statistic toolbox
![Page 3: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/3.jpg)
• Command window: enter the command/instruction• Current folder: directory where we save our work (click “browse” button if you want to
change)• Workspace: Where our data is stored in MATLAB during the current session (will be removed
if we close our current session)• Command history: record commands entered in the command window
![Page 4: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/4.jpg)
Scripts• Script: A sequence of commands• Use % to add comments for explanation/hints• Save as an .m file (save before we run it, no space in the file name)
• Demo: Display the text ‘finding nemo’ (use the command ‘disp’ to display strings)
• Demo: x=5; y=x^3; z=x+y; calculate z
![Page 5: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/5.jpg)
• What we do if we are stuck:• Use “help”! (The blue question mark on the top row)
• Use the command “doc” to get detailed information about the command.
![Page 6: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/6.jpg)
Variables• Variables are places to store data• To create a variable, simply assign a value (on the right) to a name
(on the left):• A=3.14• B=‘nemo’• First character of a variable’s name must be a LETTER; after that,
any combination of letters, numbers and _• CASE SENSITIVE! (Nemo is different from nemo) Some names for variables are reserved:
![Page 7: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/7.jpg)
Do it together• Write a script with three MATLAB variables• a=3.14;• b='nemo';• c=5*2+1• Save the script and the variables• Script will show up in the current folder as .m; variables as .mat
• How about do the following commands:• 1+1=a,• a=b=1
![Page 8: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/8.jpg)
Vectors as MATLAB variables
x=1:1:5y=linspace(1,5,5)
Use “;” to suppress output line (save space!) Imagine if you are writing x=1:0.1:1000, what would happen if you don’t put the “;”
![Page 9: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/9.jpg)
Matrices as MATLAB variables
Let’s create a 2*3 matrix:1 3 52 4 6
You can also use “:” to do the matrix above
![Page 10: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/10.jpg)
Matrix operation vs. Array operation• Matrix operation (standard
mathematical way):• Inner dimension must
agree (number of columns of A=number of rows in B)
• Array operation: element‐wise• Two matrices: same shape and size• Use dot (.) before the operator• .* ./ .^
![Page 11: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/11.jpg)
Let’s try two types operationsFirst, create matrices A and B.
![Page 12: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/12.jpg)
Creating variables by importing external data (e.g., text file, spreadsheet)
• Use Import Wizard• 1. Select file to be imported in the current folder. Right click on it and select ‘import data’.
• 2. If we do that correctly, it will show up in the workspace window.
Instead of using Import Wizard, you can also import data programmatically by using the command “import”. Use ‘doc’ for details
![Page 13: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/13.jpg)
Do the “cherry picking”• Extract data:1. Double click on the variable (large dataset) in the workspace window2. Highlight the area to extract3. Right click and select ‘create variable from selection’4. Rename the new variable5. Right click on the variable on the workspace and save it to the current folder • Example: Seattle and Houston rainfall data
![Page 14: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/14.jpg)
Plot• Method 1: use command “plot” • Use ‘doc plot’ to see the syntax of plot• Method 2: use interactive plotting tool. Hold control and select the variables in work space, then click on the “plot” button
• Then use “plottools” command for plot editing
![Page 15: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/15.jpg)
Statistics toolbox• Mean and standard deviation command• m = mean(X); s = std(X)• pH in Lake Washington example• Use “cdfplot” to display the plot of the cumulative distribution function (cdf) for the data
• Use “dfittool” to fit the data
![Page 16: MATLAB mini lecture Sep 28 - faculty.washington.edufaculty.washington.edu/markbenj/CEE357/MATLAB mini lecture_post… · Creating variables by importing external data (e.g., text](https://reader034.fdocuments.net/reader034/viewer/2022042307/5ed3246dced549277b3741d0/html5/thumbnails/16.jpg)
• Enjoy your homework