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

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

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

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

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

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

Chapter 2. MATLAB Fundamentals

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

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

Introduction to MATLAB

EGR 111 Introduction to MATLAB

(Creating Arrays & Matrices) Applied Linear Algebra in Geoscience Using MATLAB

ECE Lesson Plan - Class 1 Fall, 2001

Armstrong State University Engineering Studies MATLAB Marina 2D Arrays and Matrices Primer

Computer Packet 1 Row Operations + Freemat

Introduction to MATLAB

ENGR 1181 MATLAB 02: Array Creation

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

AN INTRODUCTION TO MATLAB

Introduction to Engineering gii

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

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

Introduction to MATLAB for Engineers, Third Edition

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

CME 192: Introduction to Matlab

SIMPLE INPUT and OUTPUT:

Introduction to MATLAB

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

University of Alberta

Introduction to MATLAB 7 for Engineers

Introduction to Matlab. By: Hossein Hamooni Fall 2014

One-dimensional Array

PART 1 PROGRAMMING WITH MATHLAB

Matrices. A Matrix (This one has 2 Rows and 3 Columns) To add two matrices: add the numbers in the matching positions:

Matlab- Command Window Operations, Scalars and Arrays

Digital Image Analysis and Processing CPE

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

Introduction to MATLAB

MATLAB TUTORIAL WORKSHEET

2.0 MATLAB Fundamentals

Performing Matrix Operations on the TI-83/84

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

CITS2401 Computer Analysis & Visualisation

1 Introduction to Matlab

Fundamentals of the J Programming Language

Vector: A series of scalars contained in a column or row. Dimensions: How many rows and columns a vector or matrix has.

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

Lecture 2. Arrays. 1 Introduction

Digital Image Processing

MATLAB GUIDE UMD PHYS401 SPRING 2012

1 Introduction to MATLAB

Vectors and Matrices. Chapter 2. Linguaggio Programmazione Matlab-Simulink (2017/2018)

Dr Richard Greenaway

Introduction to MATLAB Programming

Introduction to MATLAB

Matrices 4: use of MATLAB

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

TUTORIAL 1 Introduction to Matrix Calculation using MATLAB TUTORIAL 1 INTRODUCTION TO MATRIX CALCULATION USING MATLAB

Lab 1 Intro to MATLAB and FreeMat

Chapter 1: Number and Operations

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

1 Introduction to MATLAB

function [s p] = sumprod (f, g)

Computer Vision. Matlab

Matlab Introduction. Scalar Variables and Arithmetic Operators

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

Mathematical Operations with Arrays and Matrices

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

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

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

Intermediate Algebra. Gregg Waterman Oregon Institute of Technology

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

A very brief Matlab introduction

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

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Expr Language Reference

Laboratory 1 Octave Tutorial

Using the fprintf command to save output to a file.

Teachers Teaching with Technology (Scotland) Teachers Teaching with Technology. Scotland T 3. Matrices. Teachers Teaching with Technology (Scotland)

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

MATLAB Project: Getting Started with MATLAB

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

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

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

Introduction to MatLab. Introduction to MatLab K. Craig 1

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

Dr. Nahid Sanzida b e. uet .ac.

Getting Started with MATLAB

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

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

A Guide to Using Some Basic MATLAB Functions

JME Language Reference Manual

Array Creation ENGR 1181 MATLAB 2

Matlab Tutorial and Exercises for COMP61021

LAB 2 VECTORS AND MATRICES

How to declare an array in C?

An Introductory Guide to MATLAB

Matlab Tutorial 1: Working with variables, arrays, and plotting

9/4/2018. Chapter 2 (Part 1) MATLAB Basics. Arrays. Arrays 2. Arrays 3. Variables 2. Variables

Lecture 2 FORTRAN Basics. Lubna Ahmed

Transcription:

1 LECTURE 3

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

VARIABLE NAMES IN MATLAB Rules for declaring variables are; Are case-sensitive Can contain up to 63 characters (any characters beyond the 63rd are ignored) Must start with a letter, followed by any comb. of letters, numbers, and underscores Cannot contain spaces 3

Examples >> n=4; >> n n=4 >> N Undefined function or variable 'N'. >> 2n=4 Error: Unexpected MATLAB operator. >> frn d=4 Undefined function 'frn' for input arguments of type 'char'. 4

MATLAB, like most programming languages, requires that expressions contain a single variable to the left of the assignment operator (equals sign). Example: X = 2*A + 3*B - 15 Single variable Assignment operator Expression involving variables, functions, numbers, etc Additionally, MATLAB performs the calculations to the right of the assignment operator (=) using existing values of variables and then assigns the result to the variable to the left of the equals sign. So the expression X = X + 2 X new = X old + 2 should be interpreted as 5

