ES 240: Scientific and Engineering Computation. Chapter 2 Chapter 2: MATLAB Fundamentals Uchechukwu Ofoegbu Temple University.

Slides:



Advertisements
Similar presentations
Introduction to Engineering MATLAB – 11 Plotting - 4 Agenda Multiple curves Multiple plot.
Advertisements

Introduction to Matlab
MATLAB – What is it? Computing environment / programming language Tool for manipulating matrices Many applications, you just need to get some numbers in.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
EGR 106 – Week 2 – Arrays & Scripts Brief review of last week Arrays: – Concept – Construction – Addressing Scripts and the editor Audio arrays Textbook.
Dr. Jie Zou PHY Welcome to PHY 3320 Computational Methods in Physics and Engineering.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
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.
Matlab intro The Environment
Part 1 Chapter 2 MATLAB Fundamentals
MATLAB Fundamentals.
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.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
Creating scalars, vectors, matrices Ex1 & 2. Dot Product & Cross Product Ex3. Plotting Graphs Ex4. Conversion Table Ex5. Plotting functions Finishing Ex4.
Chapter 5 Review: Plotting Introduction to MATLAB 7 Engineering 161.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Martin Ellison University of Warwick and CEPR Bank of England, December 2005 Introduction to MATLAB.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
THE MATLAB ENVIRONMENT VARIABLES BASIC COMMANDS HELP HP 100 – MATLAB Wednesday, 8/27/2014
Introduction to MATLAB January 18, 2008 Steve Gu Reference: Eta Kappa Nu, UCLA Iota Gamma Chapter, Introduction to MATLAB,
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
How to Use MATLAB A Brief Introduction. 2 What can MATLAB do? Matrix Operations Symbolic Computations Simulations Programming 2D/3D Visualization.
MEGN 536 – Computational Biomechanics MATLAB: Getting Started Prof. Anthony J. Petrella Computational Biomechanics Group.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
INTRODUCTION TO MATLAB LAB# 01
Arrays 1 Multiple values per variable. Why arrays? Can you collect one value from the user? How about two? Twenty? Two hundred? How about… I need to collect.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
Introduction to Matlab Module #2 Page 1 Introduction to Matlab Module #2 – Arrays Topics 1.Numeric arrays (creation, addressing, sizes) 2.Element-by-Element.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
Lecture 2 - Matlab Introduction CVEN 302 June 5, 2002.
Matlab 14.html Cost: $100 Available in labs on Windows and Unix machines.
1 DKT 211 Basic Communication Engineering LAB # 1A : (Lecture 1) Introduction to Matlab  Basic Features  Scientific features  Array Operations  Script.
Matlab Screen  Command Window  type commands  Current Directory  View folders and m-files  Workspace  View program variables  Double click on a.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Engineering Analysis – Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
>> x = [ ]; y = 2*x y = Arrays x and y are one dimensional arrays called vectors. In MATLAB all variables are arrays. They allow functions.
Part 1 Chapter 2 MATLAB Fundamentals PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University All.
Lecture 20: Choosing the Right Tool for the Job. What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation.
EGR 106 Lecture 6 2-D Plotting Graphical presentation has become the standard method to show technical information. Engineers use plots to analyze, visualize,
Array Creation ENGR 1181 MATLAB 2. Civil engineers store seismic data in arrays to analyze plate tectonics as well as fault patterns. These sets of data.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
Array Creation ENGR 1187 MATLAB 2. Today’s Topics  Arrays: What are they?  Vectors  Matrices  Creating Arrays.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
Introduction to Engineering MATLAB – 4 Arrays Agenda Creating arrays of numbers  Vectors: 1-D Arrays  Arrays: 2-D Arrays Array Addressing Strings & String.
Introduction to MATLAB 1.Basic functions 2.Vectors, matrices, and arithmetic 3.Flow Constructs (Loops, If, etc) 4.Create M-files 5.Plotting.
Matlab for Engineers Matlab Environment Chapter 2.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
Manipulating MATLAB Vector, Matrices 1. Variables and Arrays What are variables? You name the variables (as the programmer) and assign them numerical.
CS100A, Fall 1998, Lecture 191 CS100A, Fall 1998 Lecture 19, Thursday Nov 05 Matlab Concepts: Matlab arrays Matlab subscripting Matlab plotting.
CS100A, Fall 1998, Lecture 201 CS100A, Fall 1998 Lecture 20, Tuesday Nov 10 More Matlab Concepts: plotting (cont.) 2-D arrays Control structures: while,
Lecture 12: M/O/F/ for Engineering Applications B Burlingame 27 April 2016.
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.
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.
How to use MATLAB (using M-files) Double click this icon To start Matlab 6.5.
ENG College of Engineering Engineering Education Innovation Center 1 Arrays in MATLAB Topics Covered: 1.Creating arrays of numbers vectors matrices.
Prof. N. P. Jadhav Presented by. Overview of MATLAB environment MATLAB is an interactive, matrix-based system for scientific and engineering numeric computation.
ECE 1304 Introduction to Electrical and Computer Engineering
EEE 244 Numerical Methods In Electrical Engineering
Part 1 Chapter 2 MATLAB Fundamentals
MATLAB How to use (using M-files) Double click this icon
MATLAB How to use (using M-files) Double click this icon
MATLAB How to use (using M-files)
Part 1 Chapter 2 MATLAB Fundamentals
244-2: MATLAB/Simulink FUNDAMENTALS
How to Use MATLAB A Brief Introduction.
Presentation transcript:

