Language Basics and Data Types ● What and why of Matlab. ● Matlab desktop. ● Familiarize ourselves with basics of language. ● Basic types of data that.

Slides:



Advertisements
Similar presentations
MATLAB – A Computational Methods By Rohit Khokher Department of Computer Science, Sharda University, Greater Noida, India MATLAB – A Computational Methods.
Advertisements

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
Introduction To MATLAB Programming
Introduction to Matlab Workshop Matthew Johnson, Economics October 17, /13/20151.
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.
Programming in Visual Basic
Lecture 7 Debugging Code & Data Import/Export © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Working with JavaScript. 2 Objectives Introducing JavaScript Inserting JavaScript into a Web Page File Writing Output to the Web Page Working with Variables.
Introduction to MATLAB Northeastern University: College of Computer and Information Science Co-op Preparation University (CPU) 10/22/2003.
Division Example 2x - 3y + 4z = 10 x + 6y - 3z = 4 -5x + y + 2z = 3 A*X = B where A = B = >> X = A\B X =
Introduction to Array The fundamental unit of data in any MATLAB program is the array. 1. An array is a collection of data values organized into rows and.
Introduction to Matlab Jianguo Wang CSSCR September 2009.
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.
Introduction to MATLAB
Lecture 4 MATLAB Windows Arithmetic Operators Maintenance Functions
REVIEW 2 Exam History of Computers 1. CPU stands for _______________________. a. Counter productive units b. Central processing unit c. Copper.
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.
A L I MAM M OHAMMAD B IN S AUD I SLAMIC U NIVERSITY C OLLEGE OF S CIENCES D EPARTMENT OF M ATHEMATICS MATLAB 251 : MATH SOFTWARE Introduction to MATLAB.
Math 15 Lecture 7 University of California, Merced Scilab A “Very” Short Introduction.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
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 ,
XP Tutorial 10New Perspectives on Creating Web Pages with HTML, XHTML, and XML 1 Working with JavaScript Creating a Programmable Web Page for North Pole.
MATLAB Tutorial EE 327 Signals and Systems 1. What is MATLAB? MATLAB – Matrix Laboratory The premier number-crunching software Extremely useful for signal.
INTRODUCTION TO MATLAB LAB# 01
1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (1): Introduction.
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
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.
Introduction to MATLAB 7 Engineering 161 Engineering Practices II Joe Mixsell Spring 2010.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Flow Control and Functions ● Script files ● If's and For's ● Basics of writing functions ● Checking input arguments ● Variable input arguments ● Output.
Term 2, 2011 Week 1. CONTENTS Problem-solving methodology Programming and scripting languages – Programming languages Programming languages – Scripting.
MATLAB Environment ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
Matlab 14.html Cost: $100 Available in labs on Windows and Unix machines.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
MATLAB – PT1 The purpose of this workshop is to get you started and to have fun with MATLAB! Let’s talk a little and decide on what we will be covering.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Files: By the end of this class you should be able to: Prepare for EXAM 1. create an ASCII file describe the nature of an ASCII text Use and describe string.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Introduction to MATLAB 7 Engineering 161 Engineering Practices II Joe Mixsell Spring 2012.
A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.
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.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
XP Tutorial 10New Perspectives on HTML, XHTML, and DHTML, Comprehensive 1 Working with JavaScript Creating a Programmable Web Page for North Pole Novelties.
An Introduction to Programming in Matlab Emily Blumenthal
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
● Preparing data sets ● CTD Data from Tomales Bay ● Clean up ● Binning ● Combined Temperature-Depth plots ● T-S scatter plots ● Multiple plots on a single.
Getting started with Matlab: Outline
Release Numbers MATLAB is updated regularly
Other Kinds of Arrays Chapter 11
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
StatLab Matlab Workshop
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.
funCTIONs and Data Import/Export
Introduction to MATLAB
Experiment No. (1) - an introduction to MATLAB
Presentation transcript:

Language Basics and Data Types ● What and why of Matlab. ● Matlab desktop. ● Familiarize ourselves with basics of language. ● Basic types of data that can be manipulated in matlab. ● More number and array stuff. ● Plotting 101. ● Saving and loading data. ● Real-world example.

