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

Similar documents
Matlab and Coordinate Systems

Matlab and Matrices. Math 45 Linear Algebra. David Arnold

Matlab and Coordinate Systems

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

Introduction to MATLAB

1 Introduction to Matlab

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

The Interpolating Polynomial

University of Alberta

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

A quick Matlab tutorial

Lab 1 Intro to MATLAB and FreeMat

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

MATLAB Tutorial Matrices & Vectors MATRICES AND VECTORS

Matlab Introduction. Scalar Variables and Arithmetic Operators

A Guide to Using Some Basic MATLAB Functions

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

1. Register an account on: using your Oxford address

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

Meeting 1 Introduction to Functions. Part 1 Graphing Points on a Plane (REVIEW) Part 2 What is a function?

Computer Packet 1 Row Operations + Freemat

AMS 27L LAB #2 Winter 2009

A QUICK INTRODUCTION TO MATLAB

EEE161 Applied Electromagnetics Laboratory 1

2.0 MATLAB Fundamentals

A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

A very brief Matlab introduction

Introduction to Matlab

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

Basic stuff -- assignments, arithmetic and functions

x = 12 x = 12 1x = 16

Matlab Tutorial: Basics

Performing Matrix Operations on the TI-83/84

Introduction to MATLAB

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

Introduction to Matlab

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

Introduction to MATLAB

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

MATLAB TUTORIAL WORKSHEET

Introduction to Matlab

Introduction to MATLAB for Engineers, Third Edition

Introduction to MATLAB

An Introduction to MATLAB II

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

Introduction to MATLAB LAB 1

Dr Richard Greenaway

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

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

The Very Basics of the R Interpreter

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:

Introduction to MATLAB Practical 1

ECE 3793 Matlab Project 1

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

Math 7 Elementary Linear Algebra PLOTS and ROTATIONS

Introduction to MATLAB 7 for Engineers

Chapter 2. MATLAB Fundamentals

EGR 111 Introduction to MATLAB

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

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

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

6.094 Introduction to MATLAB January (IAP) 2009

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

Grace days can not be used for this assignment

Variable Definition and Statement Suppression You can create your own variables, and assign them values using = >> a = a = 3.

APPM 2460 Matlab Basics

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

Introduction to MATLAB

Introduction to Matlab

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

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

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

Laboratory 1 Octave Tutorial

Summer 2009 REU: Introduction to Matlab

Matlab Tutorial and Exercises for COMP61021

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

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

Introduction to MATLAB Programming

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

Complex Dynamic Systems

MATLAB Project: Getting Started with MATLAB

ARRAY VARIABLES (ROW VECTORS)

Matlab Tutorial for COMP24111 (includes exercise 1)

Math 1322, Fall 2002 Introduction to Matlab: Basic Commands

SIMPLE INPUT and OUTPUT:

Introduction to Matlab

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

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

Introduction to Matlab

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

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

Introduction to MATLAB

EE 301 Signals & Systems I MATLAB Tutorial with Questions

Learning from Data Introduction to Matlab

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

Applied Calculus. Lab 1: An Introduction to R

Introduction to Scientific and Engineering Computing, BIL108E. Karaman

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.

Lab 1 Introduction to MATLAB and Scripts

How to learn MATLAB? Some predefined variables

Transcription:

Math 45 Linear Algebra David Arnold David-Arnold@Eureka.redwoods.cc.ca.us Abstract In this exercise you will learn how to enter edit vectors in. involving vectors scalars will be discussed. Prerequisites: None Operations

Table of Contents Introduction Working with Entering Row Ranges Suppressing Output s Workspace Entering Column The Transpose Operator Indexing Vector Operations Vector-Scalar Operations Vector-Vector Operations Elementwise Operations More Complicated Expressions Homework Assignment

