Chapter 2 MATLAB Environment

Slides:



Advertisements
Similar presentations
Introduction to Matlab
Advertisements

Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
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.
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 ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Digital Image Processing Lecture3: Introduction to MATLAB.
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
MATLAB File Management. MATLAB User File Management Matlab provides a group of commands to manage user files. For more information, type help iofun. pwd.
Data, graphics, and programming in R 28.1, 30.1, Daily:10:00-12:45 & 13:45-16:30 EXCEPT WED 4 th 9:00-11:45 & 12:45-15:30 Teacher: Anna Kuparinen.
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 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.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
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.
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.
Winter Semester 2014/2015 College of Engineering.
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 &
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.
Interduction to MATLAB Manal Alotaibi Mathematics department College of science King saud university.
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.
INTRODUCTION TO MATLAB DAVID COOPER SUMMER Course Layout SundayMondayTuesdayWednesdayThursdayFridaySaturday 67 Intro 89 Scripts 1011 Work
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.
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.
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.
MATLAB ® for Engineers, Holly Moore Fourth Edition, Global Edition © Pearson Education Limited 2015 All rights reserved. Figure 2.1 MATLAB ® opening window.
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.
Fundamentals of Windows Mouse n 4 Basic Operations: –Pointing –Clicking –Double Clicking –Dragging.
ECE 1304 Introduction to Electrical and Computer Engineering
Release Numbers MATLAB is updated regularly
Lecture: MATLAB Chapter 1 Introduction
Introduction to MATLAB for Engineers, Third Edition
2) Platform independent 3) Predefined functions
Introduction to MATLAB
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
Chapter I Introduction to MS Excel Program
Lecture 1: Introduction
MATH 493 Introduction to MATLAB
Digital Image Processing
funCTIONs and Data Import/Export
INTRODUCTION TO MATLAB
Matlab.
CSCI N317 Computation for Scientific Applications Unit 1 – 1 MATLAB
Experiment No. (1) - an introduction to MATLAB
Using Script Files and Managing Data
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.
Matlab Training Session 2: Matrix Operations and Relational Operators
Grauer and Barber Series Microsoft Access Chapter Two
Introduction to Matlab
Presentation transcript:

Chapter 2 MATLAB Environment Sections: 2-1 2-2-1 to 2-2-8 2-3-1 to 2-3-3 2-4-1 to 2-4-3

A snap shot of the MATLAB working environment Command Window type commands at >> Current Directory View folders and m-files Workspace View program variables Double click on a variable to see it in the Array Editor Command History view past commands save a whole session using diary Workspace Command Window Command History 2

2.2.1 Command Window: 2.2.2 Command History: To type a command, the cursor must be placed next to the command prompt ( >> ). Pressing Enter key executes the command. Several commands can be typed in the same line. This is done by typing a comma between the commands. When Enter key is pressed, the commands are executed in order from left to right. A previously typed command can be recalled to the command prompt with the up arrow key (↑). When the command is displayed at the command prompt, it can be modified if needed and then executed. The down-arrow key (↓) can be used to move down the list of previously typed commands. If a command is too long to fit in one line, it can be continued to the next line by typing three periods … (called an ellipsis) and pressing the Enter key. The continuation of the command is then typed in the new line. The command can continue line after line up to a total of 4,096 characters. 2.2.2 Command History: The command history window records the commands issued in the command window. clc command only clears the command window not the history Any command from the command history window can be transferred to the command window by double-clicking (which also executes the command) or by clicking and dragging the line of code into the command window

2.2.3 Workspace Window

2.2.4 Current Folder Window The current folder window lists all the files in the active directory. When MATLAB either accesses files or saves information, it uses current folder unless specified otherwise.

2.2.5 Document Window: Double-clicking on any variable listed in the workspace window launches a document window, containing the variable editor. Values stored in the variable are displayed in a spreadsheet format, which can be edited or changed.

2.2.6 Graphics Window x = [0, pi/4, pi/2, 3*pi/4, pi, 5*pi/4, 3*pi/2, 7*pi/4, 2*pi]; y = sin(x); plot(x,y)

2.2.7 Edit Window 2.2.8 Start Button To open the edit window, choose File from the menu bar, then New, and, finally Script File New Script This window allows to type and save a series of commands without executing them. One may also open the edit window by typing edit at the command prompt or by selecting the New Script button on the toolbar. 2.2.8 Start Button The start button is located in the lower left-hand corner of the MATLAB ® window. It offers alternative access to the various MATLAB ® windows, as well as to the help function, Internet products, demos and MATLAB ® toolboxes.

2.3 Solving Problems with MATLAB

