A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

Similar documents
A QUICK INTRODUCTION TO MATLAB

Lecture 1: What is MATLAB?

Chapter 1 Introduction to MATLAB

Introduction to Scientific and Engineering Computing, BIL108E. Karaman

A very brief Matlab introduction

Lecturer: Keyvan Dehmamy

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

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

Math 2250 MATLAB TUTORIAL Fall 2005

Introduction to Matlab

Matlab Workshop I. Niloufer Mackey and Lixin Shen

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

Some elements for Matlab programming

A General Introduction to Matlab

Programming in Mathematics. Mili I. Shah

Matlab Tutorial, CDS

1 Introduction to Matlab

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

Introduction to MATLAB

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

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

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

Matlab Tutorial and Exercises for COMP61021

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

Introduction to MATLAB

Introduction to Engineering gii

Introduction to MATLAB

MATLAB Constants, Variables & Expression. 9/12/2015 By: Nafees Ahmed

Grace days can not be used for this assignment

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

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

Finding, Starting and Using Matlab

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

1. Register an account on: using your Oxford address

What is MATLAB? What is MATLAB? Programming Environment MATLAB PROGRAMMING. Stands for MATrix LABoratory. A programming environment

Arithmetic and Logic Blocks

What is MATLAB and howtostart it up?

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

A 30 Minute Introduction to Octave ENGR Engineering Mathematics Tony Richardson

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

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

A Guide to Using Some Basic MATLAB Functions

Script started on Thu 25 Aug :00:40 PM CDT

MAT 343 Laboratory 2 Solving systems in MATLAB and simple programming

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

Quick MATLAB Syntax Guide

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

AMS 27L LAB #2 Winter 2009

Learning from Data Introduction to Matlab

Mathworks (company that releases Matlab ) documentation website is:

Introduction to MATLAB for CSE390

Matlab Tutorial for COMP24111 (includes exercise 1)

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

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

Introduction to GNU-Octave

Introduction to MATLAB for Numerical Analysis and Mathematical Modeling. Selis Önel, PhD

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

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

Introduction to Matlab

An Introduction to MATLAB and the Control Systems toolbox Aravind Parchuri, Darren Hon and Albert Honein

Introduction to MATLAB

Matlab Programming Introduction 1 2

Fundamentals of MATLAB Usage

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

Attia, John Okyere. Control Statements. Electronics and Circuit Analysis using MATLAB. Ed. John Okyere Attia Boca Raton: CRC Press LLC, 1999

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

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

Dr Richard Greenaway

MATLAB Workshop Dr. M. T. Mustafa Department of Mathematical Sciences. Introductory remarks

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

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

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

Introduction to MatLab. Introduction to MatLab K. Craig 1

LAB 1 General MATLAB Information 1

EP375 Computational Physics

Introduction to Matlab

General MATLAB Information 1

Introduction to Matlab

QUICK INTRODUCTION TO MATLAB PART I

CSE/Math 485 Matlab Tutorial and Demo

Dr Richard Greenaway

INTRODUCTION TO MATLAB, SIMULINK, AND THE COMMUNICATION TOOLBOX

MATLAB Project: Getting Started with MATLAB

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

AN INTRODUCTION TO MATLAB

Part V Appendices c Copyright, Todd Young and Martin Mohlenkamp, Department of Mathematics, Ohio University, 2017

MATLAB TUTORIAL WORKSHEET

MATLAB Project: Getting Started with MATLAB

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

Introduction to MATLAB

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

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

Chapter 2. MATLAB Basis

Introduction to Matlab

Introduction to MATLAB for Engineers, Third Edition

Introduction to Matlab

Introduction to MATLAB

Introduction to MATLAB Practical 1

Stokes Modelling Workshop

Introduction to MATLAB

Transcription:

A QUICK INTRODUCTION TO MATLAB Very brief intro to matlab Intro to matlab getting started Basic operations and a few illustrations This set is indepent from rest of the class notes. Matlab will be covered in recitations and occasionally in class To start type matlab under a unix terminal (or click icon under windows). You will get a matlab GUI with a command window that has the prompt >>. 2-1 2-2 I prefer to use matlab without the GUI [especially for the demos given in class]. In linux or mac OS this is done by typing into a terminal the command % matlab -nodesktop instead of % matlab To exit matlab use exit or quit >> quit Getting Help Most of the help for matlab is online. In the GUI you can click on the? icon. Often it is faster to get help by typing into the matlab window Examples >> help topic >> help or >> help rref or >> help punct Alternatively you can get the same info in a pop-out window by typing >> doc topic 2-3 Text matlab MLintro 2-4 Text matlab MLintro 2-3 2-4