Introduction This is an interactive document designed for online viewing. We ve constructed this onscreen documents because we want to make a conscientious effort to cut down on the amount of paper wasted at the College. Consequently, printing of the onscreen document has been purposefully disabled. However, if you are extremely uncomfortable viewing documents onscreen, we have provided a print version. If you click on the Print Doc button, you will be transferred to the print version of the document, which you can print from your browser or the Acrobat Reader. We respectfully request that you only use this feature when you are at home. Help us to cut down on paper use at the College. Much effort has been put into the design of the onscreen version so that you can comfortably navigate through the document. Most of the navigation tools are evident, but one particular feature warrants a bit of explanation. The section subsection headings in the onscreen uments are interactive. If you click on any section or subsection header in the onscreen document, you will be transferred to an identical location in the print version of the document. If you are in the print version, you can make a return journey to the onscreen document by clicking on any section or subsection header in the print document. Finally, the table of is also interactive. Clicking on an entry in the table of takes you directly to that section or subsection in the document. Working with This document is a working document. It is expected that you are sitting in front of a computer terminal where the software is installed. You are not supposed to read this document as if it were a short story. Rather, each time your are presented with a comm, it is expected that you will enter the comm, then hit the Enter key to execute the comm view the result. Furthermore, it is expected that you will ponder the result. Make sure that you completely underst why you got the result you did before you continue with the reading. Entering Row Entering row vectors is easy in. Enter the following comm at the prompt 1.

>> v=[1 2 3] There are a number of constructs introduced in this comm. To enter a vector, first enter a left bracket, the elements of the vector separated by spaces, a closing right bracket. You can also use commas to delimit the entries of your vector. >> v=[1,2,3] The = sign is s assignment operator. You use this operator to assign items to variables. To see that the row vector [1,2,3] has been assigned to the variable v, enter the following comm at the prompt. >> v Ranges Sometime you will need to enter a vector of equally spaced entries. This is easily accomplished in with the construct start:increment:finish. If you don t supply an increment, assumes that the increment is 1. >> x1=0:10 You can select your own increment. >> x2=0:2:10 You can even go wards. >> x3=10:-2:0 And you can get very fancy indeed. >> x4=0:pi/2:2*pi 1 The >> is the prompt. You must enter what comes after the prompt. Then you must hit the Enter key to execute the comm.

There will be times, particularly when plotting functions, that you will need a large number of entries in a vector. >> x=0:.1:10 Suppressing Output You can suppress the output of a comm by appending a semicolon. >> x=0:.1:10; This is particularly useful when the output is large you have no desire to see it. s Workspace You can get a listing of your variables in your workspace at any time by issuing the following comm at the prompt. >> who You can get even more information about the variables in your workspace by entering >> whos Note that the size of each of the variables in your workspace is given. You can also find the size of the vector v by entering >> size(v) The information supplied by the result of this last comm indicates that the vector v has 1 row 3 columns. Although you can think of the vector v as a matrix with 1 row 3 columns, you can also think of it as a row vector with length 3. Try the following comm at the prompt 2. 2 You can easily get help on any of s comms. For example, enter help length at the prompt to get help on the length comm.

>> length(v) Entering Column Entering column vectors is also easy in. Enter the following comm at the prompt. >> w=[4;5;6] Note that semicolons delimit the rows of a column vector. Try the following comms. >> w >> who >> whos >> size(w) The information supplied by the result of this last comm indicates that the vector w has 3 rows 1 column. Although you can think of the vector w as a matrix with 3 rows 1 column, you can also think of it as a column vector with length 3. Try the following comm at the prompt. >> length(w) The Transpose Operator You can use s transpose operator (a single apostrophe) to change a row vector to a column vector. >> y=(1:10) Or a column vector to a row vector. >> y=y Indexing Once you have defined a vector, you can easily access any of its elements with s powerful indexing comms. For example, enter the following vector.

