How to learn MATLAB? Some predefined variables

Similar documents
EE 301 Signals & Systems I MATLAB Tutorial with Questions

LAB 1: Introduction to MATLAB Summer 2011

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.

Laboratory 1 Introduction to MATLAB for Signals and Systems

ARRAY VARIABLES (ROW VECTORS)

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

Basic Simulation Lab with MATLAB

The value of f(t) at t = 0 is the first element of the vector and is obtained by

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:

Grace days can not be used for this assignment

A Guide to Using Some Basic MATLAB Functions

Matlab Introduction. Scalar Variables and Arithmetic Operators

MATLAB/Octave Tutorial

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

Lab 1 Introduction to MATLAB and Scripts

1-- Pre-Lab The Pre-Lab this first week is short and straightforward. Make sure that you read through the information below prior to coming to lab.

George Mason University Signals and Systems I Spring 2016

Lab P-1: Introduction to MATLAB. 3. Learn a little about advanced programming techniques for MATLAB, i.e., vectorization.

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

AN INTRODUCTION TO MATLAB

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

ECE 3793 Matlab Project 1

A very brief Matlab introduction

AMS 27L LAB #2 Winter 2009

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

UNIVERSITI TEKNIKAL MALAYSIA MELAKA FAKULTI KEJURUTERAAN ELEKTRONIK DAN KEJURUTERAAN KOMPUTER

An Introduction to MATLAB II

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

GEORGIA INSTITUTE OF TECHNOLOGY. SCHOOL of ELECTRICAL and COMPUTER ENGINEERING. ECE 2026 Summer 2018 Lab #0: Introduction to MATLAB

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

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

1 Introduction to Matlab

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

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

MATLAB TUTORIAL WORKSHEET

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

EGR 111 Introduction to MATLAB

Introduction to MATLAB

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

Laboratory 1 Octave Tutorial

What is Matlab? A software environment for interactive numerical computations

A Brief Introduction to MATLAB

SF1901 Probability Theory and Statistics: Autumn 2016 Lab 0 for TCOMK

Dr Richard Greenaway

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

Introduction to MATLAB LAB 1

University of Alberta

Getting Started with MATLAB

Introduction to MATLAB Practical 1

Stokes Modelling Workshop

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

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

Introduction to Matlab

Some elements for Matlab programming

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

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

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

DSP First. Laboratory Exercise #1. Introduction to MATLAB

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

EE-3221 MATLAB INTRODUCTION

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

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

Teaching Manual Math 2131

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

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

Introduction to Octave/Matlab. Deployment of Telecommunication Infrastructures

Lecturer: Keyvan Dehmamy

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

Learning from Data Introduction to Matlab

Introduction to Matlab

MATLAB = MATrix LABoratory. Interactive system. Basic data element is an array that does not require dimensioning.

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

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

Mathematical Operations with Arrays and Matrices

QUICK INTRODUCTION TO MATLAB PART I

Introduction to GNU-Octave

PART 1 PROGRAMMING WITH MATHLAB

Lecture 2: Variables, Vectors and Matrices in MATLAB

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

To see what directory your work is stored in, and the directory in which Matlab will look for files, type

APPM 2360 Project 2 Due Nov. 3 at 5:00 PM in D2L

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

Introduction to MATLAB

Objectives. 1 Running, and Interface Layout. 2 Toolboxes, Documentation and Tutorials. 3 Basic Calculations. PS 12a Laboratory 1 Spring 2014

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

EE 301 Lab 1 Introduction to MATLAB

A quick Matlab tutorial

Introduction to MATLAB programming: Fundamentals

Getting started with MATLAB

PC-MATLAB PRIMER. This is intended as a guided tour through PCMATLAB. Type as you go and watch what happens.

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

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.

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

Matlab Tutorial for COMP24111 (includes exercise 1)

Introduction to Engineering gii

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

Introduction to MATLAB

Introduction to MATLAB Programming

