What is Matlab?  “MATLAB® is a high-level language and interactive environment for numerical computation, visualization, and programming. Using MATLAB,

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

Introduction to M ATLAB Programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
Introduction to Matlab
Matlab Programming Huajun Wang Department of Earth Sciences, Zhejiang University Jan 25,2012.
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,
Introduction to Matlab Jianguo Wang CSSCR September 2009.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
Lecture 13: M/O/F/ for Engineering Applications - Part 1 BJ Furman 26NOV2012.
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
Matlab tutorial course Lesson 2: Arrays and data types
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
You can make this in matlab!. Matlab Introduction and Matrices.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Lecture 4 MATLAB Windows Arithmetic Operators Maintenance Functions
Objectives Understand what MATLAB is and why it is widely used in engineering and science Start the MATLAB program and solve simple problems in the command.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
Basics of MATLAB By DR. Wafaa Shabana
Matlab 14.html Cost: $100 Available in labs on Windows and Unix machines.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction to MATLAB Session 1 Simopekka Vänskä, THL 2010.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Introduction to Matlab
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
Matlab for Engineers Matlab Environment Chapter 2.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Lecture 12: M/O/F/ for Engineering Applications B Burlingame 27 April 2016.
An Introduction to Programming in Matlab Emily Blumenthal
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
ECE 1304 Introduction to Electrical and Computer Engineering
Release Numbers MATLAB is updated regularly
Introduction to Matlab
Lecture: MATLAB Chapter 1 Introduction
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
Use of Mathematics using Technology (Maltlab)
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Matlab Training Session 2: Matrix Operations and Relational Operators
Presentation transcript:

What is Matlab?  “MATLAB® is a high-level language and interactive environment for numerical computation, visualization, and programming. Using MATLAB, you can analyze data, develop algorithms, and create models and applications. The language, tools, and built-in math functions enable you to explore multiple approaches and reach a solution faster than with spreadsheets or traditional programming languages, such as C/C++ or Java™.  You can use MATLAB for a range of applications, including signal processing and communications, image and video processing, control systems, test and measurement, computational finance, and computational biology. More than a million engineers and scientists in industry and academia use MATLAB, the language of technical computing.  The basic data element is the “array”, usually referred to as a vector or matrix

learn how to program because…  Increase your freedom  Increase your scientific value  Enjoyment  Exercise your logical mind

PROGRAMMING == PROBLEM SOLVING the concept of an ALGORITHM

The process of programming  Programming is not a linear process  Lots of trial and error  Problem solving, detective work, deductive reasoning  Debugging may take longer than initial writing. Enjoy it!

Coding philosophy  There are many different ways to make things work.  The prettiest way is not always the most desirable.  Top priority is that your script does what you want it to do.  Good coding practices are important  Assume you will remember nothing next time you look at your code  Assume someone else will be using your code  Assume your script will at some point move to another computer

Navigating the Matlab Desktop  Command Window  Where you type the commands into Matlab  Command History  Contains a record of all commands entered in the Command Window  Current Folder Browser  All the contents of the Current Folder where you can save your work  Workspace Browser  Where your data and variables are stored in current Matlab session source: The MathWorks Inc

Desktop Layout source: The MathWorks Inc

Desktop Layout source: The MathWorks Inc

The file browser  Moving around through the folder hierarchy  Command line tools for navigation  pwdwhere are we? cdchange directory lslist directory contents.current directory..parent directory

The workspace and variable editor  settings variables: x = 3  clearing variables: clear x clear all  saving & loading variables: save data load data.mat

Getting help  help function  doc function  lookfor function >> help sin sin Sine of argument in radians. sin(X) is the sine of the elements of X. See also asin, sind. Reference page in Help browser doc sin

Scripts  Anything you type into the workspace can also be run from a script  “.m” files are just saved lists of matlab commands  functions

The Editor  Comments  Syntax highlighting  Code folding

Variable types  double: floating point number like 3.24  integer: no decimal places 345 Numbers

