ESS 116 Introduc)on to Data Analysis in Earth Science

Size: px
Start display at page:

Download "ESS 116 Introduc)on to Data Analysis in Earth Science"

Transcription

1 ESS 116 Introduc)on to Data Analysis in Earth Science Instructor: Mike Pritchard (include ESS116 in subject line) Image Credit: NASA Office Hours: 3317 Croul Hall, Tuesdays 1-2pm (exceppon today, 2-3pm)

2 Today s lecture 1. Lecture 2 quick review Matrices/Vectors Plot I/O 2. Lecture 3 MATLAB Programming Program vs script Algorithm example (script) User defined funcpon

3 Lecture 2 - review

4 ClarificaPon from Lab 1 C:\ vs. / for root directories and why it doesn t make sense to use them interchangeably. Make sure you submit test code that runs.

5 Vectors and Matrices Defining matrices in MATLAB: use square brackets mat2x3 = apple >> mat2x3 = [1 3 5; 2 4 6] >> mat2x3 = [1, 3, 5; 2, 4, 6] Matrix Opera/ons: use a dot in front of the operator for element-by-element (or array) operapon: >> A * B %matrix mulpplicapon >> A.* B %array mulpplicapon >> A./ B %array division >> A.^ B %array to a power 2 mat3x2 = >> mat3x2 = [1 4; 2 5; 3 6] >> mat3x2 = [1, 4; 2, 5; 3, 6]

6 Vectors and Matrices Colon operator begin:increment:end >> 0:3:60 %[0, 3, 6, 9,..,60] >> 0:60 %If not specified, the increment is assumed to be 1 Linspace funcpon: linspace(begin,end,n) >> linspace(0,60,21) %Gives 21 evenly spaced values between 0-60 >> linspace(50,-30,17) %Gives 17 evenly spaced values between 50 & -30 zeros, ones and rand >> A = zeros(5,4); %Create a 5x4 matrix of zeros, and assign to variable A >> B = ones(5,6); %Create a 5x6 matrix of zeros, and assign to variable B >> Z = rand(10,1); %Create a 10x1 matrix of random numbers between [0-1] Size : Get matrix >> size(a) >> [m, n] = size(a);

7 Matrix Elements If A is the name of a MATLAB matrix: >> A(3, 1) returns the value of the 3 rd row and 1 st column >> A(2, :) returns the 2 nd row of A >> A(3, 1) = 5; changes the 3 rd row and 1 st column of A to 5 >> A(:, 4) = 2; changes the enpre 4 th column of A to 2 Linear index: 2 (row,col) notapon: 6 4 (1, 1) (1, 2) (1, 3) (2, 1) (2, 2) (2, 3) (3, 1) (3, 2) (3, 3) (4, 1) (4, 2) (4, 3) Linear index: 6 4 (1) (5) (9) (2) (6) (10) (3) (7) (11) (4) (8) (12) >> A(7) will return the same value as A(3,2)

8 Finding Matrix Elements A = B = To find the locapons of 2 in A: >> pos = find(a==2); %linear indices >> [i j] = find(a==2); %rows and columns To find the locapons where B>5 >> pos = find(b>5) ; %linear indices >> [i j] = find(b>5); %rows and columns Combining find and replace: >> PosiPonsWhereALargerThan5 = find(a>5); >> A(PosiPonsWhereALargerThan5) = 5; Combining find and remove: >> PosiPonsWhereBLargerThan5 = find(b>5); >> B(PosiPonsWhereBLargerThan5) = [];

9 i>clicker quespon How would you replace the 5 of A by 42? A = A. A(5) = 42 B. A(3,2) = 42 C. a=find(a==5); A(a)=42; D. All of the above

10 Plot plot(x,y,'-r') Possible colors: 'b' (blue), 'g' (Green), 'k' (black), 'r' (red), Line types: -- (dashed), -. (dash dot), : (doted), - (solid) Markers: o (circle), + (plus), * (asterisk), s (square), ^ (triangle), p (star), plot(x,y,'-.b'); plot(x,y,'g*'); plot(x,y,'*:r');