For example >> doc diary gave this Example >> help mod mod modulus (signed remainder after division).... followed by a few lines of explanation......... then shows related function (s) See also REM. >> help or >> doc by itself will list the help topics Same thing as clicking the? icon in the GUI. >> a = 25; b = 3; >> mod(a,b) 1 >> mod(a,5) 0 >> mod(25.2,2) 1.2000 2-5 Text matlab MLintro 2-6 Text matlab MLintro 2-5 2-6 Basic Operations in Matlab The following is on the basics of matlab. It starts with some basic operations and the help command. A useful command I used to generate some of these examples is >>diary filename. This is equivalent to a typescript. Everything displayed on screen is saved in a file. [useful for homeworks] In what follows Everything that starts with >> is what I typed into the matlab prompt. Simple operations >> 4+6+3 This is what I typed in These lines are matlab s 13 answer >> 4*20+ 3*57 + exp(-0.1) This is what I typed in These lines are matlab s 251.9048 answer Note ing versus not ing command with semi-colon. >> a + 2 <----- do command + display result <----- results of operation shown 25 >> a+2; <----- do command - do not display result >> <----- result not displayed 2-7 Text matlab MLintro 2-8 Text matlab MLintro 2-7 2-8

Squaring and powers >> a = 12; >> a^2 144 >> a^4 20736 Right/Left divide (/ and \) >> a = 12; b = 3; >> a/b 4 >> a\b 0.2500 >> b/a 0.2500 more, disp, format >> more on more on allows you to scroll page by page disp(x) simply displays x without fillers format selects format for displaying results Options format short, long, rat,... >> format short >> pi 3.1416 Important because these have their equivalent versions for matrices 2-9 Text matlab MLintro 2-10 Text matlab MLintro 2-9 2-10 >> format long >> pi 3.141592653589793 >> format rat >> pi 355/113 Also useful format compact [avoids empty line feeds.. useful for homeworks] The command >> who lists the variable currently stored >> who your variables are a ans b >> See also >> whos which has more detail 2-11 Text matlab MLintro 2-12 Text matlab MLintro 2-11 2-12

Earlier we invoked exp which is the exponential function. Get info by typing >> help exp exp exponential. answer exp(x) is the exponential of the elements of x, e to the x. for complex z=x+i*y, exp(z) =... + a few more lines of explanation ing with see also log, log10, expm, expint. overloaded methods help sym/exp.m Explore the other elementary functions >> help elfun will list all the elementary functions used by matlab - A long list that starts like this elementary math functions. trigonometric. sin - sine. sinh - hyperbolic sine. asin - inverse sine. asinh - inverse hyperbolic sine. cos - cosine... 2-13 Text matlab MLintro 2-14 Text matlab MLintro 2-13 2-14 Complex Numbers >> c = 1-2i c = 1.0000-2.0000i >> conj(c) 1.0000 + 2.0000i >> c*conj(c) 5 >> abs(c) 2.2361 Note abs(c) is the modulus of c Matrices To define a matrix enter entries row by row, separated by a ; >> [1 1 1 ; 2 2 2 ; 3 3 3 ] 1 1 1 2 2 2 3 3 3 Could use commas for separating columns (not required) >> [ 1, 1, 1 ; 2, 2, 2 ; 3, 3, 3] ; 2-15 Text matlab MLintro 2-16 Text matlab MLintro 2-15 2-16

So, separates columns and ; separates rows. The above matrix can also be defined as >> [[1;2;3],[1;2;3], [1;2;3]] Can use matrices as blocks [very convenient!] >> B = [A, A] B = 1 1 1 1 1 1 2 2 2 2 2 2 3 3 3 3 3 3 Two important special matrix functions eye(n) and zero(n) >> eye(5) Identity matrix of size 5 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 1 It is enough to say eye(5) in this example but... Show the result of the command >> C = [ A, -A; A*A, 2*A] 2-17 Text matlab MLintro 2-18 Text matlab MLintro 2-17 2-18 eye is defined for rectangular matrices too >> eye(6,3) 1 0 0 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 zeros(m) or zeros(m,n) is defined similarly >> zeros(3,4) 0 0 0 0 0 0 0 0 0 0 0 0 2-19 Text matlab MLintro Defining a vector through loop constructs >> start=0; inc=2; last=12; >> startinclast 0 2 4 6 8 10 12 >> 0212 0 2 4 6 8 10 12 Can also use real numbers start = 0.0; inc = 0.15; last = 1.0; >> startinclast 0 0.1500 0.3000 0.4500 0.6000 0.7500 0.9000 2-20 Text matlab MLintro 2-19 2-20

