1 MATLAB Basics. 2 MATLAB Documentation /help/techdoc/ Matrix Algebra.

Slides:



Advertisements
Similar presentations
Introduction to MATLAB The language of Technical Computing.
Advertisements

Introduction to M ATLAB Programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
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.
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.
Digital Image Processing Lecture3: Introduction to MATLAB.
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.
259 Lecture 15 Introduction to MATLAB. 2 What is MATLAB?  MATLAB, which stands for “MATrix LABoratory” is a high- performance language for technical.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
1 Statistical Computing in MATLAB AMS 597 Ling Leng.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
M ATLAB Tutorial Course 1. Contents Continued  Desktop tools  matrices  Logical &Mathematical operations  Handle Graphics  Ordinary Differential.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
1 M ATLAB Short Course. History of Calculator 2 3 Introduction to Matlab Matlab is short for Matrix Laboratory Matlab is also a programming language.
Introduction to MATLAB Session 1 Prepared By: Dina El Kholy Ahmed Dalal Statistics Course – Biomedical Department -year 3.
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 M ATLAB EE 100 – EE Dept. - JUST.
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
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.
IC3003 B ASIC S CIENTIFIC C OMPUTING Lecture 1 Monday 08:30-11:30 U204a.
ELG 3120 Signal and System Analysis 1 Introduction to MATLAB TAs Wei Zhang Ozgur Ekici (Section A)(Section B) ELG 3120 Lab Tutorial 1.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
EECE 360/460 Matlab Tutorial Jan Outline What is Matlab? What is Matlab? Matlab Interface Matlab Interface Basic Syntax Basic Syntax Plotting Graphs.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
INTRODUCTION TO MATLAB LAB# 01
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
Matlab 14.html Cost: $100 Available in labs on Windows and Unix machines.
OUTLINE Overview Numbers, variables and similar in Matlab
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 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”
MATLAB Tutorial EE313 Signals and Systems Created: Thursday Jan 25, 2007 Rayyan Jaber Modified by: Jeff Andrews.
>> 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.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Introduction to Matlab Electromagnetic Theory LAB by Engr. Mian Shahzad Iqbal.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
MATLAB Lecture 1 염익준. Introduction MATLAB (MATrix LABoratory) a special purpose computer program optimized to perform engineering and scientific calculations.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
1 MATLAB 입문 An Overview of MATLAB. An Overview of MATLAB 2 Starting Matlab The default MATLAB Desktop.
Introduction to MATLAB
Numeric, Cell and Structural Arrays One of the strenghts of MATLAB is the capabilty to handle collection of numbers called ARRAYS. MATLAB refers to scalars,
Built-in MATLAB Functions Chapter 3
Introduction to Matlab
MATLAB Basics Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
Computer Simulation Lab
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
Introduction To MATLAB
INTRODUCTION TO MATLAB AM2032 JAYANTA MUKHERJEE.
Communication and Coding Theory Lab(CS491)
Introduction to Matlab
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Announcements P3 due today
Computer Simulation Lab
Presentation transcript:

1 MATLAB Basics

2 MATLAB Documentation /help/techdoc/ Matrix Algebra

3 What is MATLAB? MATLAB (Matrix laboratory) is an interactive software system. It integrates mathematical computing, visualization, and a powerful language to provide a flexible environment for technical computing. Typical uses include Math and computation Algorithm development Data acquisition Modeling, simulation, and prototyping Data analysis, exploration, and visualization Scientific and engineering graphics Application development, including graphical user interface building

4 The M ATLAB Product Family The MathWorks offers a set of integrated products for data analysis, visualization, application development, simulation, design, and code generation. MATLAB is the foundation for all the MathWorks products. Demos:

5 Using MATLAB in CUHK With Windows Version With Unix Version 200 concurrent licenses using throughout the Departments in CUHK Licenses controlled by a License Server Used by more than 10 Departments in Engineering and Science Faculties

6 Starting MATLAB Windows double-click the MATLAB shortcut icon on your Windows desktop. UNIX type matlab at the operating system prompt. After starting MATLAB, the MATLAB desktop opens.

7 Quitting MATLAB select Exit MATLAB from the File menu in the desktop, or type quit in the Command Window.

8 MATLAB Desktop

9 Command Window

10 Command History

11 Current Directory Browser

12 »workspace Command line variables saved in MATLAB workspace Workspace Browser

13 Window Preferences

14 Getting help MATLAB Documentation >> helpdesk or doc – Online Reference (HTML / PDF) – Solution Search Engine – Link to The MathWorks ( FTP site & latest documentation Submit Questions, Bugs & Requests MATLAB access - MATLAB Digest / Download upgrades

15 Using Help The help command >> help The help window >> helpwin The lookfor command >> lookfor » lookfor example DDEX1 Example 1 for DDE23. DDEX1DE Example of delay differential equations for solving with DDE23. DDEX2 Example 2 for DDE23. ODEEXAMPLES Browse ODE/DAE/BVP/PDE examples..... » help lookfor LOOKFOR Search all M-files for keyword. LOOKFOR XYZ looks for the string XYZ in the first comment line (the H1 line) of the HELP text in all M-files found on MATLABPATH. For all files in which a match occurs, LOOKFOR displays the H1 line..... » lookfor example DDEX1 Example 1 for DDE23. DDEX1DE Example of delay differential equations for solving with DDE23. DDEX2 Example 2 for DDE23. ODEEXAMPLES Browse ODE/DAE/BVP/PDE examples..... » help lookfor LOOKFOR Search all M-files for keyword. LOOKFOR XYZ looks for the string XYZ in the first comment line (the H1 line) of the HELP text in all M-files found on MATLABPATH. For all files in which a match occurs, LOOKFOR displays the H1 line.....

16 Calculations at the Command Line » -5/( )^2 ans = » (3+4i)*(3-4i) ans = 25 » cos(pi/2) ans = e-017 » exp(acos(0.3)) ans = » -5/( )^2 ans = » (3+4i)*(3-4i) ans = 25 » cos(pi/2) ans = e-017 » exp(acos(0.3)) ans = »a = 2; »b = 5; »a^b ans = 32 »x = 5/2*pi; »y = sin(x) y = 1 »z = asin(y) z = »a = 2; »b = 5; »a^b ans = 32 »x = 5/2*pi; »y = sin(x) y = 1 »z = asin(y) z = Results assigned to “ans” if name not specified () parentheses for function inputs Semicolon suppresses screen output MATLAB as a calculator Assigning Variables Numbers stored in double-precision floating point format

17 >> 2 +3 ( 5 ) >>2 *3 ( 6 ) >>1/2( ) >>2 ^3( 8 ) >>0/1( 0 ) >>1/0( Warning: Divide by zero. Inf ) >>0/0( NaN ) Up/Down arrow to recall previous commands Or use Ctrl+C and Ctrl+V to reuse commands Simple Mathematics

18 cos(x), sin(x), tan(x), asinh(x), atan(x), atanh(x), … ceil(x): smallest integer which exceeds x, e.g. ceil(-3.9) returns -3 floor(x): largest integer not exceeding x, e.g. floor(3.8) returns 3 date, exp(x), log(x), log10(x), sqrt(x), abs(x) max(x): maximum element of vector x min(x): minimum element of vector x mean(x): mean value of elements of vector x sum(x): sum of elements of vector x size(a): number of rows and columns of matrix a Some Common Functions

19 rand: random number in the interval [0, 1) realmax: largest positive floating point number realmin: smallest positive floating point number rem(x, y): remainder when x is divided by y, e.g. rem(19,5) returns 4 sign(x): returns -1, 0 or 1 depending on whether x is negative, zero or positive sort(x): sort elements of vector x into ascending order (by column if x is a matrix) Some Common Functions

20 A Matlab program can be edited and saved (using Notepad) to a file with.m extension. It is also called a M-file, a script file or simply a script. When the name of the file is entered in >>, Matlab (or right-click and then run) carries out each statement in the file as if it were entered at the prompt. You are encouraged to use this method. The M-file

21

22

23 Basic Concepts a = 2; b = 7; c = a + b; disp(c) Variables such as a, b and c are called scalars; they are single-valued. MATLAB also handles vectors and matrices, which are the key to many powerful features of the language.

24 Vectors A vector is a special type of matrix, having only one row, or one column. x = [ ] a = [5, 6, 8] y = 1:10(elements are the integers 1, 2, …, 10) z = 1:0.5:4(elements are the values 1, 1.5, …, 4 in increments of 0.5) x’ is the transpose of x. Or you can do it directly: [ ]’.

25 Working with Matrices MATLAB == MATrix LABoratory

26 The Matrix in MATLAB Rows (m) Columns (n) A = A (2,4) A (17) Rectangular Matrix: Scalar:1-by-1 array Vector:m-by-1 array 1-by-n array Matrix:m-by-n array where m, n can be 1, 2, 3, 4, …

27 Any MATLAB expression can be entered as a matrix element Entering Numeric Arrays »a=[1 2;3 4] a = »b=[-2.8, sqrt(-7), (3+5+6)*3/4] b = i »b(2,5) = 23 b = i »a=[1 2;3 4] a = »b=[-2.8, sqrt(-7), (3+5+6)*3/4] b = i »b(2,5) = 23 b = i Row separator: semicolon (;) Column separator: space / comma (,) Use square brackets [ ] Matrices must be rectangular. (Set undefined elements to zero)

28 Entering Numeric Arrays - cont. »w=[1 2;3 4] + 5 w = »x = 1:5 x = »y = 2:-0.5:0 y = »z = rand(2,4) z = »w=[1 2;3 4] + 5 w = »x = 1:5 x = »y = 2:-0.5:0 y = »z = rand(2,4) z = Scalar expansion Creating sequences: colon operator (:) Utility functions for creating matrices. (Ref: Utility Commands)

29 Numerical Array Concatenation - [ ] »a=[1 2;3 4] a = »cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a] cat_a = >> size(cat_a) ans = 6 4 »a=[1 2;3 4] a = »cat_a=[a, 2*a; 3*a, 4*a; 5*a, 6*a] cat_a = >> size(cat_a) ans = 6 4 Use [ ] to combine existing arrays as matrix “elements” Row separator: semicolon (;) Column separator: space / comma (,) Use square brackets [ ] The resulting matrix must be rectangular. 4*a

