Introduction to MATLAB

Similar documents
Getting started with MATLAB

An Introduction to MATLAB II

Chapter 1 Introduction to MATLAB

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

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

Introduction to MATLAB

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

Introduction to MATLAB

Introduzione a MatLab. Prof. Sebastiano Battiato

Introduction to Matlab

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

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

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

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

A Quick Tutorial on MATLAB. Zeeshan Ali

Introduction to Matlab

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.

Welcome to EGR 106 Foundations of Engineering II

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

Introduction to Matlab

Introduction to Engineering gii

Introduction to Matlab

Matlab Programming Introduction 1 2

2.0 MATLAB Fundamentals

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

Finding, Starting and Using Matlab

Quick MATLAB Syntax Guide

A Guide to Using Some Basic MATLAB Functions

Introduction to MatLab. Introduction to MatLab K. Craig 1

Introduction to MATLAB LAB 1

Introduction to MATLAB. Computational Probability and Statistics CIS 2033 Section 003

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

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

Dr Richard Greenaway

Eng Marine Production Management. Introduction to Matlab

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

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.

Lecturer: Keyvan Dehmamy

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

Introduction to MATLAB

Digital Image Analysis and Processing CPE

Matlab Lecture 1 - Introduction to MATLAB. Five Parts of Matlab. Entering Matrices (2) - Method 1:Direct entry. Entering Matrices (1) - Magic Square

DSP Laboratory (EELE 4110) Lab#1 Introduction to Matlab

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

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

Stokes Modelling Workshop

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

MATLAB INTRODUCTION. Risk analysis lab Ceffer Attila. PhD student BUTE Department Of Networked Systems and Services

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

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

Introduction to MATLAB

! The MATLAB language

Laboratory 1 Octave Tutorial

ECE Lesson Plan - Class 1 Fall, 2001

Desktop Command window

Programming in Mathematics. Mili I. Shah

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

AN INTRODUCTION TO MATLAB

Introduction to MATLAB Practical 1

MATLAB NOTES. Matlab designed for numerical computing. Strongly oriented towards use of arrays, one and two dimensional.

PART 1 PROGRAMMING WITH MATHLAB

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

Introduction to MATLAB for Numerical Analysis and Mathematical Modeling. Selis Önel, PhD

EGR 111 Introduction to MATLAB

Introduction to PartSim and Matlab

Getting Started. Chapter 1. How to Get Matlab. 1.1 Before We Begin Matlab to Accompany Lay s Linear Algebra Text

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

What is Matlab? The command line Variables Operators Functions

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

EP375 Computational Physics

LAB 1 General MATLAB Information 1

Matlab is a tool to make our life easier. Keep that in mind. The best way to learn Matlab is through examples, at the computer.

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Matlab Introduction. Scalar Variables and Arithmetic Operators

Introduction to Matlab

Introduction to MATLAB

Some elements for Matlab programming

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

Introduction to Matlab to Accompany Linear Algebra. Douglas Hundley Department of Mathematics and Statistics Whitman College

Introduction to MATLAB

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.

A General Introduction to Matlab

INTRODUCTION TO MATLAB

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

Introduction to MATLAB

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

Introduction to Computer Programming with MATLAB Matlab Fundamentals. Selis Önel, PhD

Introduction to GNU-Octave

A very brief Matlab introduction

Consider this m file that creates a file that you can load data into called rain.txt

MATLAB Quick Reference

EE 301 Signals & Systems I MATLAB Tutorial with Questions

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

Introduction to MATLAB

MatLab Just a beginning

Basic MATLAB Tutorial

the Enter or Return key. To perform a simple computations type a command and next press the

Edward Neuman Department of Mathematics Southern Illinois University at Carbondale

1 Introduction to MATLAB

Transcription:

ELG 3125 - Lab 1 Introduction to MATLAB TA: Chao Wang (cwang103@site.uottawa.ca) 2008 Fall ELG 3125 Signal and System Analysis P. 1

Do You Speak MATLAB? MATLAB - The Language of Technical Computing ELG 3125 Signal and System Analysis P. 2

What s MATLAB? MATLAB is a program for doing numerical computation. It was originally designed for solving linear algebra type problems using matrices. It s name is derived from MATrix LABoratory. MATLAB has since been expanded and now has built-in functions for solving problems requiring data analysis, signal processing, optimization, and several other types of scientific computations. It also contains functions for 2-D and 3-D graphics and animation. (Toolboxes) ELG 3125 Signal and System Analysis P. 3

Starting MATLAB When starting up the MATLAB program, MATLAB loads and checks that a license is available and throws up a splash screen. ELG 3125 Signal and System Analysis P. 4

Starting MATLAB Next MATLAB s desktop appears. Command Window Current Directory Workspace Browser Command History Launch Pad ELG 3125 Signal and System Analysis P. 5

MATLAB Desktop Command Window The MATLAB environment is command oriented somewhat like UNIX or DOS. A prompt appears on the screen and a MATLAB statement can be entered. Use the Command window to enter variables and run functions and M-files. When the <ENTER> key is pressed, the statement is executed, and another prompt appears. ELG 3125 Signal and System Analysis P. 6

