Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

1 EMT 101 – Engineering Programming Dr. Farzad Ismail School of Aerospace Engineering Universiti Sains Malaysia Nibong Tebal Pulau Pinang Week 10.
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Intro to Matlab 1.Vectors, matrices, and arithmetic 2.Plotting 3.Flow Constructs 4.Other Useful Things Can be found at:
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
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.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
MATLAB Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson.
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.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
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,
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (2): MATLAB Environment (Chapter 1)
Math 15 Lecture 7 University of California, Merced Scilab A “Very” Short Introduction.
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,
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.
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.
INTRODUCTION TO MATLAB LAB# 01
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
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.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Lecture 2 - Matlab Introduction CVEN 302 June 5, 2002.
Scientific Computing Introduction to Matlab Programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
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.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
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.
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.
(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 Electromagnetic Theory LAB by Engr. Mian Shahzad Iqbal.
MA/CS 375 Fall 2003 Lecture 3. .* Multiplication We can use the.* operator to perform multiplication entry by entry of two matrices:
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
Matlab for Engineers Matlab Environment Chapter 2.
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.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
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 실험 - Matlab 사용법 - Photonic Systems Laboratory School of EE, Seoul National University Photonic Systems Lab School of EECS, S.N.U.
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.
Outline What is MATLAB MATLAB desktop Variables, Vectors and Matrices Matrix operations Array operations Built-in functions: Scalar, Vector, Matrix Data.
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
ECE 1304 Introduction to Electrical and Computer Engineering
MATLAB DENC 2533 ECADD LAB 9.
MATH 493 Introduction to MATLAB
Introduction to MATLAB
Vectors, matrices, and arithmetic
INTRODUCTION TO MATLAB
Announcements P3 due today
CS 111 Introduction to Computing in Engineering and Science
Matlab Basics Tutorial
Presentation transcript:

Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting

Basic functions MATLAB as a calculator » (3+5)/7 ans = » x = (3+5)/7 x = » x x = addition -subtraction /division *multiplication ^power Sum of (3+5) divided by 7 define a variable

Basic function (continued) If you want to see how many variables you have defined so far, you can use the following command: » who Your variables are: ans x » »whos Name Size Bytes Class ans 1x1 8 double array x 1x1 8 double array Grand total is 2 elements using 16 bytes List all the variables in the current MATLAB session List all the variables with details

Basic function (continued) Once you have defined variables: » x = -10 x = -10 » y = 5*x y = -50 » x = (3+5)/7 x = *Once you have defined a value for a variable, you can use it to perform algebraic operation *By default, MATLAB displays 4 decimal digit if the value is not an integer

Special Variables In MATLAB, there are some special variables: » pi ans = » i, j, ans = i ans = i Value for  Imaginary number by default unless you change them

Format By default, the answer only displays 4 decimal places if it is not an integer If you want to change the format of the output: » format long » pi ans = Other format for the output: format short (4 decimal places) format short e (scientific notation) format long (more than 12) format bank (2 decimal place)

Built-in functions In MATLAB, there are many standard built-in functions » z = sin (pi/4) z = » q = log (4) q = Trigonometric function: sin ( ) asin ( ) cos ( ) acos ( ) tan ( ) atan ( ) ** Angle is in radian Elementary function: sqrt ( )  exp ( )e log ( )log log10 ( )log 10

Defining a vector (Row vector) MATLAB is a tool for doing computations with vectors & matrices Row Vectors: » v1 = [2 3, 3] v1 = » length (v1) ans = 3 Name of vector You can use either a space or comma to separate the elements in vector definition Square bracket Reports the number of elements in a vector

Defining a vector (Row vector) You can perform standard linear algebra operations on these vectors » v2 = 3*v1 v2 = » v3 = [2 2] v3 = 2 2 » v4 = v3+v1 ??? Undefined function or variable ‘v1’ » Multiplication by a scalar quantity Other vector operations: - Addition/subtraction of a vector -dot product …etc **Row vectors must have the same length

Defining a vector (Column vector) Similar you can also define a column vector » c1 = [1;3;2] c1 = » c2 = [2 2 4] c2 = 2 4 Use semicolon or enter the value at the next line for next row elements Square bracket

Defining a vector (Column vector) We can convert a row vector into a column vector (or vice versa) by taking the transpose which can be done by using a single quote ’ » v4 = c1’ v4 = » Transpose operator

Matrices In a similar fashion, you can define a matrix in MATLAB by doing the following: » a =[ ] a = » a = [ 5 7 9; ] a = Element in first row Use a semi-colon for next row elements Similarly, for matrix operation, it must follow the basic rule of linear algebra, eg. dimensions must agree!

Matrix Math Matrix math –Dimensions must agree Scalar math –Same as usual Scalar / matrix math –Scalar + matrix = [scalar + matrix(x, y)] –Scalar * matrix = [scalar * matrix(x, y)]

More Matrix Math The ‘.’ operator –“element by element” access Example: –x = [1 2 3];y = [4; 5; 6]; –x * y = 32 –x * y’ ERROR! –x.* y’ = [ ] For some functions (x same as above): –x ^ 2ERROR! –x. ^2fine

Creating Vectors with Constant Spacing vname = [m:q:n] m is first element q is spacing (if omitted 1 is assumed) n is last element >> z = [1:2:7] z = >> x = [1:7] x =

Creating Vectors with Constant Spacing and Known Element Count vname = linspace( xi, xf, n ) xi is first element xf is last element n is number of elements in vector omitted n gives 100 elements >> z = linspace( 1, 5, 9 ) z =

More Vector Math Examine values of f(x) for −5 ≤ x ≤ 5

Element access xretrieves entire matrix x x(1,2)retrieves element at row 1, col 2 x(1, 5:10)retrieves row 1, columns 5 to 10 (use colon) x(1,:)retrieves row 1, all columns Useful functions: size(x) rows, cols of x

Plotting » x=[4 8 11] x = » y=[4 5 9] y = » plot (x,y) » plot(x,y,’o’) » axis([ ]) » title(‘experimental data’) » xlabel(‘x’) » ylabel(‘y’) » grid on Plot the discrete data point indicated by o Change axis [xmin xmax ymin ymax] Labeling Turn on the gird on the graph Plot the data point and connected them by straight line

Plotting (continued) » z z = » plot (z(:,1),z(:,2)) All row elements in first column All row elements in second column » plot (x1, y1, x2, y2, x3, y3 ) Multiple plot on the same graph (3 set of data points) If the data is stored in a matrix

Plotting (continued) » t = 0:.3:10; » y = sin(t); » plot(t,y,’r’) ; To make a graph of y = sin(t) on the interval t = 0 to t = 10 We can also use the commant fplot: » fplot (‘ff’, [ 0 10]) Function defined by a m-file (make sure you put the quotation) Limit for x value Note the use of the colon as a ‘range’ operator

Some housekeeping commands The command save can store all the variables defined in the workspace in a binary file matlab.mat (default name) The command load can restore the workspace from the file matlab.mat You can also store individual variable by writing: save filename X Y Z You specify a filename The command clear all remove all variables in the current workspace

Making a M-file Filename has an extension.m You can use M-file to do 2 things: - script file (when you want to do many MATLAB operations at the same time, consisting of a sequence of normal MATLAB statement) - function file (to create a function) To create a M-file, go to the toolbar of the MATLAB command window and from: file  new  M-file

Script file %filename= method1.m clear all x1= (2+4)/5; x2= x1*7; y1= sqrt(15) y2= exp(5) v1 = [x1 x2] c1 = [y1;y2]; dot_product = v1*c1 display('SAY HI!!!!') » method1 y1 = y2 = v1 = dot_product = e+003 ans = SAY HI!!!! Comment (is not a MATLAB statement) Erase all previously defined variables MATLAB statement The filename is called method1.m Use the command ls to check if this file is in your currently working directory Output To inhibit echo (operation not shown)

Function file function [out] = mean (x) % To calculate the mean or average of % a list of number % X is an array of number m = length(x) out = sum(x)/m output » v = [ ] v = » avg = mean(v) m = 5 mean = avg = Indicate that this file is a function M-file Return value Name of your function * *The name of your function should be the same as your m-file name Input argument

Flow Constructs IF block if ( ) elseif end Conditions <less than <=less and equal then ==equal >greater than <greater and equal then ~=not equal As you learned from programming, there are times when you want your code to make a decision:

If statement % filename method2.m if (grade< 55) display('fail') else if(grade>85) display('grade A') else display('pass') end Output » grade=88 grade = 88 » method2 ans = grade A » # of “if” must be consistent with # of “end”

Flow Constructs For loop for i = 1:10 end For loop for i = 1:2:10 end Initial value Final value (where you stop the loop) Initial value Final value (where you stop the loop) Specify the increment

Flow Constructs While loop n=0 while (n <8) n=n+1 end condition