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

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

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

One-dimensional Array

EGR 111 Introduction to MATLAB

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

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

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

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

1 Introduction to Matlab

Array Creation ENGR 1181 MATLAB 2

MATLAB GUIDE UMD PHYS401 SPRING 2012

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

Introduction to MATLAB for Engineers, Third Edition

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

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

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

University of Alberta

Computer Packet 1 Row Operations + Freemat

Dr Richard Greenaway

Lecture 2: Variables, Vectors and Matrices in MATLAB

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

CME 192: Introduction to Matlab

How to learn MATLAB? Some predefined variables

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

Introduction to Matlab

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

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

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

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

Mathematical Operations with Arrays and Matrices

ENGR 1181 MATLAB 02: Array Creation

A very brief Matlab introduction

ARRAY VARIABLES (ROW VECTORS)

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

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

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

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

Course Layout. Go to follow instr. Accessible within campus (only for the first download)

Matlab- Command Window Operations, Scalars and Arrays

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

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Matlab Introduction. Scalar Variables and Arithmetic Operators

MATLAB SUMMARY FOR MATH2070/2970

Introduction to Matlab. By: Hossein Hamooni Fall 2014

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

Identity Matrix: >> eye(3) ans = Matrix of Ones: >> ones(2,3) ans =

Lab 1 Intro to MATLAB and FreeMat

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

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

An Introduction to MATLAB II

1 Introduction to MATLAB

PART 1 PROGRAMMING WITH MATHLAB

2 Second Derivatives. As we have seen, a function f (x, y) of two variables has four different partial derivatives: f xx. f yx. f x y.

Laboratory 1 Introduction to MATLAB for Signals and Systems

Complex Dynamic Systems

Introduction to MATLAB 7 for Engineers

Chapter 1 Introduction to MATLAB

MATLAB Tutorial III Variables, Files, Advanced Plotting

Introduction to MATLAB

A Guide to Using Some Basic MATLAB Functions

MATLAB Project: Getting Started with MATLAB

1 Introduction to MATLAB

MAT 275 Laboratory 2 Matrix Computations and Programming in MATLAB

Introduction to MATLAB Practical 1

MATLAB for Experimental Research. Fall 2018 Vectors, Matrices, Matrix Operations

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

Laboratory 1 Octave Tutorial

Introduction to MATLAB

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

Basics: vectors, matrices, arrays

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

MATLAB TUTORIAL WORKSHEET

SIMPLE INPUT and OUTPUT:

AN INTRODUCTION TO MATLAB

Chapter 2. MATLAB Fundamentals

6.094 Introduction to MATLAB January (IAP) 2009

Introduction to MATLAB Programming

Introduction to MATLAB

How to Use MATLAB. What is MATLAB. Getting Started. Online Help. General Purpose Commands

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

Computing With R Handout 1

Grace days can not be used for this assignment

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

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

ECE Lesson Plan - Class 1 Fall, 2001

Introduction to MATLAB

Teaching Manual Math 2131

INTRODUCTION TO MATLAB

Lecture 2. Arrays. 1 Introduction

The Mathematics of Big Data

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

Introduction to MATLAB LAB 1

Computer Project: Getting Started with MATLAB

Introduction to MATLAB

QUICK INTRODUCTION TO MATLAB PART I

x = 12 x = 12 1x = 16

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.

Matlab Tutorial and Exercises for COMP61021

AMS 27L LAB #2 Winter 2009

A Quick Introduction to MATLAB/Octave. Kenny Marino, Nupur Chatterji

Transcription:

Physics 326G Winter 2008 Class 2 In this class you will learn how to define and work with arrays or vectors. Matlab is designed to work with arrays. An array is a list of numbers (or other things) arranged in rows and/or columns. One-dimensional arrays are also called vectors, and can be row vectors (1 row, n columns) or column vectors (n rows, 1 column). We will deal with two-dimensional (and higher-dimensional) arrays next class. Suppose we have the following set of data: Time: 3 4 6 9 10 13 Dose: 12 14 15 17 18 20 We can enter these data into arrays by enclosing the numbers in square brackets. For example, [3 4 6 9 10 13] produces a row vector with 6 elements. To define array variables, just use the assignment operator (=): time1 = [3 4 6 9 10 13]; You can enter the array elements separated by commas if you want: time1 = [3, 4, 6, 9, 10, 13]; does the same thing. Define row vectors time1 and dose1 containing the above data. You can define column vectors in the same way but using semicolons to separate the rows. time2 = [3; 4; 6; 9; 10; 13]; produces a column vector with 6 rows. Define column vectors time2 and dose2. You can add or subtract vectors in the obvious way: time1 + dose1 time2 dose2

