Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB.

Slides:



Advertisements
Similar presentations
MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
Advertisements

Introduction to MATLAB The language of Technical Computing.
Introduction to Matlab
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 20P. 1Winter Quarter Propulsion Lab with MATLAB Lecture 20.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
Lecture 5 Review Programming Program Structures Comparison Repetition: looping or iteration Conditional execution: branching Bubble Sort.
HY436: Mobile Computing and Wireless Networks MATLAB TUTORIAL Matlab Tutorial: October 4, 2005 Elias Raftopoulos Ploumidis Manolis Prof. Maria Papadopouli.
Introduction to MATLAB
General Computer Science for Engineers CISC 106 Lecture 08 Dr. John Cavazos Computer and Information Sciences 2/27/2009.
Introduction to MATLAB 7 for Engineers
Matlab Intro. Outline Matlab introduction Matlab elements Types Variables Matrices.
Matlab intro The Environment
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 21P. 1Winter Quarter MATLAB: Structures.
MATLAB Fundamentals.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Introduction to MATLAB
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.
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.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
INTRODUCTION TO MATLAB LAB# 01
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
ES 240: Scientific and Engineering Computation. Chapter 2 Chapter 2: MATLAB Fundamentals Uchechukwu Ofoegbu Temple University.
Introduction to MATLAB. CSPP58001 MATLAB MATLAB is is a matrix-based language well suited for carrying out numerical analysis. It has many, many high-
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB Lecture 18.
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
CSE123 Lecture 3 Files and File ManagementScripts.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Fall 2006AE6382 Design Computing1 Control Statements in Matlab Topics IF statement and Logical Operators Switch-Case Disp() vs fprintf() Input() Statement.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 20P. 1Winter Quarter Propulsion Lab with.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
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 1 Introduction to MATLAB Lecture Series by CEPSTRUM Presented by Pratik Kotkar & Akash Baid.
Basics of MATLAB 2- Programming in MATLAB By DR. Wafaa Shabana
ECE 1304 Introduction to Electrical and Computer Engineering
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
MATLAB: Structures and File I/O
MATLAB PRIMER by Michael Medvinsky(2014)
StatLab Matlab Workshop
Use of Mathematics using Technology (Maltlab)
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Communication and Coding Theory Lab(CS491)
Simulation And Modelling
Matlab Intro.
INTRODUCTION TO MATLAB
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Simulation And Modelling
Matlab Intro.
Presentation transcript:

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB Lecture 18

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 2Winter Quarter MATLAB MATLAB is a program for doing numerical computation. It was originally designed for solving linear algebra type problems using matrices. It’s name is derived from MATrix LABoratory. MATLAB has since been expanded and now has built- in functions for solving problems requiring data analysis, signal processing, optimization, and several other types of scientific computations. It also contains functions for 2-D and 3-D graphics and animation.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 3Winter Quarter MATLAB The MATLAB environment is command oriented somewhat like UNIX. A prompt appears on the screen and a MATLAB statement can be entered. When the key is pressed, the statement is executed, and another prompt appears. If a statement is terminated with a semicolon ( ; ), no results will be displayed. Otherwise results will appear before the next prompt. The following slide is the text from a MATLAB screen.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 4Winter Quarter MATLAB To get started, select MATLAB Help or Demos from the Help menu. EDU >> a=5; EDU >> b=a/2 b = EDU >>

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 5Winter Quarter MATLAB Variable Names Variable names ARE case sensitive Variable names can contain up to 63 characters (as of MATLAB 6.5 and newer) Variable names must start with a letter followed by letters, digits, and underscores.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 6Winter Quarter MATLAB Special Variables ansDefault variable name for results piValue of  epsSmallest incremental number infInfinity NaNNot a numbere.g. 0/0 i and ji = j = square root of -1 realminThe smallest usable positive real number realmaxThe largest usable positive real number

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 7Winter Quarter MATLAB Math & Assignment Operators Power ^ or.^a^b or a.^b Multiplication * or.*a*b or a.*b Division / or./a/b or a./b or \ or.\b\a or b.\a NOTE: 56/8 = 8\56 - (unary) + (unary) Addition + a + b Subtraction - a - b Assignment =a = b (assign b to a )

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 8Winter Quarter Other MATLAB symbols >>prompt...continue statement on next line,separate statements and data %start comment which ends at end of line ;(1)suppress output (2)used as a row separator in a matrix :specify range

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 9Winter Quarter MATLAB Matrices MATLAB treats all variables as matrices. For our purposes a matrix can be thought of as an array, in fact, that is how it is stored. Vectors are special forms of matrices and contain only one row OR one column. Scalars are matrices with only one row AND one column

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 10Winter Quarter MATLAB Matrices A matrix with only one row AND one column is a scalar. A scalar can be created in MATLAB as follows: EDU» a_value=23 a_value = 23

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 11Winter Quarter MATLAB Matrices A matrix with only one row is called a row vector. A row vector can be created in MATLAB as follows (note the commas): EDU>> rowvec = [12, 14, 63] rowvec =

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 12Winter Quarter MATLAB Matrices A matrix with only one column is called a column vector. A column vector can be created in MATLAB as follows (note the semicolons): ED>> colvec = [13 ; 45 ; -2] colvec =

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 13Winter Quarter MATLAB Matrices A matrix can be created in MATLAB as follows (note the commas AND semicolons): EDU>> matrix = [1, 2, 3 ; 4, 5,6 ; 7, 8, 9] matrix =

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 14Winter Quarter Extracting a Sub-Matrix A portion of a matrix can be extracted and stored in a smaller matrix by specifying the names of both matrices and the rows and columns to extract. The syntax is: sub_matrix = matrix ( r1 : r2, c1 : c2 ) ; where r1 and r2 specify the beginning and ending rows and c1 and c2 specify the beginning and ending columns to be extracted to make the new matrix.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 15Winter Quarter MATLAB Matrices A column vector can be extracted from a matrix. As an example we create a matrix below: EDU>> matrix=[1,2,3;4,5,6;7,8,9] matrix = Here we extract column 2 of the matrix and make a column vector: EDU>> col_two=matrix( :, 2) col_two = 2 5 8

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 16Winter Quarter MATLAB Matrices A row vector can be extracted from a matrix. As an example we create a matrix below: EDU>> matrix=[1,2,3;4,5,6;7,8,9] matrix = Here we extract row 2 of the matrix and make a row vector. Note that the 2:2 specifies the second row and the 1:3 specifies which columns of the row. ED>> rowvec=matrix(2 : 2, 1 : 3) rowvec = 4 5 6

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 17Winter Quarter Reading Data from files MATLAB supports reading an entire file and creating a matrix of the data with one statement. >> load mydata.dat;% loads file into matrix. % The matrix may be a scalar, a vector, or a % matrix with multiple rows and columns. The % matrix will be named mydata. >> size (mydata)% size will return the number % of rows and number of % columns in the matrix >> length (myvector)% length will return the total % no. of elements in myvector

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 18Winter Quarter Plotting with MATLAB MATLAB will plot one vector vs. another. The first one will be treated as the abscissa (or x) vector and the second as the ordinate (or y) vector. The vectors have to be the same length. MATLAB will also plot a vector vs. its own index. The index will be treated as the abscissa vector. Given a vector “time” and a vector “dist” we could say: >> plot (time, dist) % plotting versus time >> plot (dist)% plotting versus index

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 19Winter Quarter Plotting with MATLAB There are commands in MATLAB to "annotate" a plot to put on axis labels, titles, and legends. For example: >> % To put a label on the axes we would use: >> xlabel ('X-axis label') >> ylabel ('Y-axis label') >> % To put a title on the plot, we would use: >> title ('Title of my plot')

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 20Winter Quarter Plotting with MATLAB Vectors may be extracted from matrices. Normally, we wish to plot one column vs. another. If we have a matrix “mydata” with two columns, we can obtain the columns as a vectors with the assignments as follows: >> first_vector = mydata ( :, 1) ; % First column >> second_vector = mydata ( :, 2) ; % Second one >>% and we can plot the data >> plot ( first_vector, second_vector )

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 21Winter Quarter Some Useful MATLAB commands whoList known variables whosList known variables plus their size helpEx: >> help sqrt Help on using sqrt lookforEx: >> lookfor sqrt Search for keyword sqrt in m-files what Ex:>> what a: List MATLAB files in a: clearClear all variables from work space clear x yClear variables x and y from work space clcClear the command window

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 22Winter Quarter Some Useful MATLAB commands whatList all m-files in current directory dirList all files in current directory lsSame as dir type testDisplay test.m in command window delete testDelete test.m cd a:Change directory to a: chdir a:Same as cd pwdShow current directory which testDisplay current directory path to test.m

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 23Winter Quarter A Useless, But Interesting, MATLAB command whyIn case you ever needed a reason

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 24Winter Quarter MATLAB Relational Operators MATLAB supports six relational operators. Less Than< Less Than or Equal<= Greater Than> Greater Than or Equal>= Equal To== Not Equal To~=

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 25Winter Quarter MATLAB Logical Operators MATLAB supports three logical operators. not~% highest precedence and&% equal precedence with or or |% equal precedence with and

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 26Winter Quarter MATLAB Logical Functions MATLAB also supports some logical functions. xor (exclusive or) Ex: xor (a, b) Where a and b are logical expressions. The xor operator evaluates to true if and only if one expression is true and the other is false. True is returned as 1, false as 0. any (x) returns 1 if any element of x is nonzero all (x) returns 1 if all elements of x are nonzero isnan (x) returns 1 at each NaN in x isinf (x) returns 1 at each infinity in x finite (x) returns 1 at each finite value in x

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 27Winter Quarter MATLAB Display formats MATLAB supports 8 formats for outputting numerical results. format long16 digits format short e5 digits plus exponent format long e16 digits plus exponent format hexhexadecimal format banktwo decimal digits format +positive, negative or zero format ratrational number (215/6) format shortdefault display

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 28Winter Quarter MATLAB Selection Structures An if - elseif - else structure in MATLAB. Note that elseif is one word. ifexpression1% is true % execute these commands elseif expression2% is true % execute these commands else% the default % execute these commands end

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 29Winter Quarter MATLAB Repetition Structures A for loop in MATLAB for x = array for x = 1: 0.5 : 10 % execute these commands end A while loop in MATLABwhile expression while x <= 10 % execute these commands end

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 30Winter Quarter Scalar - Matrix Addition EDU» a=3; EDU» b=[1, 2, 3;4, 5, 6] b = EDU» c= b+a% Add a to each element of b c =

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 31Winter Quarter Scalar - Matrix Subtraction EDU» a=3; EDU» b=[1, 2, 3;4, 5, 6] b = EDU» c = b - a %Subtract a from each element of b c =

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 32Winter Quarter Scalar - Matrix Multiplication EDU» a=3; EDU» b=[1, 2, 3; 4, 5, 6] b = EDU» c = a * b % Multiply each element of b by a c =

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 33Winter Quarter Scalar - Matrix Division EDU» a=3; EDU» b=[1, 2, 3; 4, 5, 6] b = EDU» c = b / a % Divide each element of b by a c =