MATLAB TUTORIAL WORKSHEET

Similar documents
EGR 111 Introduction to MATLAB

ELEC4042 Signal Processing 2 MATLAB Review (prepared by A/Prof Ambikairajah)

Course Layout. Go to follow instr. Accessible within campus (only for the first download)

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

MATLAB Basics. Configure a MATLAB Package 6/7/2017. Stanley Liang, PhD York University. Get a MATLAB Student License on Matworks

Constraint-based Metabolic Reconstructions & Analysis H. Scott Hinton. Matlab Tutorial. Lesson: Matlab Tutorial

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

EE 301 Signals & Systems I MATLAB Tutorial with Questions

OUTLINES. Variable names in MATLAB. Matrices, Vectors and Scalar. Entering a vector Colon operator ( : ) Mathematical operations on vectors.

APPM 2460 Matlab Basics

Matlab Tutorial. The value assigned to a variable can be checked by simply typing in the variable name:

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline (Cont d) MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An

Introduction to MATLAB LAB 1

MATLAB Project: Getting Started with MATLAB

MATLAB Project: Getting Started with MATLAB

MATLAB for beginners. KiJung Yoon, 1. 1 Center for Learning and Memory, University of Texas at Austin, Austin, TX 78712, USA

AMATH 352: MATLAB Tutorial written by Peter Blossey Department of Applied Mathematics University of Washington Seattle, WA

Outline. CSE 1570 Interacting with MATLAB. Starting MATLAB. Outline. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

McTutorial: A MATLAB Tutorial

An Introduction to MATLAB

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

MATLAB Programming for Numerical Computation Dr. Niket Kaisare Department Of Chemical Engineering Indian Institute of Technology, Madras

Matlab and Octave: Quick Introduction and Examples 1 Basics

Finding, Starting and Using Matlab

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

Getting started with MATLAB

Stokes Modelling Workshop

AMS 27L LAB #2 Winter 2009

Introduction to Matlab. By: Hossein Hamooni Fall 2014

To start using Matlab, you only need be concerned with the command window for now.

Outline. CSE 1570 Interacting with MATLAB. Outline. Starting MATLAB. MATLAB Windows. MATLAB Desktop Window. Instructor: Aijun An.

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

Colorado State University Department of Mechanical Engineering. MECH Laboratory Exercise #1 Introduction to MATLAB

University of Alberta

Lab of COMP 406. MATLAB: Quick Start. Lab tutor : Gene Yu Zhao Mailbox: or Lab 1: 11th Sep, 2013

Matlab Tutorial and Exercises for COMP61021

Laboratory 1 Octave Tutorial

This is a basic tutorial for the MATLAB program which is a high-performance language for technical computing for platforms:

Getting started with MATLAB

1 Introduction to Matlab

Starting Matlab. MATLAB Laboratory 09/09/10 Lecture. Command Window. Drives/Directories. Go to.

An Introduction to MATLAB II

Introduction to MATLAB

Introduction to MATLAB

Finding MATLAB on CAEDM Computers

How to learn MATLAB? Some predefined variables

Matlab Tutorial. Get familiar with MATLAB by using tutorials and demos found in MATLAB. You can click Start MATLAB Demos to start the help screen.

Introduction to Matlab

Introduction to Matlab

Introduction to MATLAB

6.094 Introduction to MATLAB January (IAP) 2009

MATLAB Tutorial III Variables, Files, Advanced Plotting

FreeMat Tutorial. 3x + 4y 2z = 5 2x 5y + z = 8 x x + 3y = -1 xx

Matlab notes Matlab is a matrix-based, high-performance language for technical computing It integrates computation, visualisation and programming usin

Computer Vision. Matlab

PHYS 210: Introduction to Computational Physics Octave/MATLAB Exercises 1

PHYS 210: Introduction to Computational Physics MATLAB Exercises 2

Dr Richard Greenaway

Computer Project: Getting Started with MATLAB

Introduction to MATLAB

AN INTRODUCTION TO MATLAB

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

MATLAB Tutorial EE351M DSP. Created: Thursday Jan 25, 2007 Rayyan Jaber. Modified by: Kitaek Bae. Outline

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

Matlab Tutorial for COMP24111 (includes exercise 1)

LECTURE 1. What Is Matlab? Matlab Windows. Help

Introduction to MATLAB

Dr Richard Greenaway

Introduction to Matlab

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