Basic MATLAB Intro III

Transcription:

ECE-S352 Lab 1 MATLAB Tutorial How to learn MATLAB? 1. MATLAB comes with good tutorial and detailed documents. a) Select MATLAB help from the MATLAB Help menu to open the help window. Follow MATLAB s Getting started and learn step by step. b) Use Index tab when you know the MATLAB command name. c) Search can help when you know related keywords and topics. 2. Search in Internet. For example, google MATLAB tutorial or MATLAB help, you will find a lot of useful links to study. Here s one possibility: http://www.math.ufl.edu/help/matlab-tutorial/index.html#sec1 In the following tutorial, all lines started with >> are matlab commands, please follow the tutorial and type them in your MATLAB command window to execute them. Some predefined variables i sqrt(-1) j sqrt(-1) pi 3.1415926535 >>y = 3+4*i y = 3.0000 + 4.0000i There are also a number of predefined functions that can be used when defining a variable. Some common functions that are used often are: abs magnitude of a number ( absolute value for real numbers) angle angle of a complex number, in radians cos cosine function, assumes argument is in radians. sin exp for example >> abs(y) ans = 5.0000 Be careful once you redefine the variable i and j, they will no longer represent sqrt(-1) >> i = 3 >> y = 2+5*i y = 17 If you finish your command line with semi column ;, then MATLAB won t echo the output in the screen. This is very useful when you have variables of large dimensions.

DON T try this to define a 100 by 100 zero matrix, >> X1 = zeros(100,100) It could mess up your MATLAB screen. Instead you should type: >> X1 = zeros(100,100); to suppress the output. Working with vectors and matrices >> a = 4:2:12 a = 4 6 8 10 12 a is a row vector with 5 evenly spaced elements, started with 4 and ended with 12. The default step is 1, so a = 2:1:4 is the same as a = 2:4. >>a = linspace(4, 12, 5) This will create the same row vector as above. >> a1 = -1:0.1:1 Increasing step is 0.1 here. >> help linspace help + <command name> shows help for the specific command. All possible input and output parameters for the command are explained. Some related commands are listed in the end. >> b = a b is transposed a, therefore a column vector. >> b = transpose(a) This equals to b = a. >>A = [1 2 3 4; 5 6 7 8; 9 10 11 12] A = 1 2 3 4 5 6 7 8 9 10 11 12 A is a 3 by 4 matrix. >> B = [1 1 1 1; 2 1 0 0; 3 1 9 0] B = 1 1 1 1 2 1 0 0 3 1 9 0 B is also 3 by 4. >>whos the whos lets you know all of the variables you have in your work space. >>c = A(2,:)

c is the 2 nd row of matrix A. c is a row vector, the colon after comma represents all columns. >> d = B(:, 3) d is the 3 rd column of matrix B, therefore d is a column vector >>E = B(2:3, 1:2) E = 2 1 3 1 E is a sub-matrix of B. Compare E with matrix B to see how they are related. >>F = B(2, 2:2:4) F = 1 0 F is created by taking B s 2 nd row and 2 nd,4 th columns. Remember 2:2:4 is [2, 4] >> M = B(2:end, 3) M = 0 9 end is very useful here. It refers to the last subscript value. Clearly B(end,end) is 0, and B(end,end-1) is 9. >>G = A+B +, -, *, / are defined for matrix operation. Make sure the sizes of 2 matrices match before carrying on the operations. * is the matrix multiplication. For example, A is a 3 by 4 matrix. D is 4 by 5, then A*D will be 3 by 5. If there is a matrix G having 2 rows and 4 columns, A*G will be illegal operation and MATLAB gives a error message. These matrix operations follow the rules in linear algebra. >> X = A.*B Note:.* is different from *..* is an element-wise operation. An entry in X is created from multiplying corresponding entries in A and B, that is: X(i,j) = A(i,j) *B(i,j).* equals to * for scalar variables. >>eye(6) %identity matrix >>zeros(3,4) % zero matrix >>ones(7) % one matrix >> H = [] % this defines a null matrix The above 4 commands create some standard matrices. Everything following the % is comment. MATLAB will just skip instead of interpreting it. Plotting >>h = 0.5;

