MATLAB Tutorial Dr. David W. Graham.

Slides:



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

Introduction to Matlab
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 Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Introduction to MATLAB MECH 300H Spring Starting of MATLAB.
Introduction to programming in MATLAB MATLAB can be thought of as an super-powerful graphing calculator Remember the TI-83 from calculus? With many more.
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.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
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.
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
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.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
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 ,
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
INTRODUCTION TO MATLAB LAB# 01
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.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Introduction to MATLAB adapted from Dr. Rolf Lakaemper.
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 &
Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.
MATLAB – PT1 The purpose of this workshop is to get you started and to have fun with MATLAB! Let’s talk a little and decide on what we will be covering.
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.
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 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 MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
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.
MAE 421 Matlab review Prof. Mark Glauser Created by: David Marr.
ECE 1304 Introduction to Electrical and Computer Engineering
Introduction to MATLAB
Release Numbers MATLAB is updated regularly
Prof. Mark Glauser Created by: David Marr
Computer Application in Engineering Design
MATLAB: Script and Function Files
Matlab Training Session 4: Control, Flow and Functions
Lecture 25.
L – Modeling and Simulating Social Systems with MATLAB
Basic operations in Matlab
Two-Dimensional Plots
Scripts & Functions Scripts and functions are contained in .m-files
Outline Matlab tutorial How to start and exit Matlab Matlab basics.
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
MATLAB: Structures and File I/O
Introduction To MATLAB
StatLab Matlab Workshop
Use of Mathematics using Technology (Maltlab)
MATLAB – What Is It ? Name is from matrix laboratory Powerful tool for
MATLAB – What Is It ? Name is from matrix laboratory Powerful tool for
Prof. Mark Glauser Created by: David Marr
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Lecture 2 Introduction to MATLAB
MATLAB – What Is It ? Name is from matrix laboratory Powerful tool for
Digital Image Processing
INTRODUCTION TO MATLAB
How to Use MATLAB A Brief Introduction.
Introduction to Matlab
Matlab Basics Tutorial
Presentation transcript:

MATLAB Tutorial Dr. David W. Graham

What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal processing Has many built-in functions useful for learning signals and systems

MATLAB Functionality Fundamental data type is a matrix Built-in support for complex numbers Many built-in functions Ability to create user-defined functions Interpreted language Easy to write code Do not have to compile code

Obtaining and Using MATLAB MATLAB can be found on many computers in the department and ESB ESB 813 has MATLAB on 23 computers MATLAB is in many of the computer labs on the second floor of ESB Purchasing MATLAB www.mathworks.com Student version available for $99 Need the Control Systems Toolbox ($29)

MATLAB Alternatives (FREE) Octave www.gnu.org/software/octave Scilab www.scilab.org

Use of Matrices Can create matrices/vectors in two ways MATLAB uses matrices for all math operations Can create matrices/vectors in two ways Specifically define each element in the matrix A = [ 1 2 3; 4 5 6] Commas and spaces separate columns Semicolons separate rows Use the “colon operator” tt = 0:0.1:1 varname = startval:incrementval:stopval Everything “echoes” back to the screen To suppress echoes, terminate the command with a semicolon

Accessing Individual Elements of a Variable Access individual elements in a matrix A(row,col) e.g. A(1,2) returns a value of 2 For arrays that are single row or column matrices (called vectors), you only need one position tt(5) Accessing an entire row or column Use a colon to indicate all elements within a row or column A(:,2) returns all rows in the second column This would return 2; 5

Finding the Size of a Variable size(variable_name) size(A) size(tt) Returns the size in terms of rows and columns

Determining the Variables in the Workspace who lists all variables in the workspace whos lists all variables with their sizes

Use of Matrices MATLAB inherently works with matrices All operations default to matrix operations Let A = [ 1 2 3; 4 5 6]; Let B = [1; 1; 1]; C = A*B Returns 6; 15 If you tell MATLAB to multiply, it assumes you mean matrix multiplication The ’ mark performs the transpose of the matrix Ex. A’ returns 1 4; 2 5; 3 6 For element-by-element operation (multiplication, division, exponentiation, etc.) Use the “.” operator – Put a . before the operator Ex. tt_squared = tt.*tt; This also works with division (/) and exponentiation (^)

Creating Figures Figures are used to graphically display data To create a new figure, type figure To create a specific figure number, type figure(number); Ex. figure(10); MATLAB will always plot to the most recently selected figure

Plotting Plotting continuous-time signals (make sure the sampling period is small enough) plot(independant_variable,dependant_variable); Ex. plot(tt,yy); You can change the color of the line with the optional argument plot(tt,yy,'r') Can add a second plot to the same figure by running the command hold on; Type help plot for more options

Plotting Plotting discrete-time signals stem(independant_variable,dependant_variable); Ex. stem(tt,yy); You can change the color of the markers with the optional argument stem(tt,yy,'r') Can add a second plot to the same figure by running the command hold on; Type help stem for more options

Labeling Plots You are able to label the axes and title your figures with text strings You must use text strings offset by ' marks within the appropriate function Labeling your plots xlabel('Time (s)') ylabel('Output') Titling your plots title('The Example Signal')

Creating Multiple Graphs in the Same Figure Use the subplot function to create multiple graphs in the same figure subplot(rows_of_plots,columns_of_plots,selected_plot) Ex. subplot(2,1,1); Creates two plots (one above the other) and selects the top one for plotting

Other Useful Plotting Commands Create a legend for all the traces on the plot legend('Signal 1', 'Signal 2') Change the axes ranges to display only the range you desire axis([low_x high_x low_y high_y]) Ex. axis([-10 10 0 5]); Turn the grid on or off grid Toggles the grid on or off grid on Turns the grid on grid off Turns the grid off Hold the plot to allow another signal to be plotted on the same plot hold Toggles the hold function hold on Turns the hold function on hold off Turns the hold function off

Printing and Saving Your Plots Plots can be printed directly from the figure window Plots can also be saved in a variety of formats .fig files .fig files are the default from the pull-down menu .fig files can only be reopened in MATLAB Metafile for use in Word documents and PowerPoint slides Copies the figure to the clipboard print –dmeta .eps files Encapsulated Post Script files Vectorized images For use in LaTeX files Called file_name.eps print -dmeta file_name

Help on MATLAB Functions To receive help on MATLAB functions, simply type help function_name “help” will tell you many useful characteristics about the function Input arguments Output variables Operation of the function Related Functions

Saving Your Data You are able to save all of your data in the workspace using the save function save file_name variable_1 variable_2... Ex. save ee327_example A tt Creates a file called ee327_example.mat You can later load these variables by using the load function load file_name Ex. load ee327_example will load all variables contained in the .mat file (A and tt) This command will replace any existing variables of the same name that are already in the workspace

Use of Scripts Instead of entering everything at the command line, you can write scripts (.m files) to allow you to save your steps Uses the same syntax as the command line To create a new script, type edit at the command line To edit an existing script, type edit filename The % symbol comments everything following it on the line

Example of a Script for Plotting a Line % script to plot a line % will plot a straight line of the form y = mx + b xx = -10:0.01:10; yy = 2*xx + 5; figure; plot(xx,yy) xlabel(‘Time (sec)’); ylabel(‘y(t)’); title(‘An Example of a Straight Line’);

Running a Script Multiple operations for invoking a script Enter the filename of the script at the command prompt Use the pull-down menu in Medit Simply press F5

Output of the Example Script

Debugging a Script MATLAB has a powerful debugger – use it! Can set breakpoints to stop the execution of commands Use F12 to place or remove a breakpoint (or use the “Debug” pull-down menu) When in “debug mode” Can view the values of all variables Can run other commands while in “debug” mode Can step through the lines of code line by line with the F10 button Can resume execution (up to the next breakpoint) with F5

Writing Functions in MATLAB MATLAB allows you to write your own user defined functions very easily Write functions when you have sets of code you will frequently reuse Code is written just like scripts with one extra line at the beginning The first line declares Name of the function All input arguments All output variables Format is [out1, out2, …] = function_name(in1, in2, in3, …) Ex. [yy] = makeline(time, slope, yint)

Example of a MATLAB Function [yy] = makeline(time, m, yint) % [yy] = makeline(time, m, yint) % It is always a good idea to heavily % comment your code % The initial set of comments in a % function will appear at the command % prompt if you type “help function_name” % Function to create a line yy = m*time + yint; % make sure that all output variables are written to in the lines of the function

Running a Function from the Command Line At the command line, simply call your user-defined function like you would any other function Ex. [yout] = makeline(tt,2,5);

Loops You can write loops in MATLAB functions and scripts, much like any coding language FOR loops for ii=1:2:10 % ii is the loop counter (can call it anything) % in this example, ii starts at 1, increments by 2 each time % through the loop, and stops when it equals 10 (after going through % that iteration) % can access ii in the loop like any other variable var1 = tt(ii); % this would write the value of tt at element number ii % to the variable var1 end WHILE loops while(condition) % lines of code go here % will execute this loop until the condition has been met Example while(xx < 10) xx = xx + 2; This will execute the loop until xx is greater than or equal to 10 (make sure you initialize xx before you start the loop)

Conditional Statements if condition statements elseif condition else end Conditional Operators == Is equal to ~= Not equal to < Less than > Greater than <= Less than or equal to >= Greater than or equal to Logical Operators & AND | OR ~ NOT