2.3.1 Using Variables HIGHLY RECOMMENDED to assign a unique name to values used in MATLAB. All names must start with a letter. The names can be of any length, but only the first 63 characters are used in MATLAB. (Use namelengthmax command to check) Names are case sensitive. The variable x is different from variable X The only allowable characters are letters, numbers, and the underscore. Use isvarname command to check. Output is 1 (allowed) or 0 (not allowed). Don’t use built-in functions as variable names. Use which command to check built-in functions (Try for sin/cos) MATLAB reserves a list of keywords for use by the program, which cannot be assigned as variable names. The iskeyword command causes MATLAB to list these reserved names. If variable is not selected, MATLAB calculates the expression and responds by displaying ans = followed by the numerical result of the expression in the next line.

Practice Exercise 2.2

2.3.2 Matrices: Scalar Operations Scalars are single-valued matrices. Their operations can be carried out using: Priority rule PEMDAS (Parentheses, Exponentiation, Multiplication, Division, Addition, Subtraction) >> ((2+5)^2+2*2-10/5)  = ?

Priority rules Precedence is the order operations are preformed, remember PEMDAS >> ((2+5)^2+2*2-10/5) innermost parentheses is done first 7^2 exponentiation is next 2*2 or 10/5 multiplication or division next (49 + 4 – 2) addition or subtraction next Operations are done from left to right  final result gives 51

Practice Exercise 2.3

>> 5-25^1/2*2+19 >> 5*11–5^(3*4–9) >> a=1+(y-2*x)/sqrt(y)+log(x)/x^2/3 𝑎=1+ 𝑦−2𝑥 𝑦 + 𝑙𝑜𝑔 𝑥 𝑥 2 3 𝑎=1+ 𝑦−2𝑥 𝑦 + 𝑙𝑜𝑔 𝑥 𝑥 2 3 >>(0.6*b*H*sqrt(2*g*(h1-h2))-Q)*Q^(1-b)/(dx*a*be) 1− cos⁡(𝜋) 𝑒 −𝑥 +2𝑥−3 +ln⁡(1.2x 10 −3 𝑥) 𝛾= ln⁡( 𝛽−𝛼 ) 𝛼!+1 + sin⁡ 2𝜋 𝛼 2 + 𝛽 2 y=sin 𝑎 𝜋 2 + cos⁡(𝛽𝜋) 𝑒 𝛽 +2 𝛽𝑎 2 +sin⁡(𝑎𝜋) +ln⁡(1.5x 10 −3 𝑎)

Array A collection of data values organized into rows and columns, and is fundamental unit of data in MATLAB Scalars: Arrays with 1 row and 1 column Vector: Array with one dimension Matrix: Array with more than one dimension Size of an array is specified by the number of rows and the number of columns, with the number of rows mentioned first (For example: n x m array) Total number of elements in an array is the product of the number of rows and the number of columns

2.3.2 Matrices: Array Operations Row vector: x = [1 2 3 4] Column vector: y = [1; 2; 3; 4] >> a = [1 2 3 4; 2 3 4 5 ; 3 4 5 6] and will return a = 1 2 3 4 2 3 4 5 3 4 5 6 >>b = 1:5 & >>b = [1:5] are same Both will return a row matrix b = 1 2 3 4 5 Square bracket is optional Increment is 1 (default value) >> c = 1:2:5 (first:increment:final) will return c = 1 3 5 >> d = linspace(1, 10, 3) (first:final:total evenly spaced values) d = 1 5.5 10 >> e = logspace(1, 3, 3) logspace(first,final,total values) ( 10 1 , 10 3 , 3 values between 10 1 and 10 3 ) e = 10 100 1000 x1 = [0, pi/4, pi/2, 3*pi/4, pi, 5*pi/4, 3*pi/2, 7*pi/4, 2*pi] x2 = 0: pi/4:2*pi x3 = linspace(0,2*pi,9); What do you think??? 2.3.2 Matrices: Array Operations

Make a CHOICE Smooth; Smooth??; 101 data points 9 data points x = [0, pi/4, pi/2, 3*pi/4, pi, 5*pi/4, 3*pi/2, 7*pi/4, 2*pi]; y = sin(x); plot(x,y) x = linspace(0, 2*pi, 101); Make a CHOICE Smooth; 101 data points Smooth??; 9 data points

Element by element array operation using (.) a.×b= 𝑎 1 × 𝑏 1 , 𝑎 2 × 𝑏 2 , 𝑎 3 × 𝑏 3 a./b= 𝑎 1 𝑏 1 , 𝑎 2 𝑏 2 , 𝑎 3 𝑏 3 ; a.^2= 𝑎 1 ^2, 𝑎 2 ^2, 𝑎 3 ^3 Arrays MUST have same dimension Element by element array operation using (.) a= 𝑎 1 ,𝑎 2 , 𝑎 3 ; b=2; c= 𝑐 1 ; 𝑐 2 ; 𝑐 3 ; a.×b= 𝑎 1 ×𝑏, 𝑎 2 ×𝑏, 𝑎 3 ×𝑏 a.×c=?? a./c=?? a+c=?? 2.3.2 Matrices: Array Operations

