MATLAB Lecture 1. Introduction to MATLAB

Similar documents
Computer Packet 1 Row Operations + Freemat

Introduction to MATLAB

MAT 343 Laboratory 2 Solving systems in MATLAB and simple programming

Mathematics 4330/5344 #1 Matlab and Numerical Approximation

Teaching Manual Math 2131

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

Fundamentals of MATLAB Usage

Online Technology Guide for Elementary Linear Algebra, 6e Larson/Falvo

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

Introduction to MATLAB

Lecture 2: Variables, Vectors and Matrices in MATLAB

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

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

MATLAB: The Basics. Dmitry Adamskiy 9 November 2011

ME305: Introduction to System Dynamics

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

MATLAB BEGINNER S GUIDE

Matlab (Matrix laboratory) is an interactive software system for numerical computations and graphics.

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

CDA5530: Performance Models of Computers and Networks. Chapter 8: Using Matlab for Performance Analysis and Simulation

MATLAB Premier. Middle East Technical University Department of Mechanical Engineering ME 304 1/50

MATH 5520 Basics of MATLAB

MATH 3511 Basics of MATLAB

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

LAB 2: Linear Equations and Matrix Algebra. Preliminaries

CDA6530: Performance Models of Computers and Networks. Chapter 4: Using Matlab for Performance Analysis and Simulation

2.0 MATLAB Fundamentals

Chapter 1 MATLAB Preliminaries

MATLAB GUIDE UMD PHYS401 SPRING 2012

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

CDA6530: Performance Models of Computers and Networks. Chapter 4: Using Matlab for Performance Analysis and Simulation

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

CS129: Introduction to Matlab (Code)

Chapter 1 Introduction to MATLAB

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Introduction to MATLAB

Grace days can not be used for this assignment

CSE/Math 456 and CSE/Math 550 Matlab Tutorial and Demo

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

Matrices. Matrices on the TI Creating, Storing, and Displaying Matrices Using Mathematical Functions with Matrices...

Solving Systems of Equations Using Matrices With the TI-83 or TI-84

LAB 2 VECTORS AND MATRICES

MATLAB Premier. Asst. Prof. Dr. Melik DÖLEN. Middle East Technical University Department of Mechanical Engineering 10/30/04 ME 304 1

1 Overview of the standard Matlab syntax

Graphics calculator instructions

Introduction to Engineering gii

MATH 423 Linear Algebra II Lecture 17: Reduced row echelon form (continued). Determinant of a matrix.

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

Introduction to MatLab. Introduction to MatLab K. Craig 1

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

Introduction to Matlab

Introduction to MATLAB for Engineers, Third Edition

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

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

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

Introduction to MATLAB 7 for Engineers

Matlab Tutorial, CDS

MATLAB Lesson I. Chiara Lelli. October 2, Politecnico di Milano

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

Math Scientific Computing - Matlab Intro and Exercises: Spring 2003

A GUIDE FOR USING MATLAB IN COMPUTER SCIENCE AND COMPUTER ENGINEERING TABLE OF CONTENTS

BEGINNING MATLAB. R.K. Beatson Mathematics Department University of Canterbury. 2 Matlab as a simple matrix calculator 2

A Guide to Using Some Basic MATLAB Functions

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

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

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

Introduction to MATLAB

Learning from Data Introduction to Matlab

What is MATLAB and howtostart it up?

Programming in Mathematics. Mili I. Shah

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

Part #1. A0B17MTB Matlab. Miloslav Čapek Filip Kozák, Viktor Adler, Pavel Valtr

Fall 2015 Math 337. Basic MatLab

Dr Richard Greenaway

1 Week 1: Basics of scientific programming I

QUICK INTRODUCTION TO MATLAB PART I

Introduction to MATLAB

CME 192: Introduction to Matlab

Introduction to MATLAB

0_PreCNotes17 18.notebook May 16, Chapter 12

Introduction to MATLAB

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

Lab 1 Intro to MATLAB and FreeMat

A QUICK INTRODUCTION TO MATLAB

Introduction to Matlab for Econ 511b

BRUSH UP ON MATLAB UNIVERSITY OF PAVIA. Industrial Control FACULTY OF ENGINEERING. Prof. Lalo Magni

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

An Introduction to MATLAB See Chapter 1 of Gilat

A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

Introduction to Matlab

A General Introduction to Matlab

Computer Vision. Matlab

Computational Foundations of Cognitive Science. Inverse. Inverse. Inverse Determinant

MATLAB BASICS. M Files. Objectives

Computational Photonics, Seminar 01 on Introduction into MATLAB, Page 1