Introduction to Programming for Biology Research

Physics 326G Winter Class 2. In this class you will learn how to define and work with arrays or vectors.

Summer 2009 REU: Introduction to Matlab

1 Overview of the standard Matlab syntax

Introduction to Matlab. High-Level Computer Vision Summer Semester 2015

Introduction to Matlab

Introduction to MATLAB programming: Fundamentals

Physics 326 Matlab Primer. A Matlab Primer. See the file basics.m, which contains much of the following.

MATLAB BASICS. < Any system: Enter quit at Matlab prompt < PC/Windows: Close command window < To interrupt execution: Enter Ctrl-c.

Introduction to MATLAB

You just told Matlab to create two strings of letters 'I have no idea what I m doing' and to name those strings str1 and str2.

What is MATLAB and howtostart it up?

Computational Photonics, Summer Term 2012, Abbe School of Photonics, FSU Jena, Prof. Thomas Pertsch

Matlab (Matrix laboratory) is an interactive software system for numerical computations and graphics.

Mathworks (company that releases Matlab ) documentation website is:

Unix Computer To open MATLAB on a Unix computer, click on K-Menu >> Caedm Local Apps >> MATLAB.

An Introduction to Numerical Methods

ENGR 1181 MATLAB 02: Array Creation

Time-Saving Hint: Use the up-arrow on your keyboard to cycle through old commands.

Introduction to Matlab

ENGR 1181 MATLAB 05: Input and Output

Interactive Computing with Matlab. Gerald W. Recktenwald Department of Mechanical Engineering Portland State University

EOSC 352 MATLAB Review

A = [1, 6; 78, 9] Note: everything is case-sensitive, so a and A are different. One enters the above matrix as

Grace days can not be used for this assignment

Experiment 0. Matlab and LabJack Programming Exercise. I. Purpose. II. Equipment. III. Introduction. IV. Experiment PART A: Introduction to Matlab

A/D Converter. Sampling. Figure 1.1: Block Diagram of a DSP System

Transcription:

MATLAB TUTORIAL WORKSHEET What is MATLAB? Software package used for computation High-level programming language with easy to use interactive environment Access MATLAB at Tufts here: https://it.tufts.edu/sw-matlabstudent Outline Windows in MATLAB - discuss the layout Getting Started - get acquainted with the command window by using MATLAB as a calculator Creating Vectors and Matrices - learn how to create vectors and matrices easily For Loops - discuss what a for loop is and the notation in MATLAB If Statements - discuss what an if statement is and the notation in MATLAB How to use a function with multiple inputs and outputs - discuss functions We will do some exercises at the to practice the material we have covered today! Notes This is meant to be interactive, so please ask questions! There are many things we will not have time to cover today, but we hope we are giving you a foundation of the basics to build upon. There are additional resources posted on the course website and I will be holding weekly office hours to assist with the programming component of the course! My Office Hours: Tuesdays from 4:30-5:30 in Bromfield-Pearson Office 216 1

Windows in MATLAB Command Window where you enter data, run MATLAB code, and display results Command History displays a log of the statements run in the Command Window Workspace displays current variables for use in the Command Window Editor where you can write your own functions and scripts 2

Getting Started In Command Window type, >> 2+2 <enter> >> a = 2 <enter> >> a = 3; <enter> >> a <enter> >> b = 4; <enter> >> a+b <enter> >> a-b <enter> >> a*b <enter> >> sin(0) <enter> We have learned that the semicolon at the of a statement suppresses the output. Now, let s say you don t know what the sin function does, >> help sin % Prints info about the function in the Command Window >> doc sin % Opens a new window with info about the function What does the % mean? It is the command in MATLAB that means comment. Let s check in with our workspace and see which variables we have assigned. >> help who >> who We have a, ans, and b in our workspace. If we are done with these variables, we can get rid of them by typing, >> clear a % This clears just a from your workspace >> clear % This clears all the remaining variables in our workspace >> clc % This clears the Command Window screen, not the variables Again, you can always type help (function name) if you want more details about these functions. 3

