Introduction to Matlab Workshop Matthew Johnson, Economics October 17, 2008 4/13/20151.

Slides:



Advertisements
Similar presentations
Introduction to Programming using Matlab Session 2 P DuffourJan 2008.
Advertisements

StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013.
Introduction to MATLAB The language of Technical Computing.
Introduction to M ATLAB Programming Ian Brooks Institute for Climate & Atmospheric Science School of Earth & Environment
Introduction to Matlab
Matlab Programming Huajun Wang Department of Earth Sciences, Zhejiang University Jan 25,2012.
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 for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
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 By: Dr. Maher O. EL-Ghossain.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Getting started with Matlab Numerical Methods Appendix B help/techdoc/learn_matlab/learn_matlab.html.
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.
MATLAB Lecture One Monday 4 July Matlab Melvyn Sim Department of Decision Sciences NUS Business School
1 Statistical Computing in MATLAB AMS 597 Ling Leng.
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.
You can make this in matlab!. Matlab Introduction and Matrices.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
Data, graphics, and programming in R 28.1, 30.1, Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen.
Lecture 4 MATLAB Windows Arithmetic Operators Maintenance Functions
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.
Yasser F. O. Mohammad Approximations and Round-off Errors.
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 ,
INTRODUCTION TO MATLAB LAB# 01
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
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.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Matlab 14.html Cost: $100 Available in labs on Windows and Unix machines.
ME6104: CAD. Module 4. ME6104: CAD. Module 4. Systems Realization Laboratory Module 4 Matlab ME 6104 – Fundamentals of Computer-Aided Design.
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 &
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
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.
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.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Introduction to Matlab Patrice Koehl Department of Biological Sciences National University of Singapore
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.
Introduction to Matlab Engr. Mian Shahzad Iqbal LAB NO.2
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
Release Numbers MATLAB is updated regularly
Introduction to Matlab
Statistical Computing in MATLAB
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Introduction to Matlab
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
Matlab review Matlab is a numerical analysis system
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.
Matlab Basics Tutorial
Presentation transcript:

Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151

2 Getting Help Useful links: The help links on this page include Mathworks’ Getting Started (the official online documentation of Matlab) Kermit Sigmon’s MATLAB Primer (a very good beginner manual); University of Utah’s MATLAB Tutorial and some others. MATLAB’s online help manual Statlab Consultants

3 What is MATLAB? MATLAB is a matrix-based tool for numerical computations. It’s very powerful and easy to use. MATLAB is a matrix-based tool for numerical computations. It’s very powerful and easy to use. Both programming language and interactive environment Both programming language and interactive environment Very fast native functions; very slow when processing loops Very fast native functions; very slow when processing loops Lots of available toolboxes Lots of available toolboxes

4 Acquiring Matlab Free for Yale students Free for Yale students Installed on all Statlab computers Installed on all Statlab computers

5 Launching Matlab Double-click the “MATLAB R2008a” icon on the desktop Double-click the “MATLAB R2008a” icon on the desktop

6 The Interface Main Window: Input/Output Main Window: Input/Output Workspace: consists of the variables you create during a MATLAB session; Workspace: consists of the variables you create during a MATLAB session; Command History: double click them to evaluate them; Command History: double click them to evaluate them; Current Directory browser: shows you where you are. Current Directory browser: shows you where you are.

7 Interface (continued) Can use Matlab as a calculator Can use Matlab as a calculator Type “help” for a list of all help topics Type “help” for a list of all help topics help abs gives the syntax and information about the absolute value command help abs gives the syntax and information about the absolute value command

8 Entering Matrices Matrices can be :  Entered manually A = [1 2 3 ; ; 7 8 9]  Generated by built-in functions  Loaded from a file  File>Import Data

9 Matrix operations: + addition + addition - subtraction - subtraction * multiplication * multiplication ^ power ^ power ‘ transpose ‘ transpose To make operations apply element-by-element, we precede the operators by ‘.’ To make operations apply element-by-element, we precede the operators by ‘.’

10 Subscripts: Subscripts: the element in row i and column j of A is denoted by A(i, j). Subscripts: the element in row i and column j of A is denoted by A(i, j). i,j can also be vectors of indices or logical arrays: A=4*[ ]’ b=A>18; c=[ ]’ i,j can also be vectors of indices or logical arrays: A=4*[ ]’ b=A>18; c=[ ]’ A(b), A(c) A(b), A(c)

11 The Colon Operator ‘:’ The Colon Operator ‘:’ The colon ‘:’ is one of MATLAB ’s most important operators. It has many formats: Subscript expressions involving colons refer to portions of a matrix: Subscript expressions involving colons refer to portions of a matrix: A(5:9) is the fifth to the ninth elements of A. [0:0.2:3] is a row vector containing integers from 0 to 3, in increments of 0.2 [0:0.2:3] is a row vector containing integers from 0 to 3, in increments of 0.2