Practice Exercise 2.4

Example-1 >>radians = [0, pi/4, pi/2, 3*pi/4, pi, 5*pi/4, 3*pi/2, 7*pi/4, 2*pi]; >>degrees = radians.*(180/pi); %; prevents output of results >>table = [radians, degrees] % row vector of 18 elements >>table_new = [radians’, degrees’] % two columns radians degrees

2.4 Saving Work Sessions/Variables/Command Files

Display DOES NOT affect the accuracy of calculations 2.3.3 Number Display Display DOES NOT affect the accuracy of calculations

2.4.1 Diary 2.4.2 Saving Variables To completely record and save work sessions, use diary or or diary on. To end a recording session type diary again, or diary off. A file named diary should appear in the current folder that can be retrieved by a double click. Subsequent sessions are added to the end of the file. Filename can also be specified using either diary <filename> or diary('filename'), e.g. diary my_diary or diary(‘my_diary'). 2.4.2 Saving Variables Type save to save the workspace to a file matlab.mat. Use save <filename> to assign a name. Alternatively, choose File Save Workspace As from the menu bar and the assign the file name. Type load <filename> to restore the workspace. Even selected variables (e.g. matrices) can be stored in a specified file (default extension .mat) for later use with save <filename> <list_of_variables> For ASCII format, use save <filename> <list_of_variables> -ascii. Use extension .dat or .txt in the file name, e.g.

2.4.3 Script M-Files MATLAB commands can be created and saved in files, called M-files An M-file is an ASCII text file. Can be modified whenever needed Tremendous flexibility for problem solving. M-files are stored in the current folder File name must be a valid MATLAB variable name (starting with a letter and containing only letters, numbers, and the underscore. No spaces are allowed) Two kinds of M-files, called scripts and functions

Example-2.3 𝑑𝑟𝑎𝑔=20,000 𝑁 𝜌=1× 10 −6 𝑘𝑔 𝑚 −3 𝑉=100 𝑚𝑝ℎ= ??𝑚/𝑠 One performance characteristic that can be determined in a wind tunnel is drag. The friction related to drag on the Mars Climate Observer (caused by the atmosphere of Mars) resulted in the spacecraft’s burning up during course corrections. Drag is extremely important in the design of terrestrial aircraft as well (see Figure 2.10 ). Drag is the force generated as an object, such as an airplane, moves through a fluid. Of course, in the case of a wind tunnel, air moves past a stationary model, but the equations are the same. Drag is a complicated force that depends on many factors. One factor is skin friction, which is a function of the surface properties of the aircraft, the properties of the moving fluid (air in this case), and the flow patterns caused by the shape of the aircraft (or, in the case of the Mars Climate Observer, by the shape of the spacecraft). Drag can be calculated with the drag equation: drag= 𝐶 𝑑 𝜌 𝑉 2 𝐴 2 𝐶 𝑑 = drag coefficient, which is determined experimentally, 𝜌 = air density, V = velocity of the aircraft, A = reference area (the surface area over which the air flows). Although the drag coefficient is not a constant, it can be taken to be constant at low speeds (less than 200 mph). Data were measured in a wind tunnel and reported here at 100 mph. Calculate the drag coefficient. Finally, use this experimentally determined drag coefficient to predict how much drag will be exerted on the aircraft at velocities from 0 mph to 200 mph. Example-2.3 𝑑𝑟𝑎𝑔=20,000 𝑁 𝜌=1× 10 −6 𝑘𝑔 𝑚 −3 𝑉=100 𝑚𝑝ℎ= ??𝑚/𝑠 A=1 𝑚 2 /𝑠

Drag is a mechanical force generated by a solid object moving through a fluid.

Example of MATLAB code in an M-file to solve Example 2.3 clear, clc % A Script M-file to find Drag % First define the variables drag = 20000; %Define drag in Newtons density= 0.000001; %Define air density in kg/m^3 velocity = 100*0.4470; %Define velocity in m/s area = 1; %Define area in m^2 % Calculate coefficient of drag cd = drag *2/(density*velocity^2*area); % Find the drag for a variety of velocities velocity = 0:20:200; %Redefine velocity velocity = velocity*.4470; %Change velocity to m/s drag = cd*density*velocity.^2*area/2; %Calculate drag table = [velocity',drag'] %Create a table of results