>> x=[10,13,19,23,27,31,39,43,51] Now try each of the following comms. >> x(2) >> x(7) You can easily change an element of a vector. >> x(6)=100 You can also access a range of elements. >> x([1,3,5]) >> x(1:3) >> x(1:2:length(x)) Vector Operations There are numerous operations involving vectors scalars that are hled easily in. Vector-Scalar Operations Scalar-vector operations are straightforward. Technically, you cannot add or subtract a scalar from a vector. However, this is such a common operation that allows it. Thus, if y is a vector, the comm y+2 will add 2 to each entry in the vector. Try each of the following comms study the resulting output. >> y=1:5 >> y+2 >> y-2 >> 2*y >> y/2

Of course, these operations are equally valid when using column vectors. >> w=(1:3:20) >> w+3 >> w-11 >>.1*w >> w/10 Vector-Vector Operations First enter the following vectors. >> a=1:3 >> b=4:6 Adding subtracting vectors is natural easy. Try the following comms 3. >> a,b,a+b >> a,b,a-b Of course, these operations are equally valid when using column vectors. >> a=(1:3),b=(4:6) >> a+b,a-b However, you can arrive at unexpected results if you do not remember that is a matrix environment. >> a,b,a*b This last comm leads to an error because * is s symbol for matrix multiplication you are not using it properly 4. 3 Because no semicolons are present to prevent suppression of output, the comm a,b,a+b will display the vector a, display the vector b, then display the vector a+b. 4 We ll look at s matrix multiplication operator in later activities.

You can also get into trouble if you attempt to add (or subtract) vectors of unequal lengths. >> a=1:3,b=4:7,a+b Elementwise Operations If you wish to multiply the vectors a b on an element-by-element basis, you enter the following comm. >> a=(1:3),b=(4:6) >> a,b,a.*b The symbol.* is s elementwise multiplication operator. The output is computed by multiplying the first elements of the vectors a b, then the second elements of each vector, etc. s elementwise division operator,./, works in much the same manner. >> a,b,a./b If you wish to exponentiate on an element-by-element basis, use.ˆ. >> a,a.ˆ2 More Complicated Expressions With a little practice you will soon learn to evaluate quite complicated expressions. Suppose, for example, that you wish to evaluate the expression x 2 2x 3 for values of x ranging from 1 through 10, in increments of 1. >> x=1:10 >> y=x.ˆ2-2*x-3 Take a moment to check these results with a calculator.

Let s look at another example. Suppose that you would like to evaluate the expression sin x/x for values of x ranging from 1 to 1 in increments of 0.1 5. >> x=-1:.1:1 >> y=sin(x)./x Check these results with a calculator. s elementwise operators will also work on column vectors. >> xdata=(1:10) >> xdata.ˆ2 Homework Assignment Answer questions 1-9 on regular notebook paper. Be sure to test your responses at the computer. 1. Write the comms that generate each of the following vectors. a. b. 1 2 3 ( ) 1, 2, 1, 3 c. A column vector containing all the odd numbers from 1 to 1000. d. A row vector containing all of the even numbers from 2 to 1000. 2. If x=0:2:20, write the comm that will square each entry of x. 3. If x=[0,1,4,9,16,25], write the comm that will take the square root of each entry of x. 5 To examine a list of s elementary functions, type help elfun at the prompt.

4. If x=0:.1:1, write the comm that will raise each entry of x to the power of 2/3. 5. If x=0:pi/2:2*pi, write the comm that will take the cosine of each entry of x. 6. If x=-1:.1:1, write the comm that will take the inverse sine of each entry of x. 7. If x=-1:.1:1, write the comm that will take the exponential of each entry of x. 8. If x=linspace(0,2*pi,1000), what is the 50th entry of x? What is the length of x? 9. If k=0:100, what is the 12th entry of y=0.5.ˆk? 10. Obtain a printout of the graph resulting from the following sequence of comms. t=linspace(0,2*pi,1000); x=2*cos(3*t); y=3*sin(5*t); plot(x,y) xlabel( x-axis ) ylabel( y-axis ) title( The graph of x = 2*cos(3*t), y = 3*sin(5*t). ) Finally, just for fun, try the following comm sequence. all, comet(x,y)