Matlab for Engineers Matlab Environment Chapter 2.

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
Section 2.3 Gauss-Jordan Method for General Systems of Equations
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.
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 ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Programming For Nuclear Engineers Lecture 12 MATLAB (3) 1.
1 MATLAB 基礎. 2 MATLAB  Workspace: environment (address space) where all variables reside  After carrying out a calculation, MATLAB assigns the result.
January 24, 2005 Lecture 3 - By P. Lin 1 CPET 190 Lecture 3 Problem Solving with MATLAB
INTRO TO PROGRAMMING Chapter 2. M-files While commands can be entered directly to the command window, MATLAB also allows you to put commands in text files.
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
THE MATLAB ENVIRONMENT VARIABLES BASIC COMMANDS HELP HP 100 – MATLAB Wednesday, 8/27/2014
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.
Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication.
1 Lab of COMP 406 Teaching Assistant: Pei-Yuan Zhou Contact: Lab 1: 12 Sep., 2014 Introduction of Matlab (I)
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 ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
Release Numbers MATLAB is updated regularly The Mathworks packages their software in groups, called releases, New releases are issued twice a year in the.
Computational Methods of Scientific Programming Lecturers Thomas A Herring, Room A, Chris Hill, Room ,
Matlab Workshop 1/10/07 Lesson 1: Matlab as a graphing calculator.
Input, Output, and Processing
Numerical Computation Lecture 2: Introduction to Matlab Programming United International College.
Matlab Basics Tutorial. Vectors Let's start off by creating something simple, like a vector. Enter each element of the vector (separated by a space) between.
What is MATLAB? MATLAB is one of a number of commercially available, sophisticated mathematical computation tools. Others include Maple Mathematica MathCad.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
CMPS 1371 Introduction to Computing for Engineers MatLab.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
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)
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 1 – Matlab Overview EGR1302. Desktop Command window Current Directory window Command History window Tabs to toggle between Current Directory &
Lecture 5 1.What is a variable 2.What types of information are stored in a variable 3.Getting user input from the keyboard 1.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
>> 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.
Introduction to Matlab  Matlab is a software package for technical computation.  Matlab allows you to solve many numerical problems including - arrays.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
MATLAB for Engineers, by Holly Moore. ISBN © 2007 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is.
The Hong Kong Polytechnic University Industrial Centre MatLAB Lesson 1 : Overview & Environment Edward Cheung Room W311g.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
Digital Image Processing Introduction to MATLAB. Background on MATLAB (Definition) MATLAB is a high-performance language for technical computing. The.
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.
BRIAN D. HAHN AND DANIEL T. VALENTINE THIRD EDITION Essential MATLAB® for Engineers and Scientists.
CMPS 1371 Introduction to Computing for Engineers VECTORS.
NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS ( NET 222: COMMUNICATIONS AND NETWORKS FUNDAMENTALS (PRACTICAL PART) Tutorial 2 : Matlab - Getting Started.
1 Faculty Name Prof. A. A. Saati. 2 MATLAB Fundamentals 3 1.Reading home works ( Applied Numerical Methods )  CHAPTER 2: MATLAB Fundamentals (p.24)
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
Math 252: Math Modeling Eli Goldwyn Introduction to MATLAB.
BASICS OF MATLAB Engr.Mian Shahzad Iqbal Lecturer,Telecom Department University of Engineering & Technology Taxila.
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
1-2 What is the Matlab environment? How can you create vectors ? What does the colon : operator do? How does the use of the built-in linspace function.
ECE 1304 Introduction to Electrical and Computer Engineering
Arithmetic Operations
Release Numbers MATLAB is updated regularly
Other Kinds of Arrays Chapter 11
Other Kinds of Arrays Chapter 11
MATLAB DENC 2533 ECADD LAB 9.
Use of Mathematics using Technology (Maltlab)
INTRODUCTION TO MATLAB
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
MATLAB Introduction MATLAB can be thought of as a powerful graphing calculator but with a lot more buttons! It is also a programming language, commands.
Chapter 2 MATLAB Environment
Presentation transcript:

Matlab for Engineers Matlab Environment Chapter 2

Matlab for Engineers In this chapter we’ll… Get started with Matlab Explore the Matlab windows Solve some problems using Matlab Learn how to save our work

Matlab for Engineers Section 2.1 Getting Started In Windows or Apple operating systems click on the desktop icon In Unix type matlab At the shell prompt

Matlab for Engineers Matlab opens to a default window configuration

Matlab for Engineers Matlab uses a standard windows menu bar To exit Matlab use the close icon

Matlab for Engineers Command Window Enter commands at the prompt Current Directory MATLAB Windows Command History Workspace Window

Matlab for Engineers You can use the command window much like you’d use a calculator The standard order of operation rules apply Let’s try Practice Exercise 2.1 To watch this demonstration, click in the figure window. To move to the next slide use the down arrow

Matlab for Engineers Section 2.2 Matlab Windows Matlab uses several different windows to display data, commands and results. They are not necessarily all open at once

Matlab for Engineers Let’s look at the windows one at a time

Matlab for Engineers Command Window Similar to a scratch pad Once you hit enter, you can’t edit any commands You can retype them or use the arrow keys to retrieve commands and edit them before hitting enter again Command Window

Matlab for Engineers Command History Records the commands you issue in the command window When you exit the command window, or when you issue the clc command, the command window is cleared But the command history remains Command History

Matlab for Engineers Command History You can transfer commands from the command history to the command window –Double click on a command –Click and drag

Matlab for Engineers To watch this demonstration, click in the figure window. To move to the next slide use the down arrow

Matlab for Engineers Workspace Window Keeps track of the variables you’ve defined –Name –Value –Class –Size –Bytes You may need to click on the name bar and select size and bytes in order to see these parameters Workspace Window

Matlab for Engineers To watch this demonstration, click in the figure window. To move to the next slide use the down arrow

Matlab for Engineers When you define variables in the command window, they are listed in the workspace window

Matlab for Engineers Scalar Vector 2-D Matrix

Matlab for Engineers Current Directory The current directory window is a list of files When you try to load information from a file or try to save information – Matlab uses the current directory

Matlab for Engineers Document Window If you double click on any variable in the workspace window Matlab launches a document window containing the array editor You can edit variables in the array editor

Matlab for Engineers Document Window

Matlab for Engineers Figure Window When Figures are created a new window opens It’s extremely easy to create graphs in Matlab

Matlab for Engineers The semicolon suppresses the output from each command

Matlab for Engineers

Editing Window This window allows you to type and save a series of commands without executing them There are several ways to open an editing window –From the file menu –With the new file icon

Matlab for Engineers Open an editing window from the file menu or with the new file icon

Matlab for Engineers Save and Run Write your code in the editing window, then run it using the Save and Run icon

Matlab for Engineers Section 2.3 Solving Problems with Matlab We’ve already solved some simple problems We need to understand how Matlab works to solve more complicated problems

Matlab for Engineers Variables MATLAB allows you to assign a value to a variable A=3 Should be read as A is assigned a value of 3 Use the variables in subsequent calculations

Matlab for Engineers Naming Variables All names must start with a letter They may contain letters, numbers and the underscore ( _ ) Names are case sensitive There are certain keywords you can’t use

Matlab for Engineers Use the iskeyword function for a list of keywords iskeyword ans = 'break' 'case' 'catch' 'continue' 'else' 'elseif' 'end' 'for' 'function' 'global' 'if' 'otherwise' 'persistent' 'return' 'switch' 'try' 'while'

Matlab for Engineers You can reassign function names Matlab will let you use built-in function names as variables – but it’s a really bad idea sin = 3 changes sin from a function to a variable name

Matlab for Engineers Practice Exercise 2.2 Which of these names are allowed in Matlab? test Test if my-book my_book Thisisoneverylongnamebutisitstillallowed? 1stgroup group_one zzaAbc z34wAwy?12# sin log x x x x x x bad idea

Matlab for Engineers Matrices in Matlab The basic data type Group of numbers arranged into rows and columns Single Value (Scalar) –Matrix with one row and one column Vector (One dimensional matrix) –One row or one column Matrix (Two dimensional)

Matlab for Engineers Scalar Calculations You can use MATLAB like you’d use a calculator >> ans=19 Command Prompt Result

Matlab for Engineers Assignment Operator To define a variable a we might type a=1+2 which should be read as: a is assigned a value of 1+2

Matlab for Engineers How is the assignment operator different from an equality? In algebra the equation x=3+5 means that both sides are the same In computers when we say x=3+5 we are telling the machine to store the value on the right hand side of the equation in a memory location, and to name that location x

Matlab for Engineers Is that really different? Yes!!! In algebra this is not a true statement x=x+1 In computers (assignment statements) it means replace the value in the memory location named x, with a new value equal to x+1

Matlab for Engineers Order of Operation Same as you’ve learned in math class Same as your calculator –Parentheses first –Exponentiation –Multiplication / division –Addition / subtraction

Matlab for Engineers Order of Operation 5*(3+6)= 45 5*3+6= 21 White space does not matter!!! 5*3 + 6= 21

Matlab for Engineers Parentheses Use only ( ) { } and [ ] mean something different MATLAB does not assume operators 5 * (3+4) not 5(3+4)

Matlab for Engineers Compute from left to right 5*6/6*5= 25 5*6/(6*5)= 1

Matlab for Engineers Here’s an example Find the surface area of a cylinder r = radius r = 5 h = height h = 10 π r 2 2π r * h

Matlab for Engineers To watch this demonstration, click in the figure window. To move to the next slide use the down arrow

Matlab for Engineers Array Operations Using Matlab as a glorified calculator is OK, but its real strength is in matrix manipulations

Matlab for Engineers To create a row vector, enclose a list of values in brackets

Matlab for Engineers You may use either a space or a comma as a “delimiter” in a row vector

Matlab for Engineers Use a semicolon as a delimiter to create a new row

Matlab for Engineers Use a semicolon as a delimiter to create a new row

Matlab for Engineers Hint: It’s easier to keep track of how many values you’ve entered into a matrix, if you enter each row on a separate line. The semicolons are optional

Matlab for Engineers Shortcuts While a complicated matrix might have to be entered by hand, evenly spaced matrices can be entered much more readily. The command b= 1:5 or the command b = [1:5] both return a row matrix

Matlab for Engineers The default increment is 1, but if you want to use a different increment put it between the first and final values

Matlab for Engineers To calculate spacing between elements use… linspace logspace

Matlab for Engineers Initial value in the array Final value in the array number of elements in the array

Matlab for Engineers Initial value in the array expressed as a power of 10 Final value in the array expressed as a power of 10 number of elements in the array

Matlab for Engineers Hint You can include mathematical operations inside a matrix definition statement. For example a = [0: pi/10: pi]

Matlab for Engineers Mixed calculations between scalars and arrays Matrices can be used in many calculations with scalars There is no confusion when we perform addition and subtraction Multiplication and division are a little different In matrix mathematics the multiplication operator (*) has a very specific meaning

Matlab for Engineers

Addition between arrays is performed on corresponding elements

Matlab for Engineers Multiplication between arrays is performed on corresponding elements if the.* operator is used

Matlab for Engineers Matlab interprets * to mean matrix multiplication. The arrays a and b are not the correct size for matrix multiplication in this example

Matlab for Engineers Array Operations Array multiplication.* Array division./ Array exponentiation.^ In each case the size of the arrays must match

Matlab for Engineers The matrix capability of Matlab makes it easy to do repetitive calculations For example, assume you have a list of angles in degrees that you would like to convert to radians. –First put the values into a matrix. –Perform the calculation

Matlab for Engineers

Either the * or the.* operator can be used for this problem, because it is composed of scalars and a single matrix The value of pi is built into Matlab as a floating point number, called pi

Matlab for Engineers More about pi Because pi is an irrational number, it can not be expressed exactly with a floating point representation The Matlab constant, pi, is really an approximation. If you find sin(pi) Matlab returns a very small number.

Matlab for Engineers Transpose The transpose operator changes rows to columns or vice versa.

Matlab for Engineers The transpose operator makes it easy to create tables

Matlab for Engineers table =[degrees;radians]’ would have given the same result

Matlab for Engineers The transpose operator works on both one dimensional and two dimensional arrays

Matlab for Engineers Number Display Scientific Notation –Although you can enter any number in decimal notation, it isn’t always the best way to represent very large or very small numbers –In Matlab, values in scientific notation are designated with an e between the decimal number and exponent. (Your calculator probably uses similar notation.)

Matlab for Engineers It is important to omit blanks between the decimal number and the exponent. For example, Matlab will interpret e23 as two values (6.022 and )

Matlab for Engineers Display Format Multiple display formats are available No matter what display format you choose, Matlab uses double precision floating point numbers in its calculations Matlab handles both integers and decimal numbers as floating point numbers

Matlab for Engineers Default The default format is called short If an integer is entered it is displayed without trailing zeros If a floating point number is entered four decimal digits are displayed

Matlab for Engineers Other formats Changing the format affects all subsequent displays –format long results in 14 decimal digits –format bank results in 2 decimal digits –format short returns the display to the default 4 decimal digits

Matlab for Engineers Really Big and Really Small When numbers become too large or too small for Matlab to display using the default format, it automatically expresses them in scientific notation You can force scientific notation with –format short e –format long e

Matlab for Engineers Common Scale Factor For long and short formats, a common scale factor is applied to the entire matrix if some of the elements become very large, or very small. This scale factor is printed along with the scaled values.

Matlab for Engineers Common Scale Factor

Matlab for Engineers Two other formats format + format rat

Matlab for Engineers Spacing in the command window The format command also allows us to control how tightly information is spaced in the command window –format compact –format loose – (default) Most of the examples in this presentation use format compact Notice that the value of A is still being displayed using the rat format, because we haven’t changed it back to format short

Matlab for Engineers Section 2.4 Saving Your Work If you save a MATLAB session, all that is saved are the values of the variables you have named

Matlab for Engineers Variables are saved, not the work space

Matlab for Engineers Save either by using the file menu or... Save with a command in the command window

Matlab for Engineers Matlab automatically saves to a.mat file If you want to save to another format, such as.dat, you need to explicitly tell the program save -ascii

Matlab for Engineers Script M-files If you want to save your work, you need to create an M-file File->New->M-file Type your commands in the edit window that opens

Matlab for Engineers The file is saved into the current directory It runs in the command window

Matlab for Engineers

Comments Be sure to comment your code –Add your name –Date –Section # –Assignment # –Descriptions of what you are doing and why

Matlab for Engineers The % sign identifies comments You need one on each line

Matlab for Engineers Summary Introduced the MATLAB Windows Basic matrix definition Save and retrieve MATLAB data Create and use script M-files