MATLAB Desktop Command Window (cont.) If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results will appear before the next prompt. Right is an example from a MATLAB screen. ELG 3125 Signal and System Analysis P. 7

MATLAB Desktop Command History Statements you enter in the Command window are logged in the Command Histroy. Simply double-click the command. ELG 3125 Signal and System Analysis P. 8

MATLAB Desktop Workspace Browser Consists of the set of all current variables built up during a MATLAB session and stored in memory. Add variables to the workspace by using functions, running M-files and loading saved workspaces. ELG 3125 Signal and System Analysis P. 9

MATLAB Desktop Workspace Browser (cont.) Commands for workspace variables: who, whos current variables in workspace save saveworkspace variables to *.mat file load load variables from.mat file clear, clear all clear workspace variables ELG 3125 Signal and System Analysis P. 10

MATLAB Desktop Current Directory File and folder information in the current directory ELG 3125 Signal and System Analysis P. 11

MATLAB Desktop Launch Pad access tools, demos and documentation ELG 3125 Signal and System Analysis P. 12

MATLAB Variables Variable names Variable names ARE case sensitive Variable names can contain up to 63 characters (as of MATLAB 6.5 and newer) Variable names MUST start with a letter followed by letters, digits, and underscores. (for example: x, y, my_var, Y_2, time_2_frequency) ELG 3125 Signal and System Analysis P. 13

MATLAB Variables MATLAB special variables ans Default variable name for results pi Value of π eps Smallest incremental number inf Infinity NaN Not a number e.g. 0/0 i and j i = j = square root of -1 realmin The smallest usable positive real number realmax The largest usable positive real number ELG 3125 Signal and System Analysis P. 14

MATLAB Symbols Some MATLAB symbols >> prompt... continue statement on next line, separate statements and data % start comment which ends at end of line ; (1) suppress output (2) used as a row separator in a matrix : specify range ELG 3125 Signal and System Analysis P. 15

MATLAB Matrices MATLAB treats all variables as matrices. For our purposes a matrix can be thought of as an array, in fact, that is how it is stored. Vectors are special forms of matrices and contain only one row OR one column. Scalars are matrices with only one row AND one column ELG 3125 Signal and System Analysis P. 16

MATLAB Matrices A matrix with only one row AND one column is a scalar. A scalar can be created in MATLAB as follows: ELG 3125 Signal and System Analysis P. 17

MATLAB Matrices A matrix with only one row is called a row vector. A row vector can be created in MATLAB as follows (note the commas): ELG 3125 Signal and System Analysis P. 18

MATLAB Matrices A matrix with only one column is called a column vector. A column vector can be created in MATLAB as follows (note the semicolons): ELG 3125 Signal and System Analysis P. 19

MATLAB Matrices A matrix can be created in MATLAB as 2-D form (note the commas AND semicolons): ELG 3125 Signal and System Analysis P. 20

MATLAB Matrices Matrix Operations transpose: subscription: ELG 3125 Signal and System Analysis P. 21

MATLAB Matrices Matrix Operations (cont.) whole row: whole column: ELG 3125 Signal and System Analysis P. 22

MATLAB Matrices Extracting a Sub-Matrix A portion of a matrix can be extracted and stored in a smaller matrix by specifying the names of both matrices and the rows and columns to extract. The syntax is: sub_matrix = matrix ( r1 : r2, c1 : c2 ) ; where r1 and r2 specify the beginning and ending rows and c1 and c2 specify the beginning and ending columns to be extracted to make the new matrix. ELG 3125 Signal and System Analysis P. 23

MATLAB Matrices Extracting a Sub-Matrix (cont.) ELG 3125 Signal and System Analysis P. 24

MATLAB Matrices Generating Matrix from built-in Functions zeros(m,n) MxN matrix of zeros ones(m,n) MxN matrix of ones rand(m,n) MxN matrix of uniformly distributed random numbers on (0,1) ELG 3125 Signal and System Analysis P. 25

MATLAB Operators Arithmetic Operators + unary addition - unary subtraction * multiplication / right division \ left division (56/8=8\56) ^ power complex conjugate transpose.* element-by-element mult../ element-by-element div.^ element-by-element power. transpose ELG 3125 Signal and System Analysis P. 26

MATLAB Operators Relational and Logical Operators == equal ~= not equal < less than <= less than or equal > greater than >= greater than or equal & AND OR ~ NOT ELG 3125 Signal and System Analysis P. 27

MATLAB Flow Control MATLAB has six basic flow control constructs: - if-elseif-else statements - switch-case statements - for loops - while loops - break statements: Terminate execution of WHILE or FOR loop. - continue statements: Pass control to the next iteration of FOR or WHILE loop. ELG 3125 Signal and System Analysis P. 28

MATLAB M-Files Sequences of MATLAB commands can be written to files with the extension *.m, called M-files. Entering the name of the file (without the extension) causes automatic execution of all the statements in the file. ELG 3125 Signal and System Analysis P. 29