11 Plot OpPons Line and marker oppons: 'LineWidth' : Changes line width (default is 1) 'MarkerFaceColor': followed by a color (e.g. r ) 'MarkerEdgeColor': followed by a color (e.g. k ) 'MarkerSize': Changes marker size (default is 1) Example: plot(x,y, r+:','markerfacecolor','g','markersize',5, LineWidth,2) axis([xmin xmax ymin ymax]) grid on/grid off Legend('legend line 1','legend line 2');

12 Plot oppons Adding labels and Ptle xlabel( x label (unit)'); ylabel( y label (unit)'); title( This is the plot title'); MulPple lines on the same plot: hold on % After the first plot Plot on mulpple figures call figure(1), figure(2), before plot command MulPple plots on the same figure use subplot(nrows,ncols,number) before plot command example: subplot(2,2,3) is the lower lew subplot of 2x2

13 File I/O Hard Disk Memory (RAM) File output: save command saves a matrix as an ASCII (text) or.mat (binary) file File input: load command loads in an ASCII file or.mat file All data must be numeric, i.e., no text allowed

14 Rocks.dat load cannot open the following file: granite a rhyolite b diorite c andesite d basalt e gabbro f

15 File Input: textscan fopen: opens a file and returns a File IdenPfier used to idenpfy the file later on in your code (-1 signifies an error in opening the file) file = 'rocks.dat'; fid = fopen(file); if fid==-1, error(['error opening ' file]); end cellmat = textscan(fid,'%d %f %s %c'); fclose(fid); textscan: read all the lines of the file and populate variable cellmat (one element per column) %d: integer (e.g., 235) %f: floapng-point number (e.g., ) %s: string (e.g., word) %c: character (e.g., a) fclose: Awer you are finished with a file, you should close it (prevents errors and unwanted behavior).

16 Lecture 3 MATLAB programming

17 Program vs script

18 MATLAB Programming InteracPve MATLAB use only good if problem is simple Owen, many steps are needed We also want to be able to automate repeated tasks Automated data processing is common in Earth science! Automated Earthquake DetecPon and NoPficaPon (USGS) Amazon.com s Automated Kiva Robots Automated Stream Discharge Monitoring (USGS)

19 Program vs. Script Computers only understand low-level language machine code htp://en.wikipedia.org/wiki/machine_code Low-level languages are much faster, but very difficult for humans to write efficiently MATLAB is a high-level language Uses code that is human readable Much easier to write E.g. disp, fprin}, plot, etc Compiler: Translates a high level language into an executable object code program (*.exe in MS Windows) Creates an executable file (binary) from source code (ascii) E.g. Mozilla Firefox and Microsow Office (source code: C++) firefox.exe, winword.exe (machine language executable files) MATLAB does something a litle different

20 Program vs. Script MATLAB is an interpreted language Code is read line by line by an interpreter (not a compiler) Each line of code is translated into machine language and executed on the fly No.exe file is generated (can force MATLAB to make.exe files) Because MATLAB code is not compiled source code is referred to as a script Also called M-files (end in.m) Advantages Don t need to spend Pme compiling the code to use it Don t need to recompile awer you make changes Same script will work on any operapng system with MATLAB Disadvantages Because code is compiled on the fly, some tasks can be slow* Others can change your code*

21 WriPng a script

22 Algorithm Example Before star/ng to write any code, you should break the problem down into a simple algorithm Algorithm: A sequence of steps to solve a problem Example Algorithm: Calculate Volume of a Sphere Get the input: radius of sphere Calculate the result: volume of sphere Display the result Input typically comes from: The user typing in a value when prompted A file on your hard disk Output typically goes to: The screen (i.e. the MATLAB command window) A file on your hard disk

23 Algorithm Example Example Algorithm: Calculate Volume of a Sphere 1. Get the input: radius of sphere Set the radius Store the radius in a variable 2. Calculate the result: volume of sphere Plug radius into volume equapon Store result in variable 3. Display the result We ll do this later

24 To execute a script, enter its name in the command window (don t include the.m ) Simple Script Example The top of all scripts should contain commented documentapon: H1 line: a short comment on what the script does lookfor will read this Subsequent lines Script name Author info Date Details of what the code does Usage (if a funcpon) Leave one blank line before starpng code Read by help

25 DocumenPng Your Code Any line of MATLAB code that begins with % is ignored by the interpreter Referred to as: comments Do not slow down execupon of your code MATLAB doesn t even read comments, but people do Comments allow you to tell yourself and others what you did Typically you can fit most comments into a single line %set the radius value rad = 23; %compute the area Area = pi * (rad ^ 2); %MATLAB ignores commented lines. Use them!!! In this class: uncommented code gets a zero Every line of code must have a brief comment In all scripts, separate into secpons 1) Header/DocumentaPon 2) CalculaPons 3) Plong 4) Output

26 Output statement

27 Output Statements: disp To be a useful, a script must be able to output a result Simplest output: Print to the command window Use either disp or fprin} disp can print strings or numbers disp can only print one thing at a Pme. disp can print variables For this reason, MATLAB also provides fprintf

28 Output Statements: fprin} fprin} has a somewhat confusing syntax Most programming languages have fprin} (or prin}) Syntax was inherited from C / C++ fprin} does not include a new line ( \n ) awer a string, unless you tell it to do so. Use the new line special character, \n You do not need a space before or awer a special character, but adding a space makes code easier to read. fprin} also recognizes these special characters For more info, see doc fprin} and click on the formang strings link near the botom of the page

29 Output Statements: fprin} fprin} can also be used to print variables Can apply special formang to numeric variable (Very Useful!!) When you use fprin} to print a variable, a variable is indicated by a place holder, in this case %d The variable name must be given awer the string. %f indicates that a floapng point number is to be printed. Can print mulpple variables in one line! ( disp can t do this) fprin} recognizes these conversion characters For more info, see doc fprin} and click on the formang strings link near the botom of the page

30 Output Statements: fprin} fprin} and %f can be used to control just about every aspect of the formang of a floapng point number By default, 7 digits are shown, even though MATLAB variables are stored with 17 digits of precision (if needed) MATLAB double variables can hold only 17 digits. Anything beyond the 17 th place gets displayed as a zero and is not stored. Want to print π rounded to two decimal places? Want to print a variable in scienpfic notapon with 5 decimal places? MATLAB uses a compact format by default, so numbers get displayed in scienpfic notapon. fprin} can override this!

31 Output Statements: fprin} fprin} is also great for prinpng out numbers so they are in neat columns. Note the different results. How does this work? %6.2f Leave at least 6 total spaces for each number includes decimals, exponents, and negapve signs Round to 2 decimal places %06.2f Same as above, but show leading zeros Note that fprintf treats matrices/vectors in strange ways!

32 i>clicker quespon I define the following variable in MATLAB: EarthRadius = 6371; I would like MATLAB to print: The radius of the earth is What is the correct command? A. fprintf('the radius of the earth is %d\n',earthradius); B. fprintf('the radius of the earth is %.1f\n',EarthRadius); C. fprintf('the radius of the earth is %7.2f\n',EarthRadius); D. All of the above

33 WriPng a funcpon

34 User-Defined FuncPons You have already used many built in funcpons in MATLAB plot, sin, cos, int8, double, fprin}, linspace, etc For example linspace The Call, or calling the function Arguments Where the returned value is stored

35 User-Defined FuncPons Lets look at the general funcpon setup: Example: ConverPng mph to m/s The func/on header (required for all func/ons) The reserved word, func/on (1 st line must start with this) Name of func/on (iden/cal to name of m-file without.m) Input arguments (must be provided by the user) Value that is returned (not all func/ons need to return something)

36 User-Defined FuncPons FuncPon must be in your pwd or MATLAB path Call by funcpon name no.m at end Same as scripts! Why ans = ? All variables inside a funcpon are local variables Only exist inside the funcpon!! May be confusing at first, but keeps things Pdy User doesn t care about all of the intermediate variables Only wants the returned value Why are v and mph not defined in the command window?

37 User-Defined FuncPons FuncPons can use any number of variables All of these variables are local! The user won t be aware of them unless he/she opens the m-file

38 A Poorly Writen FuncPon Why is this a bad idea? This is NOT how you return a result The result, v, is not accessible to the user

39 Another Poorly Writen FuncPon Why is this even worse?

40 Another Poorly Writen FuncPon Returned value was stored in ans Not illegal, but bad programming Local variables should not be printed to the screen Confusing!! Not accessible to the user PrinPng variables is slow

41 FuncPons That Make Plots FuncPons can Accept more than one argument (separated by commas) Make plots

42 FuncPons That Make Plots FuncPons can Accept more than one argument (separated by commas) Make plots

43 FuncPons That Return MulPple Values FuncPons can return mulpple values Or even matrices!

44 FuncPons That Return MulPple Values FuncPons can return mulpple values Or even matrices! If you only specify one variable, only the first returned value is stored How could we return both x and y as one matrix xy?

45 How could we return both x and y as one matrix xy? Change the the first line from: [x,y]=plotsinwave2(amp,lam,xmin,xmax,numpts) To xy = plotsinwave2(amp,lam,xmin,xmax,numpts) And add one line before end: i>clicker quespon A. xy = x,y; B. xy = [x;y]; C. xy = [x,y]; D. xy = {x,y};

46 A Script That Calls A FuncPon

47 Thoughts on FuncPons Why Make Func/ons? When a task is owen repeated, funcpons save Pme Faster than using prompt When Wri/ng a Func/on Start by sketching out the basic algorithm (pencil & paper) Write algorithm first as a script Difficult to troubleshoot funcpons because variables are local Once the script works, generalize it into a funcpon FuncPons do not need to return a value Some funcpons make plots, print informapon, or write files If a funcpon does not require an input argument It should probably be lew as a script Remember that scripts can call funcpons mulpple Pmes

48 Next Week Lab 3: MATLAB programming DUE: one week awer the lab starts (EEE) Bring a USB drive Lecture 4: SelecPon statements and loops

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple

Interactive MATLAB use. Often, many steps are needed. Automated data processing is common in Earth science! only good if problem is simple Chapter 2 Interactive MATLAB use only good if problem is simple Often, many steps are needed We also want to be able to automate repeated tasks Automated data processing is common in Earth science! Automated

More information

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed.

12 whereas if I terminate the expression with a semicolon, the printed output is suppressed. Example 4 Printing and Plotting Matlab provides numerous print and plot options. This example illustrates the basics and provides enough detail that you can use it for typical classroom work and assignments.

More information

Introduction to MATLAB Programming. Chapter 3. Linguaggio Programmazione Matlab-Simulink (2017/2018)

Introduction to MATLAB Programming. Chapter 3. Linguaggio Programmazione Matlab-Simulink (2017/2018) Introduction to MATLAB Programming Chapter 3 Linguaggio Programmazione Matlab-Simulink (2017/2018) Algorithms An algorithm is the sequence of steps needed to solve a problem Top-down design approach to

More information

Chapter 3: Introduction to MATLAB Programming (4 th ed.)

Chapter 3: Introduction to MATLAB Programming (4 th ed.) Chapter 3: Introduction to MATLAB Programming (4 th ed.) Algorithms MATLAB scripts Input / Output o disp versus fprintf Graphs Read and write variables (.mat files) User-defined Functions o Definition

More information

STAT 391 Handout 1 Making Plots with Matlab Mar 26, 2006

STAT 391 Handout 1 Making Plots with Matlab Mar 26, 2006 STAT 39 Handout Making Plots with Matlab Mar 26, 26 c Marina Meilă & Lei Xu mmp@cs.washington.edu This is intended to help you mainly with the graphics in the homework. Matlab is a matrix oriented mathematics

More information

Programming in Mathematics. Mili I. Shah

Programming in Mathematics. Mili I. Shah Programming in Mathematics Mili I. Shah Starting Matlab Go to http://www.loyola.edu/moresoftware/ and login with your Loyola name and password... Matlab has eight main windows: Command Window Figure Window

More information

MATLAB INTRODUCTION. Matlab can be used interactively as a super hand calculator, or, more powerfully, run using scripts (i.e., programs).

MATLAB INTRODUCTION. Matlab can be used interactively as a super hand calculator, or, more powerfully, run using scripts (i.e., programs). L A B 6 M A T L A B MATLAB INTRODUCTION Matlab is a commercial product that is used widely by students and faculty and researchers at UTEP. It provides a "high-level" programming environment for computing

More information

Matlab Tutorial 1: Working with variables, arrays, and plotting

Matlab Tutorial 1: Working with variables, arrays, and plotting Matlab Tutorial 1: Working with variables, arrays, and plotting Setting up Matlab First of all, let's make sure we all have the same layout of the different windows in Matlab. Go to Home Layout Default.

More information

AMS 27L LAB #2 Winter 2009

AMS 27L LAB #2 Winter 2009 AMS 27L LAB #2 Winter 2009 Plots and Matrix Algebra in MATLAB Objectives: 1. To practice basic display methods 2. To learn how to program loops 3. To learn how to write m-files 1 Vectors Matlab handles

More information

Prof. Manoochehr Shirzaei. RaTlab.asu.edu

Prof. Manoochehr Shirzaei. RaTlab.asu.edu RaTlab.asu.edu Introduction To MATLAB Introduction To MATLAB This lecture is an introduction of the basic MATLAB commands. We learn; Functions Procedures for naming and saving the user generated files

More information

Department of Chemical Engineering ChE-101: Approaches to Chemical Engineering Problem Solving MATLAB Tutorial Vb

Department of Chemical Engineering ChE-101: Approaches to Chemical Engineering Problem Solving MATLAB Tutorial Vb Department of Chemical Engineering ChE-101: Approaches to Chemical Engineering Problem Solving MATLAB Tutorial Vb Making Plots with Matlab (last updated 5/29/05 by GGB) Objectives: These tutorials are

More information

Introduction to MATLAB

Introduction to MATLAB 58:110 Computer-Aided Engineering Spring 2005 Introduction to MATLAB Department of Mechanical and industrial engineering January 2005 Topics Introduction Running MATLAB and MATLAB Environment Getting help

More information

Mechanical Engineering Department Second Year (2015)

Mechanical Engineering Department Second Year (2015) Lecture 7: Graphs Basic Plotting MATLAB has extensive facilities for displaying vectors and matrices as graphs, as well as annotating and printing these graphs. This section describes a few of the most

More information

INTRODUCTION TO MATLAB PLOTTING WITH MATLAB

INTRODUCTION TO MATLAB PLOTTING WITH MATLAB 1 INTRODUCTION TO MATLAB PLOTTING WITH MATLAB Plotting with MATLAB x-y plot Plotting with MATLAB MATLAB contains many powerful functions for easily creating plots of several different types. Command plot(x,y)

More information

Getting Started with MATLAB

Getting Started with MATLAB Getting Started with MATLAB Math 315, Fall 2003 Matlab is an interactive system for numerical computations. It is widely used in universities and industry, and has many advantages over languages such as

More information

MATLAB Tutorial III Variables, Files, Advanced Plotting

MATLAB Tutorial III Variables, Files, Advanced Plotting MATLAB Tutorial III Variables, Files, Advanced Plotting A. Dealing with Variables (Arrays and Matrices) Here's a short tutorial on working with variables, taken from the book, Getting Started in Matlab.

More information

1 Introduction to Matlab

1 Introduction to Matlab 1 Introduction to Matlab 1. What is Matlab? Matlab is a computer program designed to do mathematics. You might think of it as a super-calculator. That is, once Matlab has been started, you can enter computations,

More information

Mathworks (company that releases Matlab ) documentation website is:

Mathworks (company that releases Matlab ) documentation website is: 1 Getting Started The Mathematics Behind Biological Invasions Introduction to Matlab in UNIX Christina Cobbold and Tomas de Camino Beck as modified for UNIX by Fred Adler Logging in: This is what you do

More information

Introduction to MATLAB LAB 1

Introduction to MATLAB LAB 1 Introduction to MATLAB LAB 1 1 Basics of MATLAB MATrix LABoratory A super-powerful graphing calculator Matrix based numeric computation Embedded Functions Also a programming language User defined functions

More information

PART 1 PROGRAMMING WITH MATHLAB

PART 1 PROGRAMMING WITH MATHLAB PART 1 PROGRAMMING WITH MATHLAB Presenter: Dr. Zalilah Sharer 2018 School of Chemical and Energy Engineering Universiti Teknologi Malaysia 23 September 2018 Programming with MATHLAB MATLAB Environment

More information

Getting Started. Chapter 1. How to Get Matlab. 1.1 Before We Begin Matlab to Accompany Lay s Linear Algebra Text

Getting Started. Chapter 1. How to Get Matlab. 1.1 Before We Begin Matlab to Accompany Lay s Linear Algebra Text Chapter 1 Getting Started How to Get Matlab Matlab physically resides on each of the computers in the Olin Hall labs. See your instructor if you need an account on these machines. If you are going to go

More information

Programming 1. Script files. help cd Example:

Programming 1. Script files. help cd Example: Programming Until now we worked with Matlab interactively, executing simple statements line by line, often reentering the same sequences of commands. Alternatively, we can store the Matlab input commands

More information

A Brief Introduction to MATLAB

A Brief Introduction to MATLAB A Brief Introduction to MATLAB MATLAB (Matrix Laboratory) is an interactive software system for numerical computations and graphics. As the name suggests, MATLAB was first designed for matrix computations:

More information

MATLAB Introduction to MATLAB Programming

MATLAB Introduction to MATLAB Programming MATLAB Introduction to MATLAB Programming MATLAB Scripts So far we have typed all the commands in the Command Window which were executed when we hit Enter. Although every MATLAB command can be executed

More information

Matlab Tutorial and Exercises for COMP61021

Matlab Tutorial and Exercises for COMP61021 Matlab Tutorial and Exercises for COMP61021 1 Introduction This is a brief Matlab tutorial for students who have not used Matlab in their programming. Matlab programming is essential in COMP61021 as a

More information

Introduction to Matlab to Accompany Linear Algebra. Douglas Hundley Department of Mathematics and Statistics Whitman College

Introduction to Matlab to Accompany Linear Algebra. Douglas Hundley Department of Mathematics and Statistics Whitman College Introduction to Matlab to Accompany Linear Algebra Douglas Hundley Department of Mathematics and Statistics Whitman College August 27, 2018 2 Contents 1 Getting Started 5 1.1 Before We Begin........................................

More information

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab

MATH (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab MATH 495.3 (CRN 13695) Lab 1: Basics for Linear Algebra and Matlab Below is a screen similar to what you should see when you open Matlab. The command window is the large box to the right containing the

More information

Introduction to Matlab

Introduction to Matlab What is Matlab? Introduction to Matlab Matlab is software written by a company called The Mathworks (mathworks.com), and was first created in 1984 to be a nice front end to the numerical routines created

More information

How to learn MATLAB? Some predefined variables

How to learn MATLAB? Some predefined variables ECE-S352 Lab 1 MATLAB Tutorial How to learn MATLAB? 1. MATLAB comes with good tutorial and detailed documents. a) Select MATLAB help from the MATLAB Help menu to open the help window. Follow MATLAB s Getting

More information

Interactive Computing with Matlab. Gerald W. Recktenwald Department of Mechanical Engineering Portland State University

Interactive Computing with Matlab. Gerald W. Recktenwald Department of Mechanical Engineering Portland State University Interactive Computing with Matlab Gerald W. Recktenwald Department of Mechanical Engineering Portland State University gerry@me.pdx.edu Starting Matlab Double click on the Matlab icon, or on unix systems

More information

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP)

MATLAB Tutorial. Digital Signal Processing. Course Details. Topics. MATLAB Environment. Introduction. Digital Signal Processing (DSP) Digital Signal Processing Prof. Nizamettin AYDIN naydin@yildiz.edu.tr naydin@ieee.org http://www.yildiz.edu.tr/~naydin Course Details Course Code : 0113620 Course Name: Digital Signal Processing (Sayısal

More information

QUICK INTRODUCTION TO MATLAB PART I

QUICK INTRODUCTION TO MATLAB PART I QUICK INTRODUCTION TO MATLAB PART I Department of Mathematics University of Colorado at Colorado Springs General Remarks This worksheet is designed for use with MATLAB version 6.5 or later. Once you have

More information

MATLAB TUTORIAL WORKSHEET

MATLAB TUTORIAL WORKSHEET MATLAB TUTORIAL WORKSHEET What is MATLAB? Software package used for computation High-level programming language with easy to use interactive environment Access MATLAB at Tufts here: https://it.tufts.edu/sw-matlabstudent

More information

Introduction to Matlab

Introduction to Matlab Introduction to Matlab 1 Outline: What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display Facilities Flow Control Using of M-File Writing User

More information

EOSC 352 MATLAB Review

EOSC 352 MATLAB Review EOSC 352 MATLAB Review To use MATLAB, you can either (1) type commands in the window (i.e., at the command line ) or (2) type in the name of a file you have made, whose name ends in.m and which contains

More information

EGR 111 Plotting Data

EGR 111 Plotting Data EGR 111 Plotting Data This lab shows how to import data, plot data, and write script files. This lab also describes the Current Folder, the comment symbol ( % ), and MATLAB file names. New MATLAB Commands:

More information

MAT 275 Laboratory 1 Introduction to MATLAB

MAT 275 Laboratory 1 Introduction to MATLAB MATLAB sessions: Laboratory 1 1 MAT 275 Laboratory 1 Introduction to MATLAB MATLAB is a computer software commonly used in both education and industry to solve a wide range of problems. This Laboratory

More information

EE 301 Signals & Systems I MATLAB Tutorial with Questions

EE 301 Signals & Systems I MATLAB Tutorial with Questions EE 301 Signals & Systems I MATLAB Tutorial with Questions Under the content of the course EE-301, this semester, some MATLAB questions will be assigned in addition to the usual theoretical questions. This

More information

ENGR Fall Exam 1

ENGR Fall Exam 1 ENGR 1300 Fall 01 Exam 1 INSTRUCTIONS: Duration: 60 minutes Keep your eyes on your own work! Keep your work covered at all times! 1. Each student is responsible for following directions. Read carefully..

More information

MATLAB Project: Getting Started with MATLAB

MATLAB Project: Getting Started with MATLAB Name Purpose: To learn to create matrices and use various MATLAB commands for reference later MATLAB functions used: [ ] : ; + - * ^, size, help, format, eye, zeros, ones, diag, rand, round, cos, sin,

More information

Quick MATLAB Syntax Guide

Quick MATLAB Syntax Guide Quick MATLAB Syntax Guide Some useful things, not everything if-statement Structure: if (a = = = ~=

More information

APPM 2460 PLOTTING IN MATLAB

APPM 2460 PLOTTING IN MATLAB APPM 2460 PLOTTING IN MATLAB. Introduction Matlab is great at crunching numbers, and one of the fundamental ways that we understand the output of this number-crunching is through visualization, or plots.

More information

Lecturer: Keyvan Dehmamy

Lecturer: Keyvan Dehmamy MATLAB Tutorial Lecturer: Keyvan Dehmamy 1 Topics Introduction Running MATLAB and MATLAB Environment Getting help Variables Vectors, Matrices, and linear Algebra Mathematical Functions and Applications

More information

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix.

MATLAB Tutorial. Mohammad Motamed 1. August 28, generates a 3 3 matrix. MATLAB Tutorial 1 1 Department of Mathematics and Statistics, The University of New Mexico, Albuquerque, NM 87131 August 28, 2016 Contents: 1. Scalars, Vectors, Matrices... 1 2. Built-in variables, functions,

More information

Introduction To MATLAB Introduction to Programming GENG 200

Introduction To MATLAB Introduction to Programming GENG 200 Introduction To MATLAB Introduction to Programming GENG 200, Prepared by Ali Abu Odeh 1 Table of Contents M Files 2 4 2 Execution Control 3 Vectors User Defined Functions Expected Chapter Duration: 6 classes.

More information

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis

Introduction to Matlab. Summer School CEA-EDF-INRIA 2011 of Numerical Analysis Introduction to Matlab 1 Outline What is Matlab? Matlab desktop & interface Scalar variables Vectors and matrices Exercise 1 Booleans Control structures File organization User defined functions Exercise

More information

Matlab Tutorial for COMP24111 (includes exercise 1)

Matlab Tutorial for COMP24111 (includes exercise 1) Matlab Tutorial for COMP24111 (includes exercise 1) 1 Exercises to be completed by end of lab There are a total of 11 exercises through this tutorial. By the end of the lab, you should have completed the

More information

CITS2401 Computer Analysis & Visualisation

CITS2401 Computer Analysis & Visualisation FACULTY OF ENGINEERING, COMPUTING AND MATHEMATICS CITS2401 Computer Analysis & Visualisation SCHOOL OF COMPUTER SCIENCE AND SOFTWARE ENGINEERING Topic 3 Introduction to Matlab Material from MATLAB for

More information

Basic Graphs. Dmitry Adamskiy 16 November 2011

Basic Graphs. Dmitry Adamskiy 16 November 2011 Basic Graphs Dmitry Adamskiy adamskiy@cs.rhul.ac.uk 16 November 211 1 Plot Function plot(x,y): plots vector Y versus vector X X and Y must have the same size: X = [x1, x2 xn] and Y = [y1, y2,, yn] Broken

More information

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations

MATLAB Tutorial. 1. The MATLAB Windows. 2. The Command Windows. 3. Simple scalar or number operations MATLAB Tutorial The following tutorial has been compiled from several resources including the online Help menu of MATLAB. It contains a list of commands that will be directly helpful for understanding

More information

Lab 1 Intro to MATLAB and FreeMat

Lab 1 Intro to MATLAB and FreeMat Lab 1 Intro to MATLAB and FreeMat Objectives concepts 1. Variables, vectors, and arrays 2. Plotting data 3. Script files skills 1. Use MATLAB to solve homework problems 2. Plot lab data and mathematical

More information

MATLAB Project: Getting Started with MATLAB

MATLAB Project: Getting Started with MATLAB Name Purpose: To learn to create matrices and use various MATLAB commands for reference later MATLAB built-in functions used: [ ] : ; + - * ^, size, help, format, eye, zeros, ones, diag, rand, round, cos,

More information

This module aims to introduce Precalculus high school students to the basic capabilities of Matlab by using functions. Matlab will be used in

This module aims to introduce Precalculus high school students to the basic capabilities of Matlab by using functions. Matlab will be used in This module aims to introduce Precalculus high school students to the basic capabilities of Matlab by using functions. Matlab will be used in subsequent modules to help to teach research related concepts

More information

Introduction to Matlab

Introduction to Matlab Introduction to Matlab By:Mohammad Sadeghi *Dr. Sajid Gul Khawaja Slides has been used partially to prepare this presentation Outline: What is Matlab? Matlab Screen Basic functions Variables, matrix, indexing

More information

Classes 7-8 (4 hours). Graphics in Matlab.

Classes 7-8 (4 hours). Graphics in Matlab. Classes 7-8 (4 hours). Graphics in Matlab. Graphics objects are displayed in a special window that opens with the command figure. At the same time, multiple windows can be opened, each one assigned a number.

More information

Computer Project: Getting Started with MATLAB

Computer Project: Getting Started with MATLAB Computer Project: Getting Started with MATLAB Name Purpose: To learn to create matrices and use various MATLAB commands. Examples here can be useful for reference later. MATLAB functions: [ ] : ; + - *

More information

PROGRAMMING WITH MATLAB WEEK 6

PROGRAMMING WITH MATLAB WEEK 6 PROGRAMMING WITH MATLAB WEEK 6 Plot: Syntax: plot(x, y, r.- ) Color Marker Linestyle The line color, marker style and line style can be changed by adding a string argument. to select and delete lines

More information

Chapter 9. Above: An early computer input/output device on the IBM 7030 (STRETCH)

Chapter 9. Above: An early computer input/output device on the IBM 7030 (STRETCH) Chapter 9 Above: An early computer input/output device on the IBM 7030 (STRETCH) http://computer-history.info/page4.dir/pages/ibm.7030.stretch.dir/ Io One of the moon s of Jupiter (A Galilean satellite)

More information

What is MATLAB? It is a high-level programming language. for numerical computations for symbolic computations for scientific visualizations

What is MATLAB? It is a high-level programming language. for numerical computations for symbolic computations for scientific visualizations What is MATLAB? It stands for MATrix LABoratory It is developed by The Mathworks, Inc (http://www.mathworks.com) It is an interactive, integrated, environment for numerical computations for symbolic computations

More information

Graphics and plotting techniques

Graphics and plotting techniques Davies: Computer Vision, 5 th edition, online materials Matlab Tutorial 5 1 Graphics and plotting techniques 1. Introduction The purpose of this tutorial is to outline the basics of graphics and plotting

More information

Laboratory 1 Octave Tutorial

Laboratory 1 Octave Tutorial Signals, Spectra and Signal Processing Laboratory 1 Octave Tutorial 1.1 Introduction The purpose of this lab 1 is to become familiar with the GNU Octave 2 software environment. 1.2 Octave Review All laboratory

More information

EEE161 Applied Electromagnetics Laboratory 1

EEE161 Applied Electromagnetics Laboratory 1 EEE161 Applied Electromagnetics Laboratory 1 Instructor: Dr. Milica Marković Office: Riverside Hall 3028 Email: milica@csus.edu Web:http://gaia.ecs.csus.edu/ milica This laboratory exercise will introduce

More information

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain

Introduction to Matlab. By: Dr. Maher O. EL-Ghossain Introduction to Matlab By: Dr. Maher O. EL-Ghossain Outline: q What is Matlab? Matlab Screen Variables, array, matrix, indexing Operators (Arithmetic, relational, logical ) Display Facilities Flow Control

More information

Writing MATLAB Programs

Writing MATLAB Programs Outlines September 14, 2004 Outlines Part I: Review of Previous Lecture Part II: Review of Previous Lecture Outlines Part I: Review of Previous Lecture Part II: Control Structures If/Then/Else For Loops

More information

Overview. Lecture 13: Graphics and Visualisation. Graphics & Visualisation 2D plotting. Graphics and visualisation of data in Matlab

Overview. Lecture 13: Graphics and Visualisation. Graphics & Visualisation 2D plotting. Graphics and visualisation of data in Matlab Overview Lecture 13: Graphics and Visualisation Graphics & Visualisation 2D plotting 1. Plots for one or multiple sets of data, logarithmic scale plots 2. Axis control & Annotation 3. Other forms of 2D

More information

W1005 Intro to CS and Programming in MATLAB. Plo9ng & Visualiza?on. Fall 2014 Instructor: Ilia Vovsha. hgp://www.cs.columbia.

W1005 Intro to CS and Programming in MATLAB. Plo9ng & Visualiza?on. Fall 2014 Instructor: Ilia Vovsha. hgp://www.cs.columbia. W1005 Intro to CS and Programming in MATLAB Plo9ng & Visualiza?on Fall 2014 Instructor: Ilia Vovsha hgp://www.cs.columbia.edu/~vovsha/w1005 Outline Plots (2D) Plot proper?es Figures Plots (3D) 2 2D Plots

More information

Introduction to MATLAB

Introduction to MATLAB ELG 3125 - Lab 1 Introduction to MATLAB TA: Chao Wang (cwang103@site.uottawa.ca) 2008 Fall ELG 3125 Signal and System Analysis P. 1 Do You Speak MATLAB? MATLAB - The Language of Technical Computing ELG

More information

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA

Starting with a great calculator... Variables. Comments. Topic 5: Introduction to Programming in Matlab CSSE, UWA Starting with a great calculator... Topic 5: Introduction to Programming in Matlab CSSE, UWA! MATLAB is a high level language that allows you to perform calculations on numbers, or arrays of numbers, in

More information

EGR 111 Introduction to MATLAB

EGR 111 Introduction to MATLAB EGR 111 Introduction to MATLAB This lab introduces the MATLAB help facility, shows how MATLAB TM, which stands for MATrix LABoratory, can be used as an advanced calculator. This lab also introduces assignment

More information

Starting a New Matlab m.file. Matlab m.files. Matlab m.file Editor/Debugger. The clear all Command. Our Program So Far

Starting a New Matlab m.file. Matlab m.files. Matlab m.file Editor/Debugger. The clear all Command. Our Program So Far Matlab m.files It is not very convenient to do work directly in the Matlab Command Line Window since: It is hard to remember what we typed. It is hard to edit. It all disappears when we close Matlab. Solution:

More information

Schedule. Matlab Exam. Documentation. Program Building Blocks. Dynamics Makeup Exam. Matlab Exam. Semester Summary and Wrap up.

Schedule. Matlab Exam. Documentation. Program Building Blocks. Dynamics Makeup Exam. Matlab Exam. Semester Summary and Wrap up. Schedule Dynamics Makeup Exam Thursday, May 1, 2:00 p.m., Estabrook 111 3 problems, covers modules 3 & 4 You may bring 1 sheet of notes Matlab Exam See next slide Semester Summary and Wrap up Wednesday,

More information

QUIZ: What is the output of this MATLAB code? >> A = [2,4,10,13;16,3,7,18; 8,4,9,25;3,12,15,17]; >> length(a) >> size(a) >> B = A(2:3, 1:3) >> B(5)

QUIZ: What is the output of this MATLAB code? >> A = [2,4,10,13;16,3,7,18; 8,4,9,25;3,12,15,17]; >> length(a) >> size(a) >> B = A(2:3, 1:3) >> B(5) QUIZ: What is the output of this MATLAB code? >> A = [2,4,10,13;16,3,7,18; 8,4,9,25;3,12,15,17]; >> length(a) >> size(a) >> B = A(2:3, 1:3) >> B(5) QUIZ Ch.3 Introduction to MATLAB programming 3.1 Algorithms

More information

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu

ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu ECON 502 INTRODUCTION TO MATLAB Nov 9, 2007 TA: Murat Koyuncu 0. What is MATLAB? 1 MATLAB stands for matrix laboratory and is one of the most popular software for numerical computation. MATLAB s basic

More information

CSE/NEUBEH 528 Homework 0: Introduction to Matlab

CSE/NEUBEH 528 Homework 0: Introduction to Matlab CSE/NEUBEH 528 Homework 0: Introduction to Matlab (Practice only: Do not turn in) Okay, let s begin! Open Matlab by double-clicking the Matlab icon (on MS Windows systems) or typing matlab at the prompt

More information

Objectives. 1 Basic Calculations. 2 Matrix Algebra. Physical Sciences 12a Lab 0 Spring 2016

Objectives. 1 Basic Calculations. 2 Matrix Algebra. Physical Sciences 12a Lab 0 Spring 2016 Physical Sciences 12a Lab 0 Spring 2016 Objectives This lab is a tutorial designed to a very quick overview of some of the numerical skills that you ll need to get started in this class. It is meant to

More information

1. Register an account on: using your Oxford address

1. Register an account on:   using your Oxford  address 1P10a MATLAB 1.1 Introduction MATLAB stands for Matrix Laboratories. It is a tool that provides a graphical interface for numerical and symbolic computation along with a number of data analysis, simulation

More information

Lecture 2 Introduction to MATLAB. Dr.Tony Cahill

Lecture 2 Introduction to MATLAB. Dr.Tony Cahill Lecture 2 Introduction to MATLAB Dr.Tony Cahill The MATLAB Environment The Desktop Environment Command Window (Interactive commands) Command History Window Edit/Debug Window Workspace Browser Figure Windows

More information

CME 192: Introduction to Matlab

CME 192: Introduction to Matlab CME 192: Introduction to Matlab Matlab Basics Brett Naul January 15, 2015 Recap Using the command window interactively Variables: Assignment, Identifier rules, Workspace, command who and whos Setting the

More information

EOSC 473/573 Matlab Tutorial R. Pawlowicz with changes by M. Halverson

EOSC 473/573 Matlab Tutorial R. Pawlowicz with changes by M. Halverson EOSC 473/573 Matlab Tutorial R. Pawlowicz with changes by M. Halverson February 12, 2008 Getting help 1. Local On-line help (a) text-based help: >> help (b) GUI-help >> helpwin (c) Browser-based

More information

Introduc)on to Matlab

Introduc)on to Matlab Introduc)on to Matlab Marcus Kaiser (based on lecture notes form Vince Adams and Syed Bilal Ul Haq ) MATLAB MATrix LABoratory (started as interac)ve interface to Fortran rou)nes) Powerful, extensible,

