Introduction to MATLAB Zongqiang Liao Research Computing Group UNC-Chapel Hill.

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

Introduction to MATLAB
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
Introduction to Matlab EE 2303 Lab. MATLAB stands for “Matrix Laboratory” APPLICATIONS OF MATLAB:  Mathematical Calculations  Data Analysis & Visualization.
MATLAB ME1107 Y Yan Reference: MATLAB for Engineers by Holly Moore (Pearson Prentice Hall)
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.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Introduction to Matlab Οικονομίδης Δημήτρης
Introduction to 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.
Introduction to Matlab Jianguo Wang CSSCR September 2009.
Matlab intro The Environment
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
MATLAB Fundamentals.
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.
Nonparametric Econometrics1 Intro to Matlab for Data Analysis and Statistical Modeling.
Introduction to MATLAB Zongqiang Liao Research Computing Group UNC-Chapel Hill.
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,
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
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.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
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.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
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.
Chapter 1: Getting Started with MATLAB MATLAB for Scientist and Engineers Using Symbolic Toolbox.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Basics of MATLAB By DR. Wafaa Shabana
MATLAB Environment ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
Lecture 2 - Matlab Introduction CVEN 302 June 5, 2002.
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
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.
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.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Introduction to Matlab
Introduction to Matlab Electromagnetic Theory LAB by Engr. Mian Shahzad Iqbal.
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.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Introduction to Matlab Engr. Mian Shahzad Iqbal LAB NO.2
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.
Introduction to MATLAB Mark Reed Zongqiang Liao Research Computing Group UNC-Chapel Hill.
Introduction to MATLAB
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Introduction to Matlab
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
MATH 493 Introduction to MATLAB
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)
Presentation transcript:

Introduction to MATLAB Zongqiang Liao Research Computing Group UNC-Chapel Hill

2 Purpose  This course is an introductory level course for beginners.  The purpose of this course is to introduce you to some of the basic commands and features of MATLAB.

3 Course agenda  Introduction  Getting started  Mathematical functions  Matrix generation  Reading and writing data files  Basic plotting  Basic programming

4 Introduction  The name MATLAB stands for MATrix LABoratory  It is good at dealing with matrices  Vendor’s website: http//:  Advantages of MATLAB  Easiness of use  Powerful build-in routines and toolboxes  Good visualization of results  Popularity in both academia and industry  Disadvantage of MATLAB  Can be slow

5 Getting started  MATLAB desktop  The Command Window  The Command History  The Workspace  The Current Directory  The Help Browser  The Start Button

6 Getting started  Using MATLAB as a calculator >> pi ans = More examples: >> sin(pi/4) >> 2^(log(4)) >> sqrt(9)

7 Getting started  A ssign values to output variables >> x=5 x= 5 >> y = 'Bob' y = Bob

8 Getting started  Suppressing output  You can suppress the numerical output by putting a semicolon (;) at the end of the line >> t=pi/3 >> u=sin(t)/cos(t); >> v= u- tan(t);  Case sensitive  Example: “time” and “Time” are different variables >> time=61; >> Time=61;

9 Getting started  Managing the workspace  The results of one problem may have an effect on the next one  Issue a clear command at the start of each new independent calculation >> clear t or >> clear all

10 Getting started  Miscellaneous commands  To clear the Command Window >> clc  To abort a MATLAB computation ctrl-C  To continue a line …  To recall previous commands

11 Getting started  Getting help  Use help to request info on a specific function >> help sqrt  Use doc function to open the on-line version of the help menu >> doc plot  Use lookfor to find function by keywords >> lookfor regression

12 Mathematical functions  Lists of build-in mathematical functions  Elementary functions >> help elfun  Special functions >> help specfun  Such as sin(x), cos(x), tan(x), e x, ln(x)

13 Mathematical functions  Example 1 Calculate z=e -a sin(x)+10 for a=5, x=2, y=8 >> a=5; x=2; y=8; >> z=exp(-a)*sin(x)+10*sqrt(y) z=  Example 2 log(142), log10(142)

14 Matrix generation  The name MATLAB is taken from ”MATrix LABoratory.” It is good at dealing with matrices.  Actually all variables in MATLAB are matrices.  Scalars are 1-by-1 matrices  vectors are N-by-1 (or 1-by-N) matrices.  You can see this by executing >> size(x)

15 Matrix generation  Entering a matrix  Begin with a square bracket, [  Separate elements in a row with spaces or commas (,)  Use a semicolon (;) to separate rows  End the matrix with another square bracket, ]

16 Matrix generation Entering a matrix: A typical example >> A=[1 2 3; 4 5 6; 7 8 9] >> A=

17 Matrix generation  Matrix indexing  View a particular element in a matrix  For example, A(1,3) is an element of first row and third column >>A(1,3) >>ans = 3

18 Matrix generation  Colon operator in a matrix  Colon operator is very useful in the usage of MATLAB  For example, A(m:n,k:l) specifies portions of a matrix A: rows m to n and column k to l.  Examples: A(2:3, 2:3) A(2, :) A(2:end, :)

19 Matrix generation  Transposing a matrix The transposing operation is a single quote (’) >>A’  Concatenating matrices Matrices can be made up of sub-matrices >>B= [A 10*A; -A [1 0 0; 0 1 0; 0 0 1]]

20 Matrix generation  Generating vectors: colon operator  Suppose we want to enter a vector x consisting of points (0, 0.1, 0.2, 0.3,…,5) >>x=0:0.1:5;  All the elements in between 0 and 5 increase by one- tenth

21 Matrix generation  Elementary matrix generators  eye(m,n)  eye(n)  zeros(m,n)  ones(m,n)  diag(A)  rand(m,n)  randn(m,n)  logspace(a,b,n)  For a complete list of elementary matrices >>help elmat >>doc elmat

22 Reading and writing data files  Save command Example 1, save all variables in the workspace into a binary file: >> x = [1 3 -4]; >> y = [2 -1 7]; >> z = [3 2 3]; >> save Filename.mat Save only certain variables by specifying the variable names after the file name >> save Filename.mat x y

23  Save command  Example 2, save variables into ASCII data file >> save Filename.dat x y –ascii or >> save Filename.txt x y –ascii Reading and writing data files

24  load command  The data can be read back with the load command >> load Filename.mat  Load only some of the variables into memory >> load Filename.mat x  Load the ASCII data file back into memory >> load Filename.dat -ascii Reading and writing data files

25  The textread function  The load command assumes all of data is of a single type  The textread function is more flexible, it is designed to read ASCII files where each column can be of a different type  The command is: >> [A,B,C,...] = textread(filename, format, n); Reading and writing data files

26  The textread function  For example, if a text file “mydata.dat” contains the following lines: tommy 32 male 78.8 sandy 3 female 88.2 alex 27 male 44.4 saul 11 male 99.6  The command is: >> [name,age,gender,score] = textread(‘mydata.dat’, ‘%s %d %s %f’, 4); Reading and writing data files

27  The xlsread function  The xlsread function is to get data and text from a spreadsheet in an Excel workbook.  The basic command is: >> d=xlsread(‘datafile.xls’) Reading and writing data files

28 Basic plotting  A simple line plot  To plot the function y=sin(x) on the interval [0, 2 ] >>x=0:pi/100:2*pi; >>y=sin(x); >>plot(x,y) >>xlabel (‘x=0:2\pi’); >>ylabel (‘Sine of x’); >>title (‘Plot of the Sine function’);

29 Basic plotting  Plotting elementary functions

30 Basic plotting  Multiple data sets in one plot  Several graphs may be drawn on the same figure  For example, plot three related function of x: y 1 =2cos(x), y 2 =cos(x), and y 3 =0.5cos(x), on the interval [0, 2 ]

