INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.

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
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Introduction to MATLAB
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 18P. 1Winter Quarter Introduction to MATLAB.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
What is MATLAB ? MATrix LABratory –Originally, it was a front-end to FORTRAN matrix routines developed in the U. of New Mexico and Stanford –Today.
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.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
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.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
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.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Eng Ship Structures 1 Introduction to Matlab.
INTRODUCTION TO MATLAB LAB# 01
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
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.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Introduction to MATLAB. CSPP58001 MATLAB MATLAB is is a matrix-based language well suited for carrying out numerical analysis. It has many, many high-
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.
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.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
A simple classification problem Extract attributes Pattern Pattern recognition decision x C1 C2.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Digital Image Processing Introduction to M-function Programming.
Introduction to Matlab
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
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.
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.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Relational and Logical Operators EE 201 1C7-2 Spring 2012.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
ECE 1304 Introduction to Electrical and Computer Engineering
L – Modeling and Simulating Social Systems with MATLAB
Scripts & Functions Scripts and functions are contained in .m-files
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
MATLAB: Structures and File I/O
Use of Mathematics using Technology (Maltlab)
Lecture 2 Introduction to MATLAB
INTRODUCTION TO MATLAB
Presentation transcript:

INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347

Dr. Blanton - ENTC MATLAB 2 / 45 TOPICS 1.Basic MATLAB 2.Matrices 3.Operators 4.Script and function files 5.Flow control 6.Plotting

Dr. Blanton - ENTC MATLAB 3 / 45 Basic MATLAB screen shot of the Matlab window command window optional windows workspace current directory type commands here

Dr. Blanton - ENTC MATLAB 4 / 45 Matlab’s help features type “help” at the command prompt and Matlab returns a list of help topics

Dr. Blanton - ENTC MATLAB 5 / 45 Matlab’s help features >> help lang Matlab’s language constructs

Dr. Blanton - ENTC MATLAB 6 / 45 >> help for how to use Matlab’s “for” statement Matlab’s help features

Dr. Blanton - ENTC MATLAB 7 / 45 you can also access “on-line” help by clicking the question mark in the toolbar separate window Matlab’s help features

Dr. Blanton - ENTC MATLAB 8 / 45 MATLAB Variables all variables are stored in 32bit floating point format no distinction between real and integer >>a = 3; >>a = 3.0; same assignment for “a” Matlab is case sensitive >>A=3; >>a=2; AaAa

Dr. Blanton - ENTC MATLAB 9 / 45 can use numbers and underscore in variable names >>case34=6.45; names must start with a letter >>case_34=6.45; OK >>34case=23.45; results in a syntax error string (text) variables enclosed in single quotes. The variable is stored as array of characters >>title=‘This is the title’; MATLAB Variables

Dr. Blanton - ENTC MATLAB 10 / 45 to clear a variable from memory >>a=4 >>clear a if a variable is defined, typing the variable name returns its value >>a=45.57; a = Matlab returns the value >>a MATLAB Variables

Dr. Blanton - ENTC MATLAB 11 / 45 Matlab will “echo” commands unless a semi-colon is used >>a=23.2; >> >>a=23.2 a = 23.2 >> Matlab echoes the command MATLAB Variables

Dr. Blanton - ENTC MATLAB 12 / 45 Vectors column vectors row vectors >>a=[1;2;3]; >>a a = >>a=[1,2,3]; >>a a = use semi-colon to separate rows use comma to separate columns MATLAB Variables

Dr. Blanton - ENTC MATLAB 13 / 45 Matrices 2-dimensional matrices >>a=[1,2,3;4,5,6]; >>a a = again, separate columns with commas and rows with semi-colons MATLAB Variables

Dr. Blanton - ENTC MATLAB 14 / 45 A vector is a special type of matrix row vector is a 1 x n matrix, 1 row n columns column vector is a n x 1 matrix, n rows 1 column Indexing Matrix elements >>a=[1,2,3]; >>a(2) ans = 2 could also reference by a(1,2) note, a(2,1) would produce an error because “a” only has one row MATLAB Variables