More information

Objectives. 1 Running, and Interface Layout. 2 Toolboxes, Documentation and Tutorials. 3 Basic Calculations. PS 12a Laboratory 1 Spring 2014

Objectives. 1 Running, and Interface Layout. 2 Toolboxes, Documentation and Tutorials. 3 Basic Calculations. PS 12a Laboratory 1 Spring 2014 PS 12a Laboratory 1 Spring 2014 Objectives This session is a tutorial designed to a very quick overview of some of the numerical skills that you ll need to get started. Throughout the tutorial, the instructors

More information

To start using Matlab, you only need be concerned with the command window for now.

To start using Matlab, you only need be concerned with the command window for now. Getting Started Current folder window Atop the current folder window, you can see the address field which tells you where you are currently located. In programming, think of it as your current directory,

More information

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia

Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia Matlab for FMRI Module 1: the basics Instructor: Luis Hernandez-Garcia The goal for this tutorial is to make sure that you understand a few key concepts related to programming, and that you know the basics

More information

Chapter 2. MATLAB Fundamentals

Chapter 2. MATLAB Fundamentals Chapter 2. MATLAB Fundamentals Choi Hae Jin Chapter Objectives q Learning how real and complex numbers are assigned to variables. q Learning how vectors and matrices are assigned values using simple assignment,

More information

Lab 1 Introduction to MATLAB and Scripts

Lab 1 Introduction to MATLAB and Scripts Lab 1 Introduction to MATLAB and Scripts EE 235: Continuous-Time Linear Systems Department of Electrical Engineering University of Washington The development of these labs was originally supported by the

More information

2D LINE PLOTS... 1 The plot() Command... 1 Labeling and Annotating Figures... 5 The subplot() Command... 7 The polarplot() Command...