Examples: (Order of operations) Try these by hand and then verify in MATLAB. A 5; B 4; C 3; D 2; F1 AB CD F2 A*B C*D F3 B/A *C F4 B/(A *C) F5 B/A/C F6 B^ D*D F7 ( A B)^1/ 2 F8 ( A B)^ (1/ 2) A A 1 A 6*A - A^2 (Answer: ) (Answer: ) (Answer: ) (Answer: ) (Answer: ) (Answer: ) (Answer: ) (Answer: ) (Answer: ) (Answer: ) 6

Examples: (Order of operations) Write MATLAB expressions for F1 F5, then test them in MATLAB using A = 5, B = 4, C = 3, and D = 2. F1 F2 F3 F4 F5 A B C D 2 A B C 1 D 3 A 1 A A - B 3/4 1 1 B 3 7

MATRICES, VECTORS AND SCALAR Matrices are the basic elements of the MATLAB environment. A matrix is a two-dimensional array consisting of m rows and n columns. Special cases are; Column vectors (n = 1) and Row vectors (m = 1). However, MATLAB can also be used to work with variables defined by a single value called scalars. 8

SCALAR In the MATLAB environment, a matrix is a rectangular array of numbers. Special meaning is sometimes attached to 1-by-1 matrices, which are called scalars. It is better to think of everything as a matrix. Example: Type of quantity Mathematical notation: MATLAB notation: Vector A= 1 2 3 4 A = [1 2 3 4] Scalar x = 2 x = 2 9

VECTORS A vector is an ordered list of numbers. A vector is a special case of a matrix. As discussed earlier, an array of dimension 1 n is called a row vector. Whereas an array of dimension m 1 is called a column vector. The elements of vectors in MATLAB are enclosed by square brackets and are separated by spaces or by commas. 10

ENTERING A VECTOR For example, to enter a row vector v, type in the command window. >> v = [1 4 7 10 13] v = 1 4 7 10 13 Column vectors are created in a similar way, however, semicolon (;) must separate the components of a column vector, >> w = [1;4;7;10;13] w = 1 4 7 10 13 11

On the other hand, a row vector is converted to a column vector using the transpose operator. The transpose operation is denoted by an apostrophe or a single quote ('). For example >> w = v' w = 1 4 7 10 13 12

Thus, v(1) is the first element of vector v, v(2) its second element, and so forth. For example >> v(4) ans= 10 Furthermore, to access blocks of elements, we use MATLAB's colon notation ( : ). For example, to access the first three elements of v, we write, >> v(1:3) ans = 1 4 7 13

Or, all elements from the third through the last elements, >> v(3:end) ans = 7 10 13 Where end signifies the last element in the vector. If v is a vector, writing >> v(:) Produces a column vector. Whereas writing >> v(1:end) Produces a row vector. 14

COLON OPERATOR ( : ) Suppose you want to create a vector of values running from 1 to 9. Here s how to do it without typing each number: >> X = 1: 9 X= 1 2 3 4 5 6 7 8 9 The notation 1:9 is used to represent a vector of numbers running from 1 to 9 in increments of 1. The increment can be specified as the second of three arguments: >> X = 0: 2: 10 X= 0 2 4 6 8 10 15

You can also use fractional or negative increments, for example. >> A= (0: 0.1: 1) A= 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 1 Or >> A= (10: -1: 0). A= 10 9 8 7 6 5 4 3 2 1 0 16

MATHEMATICAL OPERATIONS ON VECTORS You can perform mathematical operations on vectors. For example, if X= (0: 2: 10); Then to square the elements of the vector X, type >> X.ˆ2 ans = 0 4 16 36 64 100 The period in this expression is very important; it says that the numbers in X should be squared individually, or element-by-element. Typing Xˆ2 would tell MATLAB to use matrix multiplication to multiply X by itself and would produce an error message in this case. 17

Similarly, you must type.* or./ if you want to multiply or divide vectors element-by-element. For example, to multiply the elements of the vector X by the corresponding elements of the vector Y, where Y is >>Y=[4-3 5-2 8 1] Y= 4-3 5-2 8 1 You should type >> X.*Y ans = 0-6 20-12 64 10 18

Most MATLAB operations are, by default, performed element-by-element. For example, you do not type a period for addition and subtraction. And you can type exp(x) to get the exponential of each number in X. But while adding or subtracting two vectors their size should be the same. For example, >> X+Y ans= 4-1 9 4 18 11 19

MORE EXAMPLES ON VECTORS >> V=[1 3, sqrt(9)] V = 1 3 3 >> V2=[3+4 5] V2 = 7 5 >> V3=[3 +4 5] V3 = 3 4 5 >> V+V3 ans = 4 7 9 20

>> V4= 3*V V4 = 3 9 9 >> C= [1; 3; sqrt(5)] "Entering a column vector" C = 1.0000 3.0000 2.2361 >> C2= [3; 4; 5] C2 = 3 4 5 >> C3= 2*C - 3*C2 C3 = -7.0000-6.0000-10.5279 21

22