ES 240: Scientific and Engineering Computation. Chapter 2 Chapter 2: MATLAB Fundamentals Uchechukwu Ofoegbu Temple University

ES 240: Scientific and Engineering Computation. Chapter 2 The MATLAB Environment –MATLAB uses three primary windows Command window - used to enter commands and data Graphics window(s) - used to display plots and graphics Edit window - used to create and edit M-files (programs) Depending on your computer platform and the version of MATLAB used, these windows may have different looks and feels.

ES 240: Scientific and Engineering Computation. Chapter 2 Calculator Mode The MATLAB command widow can be used as a calculator where you can type in commands line by line. Whenever a calculation is performed, MATLAB will assign the result to the built-in variable ans Example: >> ans = 39

ES 240: Scientific and Engineering Computation. Chapter 2 MATLAB Variables Using ans is useful for fast calculation; however, … … the value of ans changes with every new command; however, … … in MATLAB you can name variables. – This results in the storage of values to memory locations corresponding to the variable name. In MATLAB, you can store individual values as well as arrays; You can also store numerical data and text (which is actually stored numerically as well). In MATLAB you do not need to pre-initialize a variable; if it does not exist, MATLAB will create it for you.

ES 240: Scientific and Engineering Computation. Chapter 2Scalars To assign a single value to a variable, simply type the variable name, the = sign, and the value: >> a = 4 a = 4 Note that variable names must start with a letter, though they can contain letters, numbers, and the underscore (_) symbol

ES 240: Scientific and Engineering Computation. Chapter 2 Scalars (cont) If you append a semi-solon ( ; ) to the end of a line, the result of the computation will not be displayed. The calculation is still performed. Else, it will: >> a a = 4

ES 240: Scientific and Engineering Computation. Chapter 2 Scalars (cont) The default value for i (or j ) in MATLAB is the unit imaginary number, unless otherwise assigned. MATLAB can display results in several different formats using the format command. –The values are still stored the same way, they are just displayed on the screen differently. eg: –short - scaled fixed-point format with 5 digits –long - scaled fixed-point format with 15 digits for double and 7 digits for single –short eng - engineering format with at least 5 digits and a power that is a multiple of 3 (useful for SI prefixes)

ES 240: Scientific and Engineering Computation. Chapter 2 Format Examples >> format short; pi ans = >> format long; pi ans = >> format short eng; pi ans = e+000 >> pi*10000 ans = e+003 Note - the format remains the same unless another format command is issued.

ES 240: Scientific and Engineering Computation. Chapter 2 Arrays, Vectors, and Matrices MATLAB can automatically handle rectangular arrays of data –one-dimensional arrays are called vectors –two-dimensional arrays are called matrices. Arrays are set off using square brackets [ and ] in MATLAB Columns are separated by spaces or commas Rows are separated by semicolons