The vectors you are adding or subtracting have to be the same size. Since time1 is a 1 6 row vector and time2 is a 6 1 column vector, time1 + time2 will not work. Multiplication and division of arrays are more complicated stay tuned. You can create a sequence of numbers using the colon (:). Type 1:10 This generates a sequence of numbers from 1 to 10, and puts them in an array. You can give the array a name using the assignment operator (=). myseq = 1:10 To generate a sequence with a spacing between the elements different from one, you provide the first element, the spacing, and the last element. Try myseq = 1:2.5:10 The sequence will stop at the last element less than or equal to the limit specified. To see this, try myseq = 1:2:10 The spacing need not be positive. Try myseq = 1:-0.5:-10 You specify particular elements of an array by their element number, called their index. The array produced in the last example has 23 elements. The first is myseq(1), which is equal to 1. The third is myseq(3), and so on. Specifying an index that is not an integer, or is less than 1, or is greater than the length of the array gives an error. If you don t know the length of an array you can still address the last element by saying myseq(length(myseq)) To address several elements, you make an array of the indices you want. Try myseq([1,3,8]) and myseq(1:5)

You can add elements to an array to make it longer. Try myseq(24) = 99 Then type myseq(26) = 100 The element myseq(25) has not been defined, so Matlab just sets it equal to zero. You can also concatenate arrays like this: array1 = [1 2 3]; array2 = [5 6 7 8 9]; array3 = [array1, array2] or for column arrays array1 = [1; 2; 3]; array2 = [5; 6; 7]; array3 = [array1; array2] Note that the arrays you are concatenating here all have to have the same number of rows. Investigate the function CAT, which concatenates arrays. Figure out what the ' (prime) operator does to an array, as in array1 The elements of an array need not be numbers. Character strings are stored in arrays, with each element of the array being a single character. Strings have to be enclosed in single quotes. Define a string variable called name: name = Waldo What is name(3)? name(2:4)? STRCAT and STRVCAT concatenate arrays of strings. There are several ways to multiply arrays. First, you can always multiply an array by a scalar: 3*time1 Here, each element of the array is multiplied by the scalar factor. Second, you can multiply element by element with the.* (dot times) operator, as in a.*b. This gives a vector in which the nth element is the nth element of the array a multiplied by the nth element of the array b. Try

time1.* dose1 The vectors here have to have the same dimensions or you will get an error. Third, you can do matrix multiplication with *. Try time1 * time2 and time2 * time1 If you don t understand the results of these operations you need to review your old linear algebra notes! Note that in the operation a*b the number of columns in a must equal the number of rows in b. You can also divide an array by a scalar, in which case each element is divided by the scalar: dose1/3 You can divide two arrays element by element using dot-divide, a./b, in which case the nth element of the array a is divided by the nth element of the array b. time1./ dose2 We will get into matrix division next class. Element-by-element exponentiation works similarly. Figure out what happens when you do time1.^2 Now do the following exercises to get some practice working with arrays. 1. Create a row vector called vec1 which has elements going from 0 to 100 in steps of 4. Now create a column vector vec2 that is the transpose of vec1. 2. Create a new row vector vec3 that consists of elements 1 to 3 and 18 to 21 of vec1. 3. Create vec4 which has elements 4. Calculate the following: a. vec3+vec4 b. vec3./vec4 c. vec3.*vec4 d. vec4.^2 e. vec1/4 2 3 7 x, x, x... x, where x = 1.22.

5. Many functions will take arrays as arguments. Calculate a. sin(vec4) b. log(vec4)/log(x) 6. Define an array t that runs from 0 to 30 in steps of 0.1. Now make a new array called sint equal to the sine of your array t. The command PLOT(x,y) will make a simple graph of y vs. x. Try it. Plot sin t vs. t. Then plot sin t against t. t