What is Matlab? ● Matlab is a high-level computing language optimized for scientific computation and, in particular, matrix manipulation. It makes many common mathematical operations used by scientists simple and fast. ● Matlab is an interpreted language – code is compiled each time it is run. ● Matlab has excellent visualization tools.

Why use Matlab? ● Excellent at manipulating large amounts of tabular data (arrays, matrices) in a consistent way. The “Mat” in Matlab is for Matrix. ● Lots of built in functions for data analysis, interpolation, linear algebra, fourier analysis, visualization.... ● Add-on toolboxes for extra power. ● Mature language with GUIs, object-oriented framework.... ● Lots of independent support and free code available on the web.

Why not to use Matlab? ● Anything requiring repetition, for loops, tends to be slow. Limitation of interpreted language. Often solved using matrices. ● Not a statistics package. A statistics toolbox exists at a price. Much available online. ● Commercial, costly. Free imitations available (octave, scilab...), but lack some functionality. ● Well written C/FORTRAN code could be faster, but usually not worth the effort. ● Not suitable for parallel programming. Some imitations (scilab) have some ability to work in parallel.

Matlab Desktop

● Command Window: Where commands are entered. We will principally use this area. ● Command History/Directory Listing: Useful for repeating old commands, changing directories and loading in data. ● Launch Pad: I just use it to get the hi-tech documentation going.

Entering Commands Here we are going to familiarize ourselves with the basics of entering commands and how to interact with matlab. Start matlab and click on the command window to make it active. Try entering the following commands:  1  1;  1+2i  a = 2  B = a * pi;  whos  clear a  whos Note that the 2 nd and 5 th lines print no output. When a command ends in ';' it works silently in matlab. Lines 4 and 5 create variables. In matlab variables do not need to be declared before use. Variable names are case sensitive. The whos command shows the variables in the workspace, their size and type. clear removes variables from the workspace.

Getting Help Matlab has a number of ways to get information about functions. The simplest, yet most common, is textual help from command line. Try the following:  help sqrt  more on % Let's you page text.  help *  help plot  more off The more command lets you page through output from any matlab command. Note the use of '%' to include comments. In matlab commands, anything beyond a '%' is a comment and is ignored. Matlab also has a fairly powerful help browser that children of Bill Gates will be familiar with. Try:  helpdesk You can also access much of the documentation through the launch pad. Also, try help help for more information on getting help.

Matlab Data Types All languages allow a variety of data types: integers, floats, booleans (T/F), strings, arrays.... Matlab is no different. However, matlab is different from C and many common languages in some important ways: ● Variables and their data types do not have to be declared beforehand. ● Matlab combines integers, floats and booleans into one thing: doubles or floating point numbers.  a = 1.54 % This is a double  b = a > 1 % Logical (boolean) double. ● Strings. Matlab treats a string as a character arrays.  b = 'abcd' % NOTE that matlab uses single quotes  b(2:3) ● Arrays/Matrices of numbers. Use ';' to separate rows, ',' or blank space to separate columns.  c = [1 2 3; 4, 5, 6] % More in next class. ● Cell Arrays. Most useful way to store several strings or mixed data.  d = { 'abcd', 1, [123], 'def' } ● Structures will de discussed in the second class.

Series of Numbers / Special Numbers Creating series of numbers is easy in matlab.  2:5  2:1:5  2:2:5 % Middle number is step size.  5:1  5:-1:1  5.23:0.1:7.6  pi:0.1:2*pi Note that 2:5 is shortcut for 2:1:5. Also, 5:1 produces the empty matrix as 5:1:1 makes little sense. There are also several special numbers in matlab. NaN is used for missing data. Inf and -Inf are the responses to division by zero.  a = NaN;  2 * a  1/0, -1/0 % ',' can separate commands similar to ';'  inf + -inf Capitalization is not important when using these numbers. Also, these numbers may appear in text data files without a problem.

