EGR 111 Introduction to MATLAB

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

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

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

EE 301 Signals & Systems I MATLAB Tutorial with Questions

MATLAB TUTORIAL WORKSHEET

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

Laboratory 1 Octave Tutorial

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

Introduction to Engineering gii

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

Chapter 2. MATLAB Fundamentals

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

Introduction to MATLAB

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

Matlab Programming Introduction 1 2

EGR 111 Plotting Data

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

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

Introduction to MATLAB

McTutorial: A MATLAB Tutorial

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

2.0 MATLAB Fundamentals

General Information. There are certain MATLAB features you should be aware of before you begin working with MATLAB.

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

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Desktop Command window

University of Alberta

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

Introduction to MATLAB

Dr Richard Greenaway

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

Introduction to MatLab. Introduction to MatLab K. Craig 1

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:

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

1 Introduction to Matlab

Introduction to MATLAB

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

Introduction to MATLAB

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

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

ARRAY VARIABLES (ROW VECTORS)

EGR 111 Functions and Relational Operators

ENGR 1181 MATLAB 02: Array Creation

CME 192: Introduction to Matlab

AN INTRODUCTION TO MATLAB

MATLAB Project: Getting Started with 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.

General MATLAB Information 1

Introduction to Matlab

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

Digital Image Analysis and Processing CPE

PART 1 PROGRAMMING WITH MATHLAB

MATLAB Project: Getting Started with MATLAB

Introduction to MATLAB Programming

Chapter 1 Introduction to MATLAB

Computer Programming in MATLAB

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

Boolean Logic & Branching Lab Conditional Tests

Introduction. Matlab for Psychologists. Overview. Coding v. button clicking. Hello, nice to meet you. Variables

EGR 111 Loops. This lab is an introduction to loops, which allow MATLAB to repeat commands a certain number of times.

VARIABLES Storing numbers:

Introduction to Matlab

Complex Dynamic Systems

Basic stuff -- assignments, arithmetic and functions

An Introduction to Numerical Methods

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

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

EGR 111 Functions and Relational Operators

Introduction to MATLAB

An Introduction to MATLAB II

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

Dr Richard Greenaway

MATLAB BASICS. M Files. Objectives

Finding, Starting and Using Matlab

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

Excel R Tips. is used for multiplication. + is used for addition. is used for subtraction. / is used for division

Computer Programming ECIV 2303 Chapter 1 Starting with MATLAB Instructor: Dr. Talal Skaik Islamic University of Gaza Faculty of Engineering

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

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.

Grace days can not be used for this assignment

EEE161 Applied Electromagnetics Laboratory 1

MATLAB COURSE FALL 2004 SESSION 1 GETTING STARTED. Christian Daude 1

SMS 3515: Scientific Computing Lecture 1: Introduction to Matlab 2014

Introduction to MATLAB

Getting Started with MATLAB

Introduction to MATLAB

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

MATLAB GUIDE UMD PHYS401 SPRING 2012

Matlab- Command Window Operations, Scalars and Arrays

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

LAB 1 General MATLAB Information 1

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

Introduction to Matlab. Matlab variable types. Matlab variable types. Matlab variable types. Notes. Eugeniy E. Mikhailov. Lecture 02. Notes.

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

How to learn MATLAB? Some predefined variables

A Very Brief Introduction to Matlab

Matlab Tutorial and Exercises for COMP61021

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

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

Programming in Mathematics. Mili I. Shah

Transcription:

EGR 111 Introduction to MATLAB This lab introduces the MATLAB help facility, shows how MATLAB TM, which stands for MATrix LABoratory, can be used as an advanced calculator. This lab also introduces assignment ( = ) statements, entering vectors and matrices (the [ ] brackets, the multiple uses of the semi-colon ( ; ), and use of the colon operator ( : ). New MATLAB commands: help, lookfor, brackets [ ], colon, semicolon, linspace, and parentheses ( ) 1. Getting Started MATLAB is installed on the PC s in the computer classrooms and open lab in Shiley Hall. To start MATLAB in Windows, click on Start, All Programs, Courseware, General Engineering, MATLAB. 2. Getting Help To launch the MATLAB help facility, click on Help, and then Product help. A window should open that contains documentation for MATLAB. Click on the plus sign next to MATLAB and then the plus sign next to Getting Started link. You might want to explore the tutorials on your own time to become more familiar with MATLAB. The sections most relevant to this course are Introduction, Matrices and Arrays, Graphics, and Programming. If you know the name of the function or operator for which you want more information, you can go to MATLAB s command window and type the word help followed by the function name. EGR111 - p. 1 of 7 - Intro_to_MATLAB_rev2.docx

For example, type the following command into MATLAB s command window and then press the Enter key: help plot 3. Searching for a Keyword If you don t know the exact MATLAB command name, you can search for a keyword that might be included in the description of the command. For example, type the following command into MATLAB s command window: lookfor plot 4. Calculations MATLAB can do simple calculations, similar to your calculator. Type the following commands (each line followed by the Enter key): 4*5 3^2 The variable ans always stores the result of the most recent calculation. This feature is similar to Texas Instruments calculators. When MATLAB starts up, it assigns the value of π to the variable named pi, so it can be used in calculations such as the following: cos(pi) We can also store values into variables, so we can use the values in a later statement. Try typing the following: x = 5*5; y = sqrt(x); Notice that nothing is printed when the line ends in a semicolon, but MATLAB does execute the command. This feature is useful when you don t want to see the results of a calculation printed to the screen. The two commands above are examples of assignment commands. MATLAB evaluates the expression on the right hand side and stores the result in the variable name on the left hand side. To see the value stored in a variable, just type the variable names as follows: x y pi We can also use the variables in expressions. Try typing the following command: a = x + y EGR111 - p. 2 of 7 - Intro_to_MATLAB_rev2.docx

In MATLAB, anywhere you can put a constant number (like 4), you can also put a variable (like x) or even an expression (like 3*x+4). Type the following commands to demonstrate this feature: a = 2 b = sqrt(a+7) In MATLAB, several commands can be placed on a single line. If you separate the commands by a comma, the results of each command are printed to the Command Window. If you separate the commands by a semicolon, the commands are executed but nothing is printed to the Command Window. For example, in the command below, the values of the variables a and c are computed and stored, but they not printed because they are followed by a semicolon. >> a = 1; b = 2*4, c = 5+8; b = 8 In MATLAB, you can recall previous commands by pressing the up arrow on the keyboard. So if you wanted to run the above command again, you could just hit the up arrow, edit the command if needed, and then Enter. If you type the first few characters of a command and then hit the up arrow, MATLAB recalls the most recent command that starts with those characters. You can also click on a previous command in the command history window and drag it to the Command Window. Try these features out to see how they can save a lot of typing. Rules for variable names: In MATLAB, variable names must begin with a letter and can contain letters, digits, and underscores (but not spaces or operators like -, +, *, /). Variable names are case sensitive, so x is not the same variable as X. Examples of valid variable names are x, x2, and number_of_wheels. Examples of variable names that are NOT valid are 2x, yes!, and x*2. 5. Order of Operations The order in which operations are computed are determined by the following table. Order Operator Symbol 1 Parentheses ( ) 2 Power ^ 3 Unary Plus and + and - Minus 4 Multiplication * and / and Division 5 Addition and + and - Subtraction 6 Colon : EGR111 - p. 3 of 7 - Intro_to_MATLAB_rev2.docx

Parentheses are computed first, followed by the other operators in the order shown in the table. So in the expression 4*(3+2), the value of (3+2) is computed first, then the result is multiplied by 4. In the expression 3*2^2, the power operator (^) has higher precedence than multiply (*), so 2^2 is computed first, and the result is then multiplied by 3. So 3*2^2 = 3*(2^2) = 3*(4) = 12. Operators on the same level of precedence are computed left to right. So 6/3*2 = (6/3)*2 = (2)*2 = 4 because division and multiplication are on the same level. Unary minus refers to the case when a minus sign operates on a single value (as opposed to subtracting two values). For example, in the expression -2^2, the power has a higher precedence than the unary minus operator, so -2^2 = -(2^2) = -4. Determine the value of the following expressions and check your answers with MATLAB. 2*1+3 = 10/5*2 = 4/2+3 = -2^2+1 = 4+2/2+3 = 2^2*3 = -2*3^2+1 = 6. Vectors and Matrices Variables can store a single value (called a scalar) such as: a = 4 In MATLAB, variables can also store a row of values (called a row vector) by including the values in square brackets. Try typing the following command: b = [2 4 6 8 10] The values can be separated by spaces (as shown above) or by commas (as shown below). b = [2,4,6,8,10] We could store values in a column vector by separating the values with semicolons as follows: c = [2; 4; 6; 8; 10] EGR111 - p. 4 of 7 - Intro_to_MATLAB_rev2.docx

MATLAB can also store a table of values (called a matrix) by separating the rows of the matrix by semicolons. Try typing the following command: d = [1 2 3; 4 5 6; 7 8 9] Strings are vectors that contain characters. In MATLAB, strings are enclosed in single quotes as follows: str = 'This is a string' One of MATLAB s strengths is that it can easily store and process vectors and matrices, which can represent strings, audio signals, images, and any other large set of data. 7. Indexing Vectors and Matrices To access a particular element in a vector, type the element number in parentheses after the vector name. For example, assuming that you defined the vector c as shown above, you can access the third element of vector c by typing c(3) and replace it by 100, as follows: c(3) = 100 The command above overwrites the third element of the vector c with the new value of 100. Note that the old value (which was 6) is lost. Likewise, to access an element of a matrix, specify the row and column numbers. For example, assuming that you defined the matrix d as shown above, you can access the element in the second row and third column by typing d(2,3) and replace it by 100, as follows: d(2,3) = 100 The command above overwrites the element in the second row and third column of the matrix d with the new value of 100. Again, the old value (which was 6) is lost. If this old value will be needed in the future, you could store it as another variable, before using the command above. x = d(2,3) 8. The MATLAB Workspace Notice the Workspace in the upper right corner of the MATLAB window. Are the names inside of the Workspace familiar? The Workspace lists all variables that you have created and which are currently being stored in the computer s memory (RAM). Within MATLAB, you retrieve these stored value by using their variable names inside of the Command Window. EGR111 - p. 5 of 7 - Intro_to_MATLAB_rev2.docx

9. Colon Operator MATLAB has a way to easily create vectors called the colon operator. First, let's experiment with creating vectors. Type the following: x = 1:10 x = 1:2:10 x = 10:-1:1 t = 0:0.1:1 At this point, you have probably figured out that the notation start:inc:end generates a vector beginning with the value of start and incrementing by the value of inc up until it gets to the value of end. If the increment is not given (that is, if start:end is typed), MATLAB uses a default increment of 1. If we want to use a generated vector in an expression, we can enclose the colon expression in square brackets. For example, type the following to yield 2 times the row vector [0:0.1:1]: t = 2 * [0:0.1:1] Since the numeric operators have higher precedence than the colon operator (see the table in Section 5 of this document), the expression 0:1/10:2*pi would be equivalent to 0:(1/10):(2*pi) and would result in a vector that started at 0, incremented by 0.1, and ended just before 2*pi. 10. Linspace Function The command linspace can also be used to generate vectors of equally-spaced values. It is more convenient than the colon operator when you would rather specify the number of values to generate instead of the increment between the values. For example, to generate a vector that starts at 0, ends at 2, and has 10 elements, type the following: x = linspace(0,2,10) If you only give linspace the start and end values, it will default to 100 elements. So, x = linspace(0,2)would return a vector with 100 elements between 0 and 2. 9. Using the Colon Operator in Indexing Several elements of a vector can be selected using the colon operator. For example, let's replace the first four elements of the vector x by the value 100 as follows: w = 1:10 w(1:4) = 100 EGR111 - p. 6 of 7 - Intro_to_MATLAB_rev2.docx

In the command above, MATLAB first expands 1:4 to the vector [1 2 3 4], and then it accesses those elements and sets their value to 100. If we want to access several elements at the end of a vector, we can use the end statement: x = 1:10 x(6:end) = 100 We can access a subset of a matrix in a similar way. y = [1 2 3; 4 5 6; 7 8 9] y(2:3, 3) = 100 If we want to access a whole row or column, we can use just a colon as follows: z = [1 2 3; 4 5 6; 7 8 9] z(:, 3) = 100 The colon operator allows us to easily access any desired part of a vector or matrix. Exercise 1: Write the commands that will generate the vector x1 = [0.3 0.4 0.5 1.0] and use indexing to replace the even indexed elements (those at positions 2, 4, 6, etc.) with the value 100. Exercise 2: Write the commands that will produce a vector x2 that is the sin function of the following elements: 0, π/2, π, 3π/2, 2π. Note that you might want to use the help sin command to learn more about the sin function: specifically, whether the function uses radians or degrees. The help description also tells you that sin(x) is the sine of the elements of X. Note that X can be a scalar, a vector, or a matrix. Exercise 3: Create the matrix x3 = [1 2 3 4; 5 6 7 8; 9 10 11 12]. Then multiply this vector by 5 to create a new matrix y3. Then write the commands to change elements in the second row of y3 to the value 100. Checkpoint 1: Show the instructor your commands and the results for Exercises 1, 2, and 3. Your instructors will be especially appreciative if you use the semicolon ; to suppress output, except for your final results. EGR111 - p. 7 of 7 - Intro_to_MATLAB_rev2.docx