MATLAB M-Files There are two kinds of M-files - Scripts, which do not accept input arguments or return output arguments. They operate on data in the workspace. (Simplest form of M-files) - Functions, which can accept input arguments and return output arguments. Internal variables are local to the function. ELG 3125 Signal and System Analysis P. 30

MATLAB M-Files An example of Script M-files - Creat a file by the name myscript.m - Contents of myscript.m : x=45*pi/180; %convert degrees to radians a=sin(x); %compute sine 45 degrees b=cos(x); %compute cosine 45 degrees disp('sin(45*pi/180)='); %print header disp(a); %print result - Run myscript.m : ELG 3125 Signal and System Analysis P. 31

MATLAB M-Files An example of Function M-files - Create a file by the name cosgen.m - FUNCTION Add new function - Contents of cosgen.m : function [ y ] = cosgen ( x,a,f,p ) % COSGEN genreation of a cosine wave % y = cosgen (s, a, f, p) % y - cosine of x; % a - amplitude; % f - frequency; % p - phase y output argument x,a,f,p input arguments y=a*cos(2*pi*f*(x+p/(2*pi*f))); ELG 3125 Signal and System Analysis P. 32

MATLAB M-Files An example of Function M-files (cont.) - run the cosgen.m ELG 3125 Signal and System Analysis P. 33

MATLAB M-Files An example of Function M-files (cont.) - Hint: All the comments at the beginning of a Function M-file will be displayed as help content. ELG 3125 Signal and System Analysis P. 34

MATLAB M-Files Editing M-files through Editor Window - Use the Editor/Debugger to create and debug M-files, which are programs you write to run MATLAB functions. - The Editor/Debugger provides a GUI for basic text editing, as well as for M-file debugging. Create a new M-file: File -> New -> M-file Open an M-file: File -> Open ELG 3125 Signal and System Analysis P. 35

MATLAB Built-in Functions MATLAB also has many built-in functions - >> abs(x) Absolute value ->> exp(x) Exponential value ->> fix(x) Round towards zero ->> sqrt(x) Square root ->> log10(x), log(x) Common (base 10) logarithm, Natural logarithm ->> round(x), ceil(x) Round towards nearest integer, Round towards plus infinity ->> sin(x), cos(x) Sine, Cosine ->> size(x), length(x) Size of array, Length of vector ->>max(x), min(x) Largest, Smallest component ELG 3125 Signal and System Analysis P. 36

MATLAB Graphics MATLAB supports a number of plot types with the ability to create customized graphs. The simplest x-y type plot can be created by plot(x,y) To find out about the syntax for any of the plot commands, you can type help <commandname> inside of MATLAB The figure plotted will be shown in the Figure Window. ELG 3125 Signal and System Analysis P. 37

MATLAB Graphics Graph Functions (Summary) >> plot linear plot >> stem discrete plot >> grid add grid lines >> xlabel add X-axis label >> ylabel add Y-axis label >> title add graph title >> subplot divide figure window >> figure create new figure window >> pause wait for user response >> axis control axis scaling and appearance ELG 3125 Signal and System Analysis P. 38

MATLAB Graphics Some simple examples x = 0:pi/100:2*pi; y = sin(x); plot(x,y) xlabel('x = 0:2\pi') ylabel('sine of x') title('plot of the Sine Function') Sine of x 1 0.8 0.6 0.4 0.2 0-0.2-0.4 Plot of the Sine Function -0.6-0.8-1 0 1 2 3 4 5 6 7 x = 0:2π ELG 3125 Signal and System Analysis P. 39

MATLAB Graphics Some simple examples (cont.) t = 0:pi/100:2*pi; y1=sin(t); y2=sin(t+pi/2); plot(t,y1,t,y2) grid on 1 0.8 0.6 0.4 0.2 0-0.2-0.4-0.6-0.8-1 0 1 2 3 4 5 6 7 ELG 3125 Signal and System Analysis P. 40

MATLAB Graphics Some simple examples (cont.) t = 0:pi/100:2*pi; y1=sin(t); y2=sin(t+pi/2); subplot(2,2,1) plot(t,y1) subplot(2,2,2) plot(t,y2) 1 0.5 0-0.5-1 0 2 4 6 8 1 0.5 0-0.5-1 0 2 4 6 8 ELG 3125 Signal and System Analysis P. 41

MATLAB Graphics Some simple examples (cont.) 0.18 k=0:20; y=binopdf(k,20,0.5); stem(k,y) 0.16 0.14 0.12 0.1 binopdf(): Binomial probability density function 0.08 0.06 0.04 0.02 0 0 2 4 6 8 10 12 14 16 18 20 ELG 3125 Signal and System Analysis P. 42

Where to get help? In Matlab s prompt type: >>help Display help text in Command Window. >>lookfor Search all M-files for keyword. >>helpwin Online help displayed in the Help window. >>helpdesk Comprehensive hypertext documentation. >>demos Examples and demonstrations. >>doc Display HTML documentation in the Help browser. ELG 3125 Signal and System Analysis P. 43

Where to get help? On the web: ELG 3125 Signal and System Analysis P. 44

End of Lab 1. Thank you. Any Questions? ELG 3125 Signal and System Analysis P. 45