StatLab Matlab Workshop

Slides:



Advertisements
Similar presentations
StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013.
Advertisements

Introduction to MATLAB The language of Technical Computing.
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 By: Dr. Maher O. EL-Ghossain.
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 Basics With a brief review of linear algebra by Lanyi Xu modified by D.G.E. Robertson.
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.
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.
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.
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.
Yasser F. O. Mohammad Approximations and Round-off Errors.
A Brief Introduction to Matlab Laila Guessous Dept. of Mechanical Engineering Oakland University.
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.
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.
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.
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”
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 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
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,
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.
MATLAB ……………….matrix laboratory. Bhushan D Patil PhD Research Scholar Department of Electrical Engineering Indian Institute of Technology, Bombay Powai,
Introduction to Matlab. Outline:  What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators.
ECE 1304 Introduction to Electrical and Computer Engineering
Introduction to MATLAB
Introduction to Matlab
Release Numbers MATLAB is updated regularly
Prof. Mark Glauser Created by: David Marr
Introduction to Matlab
Introduction to Mat lab
Statistical Computing in MATLAB
Introduction to MATLAB
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Introduction to Matlab
Matlab Workshop 9/22/2018.
Matlab review Matlab is a numerical analysis system
Use of Mathematics using Technology (Maltlab)
MATLAB Tutorial Dr. David W. Graham.
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Lecture 2 Introduction to MATLAB
Introduction to MATLAB [Vectors and Matrices] Lab 2
INTRODUCTION TO MATLAB
Experiment No. (1) - an introduction to MATLAB
Announcements P3 due today
Matlab Training Session 2: Matrix Operations and Relational Operators
Laboratory in Oceanography: Data and Methods
Matlab Basics Tutorial
Presentation transcript:

StatLab Matlab Workshop 11/15/2018

Yale University StatLab Resources Useful links: http://statlab.stat.yale.edu/help/FAQ/matlab_FAQ.jsp 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 Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab What is MATLAB? MATLAB is a matrix-based tool for numerical computations. It’s very powerful and easy to use. Both programming language and interactive environment Very fast native functions; very slow when processing loops Lots of available toolboxes Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab Launching Matlab Click MATLAB in the start menu (on Unix systems: type “matlab” to enter interactive mode) Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab The Interface Main Window: Input/Output Workspace: consists of the variables you create during a MATLAB session; Command History: double click lines to evaluate them; Current Directory browser: shows you where you are. Editor/Debugger: pops up when you create an M-file (click on “New” button to launch it.) Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab Entering Matrices Matrices can be Entered manually A = [1 2 3 ; 4 5 6 ; 7 8 9] Generated by built-in functions Loaded from a file Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab Matrix operations: + addition - subtraction * multiplication ^ power ‘ transpose \ left division, / division x = A \ b is the solution of A * x = b x = b / A is the solution of x * A = b To make ‘*’ , ‘^’, ‘\’ and ‘/’ apply element-by-element, we precede the operators by ‘.’ Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab Subscripts: 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*[1 2 3 4 5 6 7 8 9]’ b=A>18; c=[5 6 7 8 9]’ A(b) gives same result as A(c) Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab The Colon Operator ‘:’ The colon ‘:’ is one of MATLAB ’s most important operators. It has many formats: [0:0.2:3] is a row vector containing integers from 0 to 3, in increments of 0.2 Subscript expressions involving colons refer to portions of a matrix: A(1:3 , 2) is the first to the third elements of the second column of A. Yale University StatLab http://www.yale.edu/statlab

Working with Matrices: Four functions that generate basic matrices: Zeros: all zeros. A = zeros(1,3) Ones: all ones. A = ones(2,4) Rand: elements are U[0,1] random variables A = rand(3,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)) Yale University StatLab http://www.yale.edu/statlab

Working with Matrices (2) A=eye(3) gives a 3-by-3 identity matrix sparse(m,n): same as zeros(m,n), use if most elements are zeros. Concatenation: join small (compatible) matrices to make bigger ones: B = [A A-2; A*2 A/4] Deleting rows and columns: B(:,2) = [ ] Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab Putting it together Many operations can take Matrix inputs. Example: A = [1 2 3 ; 4 5 6 ; 7 8 9] B=A>5 Use indices and element-by-element operations to avoid slow and unwieldy loops: beta=0.925; auxil=1:200; betavec(auxil)=beta.^auxil; betavec=betavec’ Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab 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. C = randn(5,1); Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab Functions: MATLAB provides a large number of standard elementary mathematical functions, including abs, sqrt, exp, sin. For a list of the elementary mathematical functions, type: help elfun For a list of more advanced mathematical and matrix functions, type help specfun help elmat For a list of data analysis functions, type help datafun Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab Flow Control: MATLAB has following flow controls: If statement For loops While loops Continue statement Break statement Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab if … elseif … else … end If A > B ‘greater’ elseif A < B ‘less’ elseif A == B ‘equal’ else error(‘Unexpected situation’) end Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab for … end for i = 1:m for j = 1:n H(i,j) = 1/(i+j) end Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab 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) Yale University StatLab http://www.yale.edu/statlab

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. Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab Scripts Scripts: a bunch of code grouped together; doesn’t accept argument or return output. Example open m-file editor type disp(‘Hello World’) save as test.m in c:\temp add c:\temp to path (File/Set Path) Execute by typing “test” Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab 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. For example, save this as area.m in c:temp: function ar = area(radius) ar=pi*radius^2; Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab Function functions Once you have defined a function, you can use functions that have functions as arguments – function functions! E. g. search for minima, zero values. Example: first we define the function x2-3: function x=example(input) x=input.^2-3; Now, we can easily find minima and zeros: fminbnd(@example,-2,2) fzero(@example,2) Yale University StatLab http://www.yale.edu/statlab

Yale University StatLab Learn from others There are lots of Matlab functions already out there: Google them! http://www.mathworks.com/matlabcentral/ James LeSage’s econometrics toolbox: http://www.spatial-econometrics.com/ Don’t forget to “set paths” so that Matlab can find your new .m-files. Yale University StatLab http://www.yale.edu/statlab

A Simple Linear Regression Example v = mvnrnd([0,1], sqrt([10,1;1,5]), 100); x=v(:,1) y=v(:,2) scatter( x', y' ) hold() x=[x(:,:),diag(eye(100))] a = (x'*x)^-1*x'*y plot( x, x*a(1) + a(2) ) Yale University StatLab http://www.yale.edu/statlab