Dr. Blanton - ENTC MATLAB 15 / 45 Indexing Matrix elements >>a=[1,2,3;4,5,6]; more examples >>a(2,3) ans = 6 >>a(2,2)=9; >>a a = addressing assigning MATLAB Variables

Dr. Blanton - ENTC MATLAB 16 / 45 complex-valued numbers Typically, the variable “i” or “j” is used to represent the complex variable; e.g. Then, a complex number is represented as z = a + ib Re(z) = a Im(z) = b MATLAB Variables

Dr. Blanton - ENTC MATLAB 17 / 45 complex-valued numbers Unless i or j has been previously defined, Matlab assigns i and j the complex variable value In Matlab, a complex variable is represented in the following format (assuming all variables are cleared) >>z=23+i*56; >>z z = i >>z=23+j*56; >>z z = i Matlab always uses the symbol “i” to represent a complex number MATLAB Variables

Dr. Blanton - ENTC MATLAB 18 / 45 complex-valued numbers What happens in this case? >>i=3; >> z=23+i*56; >>z z = What happens in this case? >>a=sqrt(-1); >>z=23+a*56; >>z z = MATLAB Variables

Dr. Blanton - ENTC MATLAB 19 / 45 complex-valued numbers Note, a real-valued number is a special case of a complex-valued number assigning any element of a matrix as complex-valued makes the entire matrix complex-valued >>a=[1,2]; >>a a = 1 2 >>a(1)=1+i*5; >>a a = i i MATLAB Variables

Dr. Blanton - ENTC MATLAB 20 / 45 Advanced data types n-dimensional arrays structures cell arrays MATLAB Variables

Dr. Blanton - ENTC MATLAB 21 / 45 Basic operations addition + subtraction - multiplication * division right division / left division \ >>a=3;b=4; >>c1=a/b; >>c2=a\b; ? c1=0.75 c2=1.3333…. so, be careful! MATLAB Variables

Dr. Blanton - ENTC MATLAB 22 / 45 Mixed Real and Complex valued Variables if both variables are real-valued, a real-valued result is obtained if one variable is complex-valued, Matlab recasts the real variable as complex and then performs the operation. The result is complex-valued however, the type casting is done internally, the real-valued variable remains real after the operation MATLAB Variables

Dr. Blanton - ENTC MATLAB 23 / 45 Other (Scalar) Operations Math representation Matlab interpretation >>z=y^x; >>y=exp(x); >>y=log(x); >>y=log10(x) >>y=sin(x); >>y=cos(x); >>y=tan(x); >>y=asin(x); >>y=acos(x); >>y=atan(x); MATLAB Variables

Dr. Blanton - ENTC MATLAB 24 / 45 Examples >>y=x^0.5; >>y=x^(1/2); >>y=sqrt(x); All variables in the preceding operations can be real or complex, negative or positive for x < 0, y is complex. Matlab assumes you allow complex valued numbers. If y is not to be complex, you must provide error checking. MATLAB Variables

Dr. Blanton - ENTC MATLAB 25 / 45 Matrices Only matrices of the same dimension can be added and subtracted For multiplication, the inner dimensions must be the same No error Error >>D=A+B; >>D=A-B; >>D=A*C; >>D=C*A; >>D=A+C; >>D=A*B; >>D=B*A; Matrix multiplication not commutative MATLAB Variables

Dr. Blanton - ENTC MATLAB 26 / 45 Left(\) and Right(/) Matrix “division” Math representation Matlab interpretation >>C=A\B; >>C=B/A; Remember, A must be square and full rank (linearly independent rows/columns) MATLAB Variables

Dr. Blanton - ENTC MATLAB 27 / 45 Matrix Transpose Math representation Matlab interpretation >>C=A’; For complex-valued matrices, complex conjugate transpose >>B=A’; >>b=a’; MATLAB Variables

Dr. Blanton - ENTC MATLAB 28 / 45 MATLAB m-files Two types of m-files script files collection of commands that Matlab executes when the script is “run” function files collection of commands which together represent a function, a procedure or a method Both types are separate files with a “.m” extension

