Sundermeyer MAR 550 Spring 2013 1 Laboratory in Oceanography: Data and Methods MAR550, Spring 2013 Miles A. Sundermeyer Introduction to Matlab ® Programming.

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
StatLab Workshop Yale University Maximiliano Appendino, Economics October 18 th, 2013.
Introduction to Matlab
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
Introduction to Matlab By: Dr. Maher O. EL-Ghossain.
Introduction to MATLAB
Getting started with Matlab Numerical Methods Appendix B help/techdoc/learn_matlab/learn_matlab.html.
Matlab Intro. Outline Matlab introduction Matlab elements Types Variables Matrices.
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.
Matlab intro The Environment
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.
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.
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 adapted from Dr. Rolf Lakaemper.
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 January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
MATLAB Tutorials Session I Introduction to MATLAB Rajeev Madazhy Dept of Mechanical Engineering LSU.
Introduction to Matlab 1. Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators Plotting Flow Control Using of M-File Writing.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Eng Ship Structures 1 Introduction to Matlab.
INTRODUCTION TO MATLAB LAB# 01
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
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 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.
Introduction to Matlab. What is Matlab? A software environment for interactive numerical computations Examples:  Matrix computations and linear algebra.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
MATLAB Tutorial EE313 Signals and Systems Created: Thursday Jan 25, 2007 Rayyan Jaber Modified by: Jeff Andrews.
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
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
CIS 601 Fall 2003 Introduction to MATLAB Longin Jan Latecki Based on the lectures of Rolf Lakaemper and David Young.
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
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
CIS 595 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
EEE 242 Computer Tools for Electrical Engineering Lecture I Mustafa KARABULUT.
Matlab Tutorial (material available at 1 Dr. Jim Martin Associate Professor School of Computing.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
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.
ECE 1304 Introduction to Electrical and Computer Engineering
Introduction to Mat lab
Statistical Computing in MATLAB
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
Introduction to MATLAB
StatLab Matlab Workshop
Use of Mathematics using Technology (Maltlab)
StatLab Workshop: Intro to Matlab for Data Analysis and Statistical Modeling 11/29/2018.
Lecture 2 Introduction to MATLAB
Communication and Coding Theory Lab(CS491)
Matlab Basic Dr. Imtiaz Hussain
Experiment No. (1) - an introduction to MATLAB
Announcements P3 due today
Simulation And Modeling
Laboratory in Oceanography: Data and Methods
Presentation transcript:

Sundermeyer MAR 550 Spring Laboratory in Oceanography: Data and Methods MAR550, Spring 2013 Miles A. Sundermeyer Introduction to Matlab ® Programming Software

Sundermeyer MAR 550 Spring Introduction to Matlab The name MATLAB stands for matrix laboratory Fundamentally a programming language – does not need compiling, runs off interactive command line or command scripts Matlab Website: Starting and quitting Matlab … Windows: double click the Matlab icon … Linux: run “matlab” on command line Type ‘exit’ to quit (works in either)

Sundermeyer MAR 550 Spring Introduction to Matlab.m files / coding % Code for generating plot of example of system of equations - this case % stage vs. flow rating curve, with observations at different times. % % Written by Miles A. Sundermeyer, 1/27/09 % set the variables time = [ ]; stage = [ ]; flow = [ ]; % create plot plot(stage,flow,'b*') xlabel('stage(m)') ylabel('flow (m^3s)') title('Stage vs. Discharge Rating Curve') set(gca,'xlim',[ ]) set(gca,'ylim',[ ]) % fit line to data % create X matrix for linear fit X = [stage' ones(size(time'))]; B = X\discharge';% left divide y = Xb by X to solve for B=[m b]' % (need to transpose stage to column vector first) % now B contains both the slope, m, and intercept, b stagefit = [min(stage) max(stage)]; hold on plot(stagefit,[stagefit' ones(size(stagefit'))]*B,'r-');