EGR 111 Introduction to MATLAB

A Brief MATLAB Tutorial

A Tour of Matlab for Math 496, Section 6

Digital Image Analysis and Processing CPE

1 Introduction to Matlab

Transcription:

MATLAB Lecture 1. Introduction to MATLAB 1.1 The MATLAB environment MATLAB is a software program that allows you to compute interactively with matrices. If you want to know for instance the product of a matrix a with a matrix b, then you just type in a b and the result appears immediately on screen. MATLAB has a wide range of functions to compute the determinant, inverse, LUdecomposition, eigenvalues, etc. While working with MATLAB you get on your screen always a prompt. This is >>. This means that MATLAB is ready to accept commands. If you press the enter key, the command gets executed. Unless you finish your command with a semicolon (;), MATLAB displays the result on screen. To copy the output also to a file, you can invoke the command diary. For example, >> diary( h:\session.txt ) will cause MATLAB to copy all user input and output to the file h:\session.txt. The diary off command suspends the output to the file, whereas diary on turns it back on. The command who displays the list all variables currently in use. With clear you deallocate memory space for those variables. If you want to use some or all of the variables with their values in a later MATLAB session you can save the workspace variables with the command save( h:\myvars ) which creates the binary MAT-file named myvars.mat. With load( h:\myvars ), the workspace variables are loaded from the file myvars.mat. Make sure you understand the difference between the file produced by diary and the file made by save! With diary we create a human readable text file, with save we make a binary mat-file. MATLAB records the commands you typed in. You can scroll through the list of executed commands with the arrow keys and. The arrows and enable to go to the location in the command where edits are necessary. This is convenient to correct type errors in a command, or if you need to execute lots of slightly different versions of the same instruction. MATLAB has online help facilities. The command help displays information in the current window, helpwin opens a new window, helpdesk launches your internet browser. 1.2 A short introduction to MATLAB MATLAB follows a C like syntax. For example, the equality symbol = is used for assignment while == must be used to test equality. The matrix is the fundamental data structure in MATLAB. 1.2.1 Entering variables NUMBERS < name-of-the-variable > = < value > e.g.: >> 0.65 makes that MATLAB assigns the value 0.65 to the variable x. MATLAB confirms : 0.6500 To suppress this confirmation, end a command with ; (a semicolon). Jan Verschelde, April 7, 2008 UIC, Dept of Math, Stat & CS MATLAB Lecture 1, page 1

MATRICES These are defined row-wise. A square bracket [ means to MATLAB that you wish to enter a matrix. The input of a matrix is terminated by a ]. You can enter a matrix in two ways. For instance, you want then you either type a = >> a = [ 0 1 2 ; ; 6 7 8 ] or >> a = [ 0 1 2 6 7 8 ] In both cases, MATLAB answers : 0 1 2 6 7 8 a = 0 1 2 6 7 8 1.2.2 The output The content of a variable can be gotten just by typing in the name of the variable, followed by pressing the enter key. For example, >> x MATLAB answers: 0.6500 This is the standard output format of MATLAB. To obtain the scientific notation, you switch formats as follows >> format short e After typing >> x MATLAB answers then with: 6.500e-001 If you replace in the format commands short by long, you see more decimal places. To return to the standard output format, you type >> format short Jan Verschelde, April 7, 2008 UIC, Dept of Math, Stat & CS MATLAB Lecture 1, page 2

1.2.3 Selecting en transposing You can select elements from a matrix : a(i, j) returns the element on the i-th row and j-th column. a(i, :) returns the entire i-th row of a. a(:, j) returns the entire j-th column of a. a(i : j, k : l) returns the matrix a(i... j, k... l). e.g.: >> m = a(1:2,2:3) returns m = 1 2 4 5 To transpose a matrix, you type an accent after the matrix. For example >> b = a returns b = 0 3 6 1 4 7 2 5 8 1.2.4 Calculations in MATLAB Operators you can use are the following: + addition subtraction multiplication / right division (a/b means a inv(b) ) \ left division (a\b means inv(a) b ) You can apply these operators on numbers as well as on matrices. If you want to compute something in MATLAB, then you type the expression after the prompt, e.g.: >> x+2 MATLAB answers: ans = 2.6500 The result is by default assigned to the variable ans ( = answer). You can also assign the result to a variable, e.g.: >> y = ans Then MATLAB answers: y = 2.6500 1.2.5 Some elementary functions in MATLAB log, exp, sin, cos, tan,... The argument has to be enclosed in round brackets, e.g.: log(1). abs returns the absolute value round returns the nearest integer number Jan Verschelde, April 7, 2008 UIC, Dept of Math, Stat & CS MATLAB Lecture 1, page 3

