A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences.

Slides:



Advertisements
Similar presentations
RAPTOR Syntax and Semantics By Lt Col Schorsch
Advertisements

Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Topic 9 – Introduction To Arrays. CISC105 – Topic 9 Introduction to Data Structures Thus far, we have seen “simple” data types. These refers to a single.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Introduction to Computers and Programming for Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to.
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
Loops – While, Do, For Repetition Statements Introduction to Arrays
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Introduction to a Programming Environment
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Matrix Mathematics in MATLAB and Excel
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.
CS0007: Introduction to Computer Programming Introduction to Arrays.
While Loops and Do Loops. Suppose you wanted to repeat the same code over and over again? System.out.println(“text”); System.out.println(“text”); System.out.println(“text”);
Java Unit 9: Arrays Declaring and Processing Arrays.
Computer Science 101 Introduction to Programming.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
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.
IE 212: Computational Methods for Industrial Engineering
Created by, Author Name, School Name—State FLUENCY WITH INFORMATION TECNOLOGY Skills, Concepts, and Capabilities.
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.
1 Computing Software. Programming Style Programs that are not documented internally, while they may do what is requested, can be difficult to understand.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Python Programming Chapter 6: Iteration Saad Bani Mohammad Department of Computer Science Al al-Bayt University 1 st 2011/2012.
Loops: Handling Infinite Processes CS 21a: Introduction to Computing I First Semester,
CPS120 Introduction to Computer Science Iteration (Looping)
Computer Science 12 Mr. Jean May 2 nd, The plan: Video clip of the day Review of common errors in programs 2D Arrays.
Hello.java Program Output 1 public class Hello { 2 public static void main( String [] args ) 3 { 4 System.out.println( “Hello!" ); 5 } // end method main.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
MATLAB Harri Saarnisaari, Part of Simulations and Tools for Telecommunication Course.
CPS120: Introduction to Computer Science Decision Making in Programs.
Java Data Types Assignment and Simple Arithmetic.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Introduction to MATLAB adapted from Dr. Rolf Lakaemper.
Getting Started with MATLAB (part2) 1. Basic Data manipulation 2. Basic Data Understanding 1. The Binary System 2. The ASCII Table 3. Creating Good Variables.
Working with Arrays in MATLAB
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
Programming Fundamentals. Overview of Previous Lecture Phases of C++ Environment Program statement Vs Preprocessor directive Whitespaces Comments.
Basics Copyright © Software Carpentry 2011 This work is licensed under the Creative Commons Attribution License See
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
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.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
M1G Introduction to Programming 2 2. Creating Classes: Game and Player.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
CIS 595 MATLAB First Impressions. MATLAB This introduction will give Some basic ideas Main advantages and drawbacks compared to other languages.
1 ENERGY 211 / CME 211 Lecture 4 September 29, 2008.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Windows Programming Lecture 03. Pointers and Arrays.
Introduction To Repetition The for loop
Lecture: MATLAB Chapter 1 Introduction
Other Kinds of Arrays Chapter 11
Scripts & Functions Scripts and functions are contained in .m-files
Introduction to MATLAB
Variables In programming, we often need to have places to store data. These receptacles are called variables. They are called that because they can change.
Java - Data Types, Variables, and Arrays
Use of Mathematics using Technology (Maltlab)
Variables ICS2O.
Packages and Interfaces
Unit-1 Introduction to Java
Simulation And Modeling
Class code for pythonroom.com cchsp2cs
Presentation transcript:

A (VERY) SHORT INTRODUCTION TO MATLAB J.A. MARR George Mason University School of Physics, Astronomy and Computational Sciences

FIRST…WHERE IS MATLAB W/R/T PROGRAMMING LANGUAGES? Matlab LISP family, Python, Java Fortran, Algol family, C++ C assembly language machine code (binary!) micro code Increasing ease-of-use (fairly subjective) Increasing program size (fairly objective)

WHY MATLAB? “Softer” Reasons... Most scientists and engineers know it Very widely used in industry, academia and gov Easy to learn LOTS of pre-developed application packages Very good technical support/user community “Harder” Reasons... Excellent native graphics capabilities Includes many modern numerical methods Highly optimized for scientific computing EXCEPTIONALLY FAST matrix operations Interfaces well with other languages/packages

ASSIGNMENT: single variables ASSIGNMENT: matrices (or, arrays) ITERATION (FOR loops) WHAT WE’LL STUDY HERE… MEANS GO TO THE ACCOMPANYING WORKSHEET assignments_practice.m AND READ THE COMMENTS AND CODE IN THE SECTION OF THAT WORKSHEET KEYED TO THE PRESENTATION TOPIC FOUND HERE.

ASSIGNMENT (VARIABLES) Think of a box with a label on it. We can put “stuff” inside that box. Labels on boxes allow us to refer to specific boxes, by name, at a later time, as needed. These labeled boxes are what we call a “variables”. Putting “stuff” inside the box is called assigning a value to a variable, or simply, assignment. In Matlab, we write a variable name (which creates the labeled box), then write an equals sign (=) and finally write the value we want to “put in the box”, or, assign to the variable. Examples: A = 10 a = 5 TwelveDigitsOfPi =

ASSIGNMENT (VARIABLES) More Examples: MyVariable = 1.349e-03 bushels_of_wheat = 2.0 constant = Rules: The “=” symbol DOES NOT MEAN EQUALS! It means assignment: Assign the value on the right of the “=” symbol to the variable on the left of the “=” symbol (“equals” is denoted by ==). To access what’s “in the box”, simply write (or type) the name of the box (i.e., to retrieve the value assigned to a variable, type that variable’s name). So a variable written on the right means, “retrieve the value in this box”.

ASSIGNMENT (VARIABLES) REMEMBER: Value on the right of “=” gets stored into variable on the left of “=”: var1 = 5.0 Valid assignment (creates var2, assigns it the value contained in var1): var2 = var1 Invalid assignment (generates error: var3 not previously declared – holds no value) var2 = var3

ASSIGNMENT (VARIABLES) Rules (cont): Variables can be used in assignment statements to assign values to other variables: Since typing the name of a variable retrieves the value stored in it, we can assign the value contained in one variable, to another variable: var1 = 3.0 var2 = var1 We can do math with variables, too: var3 = var2 + var1 var4 = var3  var2 var5 = var4 / var1 var6 = var2 ^ var1

ASSIGNMENT (VARIABLES) Rules (cont): In particular, we can “update” a variable (updating by 1 is usually called “incrementing”): var7 = 1 var7 = var7 + 1 Variables can be only one TYPE at a time. Numbers are principally of two types: integers (3, 0, -3) and floating point numbers, or floats ( ). Floats are further divided into single and double precision. Matlab’s floats are double precision by default. LET MATLAB HANDLE THE DETAILS!

ASSIGNMENT (VARIABLES) Rules (cont): Ending any statement with a semi-colon will cause Matlab to NOT echo the variable and its value back to the screen: EDU>> var8 = 3.0 var8 = 3 EDU>> var9 = 5.0; EDU>>

ASSIGNMENT (MATRICES or ARRAYS) Think of a MATRIX as a partitioned box: The box itself has a label (variable name) and we can store MULTIPLE things inside the box, each inside its own partition. We can manipulate the ENTIRE COLLECTION at once, just by referring to that one label (matrix name). A ==

A matrix has DIMENSIONS: In the 2 dimensional case, rows and columns. Each partition is referred to by both its row number and its column number. Rule: In Matlab, both row and column numbers START AT 1: (1,1)(1,2)(1,3) (2,1)(2,2)(2,3) (3,1)(3,2)(3,3) (ROW, COLUMN) A == ASSIGNMENT (MATRICES or ARRAYS)

We can access individual partitions (called “elements”) by writing the matrix name, followed by a left paren “(“, the row number, a comma, the column number, and a right paren “)”: A == A(1,2) A(1,1) A(2,3) A(3,2) ASSIGNMENT (MATRICES or ARRAYS)

A(1,1)A(1,2)A(1,3) A(2,1)A(2,2)A(2,3) A(3,1)A(3,2)A(3,3) (ROW, COLUMN) Rule: The name for an individual matrix element acts just like a variable. A == == (ACTUAL VALUES) Typing “A(1,1)” at the command line, will result in 4 Typing “A(2,2)” at the command line, will result in 7 Typing “A(3,3)” at the command line, will result in -10 A(1,3) ASSIGNMENT (MATRICES or ARRAYS)

Rules: Each matrix element acts like a variable (ACTUAL VALUES) varX = A(1,1) + A(1,2) varX = A(2,2)  A(1,2) varX = A(3,1) - A(2,3) varX = A(1,2) ^ A(3,2) ASSIGNMENT (MATRICES or ARRAYS) A(1,1)A(1,2)A(1,3) A(2,1)A(2,2)A(2,3) A(3,1)A(3,2)A(3,3) (ROW, COLUMN) A ====

A(1,1)A(1,2)A(1,3) A(2,1)A(2,2)A(2,3) A(3,1)A(3,2)A(3,3) (ROW, COLUMN) As a result, you can assign values to specific matrix elements, too: A ==== (NEW VALUES) A(1,1) = 100 A(2,2) = 200 A(3,3) = ASSIGNMENT (MATRICES or ARRAYS)

Matrices can be different dimensions. Here’s A 1-D matrix (called a “row vector”): (ROW, COLUMN) B == B(1,1)B(1,2)B(1,3)B(1,4)B(1,5) (ACTUAL VALUES) B == B(1,1) == 10 B(1,3) == 6B(1,5) == 2 ASSIGNMENT (MATRICES or ARRAYS)

(ROW, COLUMN) Another 1-dimensional matrix (called a “column vector”): C == (ACTUAL VALUES) C == C(1,1) == 10 C(3,1) == 6 C(5,1) == 2 C(1,1) C(2,1) C(3,1) C(4,1) C(5,1) ASSIGNMENT (MATRICES or ARRAYS)

Creating matrices in Matlab: Method #1: Like variable assignment: Assign the last element in a 1-D matrix, or the “lower right corner element” in a 2- D matrix to be some value. Now you have a matrix filled with zeros and whatever you assigned to the last element: EDU>> D(1,5) = 0 D = EDU>> D(3,3) = 10 D = ASSIGNMENT (MATRICES or ARRAYS)

Creating matrices in Matlab: Method #2: Write it out explicitly. Use either spaces or commas to separate values in a row. Use semi-colons to separate rows: EDU>> D = [1 2 3; 4 5 6; 7 8 9] D = EDU>> D = [1, 2, 3; 4, 5, 6; 7, 8, 9] D = ASSIGNMENT (MATRICES or ARRAYS)

ITERATION (FOR loops) Often times, we’ll want to repeat doing something again, and again, and again, and again…maybe millions of times…or maybe, just enough times to access and change each element of a matrix. Computers, of course, are great at doing things over and over again This is called ITERATION. Iteration is executed with something called a FOR loop

ITERATION (FOR loops) Syntax: As shown—always the same. NOTE: The keywords FOR and END come in a pair—never one without the other. for n = [1:5] n end What’s happening: n first assigned the value 1 n then written to the screen END is encountered END sends execution back to the top Repeat: n = 2, 3, 4, 5 Is n at the end of the list? Then STOP. “Loop body” (always indented) SIDE NOTE: n = [1:5] same as n = [1,2,3,4,5] loop index

ITERATION (FOR loops) So, a FOR loop is doing “implicit assignment” to the loop index n: each time “through the loop” (or, one “trip”), the loop index n has a particular value (which can’t be changed by you, only by the FOR loop). After the trip is complete (when execution reaches END), the loop index is reassigned to the next value in the 1-D matrix, from left to right. When the rightmost (last) value is reached, the FOR loop stops executing. And then statements AFTER the FOR loop (following the END keyword) are executed -- that is, the FOR loop “exits”.

ITERATION (FOR loops) FOR loop examples: Study carefully, so that you understand what each loop is doing! for n = [1:5] n^2 end mat(1,5) = 0; for n = [1:5] mat(1,n)=n end mat2(1,5) = 0; mat3(1,5) = 0; mat4(1,5) = 0; for n = [1:5] mat2(1,n) = n mat3(1,n) = n^2 mat4(1,n) = mat2(1,n) + mat3(1,n) end var1 = 2.0; var2 = 3.0; mat1(1,5) = 0; for n = [1:5] mat1(1,n) = var1+ n  var2; end

POSTSCRIPT “The only way to learn a new programming language is by writing programs in it.” Brian Kernighan & Dennis Richie “The C Programming Language”