ES 240: Scientific and Engineering Computation. Chapter 2 Array Examples >> a = [ ] a = >> b = [2;4;6;8;10] b = Note 1 - MATLAB does not display the brackets Note 2 - if you are using a monospaced font, such as Courier, the displayed values should line up properly

ES 240: Scientific and Engineering Computation. Chapter 2Matrices A 2-D array, or matrix, of data is entered row by row, with spaces (or commas) separating entries within the row and semicolons separating the rows: >> A = [1 2 3; 4 5 6; 7 8 9] A =

ES 240: Scientific and Engineering Computation. Chapter 2Puzzle At a family reunion were the following people: one grandfather, one grandmother, two fathers, two mothers, four children, three grandchildren, one brother, two sisters, two sons, two daughters, one father-in-law, one mother-in- law, and one daughter-in-law. But not as many people attended as it sounds. How many were there, and who were they?

ES 240: Scientific and Engineering Computation. Chapter 2 Useful Array Commands The transpose operator (apostrophe) can be used to flip an array over its own diagonal. For example, if b is a row vector, b’ is a column vector. The command window will allow you to separate rows by hitting the Enter key - script files and functions will allow you to put rows on new lines as well. The who command will report back used variable names; whos will also give you the size, memory, and data types for the arrays.

ES 240: Scientific and Engineering Computation. Chapter 2 Accessing Array Entries Individual entries within a array can be both read and set using either the index of the location in the array or the row and column. The index value starts with 1 for the entry in the top left corner of an array and increases down a column - the following shows the indices for a 4 row, 3 column matrix:

ES 240: Scientific and Engineering Computation. Chapter 2 Accessing Array Entries (cont) Assuming some matrix C: C = C(2) would report 3 C(4) would report 10 C(13) would report an error! Entries can also be access using the row and column: C(2,1) would report 3 C(3,2) would report 0 C(5,1) would report an error!

ES 240: Scientific and Engineering Computation. Chapter 2 Array Creation - Built In There are several built-in functions to create arrays: –zeros(r,c) will create an r row by c column matrix of zeros –zeros(n) will create an n by n matrix of zeros –ones(r,c) will create an r row by c column matrix of ones –ones(n) will create an n by n matrix one ones help elmat has, among other things, a list of the elementary matrices

ES 240: Scientific and Engineering Computation. Chapter 2 Array Creation - Colon Operator The colon operator : –is useful in several contexts. –can be used to create a linearly spaced array of points using the notation - start:diffval:limit where start is the first value in the array, diffval is the difference between successive values in the array, and limit is the boundary for the last value (though not necessarily the last value). >>1:0.6:3 ans =

ES 240: Scientific and Engineering Computation. Chapter 2 Colon Operator - Notes If diffval is omitted, the default value is 1: >>3:6 ans = To create a decreasing series, diffval must be negative: >> 5:-1.2:2 ans = If start+diffval>limit for an increasing series or start+diffval >5:2 ans = Empty matrix: 1-by-0 To create a column, transpose the output of the colon operator, not the limit value; that is, (3:6)’ not 3:6’

ES 240: Scientific and Engineering Computation. Chapter 2 Array Creation - linspace linspace is used to create a row vector with a specific number of linearly spaced points between two numbers –linspace(x1, x2, n) will create a linearly spaced array of n points between x1 and x2 >>linspace(0, 1, 6) ans = If n is omitted, 100 points are created. To generate a column, transpose the output of the linspace command.

ES 240: Scientific and Engineering Computation. Chapter 2 Array Creation - logspace logspace is used to create a row vector with a specific number of logarithmically spaced points between two numbers –logspace(x1, x2, n) will create a logarithmically spaced array of n points between 10 x1 and 10 x2 >>logspace(-1, 2, 4) ans = If n is omitted, 100 points are created. To generate a column, transpose the output of the logspace command.