30 Array Subscripting / Indexing A = A(3,1) A(3) A(1:5,5) A(:,5) A(21:25) A(4:5,2:3) A([9 14;10 15]) Use () parentheses to specify index colon operator (:) specifies range / ALL [ ] to create matrix of index subscripts ‘end’ specifies maximum index value A(1:end,end) A(:,end) A(21:end)’

31 Matrix Multiplication Inner dimensions must be equal Dimension of resulting matrix = outermost dimensions of multiplied matrices Resulting elements = dot product of the rows of the 1st matrix with the columns of the 2nd matrix »a = [ ; ]; »b = ones(4,3); »c = a*b c = »a = [ ; ]; »b = ones(4,3); »c = a*b c = [2x4] [4x3] [2x4]*[4x3] [2x3] a(2nd row).b(3rd column)

32 Array Multiplication Matrices must have the same dimensions Dimensions of resulting matrix = dimensions of multiplied matrices Resulting elements = product of corresponding elements from the original matrices Same rules apply for other array operations »a = [ ; ]; »b = [1:4; 1:4]; »c = a.*b c = »a = [ ; ]; »b = [1:4; 1:4]; »c = a.*b c = c(2,4) = a(2,4)*b(2,4)

33 bal = * rand; if bal < 5000 rate = 0.09; elseif bal < rate = 0.12; else rate = 0.15; end newbal = bal + rate + bal; disp(’New balance is: ’) disp(newbal) Deciding with if

34 for index = j:k statements end forindex = j:m:k(m is the increment) statements end Repeating with for

35 Create a program in newton.m file to calculate the square root of 2 %NEWTON Newton Method example a = 2; x = a/2; for i = 1:6 x = (x+a/x)/2; disp (x) end Square rooting with Newton Method

36 >> newton >> format long >> newton Running newton.m

37 fprintf formats the output as specified by a format string. fprintf ('format string', list of variables) fprintf ('filename', 'format string', list of variables) balance = ; fprintf('New balance: %8.3f', balance) %8.3f means fixed point over 8 columns altogether (including the decimal point and a possible minus sign), with 3 decimal places (spaces are filled in from the left if necessary). Input / Output

38 fprintf example (io_1.m) balance = 12345; rate = 0.09; interest = rate * balance; balance = balance + interest; fprintf('Interest rate: %6.3f New balance: %8.2f\n', rate, balance); >> io_1 Interest rate: New balance: >> Input / Output Examples

39 Input / Output The input statement gives the user the prompt in the text string and then waits for input from the keyboard. It provides a more flexible way of getting data into a program than by assignment statements which need to be edited each time the data must be changed. It allows you to enter data while a script is running. The general form of the input statement is: variable = input(’prompt’);

40 Interactive Input (io_2.m) balance = input('Enter bank balance: '); rate = input('Enter interest rate: '); interest = rate * balance; balance = balance + interest; fprintf('New balance: %8.2f\n', balance); >> io_2 Enter bank balance: 2000 Enter interest rate: 0.08 New balance: >> Input / Output Examples

41 2-D Plotting Specify x-data and/or y-data Specify color, line style and marker symbol (clm), default values used if ‘ clm ’ not specified) Syntax: –Plotting single line: –Plotting multiple lines: plot(x1, y1, 'clm1', x2, y2, 'clm2',...) plot(xdata, ydata, 'clm')

42 x = 0 : 10 y = 2 * x plot (x, y) plot (x, sin(x)) x = 0 : 0.1 :10; pause plot (x, sin(x)) plot (x, sin(x)), grid 2-D Plot – Examples

43 Graphs may be labelled with the following statements: gtext(’text’): writes a string in the graph window grid: add/removes grid lines to/from the current graph text(x, y, ’text’): writes the text at the point specified by x and y title(’text’): writes the text as a title on top of the graph xlabel(’text’): labels the x-axis ylabel(’text’): labels the y-axis 2-D Plot – Labels

44 The function plot3 is the 3-D version of plot. The command plot3(x,y,z) draws a 2-D projection of a line in 3-D through the points whose co- ordinates are the elements of the vectors x, y and z. plot3(rand(1,10), rand(1,10), rand(1,10)) The above command generates 10 random points in 3-D space, and joins them with lines. 3D Plot - Examples

45 MATLAB Exercise