Beginner s Matlab Tutorial



Similar documents
AMATH 352 Lecture 3 MATLAB Tutorial Starting MATLAB Entering Variables

FIRST STEPS WITH SCILAB

Lecture 2 Mathcad Basics

Introduction to Matlab

Excel Tutorial. Bio 150B Excel Tutorial 1

Years after US Student to Teacher Ratio

2+2 Just type and press enter and the answer comes up ans = 4

MATLAB Functions. function [Out_1,Out_2,,Out_N] = function_name(in_1,in_2,,in_m)

Excel Reports and Macros

How long is the vector? >> length(x) >> d=size(x) % What are the entries in the matrix d?

Excel -- Creating Charts

Microsoft Excel Tutorial

Working with Excel in Origin

Scatter Plots with Error Bars

Importing and Exporting Databases in Oasis montaj

DataPA OpenAnalytics End User Training

Introduction. Chapter 1

FREE FALL. Introduction. Reference Young and Freedman, University Physics, 12 th Edition: Chapter 2, section 2.5

MATLAB Tutorial. Chapter 6. Writing and calling functions

Data representation and analysis in Excel

Dealing with Data in Excel 2010

Tools for Excel Modeling. Introduction to Excel2007 Data Tables and Data Table Exercises

Introduction to Data Tables. Data Table Exercises

WebSphere Business Monitor V7.0 Business space dashboards

Excel Basics By Tom Peters & Laura Spielman

Create a Poster Using Publisher

INTRODUCTION TO DESKTOP PUBLISHING

Summary of important mathematical operations and formulas (from first tutorial):

0 Introduction to Data Analysis Using an Excel Spreadsheet

WebSphere Business Monitor V6.2 Business space dashboards

DERIVATIVES AS MATRICES; CHAIN RULE

Introduction to the TI-Nspire CX

MS Excel Template Building and Mapping for Neat 5

Microsoft Excel 2010 Charts and Graphs

KaleidaGraph Quick Start Guide

Appendix: Tutorial Introduction to MATLAB

The Center for Teaching, Learning, & Technology

Intellect Platform - The Workflow Engine Basic HelpDesk Troubleticket System - A102

What is a piper plot?

Ofgem Carbon Savings Community Obligation (CSCO) Eligibility System

TUTORIAL: BOARDMAKER STUDIO START-UP

Microsoft Access Rollup Procedure for Microsoft Office Click on Blank Database and name it something appropriate.

5.7. Quick Guide to Fusion Pro Schedule

Access II 2007 Workshop

Intermediate PowerPoint

CREATING EXCEL PIVOT TABLES AND PIVOT CHARTS FOR LIBRARY QUESTIONNAIRE RESULTS

Instructions for Formatting APA Style Papers in Microsoft Word 2010

Excel 2003 A Beginners Guide

Introduction to Modern Data Acquisition with LabVIEW and MATLAB. By Matt Hollingsworth

MARS STUDENT IMAGING PROJECT

This activity will show you how to draw graphs of algebraic functions in Excel.

Introduction to Microsoft Excel 2007/2010

Word 2010: Mail Merge to with Attachments

Microsoft Excel 2010 Tutorial

CD-ROM Appendix E: Matlab

Task Force on Technology / EXCEL

OECD.Stat Web Browser User Guide

Hierarchical Clustering Analysis

MetaTrader 4 and MetaEditor

How To Run A Run Dashboard On An Aca.Edu

TakeMySelfie ios App Documentation

Introduction to the use of the environment of Microsoft Visual Studio 2008

