1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (2): MATLAB Environment (Chapter 1)

Slides:



Advertisements
Similar presentations
Chapter 1 Computing Tools Data Representation, Accuracy and Precision Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction.
Advertisements

Fall 2006AE6382 Design Computing1 Numeric Representation in a Computer Learning Objectives Understand how numbers are stored in a computer and how the.
MATLAB ME1107 Y Yan Reference: MATLAB for Engineers by Holly Moore (Pearson Prentice Hall)
Lecture 6 MATLAB functions Basics of Built-in Functions, Help Feature, Elementary Functions (e.g., Polynomials, Trigonometric Functions), Data Analysis,
MATLAB Basics CS 111 Introduction to Computing in Engineering and Science.
Introduction to MATLAB 7 for Engineers
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
Introduction to Matlab. Entering Commands Constants and Functions >> pi ans = >> eps ans = e-016 >> sin(pi/2) ans = 1 >> log(1000) ans =
Introduction to MATLAB
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.
MATLAB Fundamentals.
Chapter 10 Review: Matrix Algebra
MATLAB INTRO CONTROL LAB1  The Environment  The command prompt Getting Help : e.g help sin, lookfor cos Variables Vectors, Matrices, and Linear Algebra.
Engineering Analysis ENG 3420 Fall 2009 Dan C. Marinescu Office: HEC 439 B Office hours: Tu-Th 11:00-12:00.
Fortran 1- Basics Chapters 1-2 in your Fortran book.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
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.
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.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
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.
MATLAB Basics. The following screen will appear when you start up Matlab. All of the commands that will be discussed should be typed at the >> prompt.
1 MatLab Basics Jae Hoon Kim Department of Physics Kangwon National University It contains hundreds of commands to do mathematics. Graph functions, solve.
Getting Started with MATLAB 1. Fundamentals of MATLAB 2. Different Windows of MATLAB 1.
26 - 3/20/2000AME 150L1 M ATLAB ® /20/2000AME 150L2 M ATLAB ® vs. Fortran Fortran Positives –First Compiler –Legacy Codes –Efficient Numerically.
Introduction to Engineering MATLAB – 2 Introduction to MATLAB - 2 Agenda Defining Variables MATLAB Windows.
10/24/20151 Chapter 2 Review: MATLAB Environment Introduction to MATLAB 7 Engineering 161.
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.
ES 240: Scientific and Engineering Computation. Chapter 2 Chapter 2: MATLAB Fundamentals Uchechukwu Ofoegbu Temple University.
1 Week 1: Variables, assignment, expressions READING: 1.2 – 1.4.
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.
INTRODUCTION TO MATLAB MATLAB is a software package for computation in engineering, science, and applied mathemat-ics. It offers a powerful programming.
MATLAB Lecture Two Tuesday 5 July Chapter 3.
Computer Simulation Lab Electrical and Computer Engineering Department SUNY – New Paltz SUNY-New Paltz “Lecture 2”
1 Lecture 1 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
Part 1 Chapter 2 MATLAB Fundamentals PowerPoints organized by Dr. Michael R. Gustafson II, Duke University and Prof. Steve Chapra, Tufts University All.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Basics of Matlab UC Berkeley Fall 2004, E77 Copyright 2005, Andy Packard. This work is licensed under the Creative.
INTRODUCTION TO MATLAB Dr. Hugh Blanton ENTC 4347.
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.
1 Lecture 3 Post-Graduate Students Advanced Programming (Introduction to MATLAB) Code: ENG 505 Dr. Basheer M. Nasef Computers & Systems Dept.
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
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)
MATLAB Constants, Variables & Expression Nafees Ahmed Asstt. Professor, EE Deptt DIT, DehraDun.
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
Other Kinds of Arrays Chapter 11
INTRODUCTION TO BASIC MATLAB
Seminar 1 for DCSP Using Matlab.
MATLAB DENC 2533 ECADD LAB 9.
Introduction to MATLAB
Introduction to MATLAB
Introduction to Matlab
INTRODUCTION TO MATLAB
Experiment No. (1) - an introduction to 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.
CS 111 Introduction to Computing in Engineering and Science
-seminar 1 for digital signal processing
Basics of Matlab UC Berkeley Fall 2004, E Copyright 2005, Andy Packard
Chapter 2 MATLAB Environment
Presentation transcript:

1 Computer Programming (ECGD2102 ) Using MATLAB Instructor: Eng. Eman Al.Swaity Lecture (2): MATLAB Environment (Chapter 1)

2 Objectives  CHAPTER (1):MATLAB Environment -Perform simple calculations with MATLAB. -Observe the display formats available and select specific formats. -List the predefined MATLAB variables. -Perform arithmetic calculations. -Introduce simple matrix operations.