31 Basic plotting  Multiple data sets in one plot >> x = 0:pi/100:2*pi; >> y1 = 2*cos(x); >> y2 = cos(x); >> y3 = 0.5*cos(x); >> plot(x,y1,‘--’,x,y2,‘-’,x,y3,‘:’) >> xlabel(‘0 \leq x \leq 2\pi’) >> ylabel(‘Cosine functions’) >> legend(‘2*cos(x)’,‘cos(x)’,‘0.5*cos(x)’) >> title(‘Typical example of multiple plots’)

32 Basic plotting  Multiple data sets in one plot

33 Basic plotting  Subplot  The graphic window can be split into an m*n array of small windows.  The windows are counted 1 to mn row-wise, starting from the top left  For example, plot three related function of x: y 1 =sin(3 x), y 2 =cos(3 x), y 3 =sin(6 x), y 4 =cos(6 x), on the interval [0, 1]

34 Basic plotting  Subplot >> x = 0:1/100:1; >> y1 = sin(3*pi*x); >> y2 = cos(3*pi*x); >> y3 = sin(6*pi*x); >> y4 = cos(6*pi*x); >> title(‘Typical example of subplots’) >> subplot(2,2,1), plot(x,y1) >> xlabel(‘0 \leq x \leq 1’), ylabel(‘sin(3 \pi x)’) >> subplot(2,2,2), plot(x,y2) >> xlabel(‘0 \leq x \leq 1’), ylabel(‘cos(3 \pi x)’) >> subplot(2,2,3), plot(x,y3) >> xlabel(‘0 \leq x \leq 1’), ylabel(‘sin(6 \pi x)’) >> subplot(2,2,4), plot(x,y4) >> xlabel(‘0 \leq x \leq 1’), ylabel(‘cos(6 \pi x)’)

35 Basic plotting  Subplot

36 Programming in MATLAB  M-File scripts  In order to repeat any calculation and/or make any adjustments, it is create a file with a list of commands.  “File  New  M-file”  For example, put the commands for plotting soil temperature into a file called scriptexample.m

37 Programming in MATLAB  M-File scripts  Enter the following statements in the file load 'soilT.dat'; time=soilT(:,1); soil_temp_mor=soilT(:,2); soil_temp_aft=soilT(:,3); plot(time,soil_temp_mor,'--',time,soil_temp_aft,'-'); xlabel('Time'); ylabel('Soil temperature'); legend('Morning','Afternoon'); title('Soil Temperature');  Save and name the file, scriptexample.m Note: the first character of the filename must be a letter

38 Programming in MATLAB  M-File scripts  Run the file

39 Programming in MATLAB  M-File scripts  MATLAB treats anything that appears after the % on a line as comments and these line will be ignored when the file runs % % scriptexample.m is to display soil temperature in the morning and %the afternoon. %

40 Programming in MATLAB  M-File functions  Functions are routines that are general and applicable to many problems.  To define a MATLAB function:  Decide a name for the function, making sure that it does not conflict a name that is already used by MATLAB.  Document the function  The first command line of the file must have this format: function[list of outputs]=functionname(list of inputs) …….  Save the function as a M-file

41 Programming in MATLAB  M-File functions  Consider an example to plot the piecewise defined function:

42 Programming in MATLAB  M-File functions  It is convenient to have a separate file which can do a specific calculation. function [F]= eff(x) % Function to calculate values % Input x % Output F for i=1:length(x) if x(i)<0.5 F(i)=x(i)^2; else F(i)=0.25; end

43 Programming in MATLAB  M-File functions  To evaluate this function, a main program is needed. This main program provides input arguments % Main program, use function: eff.m x=-1:0.01:1; plot(x,eff(x)); grid xlabel('x'); ylabel('F'); title('The Piecewise Defined Function:');

44 Programming in MATLAB  M-File functions  Run the main file

45 Questions and Comments?  For assistance with MATLAB, please contact the Research Computing Group:   Phone: HELP  Submit help ticket at