12 Matrices and Random Numbers: Four functions that generate basic matrices: Zeros: all zeros. A = zeros(1,3) Zeros: all zeros. A = zeros(1,3) Ones: all ones. A = ones(2,4) Ones: all ones. A = ones(2,4) Rand: elements are U[0,1] random variables A = rand(3,5) Rand: elements are U[0,1] random variables A = rand(3,5) Randn: elements are standard-normal random variables A = randn(2,5) Randn: elements are standard-normal random variables A = randn(2,5) Be careful: Matlab always sets the same seed. Get ‘more random’ numbers by typing rand('state', sum(100*clock)) Be careful: Matlab always sets the same seed. Get ‘more random’ numbers by typing rand('state', sum(100*clock))

13 A=eye(3) gives a 3-by-3 identity matrix A=eye(3) gives a 3-by-3 identity matrix sparse(m,n): same as zeros(m,n), use if most elements are zeros. sparse(m,n): same as zeros(m,n), use if most elements are zeros. Concatenation: join small (compatible) matrices to make bigger ones: Concatenation: join small (compatible) matrices to make bigger ones: B = [A A-2; A*2 A/4] Deleting rows and columns: Deleting rows and columns: B(:,2) = [ ] B(:,2) = [ ]

14 Suppressing Output: If you simply type a statement and press Enter, MATLAB automatically displays the results on screen. If you end the line with a semicolon ‘;’, MATLAB performs the computation but does not display any result. Example: C = randn(5,1) v.s. Example: C = randn(5,1) v.s. C = randn(5,1); C = randn(5,1);

Aside: You can run Matlab remotely! Available to anyone with a Yale pantheon account Available to anyone with a Yale pantheon account You need the following (free) software You need the following (free) software Xming (available from Xming (available from SSH (available from SSH (available from Useful if you don’t want to have Matlab take up memory/processing power on you computer Useful if you don’t want to have Matlab take up memory/processing power on you computer 15

Aside: You can run Matlab remotely! Instructions: Instructions: Start Xming (it will run in the background) Start Xming (it will run in the background) Log in to eli.yale.edu through SSH Log in to eli.yale.edu through SSH In SSH click on Edit, then click Settings, then click Tunneling In SSH click on Edit, then click Settings, then click Tunneling Check the box at the bottom that says “Tunnel X11 Connections” and click OK Check the box at the bottom that says “Tunnel X11 Connections” and click OK You’re all set! Type “matlab &” in SSH to run the program You’re all set! Type “matlab &” in SSH to run the program Note that this is an older version of matlab Note that this is an older version of matlab 16

17 Functions: MATLAB provides a large number of standard elementary mathematical functions, including abs, sqrt, exp, sin. MATLAB provides a large number of standard elementary mathematical functions, including abs, sqrt, exp, sin. For a list of the elementary mathematical functions, type: For a list of the elementary mathematical functions, type: help elfun help elfun For a list of more advanced mathematical and matrix functions, type For a list of more advanced mathematical and matrix functions, type help specfun help elmat

18 Programming with MATLAB: Files that contain code in the MATLAB language are called M-files. You can create M- files using the matlab editor, then use them as you would any other MATLAB functions or commands. There are two types of M-files: Scripts and Functions. Files that contain code in the MATLAB language are called M-files. You can create M- files using the matlab editor, then use them as you would any other MATLAB functions or commands. There are two types of M-files: Scripts and Functions.

19 Scripts Scripts: a bunch of code grouped together; doesn’t accept argument or return output. Scripts: a bunch of code grouped together; doesn’t accept argument or return output. You should do most of your work using scripts! You should do most of your work using scripts! Example: Example: open m-file editor open m-file editor type disp(‘Hello’) type disp(‘Hello’) type disp(‘World’) type disp(‘World’) save as test.m in your current directory save as test.m in your current directory type “test” in Matlab type “test” in Matlab

20 Functions: Functions are M-files that can accept input arguments and return output arguments. The name of the M-file and of the function should be the same. Functions are M-files that can accept input arguments and return output arguments. The name of the M-file and of the function should be the same. For example, save this as area.m: function ar = area(radius) ar=pi*radius^2; For example, save this as area.m: function ar = area(radius) ar=pi*radius^2;

21 Flow Control: MATLAB has following flow controls: If statement If statement For loops For loops While loops While loops

22 if … elseif … else … end if A > B 'greater' elseif A < B 'less' else 'equal' end

23 for … end beta=0.925; a=1:100000; for i = 1: betavec(i)=beta^a(i); end betavec But you should avoid for loops if possible newbetavec=beta.^a

24 Graphics: plot x = [0 :.01 : 2*pi]; y = sin(x); plot(x,y) y2 = sin(x-.25) y3 = sin(x-.5) plot(x,y,x,y2,x,y3)

Cool Graphs! [x,y] = meshgrid([-2:.2:2]); [x,y] = meshgrid([-2:.2:2]); Z = x.*exp(-x.^2-y.^2); Z = x.*exp(-x.^2-y.^2); surf(x,y,Z,gradient(Z)) surf(x,y,Z,gradient(Z)) 25

26 Learn from others There are lots of Matlab functions already out there: There are lots of Matlab functions already out there: Google them! Google them! James LeSage’s econometrics toolbox: James LeSage’s econometrics toolbox: