Introduction to MATLAB

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

Matlab Programming Introduction 1 2

PROGRAMMING WITH MATLAB DR. AHMET AKBULUT

Programming in Mathematics. Mili I. Shah

Introduction to MATLAB

SMS 3515: Scientific Computing Lecture 1: Introduction to Matlab 2014

Introduction to Engineering gii

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

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

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

Welcome to EGR 106 Foundations of Engineering II

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

2.0 MATLAB Fundamentals

Finding, Starting and Using Matlab

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

Chapter 1 Introduction to MATLAB

Dr Richard Greenaway

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

Introduction to MatLab. Introduction to MatLab K. Craig 1

MATLAB Basics EE107: COMMUNICATION SYSTEMS HUSSAIN ELKOTBY

Introduction to MATLAB. Computational Probability and Statistics CIS 2033 Section 003

PART 1 PROGRAMMING WITH MATHLAB

Getting started with MATLAB

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

ECE Lesson Plan - Class 1 Fall, 2001

Introduction to MATLAB. Simon O Keefe Non-Standard Computation Group

EGR 111 Introduction to MATLAB

Introduction to MATLAB

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

Introduction to. The Help System. Variable and Memory Management. Matrices Generation. Interactive Calculations. Vectors and Matrices

AN INTRODUCTION TO MATLAB

Introduction to MATLAB

LAB 1 General MATLAB Information 1

A Guide to Using Some Basic MATLAB Functions

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

Introduction to Scientific and Engineering Computing, BIL108E. Karaman

Introduction to Matlab

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

General MATLAB Information 1

Chapter 2. MATLAB Basis

Numerical Analysis First Term Dr. Selcuk CANKURT

MATLAB The first steps. Edited by Péter Vass

Computer Programming in MATLAB

SECTION 1: INTRODUCTION. ENGR 112 Introduction to Engineering Computing

MAT 343 Laboratory 1 Matrix and Vector Computations in MATLAB

MATLAB QUICK START TUTORIAL

CME 192: Introduction to Matlab

MATLAB BASICS. M Files. Objectives

Dr Richard Greenaway

VARIABLES Storing numbers:

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

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

! The MATLAB language

Interactive Computing with Matlab. Gerald W. Recktenwald Department of Mechanical Engineering Portland State University

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

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

Introduction to Matlab

Ordinary Differential Equation Solver Language (ODESL) Reference Manual

Introduction to Matlab

Chapter 1: An Overview of MATLAB

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

Matlab Lecture 1 - Introduction to MATLAB. Five Parts of Matlab. Entering Matrices (2) - Method 1:Direct entry. Entering Matrices (1) - Magic Square

Lecturer: Keyvan Dehmamy

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

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

McTutorial: A MATLAB Tutorial

Some elements for Matlab programming

Inlichtingenblad, matlab- en simulink handleiding en practicumopgaven IWS

1 Introduction to MATLAB

Introduction to MATLAB

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

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

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

Laboratory 1 Octave Tutorial

Introduction to MATLAB

AMS 27L LAB #2 Winter 2009

Chapter 1 MATLAB Preliminaries

A QUICK INTRODUCTION TO MATLAB

Introduction to MATLAB Programming

A General Introduction to Matlab

EP375 Computational Physics

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

Introduction to Matlab

1 Introduction to MATLAB

A QUICK INTRODUCTION TO MATLAB. Intro to matlab getting started

Variables are used to store data (numbers, letters, etc) in MATLAB. There are a few rules that must be followed when creating variables in MATLAB:

1 Introduction to Matlab

AMS 27L LAB #1 Winter 2009

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

MATLAB Project: Getting Started with MATLAB

ME 121 MATLAB Lesson 01 Introduction to MATLAB

Introduction to MATLAB

MATLAB Project: Getting Started with MATLAB

Starting MATLAB To logon onto a Temple workstation at the Tech Center, follow the directions below.

Digital Image Analysis and Processing CPE

Math 2250 MATLAB TUTORIAL Fall 2005

ELEMENTARY MATLAB PROGRAMMING

QUICK INTRODUCTION TO MATLAB PART I

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

Lab 1 - Worksheet Spring 2013

Transcription:

to MATLAB Spring 2019 to MATLAB Spring 2019 1 / 39

The Basics What is MATLAB? MATLAB Short for Matrix Laboratory matrix data structures are at the heart of programming in MATLAB We will consider arrays in n-dimensions where 1D = vectors and 2D = matrices, e.t.c. In addition MATLAB is also a programming language It is an interpreted language (i.e no need to compile) Commands are executed line by line to MATLAB Spring 2019 2 / 39