3 1.1 MATLAB as a Calculator  MATLAB performs simple calculations as if it were a calculator.  The basic arithmetic operators are + - * / ^ and these are used in conjunction with brackets: (parentheses ).  The symbol^ is used to get exponents (powers): 2^4=16. You should type in commands shown following the prompt: >> (in command window).  For example, type 5+3 and press the ENTER key: » 5+3 ans = 8

4 1.1 MATLAB as a Calculator-cont  Another example >> 2 + 3/4*5 ans = >> Is this calculation 2 + 3/(4*5) or 2 + (3/4)*5?  Matlab works according to the priorities ( Operator precedence ) : 1. quantities in brackets, 2. powers 2 + 3^2 )2 + 9 = 11, 3. * /, working left to right (3*4/5=12/5), , working left to right (3+4-5=7-5), Thus, the earlier calculation was for 2 + (3/4)*5 by priority 3.

5 1.1 MATLAB as a Calculator-cont  Another examples

6 1.1 MATLAB as a Calculator-cont  Another examples » 1+2-3*4/5 ans = » -4/5*3+2+1 ans =

7 1.2 Defining Variables  Variables are named locations in memory where numbers, strings and other elements of data may be stored while the program is working.  Variable names are combinations of letters, digits, and the underscore character, but must start with a latter (These are allowable: NetCost, Left2Pay, x3, X3, z25c)  We define variables by typing a variable name followed by the equals sign and then a value or a mathematical expression. Example: » A5=6 A5 = 6

8 1.2 Defining Variables-cont Variable types:  Not all variables are created equal.  The most commonly used unit of computer storage is the byte, itself made of up 8 bits.  A byte represents an 8-bit binary number, i.e. it is an eight digit number in base 2 (as opposed to base 10 or decimal with which we are most familiar.)

9 1.2 Defining Variables-cont Variable types:  A byte can thus hold the equivalent of decimal integers 0 ( ) through 255 ( ).  To represent other forms of information than simply the integers 0-255, computers use different types of encoding and groups of bytes to represent e.g. letters, punctuation, and numbers of greater precision and magnitude.

Defining Variables-cont Variable types:  The byte arose as the fundamental unit because it is capable of representing all the most common numbers, letters (upper and lower case), punctuation, and special control characters commonly used in computer communication (things like,,, etc.).  The most common encoding for alpha-numerics is the American Standard Code for Information Interchange, or ASCII.

Defining Variables-cont  MATLAB does not require you to declare the names of variables in advance of their use. This is actually a common cause of error.  variable names are case sensitive  variable names start with a letter and can contain up to 31 characters which include letters, digits and underscore ( punctuation characters and math operators are not allowed)

Defining Variables-cont  To know what is stored in a variable type its name at the command prompt  Matlab has built in variable names. the following are some of the built in variables: ( ans, pi, eps, flops, inf, NaN or nan, i, j, nargin, nargout, realmin, realmax). Avoid using built-in names.

Defining Variables-cont

Defining Variables-cont  Rules for Variable Names: Although variable names can be of any length, MATLAB uses only the first N characters of the name, (where N is the number returned by the function namelengthmax), and ignores the rest. >>N = namelengthmax N = 63

Defining Variables-cont  Rules for Variable Names-cont: Making Sure Variable Names Are Valid. ( Using: isvarname function ). Ex: >>isvarname 8thColumn ans = 0 Matlab Variables Names Legal variable names: Should not be the name of a built-in variable, built-in function, or user-defined function Examples: xxxxxxxxx pipeRadius widgets_per_box mySum mysum

Defining Variables-cont Important commands:  clc => Clear the command window  clear or clear variables => clears all variables from the workspace.  Delete => Delete files.  quit => Terminates MATLAB  who, whos =>List a directory of variables currently in memory. Lists the current variables, their sizes, and whether they have nonzero imaginary parts.  Lookfor => search tool  Save =>command saves variables from the workspace to a named file  Load => recover Data

Defining Variables-cont Looking for Functions Syntax: lookfor string searches first line of function descriptions for “string”. Example: >> lookfor cosine produces ACOS Inverse cosine. ACOSH Inverse hyperbolic cosine. COS Cosine. COSH Hyperbolic cosine.

Defining Variables-cont Example: » a=5; » A=7.9; » a a = 5 » A A = » clc » who

Functions(Built-In Functions) » Except for basic commands such as addition, subtraction, multiplication and division, most MATLAB commands are functions. Functions usually require an input argument (such as x above) and they return value. A)Trigonometric Functions Those known to Matlab are sin, cos, tan and their arguments should be in radians. e.g. to work out the coordinates of a point on a circle of radius 5 centered at the origin and having an elevation 30o = pi/ 6 radians: >> x = 5*cos(pi/6), y = 5*sin(pi/6) x = y =

Functions(Built-In Functions) A) Trigonometric Functions-cont The inverse trig functions are called asin, acos, atan (as opposed to the usual arcsin or sin-1etc.). The result is in radians. >> acos(x/5), asin(y/5) ans = >> pi/6 ans = B) Other Elementary Functions These include sqrt, exp, log, log10…..etc.