Creating Vectors and Matrices To enter vectors use square brackets To create a row vector separate elements with commas or spaces To create a column vector separate elements with semicolons The transpose of a vector (or matrix) is expressed using an apostrophe >> x = [1,2,3] >> x = [1 2 3] >> y = [4;5;6] >> x = x' You can create a row vector by indicating a starting value, an increment, and an ing value, all separated by a colon >> z = 1:1:10 >> w = 1:0.5:5 >> u = [1:1:10]' % If you want a column vector instead You create matrices in a similar way >> A = [1 2 3;4 5 6;7 8 9] >> B = [x y] >> C = [x';y'] Let s explore extracting different elements of a matrix >> A(1,1) % Extracts the element in the (1,1) position >> A(2,:) % Extracts the second row of the matrix >> A(:,2) % Extracts the second column of the matrix A note about multiplication: You should be aware that does multiplication and matrix multiplication, while. does element-wise multiplication. 5 min Break Use this time to catch up and play around with what we have learned so far We will be walking around to assist and answer any questions 4

for Loops First, let s clear our workspace: >> clear >> clc Now, let s create a row vector with 5 elements. >> x = 1:0.5:3; Say we want to add 1 to every element in our vector. How should we do this? >> x(1) = x(1) + 1; >> x(2) = x(2) + 1; >> x(3) = x(3) + 1; >> x(4) = x(4) + 1; >> x(5) = x(5) + 1; >> x This is very tedious and imagine if you had a much larger vector, doing this by hand would be very annoying. How can we do this more efficiently? Option 1: >> x = 1:0.5:3; >> x = 2:0.5:4 But what if you weren t just adding one, this might not be feasible. Option 2: >> x = 1:0.5:3; >> x = x+2 This is probably the easiest option, but let s use a for loop for learning purposes! Option 3: >> x = 1:0.5:3; >> for i = 1:5 x(i) = x(i) + 1; >> x This is called a for loop. Let s read the help documentation! >> help for The documentation tells us that for loops repeat statements a specific number of times. 5

if Statements Once again let s clear our workspace. >> clear >> clc Let s make another vector with 5 elements. >> x = [1 0 1 0 1]; How would we update the vector, so that the elements that are 1 become 3 and the elements that are 0 become 1? We could do this by hand or just build a new matrix, but again if we were doing something more complicated this would not be feasible. Solution: Use a for loop and an if statement! >> for i = 1:5 if x(i) == 1 x(i) = x(i) + 2; elseif x(i) == 0 x(i) = x(i) - 1; >> x We used the for loop just like we did above, but we added an if statement. We will now look at the documentation for if statements. >> help if 5 min Break Use this time to catch up and play around with what we have learned so far We will be walking around to assist and answer any questions 6

How to use a function with inputs and outputs MATLAB has many built-in functions that will help you do what you want. For example, MATLAB has functions eye, ones, zeros that enable you to create an identity matrix, ones array and zeros array. We will look at eye in more detail. >> clear >> clc >> help eye We can see that there are a lot of different ways to use eye, but let s just look at the first two options. >> eye(3) % This will give us a 3-by-3 identity matrix and stores it in ans >> I = eye(3) % This will do the same thing, but saves it in I instead of ans >> I = eye(3,3) % This does the same exact thing as above >> I = eye(3,4) % This gives us a 3-by-4 identity matrix The 3 and 4 are called inputs and I is an output. If a function has more than one output the notation is [output1,output2] = function name(input1,input2) Here are a few notes about writing your own function: Use the editor to write your function Save your function as a.m file (m-file) See the example of a function above to see the notation In order to use your function in the Command Window, your current directory must contain the function m-file (you can type pwd in the Command Window to see what your current directory is) If your function isn t working exactly how you want and you can t seem to figure out why, use the debugging feature in MATLAB! This allows you to step through the code one line at a time to see what is really happening. If you want to stop any function from running to completion, hit Ctrl+C 7

Exercise 1 Plot sin(x) on the interval [ pi, pi] using 0.01 spacing. Exercise 2 Write a function that accepts a vector of any size as input, adds 2 to every element greater than 5 and adds 1 to every element less than or equal to 5 and outputs the updated vector. Some helpful hints: Remember to use the Editor to write your function Check out the MATLAB functions length or size 8

Exercise 1 Solution x = -pi:0.01:pi; plot(x,sin(x)) Exercise 2 Solution In the Editor write the function and save it as a.m file. Use the function in the Command Window. function x = addtwoaddone(x) % addtwoaddone adds two to each element of a vector that is greater than 5 and % adds one to every element that is less than or equal to 5. n = length(x); for i = 1:n if x(i) > 5 x(i) = x(i) + 2; else x(i) = x(i) + 1; 9