The Basics Why MATLAB? 1 MATLAB is used by engineers and scientists world wide to analyze and design products and systems 2 MATLAB is a powerful programming tool for Mathematics and Statistics because it has built-in functions for performing a wide variety of mathematical operations, plotting and visualization of data. 3 MATLAB has additional packages that can be installed for specific subject areas, e.g Symbolic Math Toolbox for performing calculus on functions, Deep Learning Toolbox for exploring AI algorithms. 4 The MATLAB programming environment allows one to customize programs to fit specifications. to MATLAB Spring 2019 3 / 39

The MATLAB Development Environment Objectives Familiarize yourself with the MATLAB programming environment At the most basic level we can think of MATLAB as a very powerful graphing calculator! (with may built-in functions) Learn how to use MATLAB to: 1 execute simple commands. 2 create variables and display their values to MATLAB Spring 2019 4 / 39

The MATLAB Development Environment MATLAB WINDOWS MATLAB has 8 main windows, the first 4 are on the main screen by default Command window Command history window Workspace window Current directory window Figure window Editor window Help window Launch pad window Enter commands and variables, run programs History of commands entered in window Information about current variables Shows files in the current folder/directory Output from graphic commands Create and debug scripts and functions Get help on functions and scripts Access to tools, demos and documentation Some commands clc - Clear command window clf - Clear figure exit or quit - Quit MATLAB disp( text ) - Displays text to MATLAB Spring 2019 5 / 39

Variables and Expressions Variables and Expressions Variable - A value that can change depending on conditions or information passed to the program. In contrast, a program may have data values that are fixed constants. Expressions - These are created using values, variables previously created, mathematical operators and built in functions e.g. >> 2*cos(1) + sqrt(2) ans = 2.4948 Adding a semi-colon (;) at the end of the expression suppresses screen output to MATLAB Spring 2019 6 / 39

Variables and Expressions Expressions and formatting The default in MATLAB is to display numbers that have decimal points with 4 decimal places. There are many other options 1 format long will result in 15 decimal places >> format long >> 2*cos(1) + sqrt(2) ans = 2.494818174109374 2 format shorteng will result in 4 decimal places engineering format. >> format shorteng >> 2*cos(1) + sqrt(2) ans = 2.4948e+000 to MATLAB Spring 2019 7 / 39

Variables and Expressions Basic formats displaying π format short (default) 3.1416 format long 3.141592653589793 format rat 355/113 format bank 3.14 format short e 3.1416e+00 format long e 3.141592653589793e+00 format compact (no blank lines) to MATLAB Spring 2019 8 / 39

Variables and Expressions Creating Variables We create variables in MATLAB using an assignment statement >> variablename = expression REMARKS 1 The = is an assignment operator (unlike the mathematics equal sign, does not mean equality) 2 The expression is evaluated at the result is stored in variablename Use variable names that make sense, e.g. if a variable to store the radius the name radius makes sense, y does not! to MATLAB Spring 2019 9 / 39

Variables and Expressions Rules for variable names 1 Must begin with a letter of the alphabet, after that it may contain letters,digits, and the underscore but NO SPACES! e.g. interest rate,my num 1, e.t.c. 2 The limit to the number of characters in a variable name is 63 stored in namelengthmax. 3 MATLAB is case sensitive so INTEREST RATE is different from Interest rate, e.t.c 4 Certain words are reserved so they should not be used as variable names. 5 Names of built in functions cannot be used as variable names. to MATLAB Spring 2019 10 / 39

Variables and Expressions Some reserved variable names You should not these variables in your code ans: The result of a previous calculation computer: The type of computer you are on eps: The smallest positive number ɛ that satisfies 1 + ɛ > 1 i,j: The imaginary unit ( 1) realmax,realmin: The largest and smallest real numbers that can be represented on this machine. to MATLAB Spring 2019 11 / 39

Variables and Expressions Commands related to Variables 1 who shows variables that have been defined 2 whos shows more information about the type of variables 3 clear clears out all variables. You can also specify a specific variable to clear. 4 save saves all your variables in a file matlab.mat to MATLAB Spring 2019 12 / 39