ES 240: Scientific and Engineering Computation. Chapter 2 Mathematical Operations Mathematical operations in MATLAB can be performed on both scalars and arrays. The common operators, in order of priority, are: ^Exponentiation4^2 = 8 -Negation (unary operation) -8 = -8 */*/ Multiplication and Division 2*pi = pi/4 = \Left Division6\2 = Addition and Subtraction 3+5 = = -2

ES 240: Scientific and Engineering Computation. Chapter 2 Order of Operations The order of operations is set first by parentheses, then by the default order given above: –y = -4 ^ 2 gives y = -16 since the exponentiation happens first due to its higher default priority, but –y = (-4) ^ 2 gives y = 16 since the negation operation on the 4 takes place first

ES 240: Scientific and Engineering Computation. Chapter 2 Complex Numbers All the operations above can be used with complex quantities (i.e. values containing an imaginary part entered using i or j and displayed using i) –x = 2+i*4; (or 2+4i, or 2+j*4, or 2+4j) y = 16; 3 * x ans = i –x+y ans = i

ES 240: Scientific and Engineering Computation. Chapter 2 Vector-Matrix Calculations MATLAB can also perform operations on vectors and matrices. The * operator for matrices is defined as the outer product or what is commonly called “matrix multiplication.” –The number of columns of the first matrix must match the number of rows in the second matrix. –The size of the result will have as many rows as the first matrix and as many columns as the second matrix. –The exception to this is multiplication by a 1x1 matrix, which is actually an array operation. The ^ operator for matrices results in the matrix being matrix-multiplied by itself a specified number of times. –Note - in this case, the matrix must be square!

ES 240: Scientific and Engineering Computation. Chapter 2 Element-by-Element Calculations You may need to carry out computations item by item in a matrix or vector. The MATLAB manual calls these array operations. They are also often referred to as element-by-element operations. MATLAB defines.* and./ (note the dots) as the array multiplication and array division operators. –For array operations, both matrices must be the same size or one of the matrices must be 1x1 Array exponentiation (raising each element to a corresponding power in another matrix) is performed with.^ –Again, for array operations, both matrices must be the same size or one of the matrices must be 1x1

ES 240: Scientific and Engineering Computation. Chapter 2 Built-In Functions There are several built-in functions you can use to create and manipulate data. The built-in help function can give you information about both what exists and how those functions are used: –help elmat will list the elementary matrix creation and manipulation functions, including functions to get information about matrices. –help elfun will list the elementary math functions, including trig, exponential, complex, rounding, and remainder functions. The built-in lookfor command will search help files for occurrences of text and can be useful if you know a function’s purpose but not its name

ES 240: Scientific and Engineering Computation. Chapter 2Graphics MATLAB has a powerful suite of built-in graphics functions. Two of the primary functions are plot (for plotting 2-D data) and plot3 (for plotting 3-D data). In addition to the plotting commands, MATLAB allows you to label and annotate your graphs using the title, xlabel, ylabel, and legend commands.

ES 240: Scientific and Engineering Computation. Chapter 2 Plotting Example –t = [0:2:20]’; g = 9.81; m = 68.1; cd = 0.25; v = sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*t); plot(t, v)

ES 240: Scientific and Engineering Computation. Chapter 2 Plotting Annotation Example –title('Plot of v versus t'); xlabel('Values of t'); –ylabel('Values of v'); grid

ES 240: Scientific and Engineering Computation. Chapter 2 Plotting Options In MATLAB, you can use several different colors, point styles, and line styles to plot data. –These are specified at the end of the plot command using plot specifiers as found in Table 2.2 (pg 34). –The default case for a single data set is to create a blue line with no points. –If a line style is specified with no point style, no point will be drawn at the individual points; –If a point style is specified with no point style, no line will be drawn. –Examples of plot specifiers: ‘ro:’ - red dotted line with circles at the points ‘gd’ - greed diamonds at the points with no line ‘m--’ - magenta dashed line with no point symbols

ES 240: Scientific and Engineering Computation. Chapter 2 Other Plotting Commands hold on and hold off –hold on tells MATLAB to keep the current data plotted and add the results of any further plot commands to the graph. This continues until the hold off command, which tells MATLAB to clear the graph and start over if another plotting command is given. –hold on should be used after the first plot in a series is made. subplot(m, n, p) –subplot splits the figure window into an mxn array of small axes and makes the p th one active. Note - the first subplot is at the top left, then the numbering continues across the row. This is different from how elements are numbered within a matrix!

ES 240: Scientific and Engineering Computation. Chapter 2 Group work Do problems 2.1, 2.3, 2.5, 2.7, 2.8, 2.10 Print out the display on your command window Print out all plots