Basic calculations in Matlab  Matlab is a powerful graphical calculator Square root Exponential Absolute value source: The MathWorks Inc Functions?

Basic calculations in Matlab

-each result is stored in its own variable in the workspace and can be used later in the same session -at any time you can clear (some or all) your variables -if no specific variable name is aasigned to the result, the default is ‘ans’ source: The MathWorks Inc

Basic calculations in Matlab  Variables are data containers  All variables are arrays  Scalar (1x1)  Vectors (row or column)  Matrices (m x n)  Default data type is double  double = 8 bytes  16 digits of precision source: The MathWorks Inc

Basic calculations in Matlab  Creating Variables salary = 900 Variable name Assign operator value Variables are case sensitive ie. thisvar and Thisvar and THISVAR are all different whos = gives a list of the variables in the workspace Matlab uses the first 31 characters of a variable name only source: The MathWorks Inc

Basic calculations in Matlab: Variables  Once variables or graphics are created, they remain stored in memory if not explicitly cleared or overwritten.  This is a common source of errors, since old variables may be mixed with new ones.  Clear variables:  clear all % deletes all variables, compiled functions, etc…  clc% clears the Command Window  close all % close all figures Source: HKA & AB slides

Basic calculations in Matlab: Variables Syntax errors source: The MathWorks Inc

Vectors and matrices  Vectors are like lists a = [1,2,3,4,5]  Matrices are like lists of lists a = [ 1,3,5,7; 2,4,6,8 ]  Matrices can have many dimensions

Creating Vectors  Vectors are one dimensional arrays (either column or row vectors) m = Row vector Column vector source: The MathWorks Inc

Creating vectors >> a = [ ] a = >> a = [1,2,3,4,5] a = >> a = [1:5] a = >> x = [ ]; y = [ ]; >> z = [ x y ] z = >> z = [x ; y]; % this is a matrix

Creating Vectors x = a : dx : b first number last number Interval (default is 1) Also: >> x = x’ transpose Example: >> x=[1:3:13] >> x = x’ For output not to appear on screen but be stored in the workspace use semicolon: >> x=[1:3:13]; Can be used to give multiple commands >> a = 1; b = 2; c = 3; source: The MathWorks Inc

Creating vectors  Data can be created in a linear sequence using built in Matlab functions  linspace(x1, x2, N)  This function generates N points linearly between X1 and X2.  For example:  >> a = linspace(1,19,10)  a =  If N is not defined it defaults to 100

Creating Matrices  Matrices are two dimensional arrays  A couple common ways to create matrices: Use square brackets when creating arrays source: The MathWorks Inc

Creating matrices >> a = [1 2 3; 4 5 6] a = >> a = [1 2 3; 4 5 6; 7 8 9] a =

Creating matrices >> ones(3) ans = >> ones(2,3) ans = >> zeros(3,4) ans = (rows,columns)

Creating matrices >> rand(3) ans = >> nan(4) ans = NaN NaN NaN NaN NaN = “Not a Number”

Creating Matrices Other ways to enter matrices: Loaded from external data files (check save & load) Generated using built-in functions Created with defined functions in M-files Source: HKA & AB slides

Describing matrices  size() will tell you the dimensions of a matrix  length() will tell you the length of a vector

Accessing elements >> a = [0:4] a = >> a(2) ans = 1 >> b = [1 2 3;4 5 6;7 8 9] b = >> b(2,3) ans = 6 Also try: x = [1:0.1:10]

Accessing elements >> b(1:3,1) ans = >> b(1,:) ans = 1 2 3

Accessing elements >> a = [0:4] a = >> a(2) ans = 1 >> b = [1,2,3;4,5,6;7,8,9] b = >> b(2,3) ans = 6

Accessing elements >> b(1:3,1) ans = >> b(1,:) ans = 1 2 3

Accessing elements >> a = [1:.5:5]; >> a([1 2 4]) ans = >> indices = [5 6 7]; >> a(indices) ans = >> a([5 6 7]) ans = equivalent

Accessing elements >> odds = [1:2:100]; >> odds([26:50, 1:25]) ans = Columns 1 through Columns 15 through Columns 29 through Columns 43 through

Accessing elements >> [26:50,1:25] ans = Columns 1 through Columns 15 through Columns 29 through Columns 43 through

Manipulating arrays source: The MathWorks Inc

Concatenation & Deleting  Concatenation is the process of joining small matrices to make bigger ones. For example: B = [A A+32; A+48 A+16]  To set a single element to zero: A(3,2) = 0 % Note how to address elements!  To delete the third column of B use: B(:,3) = [ ] Source: HKA & AB slides

Vector math  Adding a constant to each element in a vector  Adding two vectors >> a = [1 2 3] a = >> a + 1 ans = >> b = [5 1 5] b = >> a + b ans = 6 3 8

Vector multiplication  The * sign refers to matrix multiplication: >> a = [1 2 3] a = >> b = [2 2 4] b = >> a * b Error using * Inner matrix dimensions must agree. >> b = b' b = >> a * b ans = 18

Vector multiplication  The.* sign refers to element-wise multiplication: >> a = [1 2 3] a = >> b = [2 2 4] b = >> a.* b ans = >> a * 4 ans = >> a.* 4 ans =

Operators  Element-wise operators:.*multiplication./ division.^exponentiation  Many other functions work element-wise, e.g.: >> a = [1 4 9] a = >> sqrt(a) ans = 1 2 3

relational operators  == equal to (distinguish from = which sets a value)  ~= not equal to  > greater than  <less than  >=greater than or equal to  <= less than or equal to

>> 1 == 2 ans = 0 >> 1 < 2 ans = 1 >> 1 = 2 1 = 2 | Error: The expression to the left of the equals sign is not a valid target for an assignment. >> x = 5; >> x < 100 ans = 1 0 means FALSE 1 means TRUE