Display Formats Values are displayed with MATLAB i n several ways. CommentsDisplayMatlab command default3.1416Format short 16 digit Format long 5 digit plus exponent3.1416e+000Format short e 16 digit plus exponent e+ 000 Format long e 2 decimal places3.14Format bank positive+Format + ratio355/113Format rat hexadecimal400921fb54442d18Format hex

Saving the Variables Stored in Memory Suppose a large number of variables are defined. You need to quit using MATLAB f o r the moment, but you would like to use these variables in the future. This can be done in two ways: 1.Use the MATLAB menus and use the File and Save Workspace As menu selections. 2.Or you can use the MATLABSAVE command.

Saving the Variables Stored in Memory Example: » a=l; » b=2; » c=3; » d=4; » e=5; » f=6; » g=7; » who » save Saving to: mat lab. mat %To see that we can restore the variables, let's clear the workspace:

Saving the Variables Stored in Memory Example-cont: » clear » who Your variables are: No variables are in memory. %Now let's use the LOAD command to load the default file, matlab.mat, and restore the variables: load Loading from: matlab.mat » who

Saving the Variables Stored in Memory Example-cont: %If you wish to save the variables in a file with a different name, you can use the LOAD and SAVE commands followed by a file name » save xyzzy %Let's clear the workspace and restore the variables using file xyzzy. » clear » who Your variables are: No variables are defined. %To load the variables saved in file xyzzy, use the LOAD command with the file name: » load xyzzy » who

Saving the Variables Stored in Memory Example-2: » a=5; » A=7.9; » a a = 5 » A A = » clc » who

27 Example-cont: » save A a (A: File name a: variable to be saved) » clear Now list the variables stored in memory: » who Your variables are: Now no variables are listed. »load A » who Your variables are: As X 1.5 Saving the Variables Stored in Memory

28 » Pure imaginary and complex numbers are expressed in MATLAB by the letters i or j. Both i and j are defined as. If we take the square root of -1, MATLAB displays a number containing the letter i. The MATLAB SQRT function calculates the square root of a number. » sqrt(-1) ans = i 1.7 Complex Numbers Real part. Imaginary part.

29 MATLAB automatically handles calculations with complex numbers Examples: » (1+2i)/(3+Si) ans = i » a=3+4i; » b=l+i; » a*a ans = i » a-b ans = i » a+b ans = i 1.7 Complex Numbers

30 Unit Imaginary Numbers i and j are ordinary Matlab variables pre-assigned with the value √−1. >> i^2 ans = Both or either i and j can be reassigned >> i = 5; >> t = 8; >> u = sqrt(i-t) (i-t = -3, not -8+i) u = i >> u*u ans = Complex Numbers

Complex Numbers

Complex Numbers Functions for Complex Arithmetic Examples: Remember: There is no “degrees” mode in Matlab. All angles are in radians.

33  MATLAB can store several numerical values in a single variable. For most of the text, we'll refer to these variables as arrays. MATLAB however, treats these variables as either matrices or arrays.  In MATLAB the default uses matrix algebra rather than array operations. Array operations are defined as element-by-element operations. 1.8 Matrices and Vectors matrix algebra operators +addition -subtraction *multiplication /division ^power ‘complex conjugate transpose Array operators.*element-by-element multiplication./element-by-element divsion.^element-by-element power.‘transpose

Matrices and Vectors  In this section we will use the terms matrix and vector. A vector is a matrix with one row (a row vector) or one column (a column vector). Row Vectors A row vector is an 1-by-n matrix so the number of rows is always one. The number of columns can be any integer value except one. Column Vectors A column vector is an n-by-1 matrix so the number of columns is always one. The number of Rows can be any integer value except one.

Matrices and Vectors Scalars Any single element or numerical value. Or said other way, any vector or matrix that has one row and one column. (Its size is equal to 1,1.) Square Matrices Any matrix that has the number of rows equal to the number of columns (m = n).  In MATLAB matrices and vectors are created using square brackets [ ]. Rows are separated by the semicolon character (;).

Matrices and Vectors Vectors (arrays) are defined as >> v = [1, 2, 4, 5] >> w = [1; 2; 4; 5] Matrices (2D arrays) defined similarly >> A = [1,2,3;4,-5,6;5,-6,7]

Matrices and Vectors Matrix Multiplication Matrices are multiplied by using the * operator: Example: » c=A*B » c2=B*A » d=5*A matrix by a scalar

Matrices and Vectors Matrix Addition and Subtraction Example: » q=A-B q= The Inverse of a Matrix

Matrices and Vectors The Determinant of a Matrix Example: Solving Systems of Equations » det(B) ans = 0 In matrix notation, we write this system of equations as Ax =b where

Matrices and Vectors Solving Systems of Equations We now solve for x using A -1 *b. » x=inv(A)*b x =

41