(!' ) "' # "*# "!(!' +,

Metatrader 4 Tutorial

In this session, we will explain some of the basics of word processing. 1. Start Microsoft Word 11. Edit the Document cut & move

Workspaces Creating and Opening Pages Creating Ticker Lists Looking up Ticker Symbols Ticker Sync Groups Market Summary Snap Quote Key Statistics

Setting up Auto Import/Export for Version 7

Appendix A How to create a data-sharing lab

Intro to Excel spreadsheets

Getting Started with R and RStudio 1

Spreadsheet. Parts of a Spreadsheet. Entry Bar

UCL Depthmap 7: Data Analysis

Tutorial 2: Reading and Manipulating Files Jason Pienaar and Tom Miller

How To Use Spss

Creating Bar Charts and Pie Charts Excel 2010 Tutorial (small revisions 1/20/14)

Excel 2007 A Beginners Guide

A simple three dimensional Column bar chart can be produced from the following example spreadsheet. Note that cell A1 is left blank.

Spreadsheets and Laboratory Data Analysis: Excel 2003 Version (Excel 2007 is only slightly different)

GUI Input and Output. Greg Reese, Ph.D Research Computing Support Group Academic Technology Services Miami University

Importing TSM Data into Microsoft Excel using Microsoft Query

Microsoft Access 2010 Overview of Basics

Microsoft Office PowerPoint Creating a new presentation from a design template. Creating a new presentation from a design template

Chapter 4 Displaying and Describing Categorical Data

SAP Business Intelligence (BI) Reporting Training for MM. General Navigation. Rick Heckman PASSHE 1/31/2012

edgebooks Quick Start Guide 4

DATA VISUALIZATION WITH TABLEAU PUBLIC. (Data for this tutorial at

ORACLE BUSINESS INTELLIGENCE WORKSHOP

Simple Programming in MATLAB. Plotting a graph using MATLAB involves three steps:

SQL Server 2005: Report Builder

CNCTRAIN OVERVIEW CNC Simulation Systems

Topography of an Origin Project and Workspace

Tutorial Program. 1. Basics

The following is an overview of lessons included in the tutorial.

Getting Started with Barchart Professional

MATLAB 7 Creating Graphical User Interfaces

Oracle Data Miner (Extension of SQL Developer 4.0)

Using an Access Database

How to create and personalize a PDF portfolio

Assignment 09. Problem statement : Write a Embedded C program to switch-on/switch-off LED.

MicroStrategy Desktop

Transcription:

Christopher Lum lum@u.washington.edu Introduction Beginner s Matlab Tutorial This document is designed to act as a tutorial for an individual who has had no prior experience with Matlab. For any questions or concerns, please contact Christopher Lum lum@u.washington.edu Starting the Program 1. Start Matlab. After the program starts, you should see something similar to that shown in Figure 1 (the actual display may vary depending on the version of Matlab you have installed). Christopher W. Lum lum@u.washington.edu Page 1/16

Figure 1: Basic Matlab interface showing only Command Window 2. If your window does not appear like this, it is possible that different windows are currently activated. Let us change the appearance and activate some useful windows. First, we ll start a new.m file. To do this use File > New > Script Or File > New > M-File 3. This starts a new M-file which can be edited (more on this later). This probably opens the editor in a new window as shown below in Figure 2. Christopher W. Lum lum@u.washington.edu Page 2/16

Figure 2: Screenshot of new m-file editor in new window 4. We would like to be able to see both the editor and the Command Window at the same time. Go back to the m-file editor and select Desktop > Dock Editor This will attach the m-file editor to the Command Window 5. We would also like to activate the Workspace window. To do this, go to the Command window and select Desktop > Workspace This will activate the Workspace window. 6. You can now drag around the 3 activated windows (Command Window, m-file editor, and Workspace) to arrange the views as you like. To drag a window, simply click on the window and then drag the blue bar (see Figure 3). The Matlab interface should now similar to Figure 3. Christopher W. Lum lum@u.washington.edu Page 3/16

Figure 3: MATLAB interface Christopher W. Lum lum@u.washington.edu Page 4/16

Using Matlab 1. Matlab stores most of its numerical results as matrices. Unlike some languages (C, C++, C#), it dynamically allocates memory to store variables. Therefore, it is not necessary to declare variables before using them. Let s begin by simply adding two numbers. Click in the Command Window. You will see a flashing symbols next to the >> symbol. Enter the following commands 1. Type in x = 3 then hit enter 2. Type in y = 2; then hit enter (note the semicolon here!) 3. Type z = x + y then hit enter Figure 4: Entering in scalar values into Matlab All declared variables appear in the workspace. Recall that these values are stored as matrices. The size column tells us the dimension of the matrix. As expected, all these variables are 1x1 scalar values. To double check on value stored in this matrix, simply double click any of the variables in the Workspace. 2. Now, let s assume that x and y are actually components of a 2D vector. Let s x construct the vector v =. Note that we are making a column vector of size y Christopher W. Lum lum@u.washington.edu Page 5/16

2x1. We use the [ to denote the start of a matrix and ] to denote the end of the matrix. The command to construct the vector is shown below Also notice that in the workspace, the variable v is of size 2x1 as expected. 3. In a similar fashion, if we want to create a horizontal vector, we use the space instead of the ; to separate elements. For example Once again notice that the variable p is created in the workspace and it is of size 1x2 as expected 4. We can create a 2D matrix in a similar fashion. You can use the [ to start the matrix, type in the first row with spaces in between elements, use a ; to start the next row, and then repeat. Finally close the matrix with a ]. For example, to create a 3x2 matrix, we can use syntax like Notice that we used the variable p as the last row. This is possible since the dimensions match. 5. Matlab treats most variables as matrices, and therefore operations like addition, multiplication, etc. must be done with matrices whose dimensions are consistent. For example, we could enter Christopher W. Lum lum@u.washington.edu Page 6/16

Note: Try multiplying v on the left of A (ie v*a) and see what happens. This is not allowed because the dimensions are not consistent. This will become important later. 6. Now, let s assume that we wanted to change the value of x. This would tedious to retype in all the commands again, so let s use the m-file to avoid retyping in all the commands each time we make a change. The m-file is like a source file which will run all your commands in a top to bottom fashion. You can use the % sign to comment out lines. A sample m-file is shown below in Figure 5. Figure 5: Example m-file for code so far 7. You can now run this file by hitting the Run button or hitting F5. At this point, Matlab will force you to save you project. Navigate to your desired directory and Christopher W. Lum lum@u.washington.edu Page 7/16

save the m-file. After you hit save, you will be prompted with a warning message as shown in Figure 6. Figure 6: Current directory warning This warning appears because the m-file that you are trying to run is not located in the current working directory. Therefore, you should select the first option (Change Matlab current directory) and hit OK. 2D Plotting with Matlab Matlab has powerful plotting functions which make visualizing functions easy. Let s consider the function f 1 ( x) = 3x + 4 1. If you were to plot this by hand on graph paper, you would probably follow a procedure such as a. Choose a series of x values where you would like to evaluate the function. b. Compute the corresponding y value at each of these x values. c. Plot each x,y pair. d. Connect the points with a straight line. Christopher W. Lum lum@u.washington.edu Page 8/16

Table 1: Table of coordinates x y = f(x) -1 3*(-1) + 4 = 1 0 3*(0) + 4 = 4 1 3*(1) + 4 = 7 2 3*(2) + 4 = 10 Etc. Etc. Figure 7: Plotting coordinates by hand We can first create a vector which represents the x values where we would like to evaluate the function. Syntax in you m-file might look like 2. We can now evaluate the function at these x values. Syntax in your m-file might look like 3. Now that we have two vectors (one representing the x values and the other representing the y values), we can plot the function to see what it looks like. To do this we will use the plot function. To obtain help about any of Matlab s functions, simply type in help name_of_desired_function into the Command window. For example for help on the plot command, you would type help plot in the command window. Syntax in your m-file might look like The resulting plot is shown in Figure 8. Christopher W. Lum lum@u.washington.edu Page 9/16

Figure 8: Plot of function 4. We can label the axis, add a title, turn on a grid, put a title and legend on the plot, and force the axis to a set range. Are more complete plotting syntax in your m- file might look like. The resulting plot is shown in Figure 9 Christopher W. Lum lum@u.washington.edu Page 10/16

Figure 9: Figure with added options 5. You can export this figure so that it may be easily included into other documents. To do this, go to the figure and select File > Save As (choose export file type the hit Save ) The resulting figure is shown in Figure 10. Christopher W. Lum lum@u.washington.edu Page 11/16

Figure 10: Plot of function f1 after being saved as a.jpg Let s now consider a second function of the form f ( t) = g 2 ( t) h( t) Where 2 g ( t) = 3t h ( t) = sin 4t + 4 ( ) 1. Once again, the first thing we need to do is create a list of t values where we would like to evaluate the function. Instead of manually typing in values, we can use the syntax as shown below This says create a 1 dimensional matrix (a vector) starting from 0, ending at 5, with increments of 1. In other words, the variable t should look like (note that it is a 1 x 6 matrix) t 0 1 2 3 4 5 Christopher W. Lum lum@u.washington.edu Page 12/16

2. Let s now create a variable gt which is tempted to try syntax such as 2 g ( t) = 3t. At first, you might be This will cause an error. To understand why, recall that Matlab treats most variables as matrices. Also, the command t^2 is interpreted as t*t. If you recall, t is a 1 x 6 matrix. You cannot multiply a 1 x 6 matrix with a 1 x 6 matrix. This is what causes the error. In reality, what you would like to do is t 2 0 2 1 2 2 2 3 2 4 2 5 2 In other words, you would like to apply the square operation to each element of the matrix individually. This is known as an element-wise operation. The syntax to perform this is The. In front of the operator denotes an element-wise operation 3. We can create ( t) = sin( 4t + 4) h using the syntax Note here that the term 4*t is a 1 x 6 matrix and the 4 is simply a 1 x 1 scalar. In this case, when adding a 1x1 to any other sized matrix, it will automatically apply an element-wise operation. This is why you do not need to use a.+ to perform the addition. The same goes for scalar multiplication (multiplying a 1 x 1 matrix into an m x n matrix). 4. We can finally evaluate f ( t) = g( t) h( t) 2 using syntax of Notice that we need to use element-wise multiplication.* because gt is a 1x6 and ht is a 1x6 matrix. 5. Once again, we can plot this function. To illustrate how the plotting operates, let s plot the x,y points as large red x marks. Syntax to do this is shown below Christopher W. Lum lum@u.washington.edu Page 13/16

The output is shown below in Figure 11. Figure 11: Plot of function f2(t) using only 6 points 6. As can be seen in Figure 11, this is not a very fine resolution representation of the function. To fix this, we can go back to where we defined the variable t and choose a smaller step size. For example change to yields the plot shown below in Figure 12. Christopher W. Lum lum@u.washington.edu Page 14/16

Figure 12: Function f2 after decreasing step size in t definition A collection of m-file syntax for this section is shown in Figure 13. Christopher W. Lum lum@u.washington.edu Page 15/16

Figure 13: Complete m-file for 2D Plotting with Matlab section Version History: 09/14/04: Created: 11/23/05: Updated: Made this format match other to-do documents and removed references to AA547. 12/01/05: Updated: Changed headers to match how-to template 12/09/05: Updated: Made changes to layout and added footer. 09/15/09: Updated: Minor changes 09/25/10: Updated: Made major changes to make it more readable and understandable. Changed content significantly. 04/12/12: Updated: Updated some screenshots for new Matlab version. Christopher W. Lum lum@u.washington.edu Page 16/16