Variables and Expressions Mathematical Operators Operator precedence rules * multiplication + addition - negation, subtraction / division (division by, e.g. 10/5 = 2) \ division (division into, e.g, 5 \10 = 2) ^ exponentiation (e.g. 5 2 = 25) MATLAB follows mathematical rules for order of operations: {()} { ˆ } { * /, \ } { +, - } You can use parenthesis to change the precedence of an expression. to MATLAB Spring 2019 13 / 39

Variables and Expressions MATLAB built-in functions and constants Some basic functions and constants: sqrt(x) square root exp(x) Exponential (e x ) abs(x) Absolute value log(x) Natural logarithm sin(x) Sine of x cos(x) Cosine of x tan(x) Tangent of x pi π i 1 inf NaN not a number, such as the result of 0/0. to MATLAB Spring 2019 14 / 39

Variables and Expressions Help and lookfor commands There are many built-in functions in MATLAB: Use lookfor can be used to find a specific command to perform a task. Use help to find out information about a known command. to MATLAB Spring 2019 15 / 39

Variables and Expressions diary function You can keep a record of your MATLAB sessions using the diary function. Calling >> diary filename will save the commands your enter into the command window along with the output in a plain text file. diary off and diary on will pause and restart the recording, resp. to MATLAB Spring 2019 16 / 39

Variables and Expressions MATLAB script files a.k.a m-files MATLAB script files (also known as m-files) are useful for running commands multiple types without having to type them into the command line. Script files are named with an extension.m You can type in your commands into the m-file, edit, save them and execute them as many times as you want. The MATLAB editor is the best way to create m-files. A typical script file will contain MATLAB commands and comments Comments describe the purpose of the script file and any useful information about the commands executed Comments within m-files begin with (% ) to MATLAB Spring 2019 17 / 39

Vectors and matrices are used to store values of the same type A vector can be either column vector or a row vector. Matrices can be visualized as a table of values with dimensions r c (r is the number of rows and c is the number of columns). to MATLAB Spring 2019 18 / 39

Creating row vectors Place the values that you want in the vector in square brackets separated by either spaces or commas. e.g >> vec=[1 2 3 4 5] vec = 1 2 3 4 5 >> vec=[1,2,3,4,5] vec= 1 2 3 4 5 to MATLAB Spring 2019 19 / 39

Creating row vectors - colon operator If the values of the vectors are regularly spaced, the colon operator can be used to iterate through these values. e.g. >> vec = 1:5 vec = 1 2 3 4 5 A step value can also be specified with another colon in the form (first:step:last) >>vec = 1:2:9 vec = 1 3 5 7 9 to MATLAB Spring 2019 20 / 39

Creating row vectors - colon operator Example In using (first:step:last) What happens if adding the step value would go beyond the range specified by last? e.g: >>v = 1:2:6 Answer >> v= 1:2:6 v = 1 3 5 to MATLAB Spring 2019 21 / 39

Creating row vectors - colon operator Example How can we use (first:step:last) to generate the vector v1 = [9 7 5 3 1 ]? Answer >> v1= 9:-2:1 v1 = 9 7 5 3 1 to MATLAB Spring 2019 22 / 39

Creating row vectors - linspace function linspace >>linspace(x,y,n) creates a row vector with n values in the inclusive range from x to y. Example >>v2 = linspace(3,15,5) v2 = 3 6 9 12 15 to MATLAB Spring 2019 23 / 39

Vector concatenation We can use existing vectors to create new ones >> v1= 9:-2:1 v1 = 9 7 5 3 1 >> v2 = linspace(3,15,5) v2 = 3 6 9 12 15 >> newvec = [v1 v2] newvec = 9 7 5 3 1 3 6 9 12 15 to MATLAB Spring 2019 24 / 39

Accessing elements of a vector newvec newvec = 9 7 5 3 1 3 6 9 12 15 5 th element >> newvec(5) ans = 1 Elements 4 through 6 >> newvec(4:6) ans = 3 1 3 to MATLAB Spring 2019 25 / 39

Accessing elements of a vector newvec newvec = 9 7 5 3 1 3 6 9 12 15 Elements 2, 3, 7 >>newvec([2 3 7]) ans = 7 5 6 To set the first entry to 10 >> newvec(1)=10 newvec = 10 7 5 3 1 3 6 9 12 5 to MATLAB Spring 2019 26 / 39

Modifying vector entries Create a vector of size 10 with all zero entries >> vec=zeros(1,10) vec = 0 0 0 0 0 0 0 0 0 Set entries from 2 to 4 to 1 >> vec(2:4)=1 vec = 0 1 1 1 0 0 0 0 0 Set entries from 2 to 4 to 2 to 4! >> vec(2:4)=2:4 vec = 0 2 3 4 0 0 0 0 0 to MATLAB Spring 2019 27 / 39