2D LINE PLOTS... 1 The plot() Command... 1 Labeling and Annotating Figures... 5 The subplot() Command... 7 The polarplot() Command... Contents 2D LINE PLOTS... 1 The plot() Command... 1 Labeling and Annotating Figures... 5 The subplot() Command... 7 The polarplot() Command... 9 2D LINE PLOTS One of the benefits of programming in MATLAB

More information

Logical Subscripting: This kind of subscripting can be done in one step by specifying the logical operation as the subscripting expression.

Logical Subscripting: This kind of subscripting can be done in one step by specifying the logical operation as the subscripting expression. What is the answer? >> Logical Subscripting: This kind of subscripting can be done in one step by specifying the logical operation as the subscripting expression. The finite(x)is true for all finite numerical

More information

Matlab Programming Arrays and Scripts 1 2

Matlab Programming Arrays and Scripts 1 2 Matlab Programming Arrays and Scripts 1 2 Mili I. Shah September 10, 2009 1 Matlab, An Introduction with Applications, 2 nd ed. by Amos Gilat 2 Matlab Guide, 2 nd ed. by D. J. Higham and N. J. Higham Matrix

More information

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB?

Appendix A. Introduction to MATLAB. A.1 What Is MATLAB? Appendix A Introduction to MATLAB A.1 What Is MATLAB? MATLAB is a technical computing environment developed by The Math- Works, Inc. for computation and data visualization. It is both an interactive system

More information

Worksheet 6. Input and Output

Worksheet 6. Input and Output Worksheet 6. Input and Output Most programs (except those that run other programs) contain input or output. Both fortran and matlab can read and write binary files, but we will stick to ascii. It is worth

More information

DSP Laboratory (EELE 4110) Lab#1 Introduction to Matlab

DSP Laboratory (EELE 4110) Lab#1 Introduction to Matlab Islamic University of Gaza Faculty of Engineering Electrical Engineering Department 2012 DSP Laboratory (EELE 4110) Lab#1 Introduction to Matlab Goals for this Lab Assignment: In this lab we would have

More information

5. MATLAB I/O 1. Beyond the Mouse GEOS 436/636 Jeff Freymueller, Sep 26, The Uncomfortable Truths Well, hop://xkcd.com/568 (April 13, 2009)

5. MATLAB I/O 1. Beyond the Mouse GEOS 436/636 Jeff Freymueller, Sep 26, The Uncomfortable Truths Well, hop://xkcd.com/568 (April 13, 2009) 5. MATLAB I/O 1 Beyond the Mouse GEOS 436/636 Jeff Freymueller, Sep 26, 2017 The Uncomfortable Truths Well, hop://xkcd.com/568 (April 13, 2009) Topics Loading and Saving the Workspace File Access Plo$ng

More information

MATLAB. MATLAB Review. MATLAB Basics: Variables. MATLAB Basics: Variables. MATLAB Basics: Subarrays. MATLAB Basics: Subarrays

MATLAB. MATLAB Review. MATLAB Basics: Variables. MATLAB Basics: Variables. MATLAB Basics: Subarrays. MATLAB Basics: Subarrays MATLAB MATLAB Review Selim Aksoy Bilkent University Department of Computer Engineering saksoy@cs.bilkent.edu.tr MATLAB Basics Top-down Program Design, Relational and Logical Operators Branches and Loops

More information

ELEN E3084: Signals and Systems Lab Lab II: Introduction to Matlab (Part II) and Elementary Signals

ELEN E3084: Signals and Systems Lab Lab II: Introduction to Matlab (Part II) and Elementary Signals ELEN E384: Signals and Systems Lab Lab II: Introduction to Matlab (Part II) and Elementary Signals 1 Introduction In the last lab you learn the basics of MATLAB, and had a brief introduction on how vectors

More information

MATLAB PROGRAMMING LECTURES. By Sarah Hussein

MATLAB PROGRAMMING LECTURES. By Sarah Hussein MATLAB PROGRAMMING LECTURES By Sarah Hussein Lecture 1: Introduction to MATLAB 1.1Introduction MATLAB is a mathematical and graphical software package with numerical, graphical, and programming capabilities.

More information

EE168 Handout #6 Winter Useful MATLAB Tips

EE168 Handout #6 Winter Useful MATLAB Tips Useful MATLAB Tips (1) File etiquette remember to fclose(f) f=fopen( filename ); a = fread( ); or a=fwrite( ); fclose(f); How big is a? size(a) will give rows/columns or all dimensions if a has more than

More information

Basic plotting commands Types of plots Customizing plots graphically Specifying color Customizing plots programmatically Exporting figures

Basic plotting commands Types of plots Customizing plots graphically Specifying color Customizing plots programmatically Exporting figures Basic plotting commands Types of plots Customizing plots graphically Specifying color Customizing plots programmatically Exporting figures Matlab is flexible enough to let you quickly visualize data, and

More information

ECE 202 LAB 3 ADVANCED MATLAB

ECE 202 LAB 3 ADVANCED MATLAB Version 1.2 1 of 13 BEFORE YOU BEGIN PREREQUISITE LABS ECE 201 Labs EXPECTED KNOWLEDGE ECE 202 LAB 3 ADVANCED MATLAB Understanding of the Laplace transform and transfer functions EQUIPMENT Intel PC with

More information

Creates a 1 X 1 matrix (scalar) with a value of 1 in the column 1, row 1 position and prints the matrix aaa in the command window.

Creates a 1 X 1 matrix (scalar) with a value of 1 in the column 1, row 1 position and prints the matrix aaa in the command window. EE 350L: Signals and Transforms Lab Spring 2007 Lab #1 - Introduction to MATLAB Lab Handout Matlab Software: Matlab will be the analytical tool used in the signals lab. The laboratory has network licenses

More information

Worksheet 1. Hello Matlab

Worksheet 1. Hello Matlab Worksheet 1. Hello Matlab This worksheet gives a quick introduction to Matlab. Start Matlab from the START - PROGRAMS - MATLAB6.5 - MATLAB6.5 menu. You should now be faced by a terminal window awaiting

More information