Dr. Blanton - ENTC MATLAB 29 / 45 To create an m-file, open the Matlab text editor Click on the “page” icon The Matlab text editor window will open MATLAB m-files

Dr. Blanton - ENTC MATLAB 30 / 45 Script Files On the command line >>x=3.0; >>y=x^2; >>y y = 9.0 >> In the script file named test.m On the command line >>test y = 9.0 >> MATLAB m-files

Dr. Blanton - ENTC MATLAB 31 / 45 Script Files script files share the workspace memory >>x=5.0; >>test >>y y = 25.0 >> test.m script MATLAB m-files

Dr. Blanton - ENTC MATLAB 32 / 45 Script Files script files can call other script files >>outter y = 36.0 >> inner.m script outter.m script MATLAB m-files

Dr. Blanton - ENTC MATLAB 33 / 45 Function Files Matlab identifies function files from script files by using the “function” and “return” keywords the name of the function file must be the same name as the function MATLAB m-files

Dr. Blanton - ENTC MATLAB 34 / 45 The function file x2.m Function Files >>r=3; >>d=x2(r); >>d d = 9.0 >> >>h=x2(4.2); >>h h = >> MATLAB m-files

Dr. Blanton - ENTC MATLAB 35 / 45 Function Files Multiple Inputs and Outputs outputs in square brackets, [ ] inputs in parentheses ( ) MATLAB m-files

Dr. Blanton - ENTC MATLAB 36 / 45 variables created in the function are not retained in the workspace, except for the output variables the function does not have access to workspace variables, except for the inputs Function Files variables passed to the function are “copies” of the workspace variables. Changing their value inside the function has no effect on their value in the workspace. MATLAB m-files

Dr. Blanton - ENTC MATLAB 37 / 45 The “while” and “if” statements if expression statements end if expression statements1 else statements2 end Matlab evaluates expression as logical “true” or “false” “false” equivalent to zero “true” equivalent to any non-zero number statements, any valid Matlab command while expression statements end MATLAB Flow Control

Dr. Blanton - ENTC MATLAB 38 / 45 MATLAB Flow Control evaluating expression any valid equation a=4; b=5; c=5; if a+b if b-c “True” “False” watch out for round-off and word length error if sin(0) if sin(pi) sin(pi) = 1.22e-16 “False” “True” conditional operators == equal to < less than > greater than <= less than or equal to >= greater than or equal to ~= not equal to logical operators & and | or while(3<=a)&(a<=5)

Dr. Blanton - ENTC MATLAB 39 / 45 The “for” statement for index = start : [increment :] end statements end index, start, increment, and end do not need to be integer valued increment is optional, if increment is not specified increment defaults to 1 index can be incremented positive (increment > 0) or negative (increment < 0) loop stops when index > end (or index < end) MATLAB Flow Control

Dr. Blanton - ENTC MATLAB 40 / 45 example script file to cycle through x values function file to generate the y values MATLAB Flow Control

Dr. Blanton - ENTC MATLAB 41 / 45 Basic 2D plotting functions plot(x1,y1[,x2,y2,x3,y3.....]) xlabel(‘x axis name’) ylabel(‘y axis name’) title(‘graph name’) Additional functions grid on grid off axis([xmin,xmax,ymin,ymax]) MATLAB PLOTTING

Dr. Blanton - ENTC MATLAB 42 / 45 example y = sin(t) the “plot” function alone MATLAB PLOTTING

Dr. Blanton - ENTC MATLAB 43 / 45 example y = sin(t) script file to generate a graph of y = sin(t) MATLAB PLOTTING

Dr. Blanton - ENTC MATLAB 44 / 45 function file to generate a graph of y = sin(t) >>graphsin >> example y = sin(t) MATLAB PLOTTING

Dr. Blanton - ENTC MATLAB 45 / 45 “legend” remembers the order the graphs were plotted Adding a Legend for multiple graphs MATLAB PLOTTING