Sundermeyer MAR 550 Spring Introduction to Matlab.m files / coding Best Programming Practices Use.m files for all your code for sake of documentation and repeatability Include header summarizing what code does, inputs and outputs, author, and date written/modified.Include header summarizing what code does, inputs and outputs, author, and date written/modified. Use vertical and horizontal whitespace generously. Indentation and spacing should reflect the block structure of the code.Use vertical and horizontal whitespace generously. Indentation and spacing should reflect the block structure of the code. Comments should describe what is happening, how it is being done, what parameters mean, which globals are used, and any restrictions or bugs. Avoid unnecessary comments.Comments should describe what is happening, how it is being done, what parameters mean, which globals are used, and any restrictions or bugs. Avoid unnecessary comments. Variable names length trades off with clarity of expression use descriptive names whenever possible for clarity, simple names for things like ‘for’ loops.use descriptive names whenever possible for clarity, simple names for things like ‘for’ loops. give units where appropriate (as comments, e.g., % (m/s)) Function names should reflect what they do and what they return

Sundermeyer MAR 550 Spring Introduction to Matlab Getting Started / Introduction / Product Overview Desktop Tools and Development Environment MATLAB desktop and Command Window editor & debugger, code analyzer, help browser, viewer for workspace & files Mathematical Function Library Computational algorithms from basic (e.g., sum, sine, cosine, complex arithmetic) to sophisticated functions (e.g., matrix inverse, matrix eigenvalues, Bessel functions, FFTs). The Language A high-level matrix/array language with control flow statements, functions, data structures, input/output, and object-oriented programming features. Graphics Extensive graphing / annotation: incl. 2-D, 3-D, image processing, animation Ability to build graphical user interfaces on MATLAB applications External Interfaces Libraries for writing C and Fortran programs to interact with MATLAB Ability to call external routines from MATLAB and vice versa

Sundermeyer MAR 550 Spring Introduction to Matlab Getting Started / Matrices and Arrays / Expressions Variables Declarations or dimension not required Variable names consist of a letter, followed by any number of letters, digits, or underscores Case sensitive e.g., > num_students = 25 creates a 1-by-1 matrix named num_students and stores the value 25 in its single element Numbers Conventional decimal notation Scientific notation uses the letter ‘e’ to specify a power-of-ten scale factor Imaginary numbers use either i or j as a suffix (read more for rules on imag numbers) e.g., e e23 1i j 3e5i

Sundermeyer MAR 550 Spring Operators (see also ‘help ’ for list – worth doing once to see) + Addition-Subtraction *Multiplication /Division \Left division (described in Linear Algebra in MATLAB documentation) ^Power ‘Complex conjugate transpose ( )Specify evaluation order Functions standard elementary math functions (abs, sqrt, exp, sin) special functions for useful constants pi i,jImaginary unit, InfInfinity (e.g., 1/0) nanNot-a-number (e.g., 0/0, inf/inf, 0*inf) Introduction to Matlab Getting Started / Matrices and Arrays / Expressions

Sundermeyer MAR 550 Spring Examples of Matlab Expressions > rho = (1+sqrt(5))/2 rho = > a = abs(3+4i) a = 5 > z = sqrt(besselk(4/3,rho-i)) z = i Introduction to Matlab Getting Started / Matrices and Arrays / Expressions

Sundermeyer MAR 550 Spring Entering Matrices > A = [ ; ; ; ] A = Sum, transpose, and diag > sum(A) ans = > A’ ans = Introduction to Matlab Getting Started / Matrices and Arrays / Matrices and Magic Squares (Note: may use space or comma for elements of same row)

Sundermeyer MAR 550 Spring > sum(A')’ ans = 34 > diag(A) ans = Introduction to Matlab Getting Started / Matrices and Arrays / Matrices and Magic Squares > sum(diag(A)) ans = 34 > sum(diag(fliplr(A))) ans = 34 Sum, transpose, and diag (cont’d)

Sundermeyer MAR 550 Spring Introduction to Matlab Getting Started / Matrices and Arrays / Matrices and Magic Squares Subscripts The element in row i and column j of A is denoted by A(i,j). For example, A(4,2) is the number in the fourth row and second column. It is also possible to refer to the elements of a matrix with a single subscript, A(k). If you try to use the value of an element outside of the matrix, it is an error: > t = A(4,5) ??? Index exceeds matrix dimensions. If you store a value in an element outside of the matrix, the size increases to accommodate the newcomer: >X = A; >X(4,5) = 17 X =

Sundermeyer MAR 550 Spring Introduction to Matlab Getting Started / Matrices and Arrays / Matrices and Magic Squares The Colon Operator One of most important Matlab operators - occurs in several different forms > 1:10 ans = To obtain non-unit spacing, specify an increment: >100:-7:50 ans = >0:pi/4:pi ans =

Sundermeyer MAR 550 Spring Introduction to Matlab Getting Started / Matrices and Arrays / Matrices and Magic Squares The Colon Operator (cont’d) Subscript expressions involving colons refer to portions of a matrix: e.g., A(1:k,j) refers to the first k elements of the j th column of A. >sum(A(1:4,4))% computes the sum of the fourth column. ans = 34 or >sum(A(:,end))% keyword ‘end’ refers to last row or column ans = 34

Sundermeyer MAR 550 Spring Introduction to Matlab Useful Tidbits … Useful Tidbits ‘who’ and ‘whos’- returns variable names, types, sizes - displays variable help - displays header of function lookfor - searches headers for keywords up, down, left, right arrows- to repeat/modify previous commands semicolon after command- suppresses output Anything +-*/ nan = nan

Sundermeyer MAR 550 Spring Introduction to Matlab Getting Started / Matrices and Arrays / Working with Matrices Functions that generate basic matrices zeros(n,m) % [n x m] matrix of zeros ones(n,m)% [n x m] matrix of ones rand(n,m)% [n x m] matrix of uniformly distributed random elements randn(n,m)% [n x m] matrix of normally distributed random elements e.g., > Z = zeros(2,4) Z = 0 0 > F = 5*ones(3,3) F = > N = fix(10*rand(1,10)) N = > R = randn(4,4) R =

Sundermeyer MAR 550 Spring Introduction to Matlab Getting Started / Matrices and Arrays / Working with Matrices Concatenation Joining smaller matrices to make bigger ones e.g., > B = [A A+32; A+48 A+16] B =

Sundermeyer MAR 550 Spring Introduction to Matlab Getting Started / Matrices and Arrays / More about Matrices and Arrays Build a table of squares and powers of 2: e.g., > n = (0:9)'; > pows = [n n.^2 2.^n] pows =

Sundermeyer MAR 550 Spring Introduction to Matlab Getting Started / Matrices and Arrays / Working with Matrices Deleting Rows and Columns Delete the second column of X, use e.g., > A (:,2) = [ ] A = Deleting a single element from a matrix A(1,2) = [ ] % results in an error Using a 1-d subscript deletes element(s) and reshapes remaining elements into a row vector e.g., > X=A X(2:2:10) = [ ] X =

Sundermeyer MAR 550 Spring Introduction to Matlab Getting Started / Matrices and Arrays / Working with Matrices Zero out a portion of B: e.g., > B(1:2,2:3) = 0 B =

Sundermeyer MAR 550 Spring Introduction to Matlab Getting Started / Matrices and Arrays / More about Matrices and Arrays Logical Subscripting Eliminate missing data e.g., > x = [ NaN ]; > x = x(isfinite(x)) x = e.g., > x = x(abs(x-mean(x)) <= 3*std(x)) x =

Sundermeyer MAR 550 Spring Introduction to Matlab Getting Started / Matrices and Arrays / More about Matrices and Arrays The ‘find’ Function e.g., find indices of the primes in A: > k = find(isprime(A))' k = > A(k) ans = Note: lhs index in an assignment statement preserves matrix structure e.g., > A(k) = NaN A = 16 NaN NaN NaN NaN 10 NaN NaN