Basic Array Operations I will discuss matrices in detail next week, but it is useful at this point to mention a couple simple operations.  a = [ 1 2; 3 4 ];  a(1,:) % Gets first row  a(:,1) % Gets first column  a(1,1) % Gets first element  2 * a  a * a % Matrix multiplication in the math sense  a.* a % Element-by-element Matrix multiplication  a ^ 3  a.^ 3  a.^ a % a(i,j) raised to the a(i,j) There are a number of other matrix (and otherwise) operators. Try help ops for more info. Note the difference between * and.*: * performs row*column type matrix multiplication that you will see in math textbooks;.* is equivalent to a(i,j)*a(i,j). This second form is very common in matlab and part of the reason matlab is so good at matrix manipulation.

Plotting 101 Matlab has extremely good visualization which could easily be an entire course. Fortunately, the simple things are simple.  a = 0:0.2:3; % Series of numbers from 0 to 3.  b = exp( a ); % exponential of those numbers  plot( a, b )  figure % Opens empty figure window.  plot( a, a, a, b )  title( 'Linear vs. Exponential Growth' )  xlabel( 'time' )  % I don't care for those colors. Lets change them.  plot( a, a, 'b', a, b, 'r-.' ) Note that the last plot command erased the previous one. This is the default, but it can be changed (see help hold). The documentation of plot explains the format strings ( 'b' and 'r-.' ) used in the last command. The appearance of figures can be changed interactively. Try clicking on the and zooming in on a part of the figure.  close all

Saving Data to Files Matlab allows valuable data to be saved in both text and binary formats. The binary format is a native matlab format (.mat) that allows variables to be easily saved and loaded back in later on.  clear all % Start with a clean slate.  a = ((1:100)^2)' % ' is the transpose operator  b = ((1:100)^3)'  save % Save all to matlab.mat  save a_var.mat a % Save only a in binary format  save - - ascii a_var.txt a % Save a in text format The first form of save is a quick way to save all variables into the file 'matlab.mat'. Take a look at the 'a_var.txt' file. This file could now be loaded in any spreadsheet.  open a_var.txt Finally, matlab is capable of saving all commands you enter and their output in a diary file.  diary my_diary.txt  whos  diary off  open my_diary.txt

Loading Data from Files Matlab loads in both binary files and tab-separated text files containing numbers.  clear all  load a_var.mat  whos % Note variable names.  load a_var.txt  whos  a2 = load('a_var.txt')  clear all  load % Loads in matlab.mat  whos Note that binary files preserve the variable names from when they were saved. When text files are loaded in, the default is create a variable with the name of the file (no extensions) containing the data. Text data files may contain matlab style comments. For example, the following is a perfectly acceptable file header: % This file was created on % yearGPA % A good year % Who wants to be average anyway

Real-world Example Here we are going to load in some BML MOMS data and play with it. First, click here to download the data file. If the link doesn't work, take a look at the end of this page. Save the file as 'moms.txt'.  open moms.txt  load moms.txt The columns of the file are explained in the comments of the file. The column 'yearday' is the number of days since It is useful for temporal plots. The exercise for this class is to first make a plot of time vs. temperature. Make sure to add a title and some axis labels so I know what you are plotting. Next calculate the the NS component of the wind stress. For our purposes, the formula for NS wind stress is where u and v are the components of the wind velocity. Next, plot time vs. v-wind stress on a new figure. Include a zero line to ease visualization. Compare the two figures and note that temperature increases when wind stress is negative (from the South). Don't worry if you don't get it. The solution is on the next slide.

Solution  load moms.txt  plot(moms(:,4), moms(:,5))  title('Water Temperature for BML, 2001')  xlabel( 'Day of the Year' )  ylabel( 'Temperature (^{o}C)' )  figure  wind_speed = sqrt( moms(:,6).^2 + moms(:,7).^2 );  ns_wind_stress = moms(:,7).* wind_speed;  plot( moms(:,4), ns_wind_stress,...  moms(:,4), 0 * moms(:,4) )  %%... is used to continue lines. %%%  title( 'NS Wind Stress for BML, 2001' )  xlabel( 'Day of the Year' )  ylabel( 'NS Wind Stress' )