Logical operations  & logical AND operation  | logical OR operation (what about XOR?)  ~ logical NOT operation  == logical equality (unlike “=“, which means assign the result of the expression on the right to the variable on the left Examples: a=1; b=-2;  Logic1 = (a==0 & b==0);  Logic2 = (a==0 | b==0);  Logic3 = (a==1 | b==0);  Logic4 = (a==1 & b==-2);  All logical operations work in “vector” mode as well, so for example if a and b are row or column vectors of equal size then (a==b) evaluates to a vector of the same size as a and b, whose value is 1 if the corresponding elements of a and b are equal, and 0 otherwise. e.g. a = [ ]; b = [ ]; c = (a==b) whos

>> x = 5; y = 1; >> x > 4 & y > 4 ans = 0 >> (x>4) & (y>4) ans = 0 >> (x>4) | (y>4) ans = 1 >> (y>4) ans = 0 >> ~(y>4) ans = 1

Finding values within a matrix >> x = rand(1,10) x = Columns 1 through Columns 9 through >> find(x>.5) ans =

Finding values within a matrix >> indicesWhereBig = find(x>.5) indicesWhereBig = >> x(indicesWhereBig) ans = >> x(find(x>.5)) ans =

Logical indexing >> x>.5 ans = >> vec = ans; >> whos vec Name Size Bytes Class Attributes vec 1x10 10 logical >> x(vec) ans = >> x(x>.5) ans = equivalent to x(find(x>.5))

Logical indexing >> newvec = [ ] newvec = >> whos newvec Name Size Bytes Class Attributes newvec 1x10 80 double >> x(numvec) Subscript indices must either be real positive integers or logicals. >> numvec = logical(numvec); >> x(numvec) ans =

Logical indexing >> x = [1:100]; >> x(x<=23) ans = Columns 1 through Columns 15 through >> x = [1:10]; >> x(x<5) = 0 x =

Computing with arrays: Matrix operations For multiplication, Matlab assumes variables are matrices. Therefore size limitations apply, ie. inner dimensions must agree source: The MathWorks Inc

Computing with arrays: Array (or Element) operations Place a dot(.) before multiplication or powers to perform element-wise operations. In these cases, matrices must have exact same dimensions. source: The MathWorks Inc

Q&A: Match the expected outcome to the operators used. >> A.* B >> A * B ? source: The MathWorks Inc

Multivariate Data Consider a data set with three variables: Heart rate, Weight and Hours of exercise per week for five observations. The resulting array might look like D = [ ; ; ; ; ] source: HKA & AB slides

Multivariate Data Calculate mean and standard deviation for D mu = mean(D), sigma = std(D) mu = sigma = For a list of the data analysis functions available in MATLAB, >> help datafun If you have access to the Statistics Toolbox, >> help stats source: HKA & AB slides

Exercises  Create a row vector from 0 to 27 with intervals of 3  Create a column vector with the same contents  What are the remainders after dividing this vector by 2?  Create the first 11 ‘perfect squares’  Create a column vector with the elements from 100 to 0  1+1+1/2+1/6+1/24 (order of operations: MDAS)  e to the power of 3 (use help to find the right function)  3cos(  ), sin([0,  /4,  /2, 3  /4,  ])  logarithm of 1000  Create a row vector with the odd numbers between 0 and 50 source: HKA & AB slides

● Create a matrix m that contains two rows of data. The top row are the numbers 4, 5 and 6 and the bottom row 9, 6 and 3. ● Create a matrix n that contains two columns of data. The 1st column are the numbers 1, 3, 5 and 7 and the 2nd column are 2, 4, 6 and 8. ● What is: (1) m.^2 (2) n.^2 (3) n*m (4) m'*n'

A = [ ; ; ; ] sum (A) sum the transpose of A sum the elements on the main diagonal (address the elements directly or use the diag() command) Remind you of anything ? help magic ● Create a matrix A that increases in steps of 1 from 3 up to 107 ● Create a matrix B that decreases in steps of 0.5 from 333 down to -10 ● Create a 2d matrix called C of size 100 rows by 100 columns containing only the number 1 ● Create a 2d matrix called C of size 100 rows by 100 columns containing only the number 2 ● Try defining: D = [ 1, 2, 3, 4; ] What did I do wrong? ● If I define E = [ 10:1 ] what would you expect E to be? Try it. Can you understand what has happened?

● Investigate the Matlab GUI and make sure you know where the different parts can be found. For example, using the GUI: ● What (if any) variables are currently in your workspace? ● What files are in your current directory? ● What was your last but one command? ● Use the commands length and size to check your variables ● Investigate the help available in matlab. Using the help: ● Find the function for standard deviation. Find the function for variance. ● Read the help on the standard deviation and variance functions ● Apply these two functions to the matrix a=[1:10] ● Do you get and ?

Clear the workspace using >> clear all Now define a data variable x as: >> x = [ ] Predict what you think will happen in each of these cases: (1) >> x(1) (2) >> x(3) (3) >> x(10) (4) >> x[1] Now test your predictions by typing the commands into Matlab. Make sure you clearly understand what is happening in each case.

● Clear the workspace >> clear all ● Create matrix X: >> X = 333:-3:3; ● How many rows and columns does X consist of? ● Using bracket notation, create a new matrix Y from X that has 3 times as many columns as X but is still only 1 row ● Check this is right with: >> size(Y) should return ● Using bracket notation, create a new matrix Z from X that is the same no of columns as X but has 3 rows Check this is right with: >> size(Z) should return 3 111

● Create the following 3x3 matrices: >> G = [ 1 2 3; 4 5 6; 7 8 9] >> H = [ ; ; ] ● Now replace column 1 of G with row 3 of H ● Create matrix J: >> J = [ 1:10; 11:20; 21:30 ] ● Remove the last 5 columns of J and save the resultant matrix in a new variable called K ● Remove the first 2 rows of J and save the resultant matrix in a new variable called L ● Create A=[1 2 3; 4 5 6; 7 8 9]; B = [A A+32; A+48 A+16] Find the coordinates and values of the elements of B that are greater than 53