STAT/MATH 395 A - PROBABILITY II UW Winter Quarter Matlab Tutorial

Similar documents
EE 350. Continuous-Time Linear Systems. Recitation 1. 1

A Brief Introduction to MATLAB

Stokes Modelling Workshop

1 Introduction to Matlab

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Introduction to MATLAB Programming

Laboratory 1 Octave Tutorial

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

A Guide to Using Some Basic MATLAB Functions

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

Complex Dynamic Systems

EGR 111 Introduction to MATLAB

2.0 MATLAB Fundamentals

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

An Introductory Tutorial on Matlab

Introduction to MatLab. Introduction to MatLab K. Craig 1

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Introduction to Matlab

Introduction to Matlab

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

MATLAB Fundamentals. Berlin Chen Department of Computer Science & Information Engineering National Taiwan Normal University

Introduction to MATLAB LAB 1

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

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

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

Introduction to MATLAB

Eric W. Hansen. The basic data type is a matrix This is the basic paradigm for computation with MATLAB, and the key to its power. Here s an example:

University of Alberta

What is MATLAB and howtostart it up?

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

Experiment 1: Introduction to MATLAB I. Introduction. 1.1 Objectives and Expectations: 1.2 What is MATLAB?

1. Register an account on: using your Oxford address

Digital Image Analysis and Processing CPE

PART 1 PROGRAMMING WITH MATHLAB

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

EE3TP4: Signals and Systems Lab 1: Introduction to Matlab Tim Davidson Ext Objective. Report. Introduction to Matlab

Chapter 2. MATLAB Fundamentals

ME 121 MATLAB Lesson 01 Introduction to MATLAB

Introduction to Engineering gii

Introduction to MATLAB Practical 1

An Introduction to Numerical Methods

Dr Richard Greenaway

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

Lab. Manual. Practical Special Topics (Matlab Programming) (EngE416) Prepared By Dr. Emad Saeid

CSE/Math 485 Matlab Tutorial and Demo

Introduction in MATLAB (TSRT04)

PowerPoints organized by Dr. Michael R. Gustafson II, Duke University

ARRAY VARIABLES (ROW VECTORS)

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

Programming in Mathematics. Mili I. Shah

Assignment 02 (Due: Monday, February 1, 2016)

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

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

Introduction to MATLAB

Numerical Methods in Engineering Sciences

MATLAB Tutorial. Primary Author: Shoumik Chatterjee Secondary Author: Dr. Chuan Li

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.

CSE/Math 456 and CSE/Math 550 Matlab Tutorial and Demo

Chapter 1 Introduction to MATLAB

EGR 102 Introduction to Engineering Modeling. Lab 05A Managing Data

How to learn MATLAB? Some predefined variables

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

AMS 27L LAB #2 Winter 2009

Introduction to Matlab

Introduction to MATLAB

Some elements for Matlab programming

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

Creates a 1 X 1 matrix (scalar) with a value of 1 in the column 1, row 1 position and prints the matrix aaa in the command window.

Desktop Command window

Grace days can not be used for this assignment

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

Matlab Tutorial and Exercises for COMP61021

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

Summer 2009 REU: Introduction to Matlab

Matlab Tutorial: Basics

Signals and Systems Profs. Byron Yu and Pulkit Grover Fall Homework 1

Matlab Tutorial for COMP24111 (includes exercise 1)

ELEN E3084: Signals and Systems Lab Lab II: Introduction to Matlab (Part II) and Elementary Signals

Introduction to Matlab

MATLAB Premier. Middle East Technical University Department of Mechanical Engineering ME 304 1/50

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

Introduction to Matlab

Introduction to Matlab

Introduction to GNU-Octave

A very brief Matlab introduction

The Department of Engineering Science The University of Auckland Welcome to ENGGEN 131 Engineering Computation and Software Development

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

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

CME 192: Introduction to Matlab

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

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group

AN INTRODUCTION TO MATLAB

MATLAB SUMMARY FOR MATH2070/2970

Matlab and Vectors. next page. close. exit. Math 45 Linear Algebra. David Arnold.

Introduction to MATLAB programming: Fundamentals

Introduction to MATLAB

Transcription:

STAT/MATH 395 A - PROBABILITY II UW Winter Quarter 2016 Néhémy Lim Matlab Tutorial 1 Introduction Matlab (standing for matrix laboratory) is a high-level programming language and interactive environment for numerical computation, visualization and programming. Matlab is developed by MathWorks. It allows matrix manipulations; plotting of functions and data; implementation of algorithms; creation of user interfaces; interfacing with programs written in other languages, including C, C++, Java, and FORTRAN; analyze data; develop algorithms; and create models and applications. It has numerous built-in commands and math functions that help you in mathematical calculations, generating plots, and performing numerical methods. 2 How to Connect to the Virtual Lab? The Department of Statistics Virtual Lab provides access to a dedicated, temporary virtual machine. 2.1 Installation Go to: https://horizon.stat.washington.edu and sign in with your NETID username and password (make sure to select the NETID domain from the login box). The first time you visit the site, you will be prompted to download a client utility to connect to the lab. Installation instructions are provided. Once the client utility has launched connect to https://horizon.stat.washington.edu: Linux users. You need to download the VMware View Client software. There is a package available in the Debian package repository. 2.2 Access to Network storage Since the virtual machines are refreshed with each use, it is important to store any data you want to save in a network location, for instance, a repository in your personal U Drive. Follow this link if you need more information on how to access your U Drive : Access U Drive 1

3 Starting Matlab Start Matlab and specify a repository in your U Drive where all your work will be stored. Interactive mode. In the Command Window area, commands can be entered at the command line. Perform the following operations. >> sqrt(4) >> sqrt(4); >> 6.0e-2*cos(pi/4) Command help displays the help text for a Matlab function. The percent symbol % is used for indicating a comment line. >> help sqrt; % getting help on function sqrt Script. Matlab script files are program files with.m extension. In these files, you write series of commands, which you want to execute together. Scripts do not accept inputs and do not return any outputs. After creating and saving the file, you can run it by typing the filename (without extension) in the Command Window. Example. Choose NEW > Script. Type the following code (or copy here : burgers script.m) : nb burgers = 2; burger price = 10; fries price = 2.5; total = nb burgers*burger price + fries price Name and save the file. Type burgers script in the Command Window and see the result. 4 Data manipulation Matlab views most data as matrices. The table below summarizes the main types of variables that you will use in Matlab. Type of variables Matlab representation Example scalar 1 1 matrix toto = 1 n-long row vector 1 n matrix titi = [1 2] m-long column vector m 1 matrix titi = [1; 2] matrix with m rows and n columns m n matrix tutu = [1 2 3; 4 5 6] 2

4.1 Variables Workspace variables. Routine whos displays all variables in the currently active workspace, with information about their sizes and types. You can also use length and size functions on a specific variable. >> whos >> length(toto) >> size(toto) >> size(tutu) Predefined variables. Matlab has a number of built-in variables, such as pi for π or complex numbers i or j. Avoid using them to name your variables. >> pi >> i >> j Math functions. It is possible to apply many common math functions to variables (scalars, vectors or matrices). These include sin, cos, tan, exp, sqrt, power, log, abs. Remark. Some functions may have unexpected behaviors according to the types of variables. For instance, if M is a square matrix, power(m, 2) squares each element of M while M*M or Mˆ2 performs matrix multiplication. Therefore, use the help function to make sure what operation you are performing. 4.2 Special vectors and matrices Evenly spaced vectors. Matlab allows to generate regularly spaced row vectors. (a) Create a regularly spaced row vector with first value 1 and last value 11, with increment 2. >> help : (b) Create a row vector of 7 evenly spaced values with first value 1 and last value 11. >> help linspace Particular matrices. There are several ways to initialize matrices with particular values. (a) Create a row vector of size 1 3 full of zeros. >> help zeros (b) Create a matrix of size 2 3 full of ones. >> help ones (c) Create an identity matrix of size 2 2. 3