What about column vectors? Explicitly >> col_vec = [2;4;6;8] col_vec = 2 4 6 8 No direct way using colon operator or linspace function...but we can fix this! to MATLAB Spring 2019 28 / 39

What about column vectors? Simply take the transpose of your row vector! >> row_vec =2:2:8 row_vec = 2 4 6 8 >> col_vec = row_vec col_vec = 2 4 6 8 to MATLAB Spring 2019 29 / 39

Matrix variables We generalize the creation of row and column vectors, e.g. >> mat1 = [3 4 5; 2 5 6] mat = 3 4 5 2 5 6 Or use the colon operator within rows >> mat2=[1:3; 4:6] mat2 = 1 2 3 4 5 6 to MATLAB Spring 2019 30 / 39

Caution NOTE: There must always be the same number of values in each row If you attempt to create a matrix with different number of values in the rows MATLAB will complain: >> mat=[1 2 4; 3 4] Error using vertcat Dimensions of matrices being concatenated to MATLAB Spring 2019 31 / 39

Matrices of random numbers rand(n) - returns an n n matrix of random values on (0, 1). rand(m,n) - returns an m n matrix. A 4 4 matrix of random entries on (0, 1). >> rand(3) ans = 0.9649 0.9572 0.1419 0.1576 0.4854 0.4218 0.9706 0.8003 0.9157 A (4 3) matrix of random entries on (0, 1). >> rand(2,3) ans = 0.7922 0.6557 0.8491 0.9595 0.0357 0.9340 to MATLAB Spring 2019 32 / 39

Referring to and modifying matrix elements Given the following 3 4 matrix >> mat=[1:4;5:8;9:12] mat = 1 2 3 4 5 6 7 8 9 10 11 12 We can extract element mat 2,3 as follows: >> mat(2,3) ans = 7 The submatrix consisting of rows 1, 2 and columns 2, 3 : >> mat(1:2,2:3) ans = 2 3 6 7 to MATLAB Spring 2019 33 / 39

Referring to and modifying matrix elements Given >> mat=[1:4;5:8;9:12] mat = 1 2 3 4 5 6 7 8 9 10 11 12 Extract the first row of mat >> mat(1,:) ans = 1 2 3 4 Extract the first column of mat >> mat(:,1) ans = 1 5 9 to MATLAB Spring 2019 34 / 39

Referring to and modifying matrix elements Given >> mat=[1:4;5:8;9:12] mat = 1 2 3 4 5 6 7 8 9 10 11 12 Set the first column of mat to [5;6;7] >> mat(:,1) = 5:7 mat = 5 2 3 4 6 6 7 8 7 10 11 12 to MATLAB Spring 2019 35 / 39

Deleting vector entries Given a vector with 8 random integers between 1 and 10 >> vec=round((rand(1,8))*10) vec = 7 2 1 5 10 3 6 We can delete the 3rd entry using the empty vectors [] as >> vec(3)=[] vec = 7 2 5 10 3 6 2 To delete multiple entries, simply pass in an index array, e.g. to delete all even entries: >> vec(2:2:length(vec)) =[] vec = 7 1 10 6 Note: Here, we can also use end as >>vec(2:2:end) = [] to MATLAB Spring 2019 36 / 39

Given a 5 5 matrix >> M=magic(5) %look up the magic function! M = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 Delete the 3rd column >> M(:,3)=[] M = 17 24 8 15 23 5 14 16 4 6 20 22 10 12 21 3 11 18 2 9 to MATLAB Spring 2019 37 / 39 Deleting matrix entries

Given a 5 5 matrix >> M=magic(5) M = 17 24 1 8 15 23 5 7 14 16 4 6 13 20 22 10 12 19 21 3 11 18 25 2 9 Add a 6th row >> [M,[1:5] ] ans = 17 24 1 8 15 1 23 5 7 14 16 2 4 6 13 20 22 3 10 12 19 21 3 4 11 18 25 2 9 5 to MATLAB Spring 2019 38 / 39 Adding entries

Some useful array commands Find out what each command does before starting Homework 1. 1 length 2 size 3 numel 4 max 5 min 6 sort 7 zeros 8 ones 9 reshape 10 eye 11 spy to MATLAB Spring 2019 39 / 39