A QUICK INTRODUCTION TO MATLAB

Similar documents
A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

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

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?

Some elements for Matlab programming

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.

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

Matlab Tutorial and Exercises for COMP61021

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

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

Mathworks (company that releases Matlab ) documentation website is:

Learning from Data Introduction to Matlab

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?

What is MATLAB and howtostart it up?

Introduction to GNU-Octave

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

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

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

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

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

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

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

MATLAB Project: Getting Started with MATLAB

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

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

Introduction to MATLAB

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

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

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

Computer Project: Getting Started with MATLAB

Transcription:

A QUICK INTRODUCTION TO MATLAB Very brief intro to matlab Basic operations and a few illustrations This set is independent from rest of the class notes. Matlab will be covered in recitations and occasionally in class 2-1

Intro to matlab getting started 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-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 2-3 Text: matlab MLintro 2-3

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 >> help topic Examples >> help or >> help rref or >> help punct Alternatively you can get the same info in a pop-out window by typing: >> doc topic 2-4 Text: matlab MLintro 2-4

For example: >> doc diary gave this: >> help or >> doc by itself will list the help topics Same thing as clicking the? icon in the GUI. 2-5 Text: matlab MLintro 2-5

Example: >> help mod mod modulus (signed remainder after division).... followed by a few lines of explanation......... then: shows related function (s): See also REM. >> a = 25; b = 3; >> mod(a,b) 1 >> mod(a,5) 0 >> mod(25.2,2) 1.2000 2-6 Text: matlab MLintro 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. 2-7 Text: matlab MLintro 2-7

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: ending versus not ending 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-8 Text: matlab MLintro 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 Important because these have their equivalent versions for matrices 2-9 Text: matlab MLintro 2-9

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 2-10 Text: matlab MLintro 2-10

>> format long >> pi 3.141592653589793 >> format rat >> pi 355/113 Also useful: format compact [avoids empty line feeds.. useful for homeworks] 2-11 Text: matlab MLintro 2-11

The command >> who lists the variable currently stored >> who your variables are: a ans b >> See also: >> whos which has more detail 2-12 Text: matlab MLintro 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 ending with see also log, log10, expm, expint. overloaded methods help sym/exp.m 2-13 Text: matlab MLintro 2-13

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-14 Text: matlab MLintro 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 2-15 Text: matlab MLintro 2-15

Matrices To define a matrix enter entries row by row, separated by a ; >> A = [1 1 1 ; 2 2 2 ; 3 3 3 ] A = 1 1 1 2 2 2 3 3 3 Could use commas for separating columns (not required): >> A = [ 1, 1, 1 ; 2, 2, 2 ; 3, 3, 3] ; 2-16 Text: matlab MLintro 2-16

So:, separates columns and ; separates rows. The above matrix can also be defined as >> A = [[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 Show the result of the command: >> C = [ A, -A; A*A, 2*A] 2-17 Text: matlab MLintro 2-17

Two important special matrix functions eye(n) and zero(n) >> A = eye(5) Identity matrix of size 5 A = 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... 2-18 Text: matlab MLintro 2-18

eye is defined for rectangular matrices too >> A = eye(6,3) A = 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: >> A = zeros(3,4) A = 0 0 0 0 0 0 0 0 0 0 0 0 2-19 Text: matlab MLintro 2-19

Defining a vector through loop constructs >> start=0; inc=2; last=12; >> start:inc:last 0 2 4 6 8 10 12 >> 0:2:12 0 2 4 6 8 10 12 Can also use real numbers start = 0.0; inc = 0.15; last = 1.0; >> start:inc:last 0 0.1500 0.3000 0.4500 0.6000 0.7500 0.9000 2-20 Text: matlab MLintro 2-20

>> x = 0:0.15:1 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: >> A = [1:4; 4:7] 1st row = 1:4 = 1 2 3 4 2nd row = 4:7 = 4 5 6 7 A = 1 2 3 4 4 5 6 7 >> A = [0.0:0.1:0.5; 2.1:0.2:3.1] Must have same number of entries in the 2 rows A = 0 0.1000 0.2000 0.3000 0.4000 0.5000 2.1000 2.3000 2.5000 2.7000 2.9000 3.1000 2-21 Text: matlab MLintro 2-21

The function size Everything in matlab is considered a matrix. size(x) dimensions of the object x gives the >> x = x = 0.0:0.1:0.8; 0.0 0.1... 0.8 (9 entries) >> size(x) 1 9 <---- 1 row, 9 columns >> A = [1:4; 4:7]; >> size(a) 2 4 <---- 2 rows 4 columns >> >> size(pi) number pi = a scalar 1 1 <---- 1 row 1 column 2-22 Text: matlab MLintro 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. 2-23 Text: matlab MLintro 2-23

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-24 Text: matlab MLintro 2-24

Colum/row access; submatrices [Try these!] >> A = randn(5,10); generate 5x10 random matrix >> B = A(2:5,5:10); subarray of rows 2 to 5 and columns 5 to 10 >> B = A(1:2:5,2:2:10); extract odd rows and even columns of A >> r = A(1,:) 1st row of A >> c = A(:,3:5) 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 2-25

Simple plotting 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-26

Try the following commands and explain what they do x = [0:0.01:2*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]) 2-27 Text: matlab MLintro 2-27

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-28 Text: matlab MLintro 2-28

Relational operators. - Equal == - Not equal = - Less than < - Greater than > - Less than or equal <= - Greater than or equal >= Example: >> a = 1; b = 0; c = 2; >> a+b+c == c+a+b 1 Not to confuse with = : >> a+b+c = c+a+b??? Error: Assignment statements cannot produce a result. 2-29 Text: matlab MLintro 2-29

Comparisons can be done on vectors and matrices: >> a = 1:2:20 a = 1 3 5 7 9 11 13 15 17 19 >> b = 2:2:21 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-30

Conditionals If statement Simplest form: if (logical-expression) : commands : end If statement More general form: if (logical-expression) commands elseif (logical-expression) commands else commands end 2-31 Text: matlab MLintro 2-31

Loops For loop Simplest form: for j=1:m : commands : end Examples of other constructs for j=0:3:31 for j=100:-1:0 for j=0.1:0.1:2.4 2-32 Text: matlab MLintro 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=1:100 x = 0.5*(x+a/x); if abs(x^2-a) < tol break; end end 2-33 Text: matlab MLintro 2-33

While loop Generic form: while (logical) : commands : end 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); end The above needs a fix [potential for infinite loop] 2-34 Text: matlab MLintro 2-34