>> help eye Concatenating matrices. You can concatenate two matrices to create a larger matrix. The pair of square brackets [] is the concatenation operator. Matlab allows two types of concatenations: 1. When you concatenate two matrices by separating those with a space, they are just appended horizontally. It is called horizontal concatenation. 2. If you concatenate two matrices by separating those using semicolons, they are appended vertically. It is called vertical concatenation. Example. >> M = [1 2; 3 4] >> v1 = [5 6] >> v2 = [7; 8] (a) Create a matrix M1 as the vertical concatenation of M and v1. (b) Create a matrix M2 as the horizontal concatenation of M and v2. 4.3 Manipulating vectors and matrices Access to the elements of a matrix. Execute the following commands and learn how to access different parts of a matrix. >> v1(2) >> M(2, 1) >> M(1, :) >> M(:, 2) >> M2(1:2, 2:3) Commonly used operators. Matlab supports the following commonly used operators for matrices. Operator Purpose Example ˆ Scalar and matrix exponentiation Mˆ2 ' transpose of a matrix v1' M' + addition of matrices v1' + v2 M1 + M2' - subtraction of matrices v1' - v2 M1 - M2' * Scalar and matrix multiplication v1 * v2 M1 * M2 / Right-division operator help mrdivide \ Left-division operator help mldivide.ˆ raise all the elements of a matrix to some power v1.ˆ2 M1.ˆ2.* elementwise multiplication v1.* v2' M1.* M2'./ or.\ elementwise division v1./ v2' M1./ M2' 4

5 Functions Notions. Matlab also allows to create functions. Functions can accept inputs and return outputs. Creating your own functions allows you to execute a piece of code that you use regularly. Matlab functions are program files with.m extension. The function must have the same name as the Matlab file that contains it. Prototype. The first line of the file specifies the prototype of the function. For instance, here s the prototype of function my function contained in file my function.m function [out 1, out 2,...,out p] = my function(in 1, in 2, in 3,...,in n) Important remark. All the functions you define, even the simplest ones, must be commented! Describe the types and the meaning of inputs and outputs. Example. Choose NEW > Function. Type the following code (or copy here : square fun.m) : function [x square] = square fun(x) % square fun.m: % % DESCRIPTION: Compute the square of a real number x % % INPUT ARGUMENTS: % x scalar % % OUTPUT ARGUMENTS: % x square scalar, the square of x x square = x*x; end % End function square fun Name and save the file as square fun.m. Enter the following commands in the Command Window. This shows how to call a function. >> y = 2; z = 4; >> [y square] = square fun(y) >> [z square] = square fun(z) 5.1 Conditional and Loop Control Statements Matlab, like many other languages, allows to use conditional and loop control statements. >> help if >> help for >> help while >> help switch Example. Let f be a function defined as follows: 0 x < 1 x + 1 1 x 0 f(x) = 1 x 0 < x 1 0 x > 1 (1) 5

Create a Matlab function hat fun that computes function f for all values contained in a vector. Use only one conditional statement and one loop control statement. Here is the expected outcome : >> vec values=[-3-0.7 0.45 2.6]; >> [hat fun vec]=hat fun(vec values) hat fun vec = 0 0.3000 0.5500 0 Remark. The use of loops may significantly slow down Matlab. Matlab programming is based on a paradigm called vectorization. This means that you should consider using Matlab matrix and vector operations rather than writing loop-based code. 6 Plotting Graphs Matlab also allows to plot graphs of functions. >> help plot >> help title >> help xlabel >> help ylabel Example. Consider the following sinusoid functions: >> x = -2*pi:.1:2*pi; >> y = sin(x); >> z = cos(x); (a) Plot function sin for the range of values in x. Give a title, labels to the x-axis and y-axis on the graph. (b) Plot functions sin and cos for the range of values in x on the same graph. Draw the cos curve as a dotted red line. Add legends to both curves. >> help hold >> help legend Example. Write a script to graph function f defined by Equation (1) for 500 equally spaced values between -3 and 3. 6