1.2.6 Matrix operations inv(a) computes the inverse of a det(a) computes the determinant of a cond(a) computes the condition number of a rank(a) computes the rank of a size(a) returns number of rows and columns of a norm(a) computes the norm of a vector or a matrix eye(n) gives the n-dimensional unit matrix diag(d) returns a diagonal matrix with elements d(i) on its diagonal rand(n, m) generates a random n-by-m matrix orth(a) returns an orthonormal basis for the range of a lu(a) returns the factors of the LU-decomposition of a After typing >> [l, u] = lu(a) MATLAB returns two matrices l and u: l is a (eventually permuted) lower triangular matrix with ones on the diagonal and u is an upper triangular matrix. To solve a linear system a b, you can use the MATLAB division >> x=a\b eig(a) computes eigenvalues and eigenvectors. After typing eig(a) MATLAB gives you a vector with all eigenvalues of a. To obtain the eigenvectors as well, you type >> [v,d]=eig(a) The two matrices v and d on return have the following meaning: d is a diagonal matrix with the eigenvalues of a on its diagonal, and v contains in its columns the eigenvectors of a. Without roundoff, a*v == v*d. 1.2.7 Programs You can collect a sequence of MATLAB commands in one file, which should have the.m extension. This sequence of commands is then executed when you type in the name of that file (without the extension). To ensure that MATLAB will find your program, you may have to adjust MATLAB search path with the command path Typing path displays MATLAB s current search path. To append a directory to this path, type path(path, c:\temp\my files ) for instance. Jan Verschelde, April 7, 2008 UIC, Dept of Math, Stat & CS MATLAB Lecture 1, page 4

1.3 Assignments 1. Type tour in a MATLAB session, follow the Intro to MATLAB link and browse through the various aspects that interest you. 2. Visit http://www.mathworks.com/matlabcentral/fileexchange, this is the site of the MATLAB Central File Exchange which contains user contributions. Download one file, and answer the following two questions: (1) Why did you choose this type of application? (2) What did you learn from it? 3. Type in u = [2 3 1 4]; v = [4 3 2 1]; w = [1 2 3 4]; Give MATLAB commands to create a matrix a that has as rows u, v, and w. The matrix a defines the augmented matrix of a 3x3 linear system with right hand size vector in its fourth column. Compute its reduced row echelon form with rref. What is the solution of the corresponding linear system? Verify that the solution vector x makes b - a*x (almost) zero. [ ] [ 1 2 2 4. The linear system A b is defined by A = and b = 2 3 5 Give the MATLAB commands to enter A and b, to find x, the solution to A b, and finally to compute the norm of b Ax. { 2x1 + 3x 5. Consider the linear system 2 = 1 2x 1 + x 2 = 3. Give all the MATLAB commands to define this linear system in the format A b, with [x 1 x 2 ] T, i.e.: what are the commands to define A and b? What is the solution of this system? 6. Type a = [1 2 3; 4 5 6; 7 8 9], followed by [l, u] = lu(a). Test whether l u equals a. Notice that u is upper triangular. Explain why l is not lower triangular. (hint: type help lu.) 7. Generate a random 6 6 matrix a and compute the LU-decomposition of a, storing the factors of the LU-decomposition in the matrices l and u. (a) Test whether l u equals a by computing r = a l u. Is r the zero matrix? Interpret the results and explain what went wrong. (b) The determinant of a product of two square matrices is the product of the determinants of the factors in the product. Test whether det(a) = det(l) det(u). What is the most efficient way to compute the determinant of a matrix, given its LU-decomposition? 8. Create a random 3-by-3 matrix a and compute its eigenvectors and eigenvalues via [v, d] = eig(a). (a) Verify the accuracy of the computed results via the norm of the matrix a v v d. Interpret the result. (b) The product of a with the kth column of v is d(k, k) times the k column of v. Give the MATLAB commands and the output to verify this property of eigenvectors and eigenvalues. 9. A simplex is the three dimensional analogue to a triangle, spanned by four points: a = [2 3-1]; b = [2 4 9]; c = [3-1 8]; d = [4 3 1]; Start by typing in this line in a MATLAB session. To compute the volume of this simplex, we proceed as follows: (1) define the matrix whose rows are b - a, c - a, and d - a; (2) compute the absolute value of the determinant of this matrix; (3) divide this value by six. What is the volume of this simplex? Give all relevant MATLAB commands. ]. Jan Verschelde, April 7, 2008 UIC, Dept of Math, Stat & CS MATLAB Lecture 1, page 5