>> x = 00.151 x = 0 0.1500 0.3000 0.4500 0.6000 0.7500 0.9000 Quite convenient for doing simple plots (see later) Can use loop constructs in matrices as well >> [14; 47] 1st row = 14 = 1 2 3 4 2nd row = 47 = 4 5 6 7 1 2 3 4 4 5 6 7 >> [0.00.10.5; 2.10.23.1] Must have same number of entries in the 2 rows 0 0.1000 0.2000 0.3000 0.4000 0.5000 2.1000 2.3000 2.5000 2.7000 2.9000 3.1000 The function size Everything in matlab is considered a matrix. size(x) dimensions of the object x gives the >> x = x = 0.00.10.8; 0.0 0.1... 0.8 (9 entries) >> size(x) 1 9 <---- 1 row, 9 columns >> [14; 47]; >> size(a) 2 4 <---- 2 rows 4 columns >> >> size(pi) number pi = a scalar 1 1 <---- 1 row 1 column 2-21 Text matlab MLintro 2-22 Text matlab MLintro 2-21 2-22 Vector operations >> x+y; adding 2 vectors of same shape >> 0.15*x -.0*y; linear comb. of x and y >> y = exp(-x) point-wise exponential of -x y = 1.0000 0.8607 0.7408 0.6376 0.5488 0.4724 0.4066 cannot square a vector >> >> [1 2 3]^2 Error using ^ Inputs must be a scalar and a square matrix. To compute elementwise POWER, use POWER (.^) instead. Pointwise (array) product >> a = [2, 3 4] ; b = [ 0 5 6] ; >> c = a.* b c = 0 15 24 Let us go back to z = x 2. To square the components of x, do >> y = x.^ 2 y = 0 0.0225 0.0900 0.2025 0.3600 0.5625 0.8100 or >> y = x.* x y = 0 0.0225 0.0900 0.2025 0.3600 0.5625 0.8100 2-23 Text matlab MLintro 2-24 Text matlab MLintro 2-23 2-24

Colum/row access; submatrices [Try these!] Simple plotting >> randn(5,10); generate 5x10 random matrix >> B = A(25,510); subarray of rows 2 to 5 and columns 5 to 10 >> B = A(125,2210); extract odd rows and even columns of A >> r = A(1,) 1st row of A >> c = A(,35) column 3 to 5 of A >> A(,3) = A(,3)+0.5*A(,1) Add 1/2 of col. 1 to col. 3 Very useful r = r() forces r to be a column vector 2-25 Text matlab MLintro Matlab provides powerful graphics capabilities 2D plots, 3D surfaces. The simplest command >> plot(x,y) causes matlab to popout a window which has the following plot 0.9 0.8 0.7 0.6 0.5 0.4 0.3 0.2 0.1 0 0 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 2-26 Text matlab MLintro 2-25 2-26 Try the following and explain what they do x = [00.012*pi] ; y = x.* cos (x) ; plot(x,y); hold on z = 1./ ( 1/6 + y.^2); plot(x,z, r-- ); plot([0, 2*pi],[0 0]); plot([0, 0],[0, 7]); axis([-1 7-4 8]) Basic operators Standard arithmetic operators +,,, / Unary operations (for example A). Back-slash operator x = A\b where A is a matrix and b a vector (or matrix) then x = A 1 b. [to be seen later in the class.] 2-27 Text matlab MLintro 2-28 Text matlab MLintro 2-27 2-28

Relational operators. Example >> a = 1; b = 0; c = 2; >> a+b+c == c+a+b 1 Not to confuse with = - Equal == - Not equal = - Less than < - Greater than > - Less than or equal <= - Greater than or equal >= >> a+b+c = c+a+b??? Error Assignment statements cannot produce a result. 2-29 Text matlab MLintro Comparisons can be done on vectors and matrices >> a = 1220 a = 1 3 5 7 9 11 13 15 17 19 >> b = 2221 b = 2 4 6 8 10 12 14 16 18 20 >> a == b 0 0 0 0 0 0 0 0 0 0 >> a+1 == b 1 1 1 1 1 1 1 1 1 1 Note 1 means true, 0 means false 2-30 Text matlab MLintro 2-29 2-30 Conditionals Loops If statement Simplest form if (logical-expression) For loop Simplest form for j=1m If statement More general form if (logical-expression) elseif (logical-expression) else 2-31 Text matlab MLintro Examples of other constructs for j=0331 for j=100-10 for j=0.10.12.4 2-32 Text matlab MLintro 2-31 2-32

Example Simple version of script to compute the square root of 5. [shown in class] tol = 1.e-10; a = 5; x = a; for i=1100 x = 0.5*(x+a/x); if abs(x^2-a) < tol break; While loop Generic form while (logical) For the square example you can achieve the same result with a while loop tol = 1.e-10; a = 5; x = a; while abs(x^2 - a) > tol x = 0.5*(x+a/x); The above needs a fix [potential for infinite loop] 2-33 Text matlab MLintro 2-34 Text matlab MLintro 2-33 2-34