>> x = 0:h:4; >> y = sin(2*pi*x); >> plot(x,y); This will open a figure window and plot a continuous x-y curve. >>title( an example of sin function ); Add title! >>xlabel( time ); >>ylabel( signal ); >> axis off; >> figure; This command creates a new figure window. >> plot(x,y, r* ); Plot x-y in the new figure window. What is the difference between the 2 figures? >> help plot If you want to plot several figures in one figure window, you should use subplot. >>help subplot >>close all; This command closes all figure windows. >> y1 = sin(3*pi*x +2) + 0.5; >> y2 = cos(1.5*pi*x); >>figure; >>subplot(2,1,1); This tells MATLAB you will treat current figure window as 2 by 1 sub-windows and you are going to plot in the 1 st window. >>plot(x,y, r. ); >>subplot(2,2,3); Suppose the figure has 2 by 2 sub-windows, the 3th sub-window is the left bottom one. >>plot(x,y1, b* ); >>subplot(2,2,4); >> plot(x,y2, gd ); M-files M-files are macros of MATLAB commands that are stored as ordinary files with extension m, that is filename.m. An M-file can be either a function with input and output variables or a list of commands. >>edit Open the edit window. For example, you can have a M-file called mylab1.m with the following lines: t = -3:0.1:3; x = t.^2; plot(t,x); title( x = t^2 function );

This M-file is just a list of commands. Save the file in your working directory and change current MATLAB path to your working directory, or add your working directory to the MATLAB searching path by selecting Set path in the File menu so that MATLAB knows where to find your file. >>mylab1 Now let s see an example of function with input and output. Create a file in your working directory named yplusx.m that contains the following commands: function z = yplusx(y,x) z = y + x; Save the file and in the MATLAB command window, typing following lines: >> x0 = 3; >> y0 = 12; >> h = yplusx(x0,y0) h = 15 Note: the first line in yplusx.m file has to start with keyword function. The name of the file must be the same as the name of the function in order to work properly. LOOPS (for,while) >> x = []; >> for n = 1:10 x = [x n^2]; end >> x is a 1 by 10 vector: [1 4 9 16 25..] Read and save images using functions from image processing toolbox >> I=imread('pout.tif'); >> imshow(i); >> help imread; >> help imwrite; >> I1 = double(i); Most functions in MATLAB deal with only double data type. Since the output data of imread has type of unsigned integer, usually it needs to be converted to double before further processing. >> I2 = uint8(i1); Other useful commands include: imagesc, colormap, imhist etc.

Assignments: 1. Write a matlab m-file functions which generates and plots each of the following signals: a) y ( t) = 10cos(2* pi * f * t + θ ), where 2 t 2 milliseconds, and any f and θ can be chosen. Plot t vs. y(t). Check the relationship between time intervals and frequencies. Check how the signals are shifted due to the phase, θ. b) An exponentially damped sinusoidal signal is defined by x( t) = 20sin(2* pi *1000* t pi / 3) *exp( at) where the exponential parameter a is a variable taking the following set of values: a = 500, 750, 1000. Investigate the effect of varying a on the signal x(t) for 2 t 2 milliseconds. (Hint: use.* instead of * to write the x(t) expression. ) 2. Plot following functions: a) A square wave using the function square. b) A triangular wave using the function sawtooth. c) A growing and decaying exponential using function exp. d) A cosine signal. f) A step function. g) A ramp function. h) An impulse function. Deliverables: The report is to follow the format instructions (see the lab policy document on this web site). In addition to the narrative, include text of the m-files you write as well as sample plots (described above). Make sure graphs are properly labeled. There is no electronic submission for this lab.