Introduction to Programming for Mechanical Engineers (ME 319)

Slides:



Advertisements
Similar presentations
Multidimensional Array
Advertisements

Introduction to MATLAB The language of Technical Computing.
COMP 116: Introduction to Scientific Programming Lecture 37: Final Review.
Applied Informatics Štefan BEREŽNÝ
Introduction to MATLAB for Biomedical Engineering BME 1008 Introduction to Biomedical Engineering FIU, Spring 2015 Lesson 2: Element-wise vs. matrix operations.
CIS 101: Computer Programming and Problem Solving Lecture 2 Usman Roshan Department of Computer Science NJIT.
EGR 106 – Week 2 – Arrays Definition, size, and terminology Construction methods Addressing and sub-arrays Some useful functions for arrays Character arrays.
Lecture 2 MATLAB fundamentals Variables, Naming Rules, Arrays (numbers, scalars, vectors, matrices), Arithmetical Operations, Defining and manipulating.
EGR 106 – Week 3 – More on Arrays Brief review of last week Additional ideas: – Special arrays – Changing an array – Some array operators – Character arrays.
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.
CE 311 K - Introduction to Computer Methods Daene C. McKinney
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.
Chapter 10 Review: Matrix Algebra
CSE123 Lecture 5 Arrays and Array Operations. Definitions Scalars: Variables that represent single numbers. Note that complex numbers are also scalars,
Introduction to MATLAB CBE 502 Mathematical Methods of Engineering Analysis.
Row 1 Row 2 Row 3 Row m Column 1Column 2Column 3 Column 4.
ECE 1304 Introduction to Electrical and Computer Engineering Section 1.1 Introduction to MATLAB.
MATLAB An Introduction to MATLAB (Matrix Laboratory) 1.
Introduction to Engineering MATLAB – 6 Script Files - 1 Agenda Script files.
Lecture 28: Mathematical Insight and Engineering.
Matrix Arithmetic. A matrix M is an array of cell entries (m row,column ) and it must have rectangular dimensions (Rows x Columns). Example: 3x x.
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.
A string is an array of characters Strings have many uses in MATLAB Display text output Specify formatting for plots Input arguments for some functions.
Chapter 2 Creating Arrays Legend: MATLAB command or syntax : Blue MATLAB command OUTPUT : LIGHT BLUE.
(The Transpose Operator) 1 >> C=[ ; ; ] C = >> D=C' D =
Matlab Basic. MATLAB Product Family 2 3 Entering & Quitting MATLAB To enter MATLAB double click on the MATLAB icon. To Leave MATLAB Simply type quit.
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.
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
CMPS 1371 Introduction to Computing for Engineers VECTORS.
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.
Project Teams Project on Diffusion will be a TEAM project. 34 Students in class so 6 teams of 5 and one Team of 4. Determine members of team and a team.
LAB 2 Vectors and Matrices Dr.Abdel Fattah FARES.
Elementary Matrix Theory
ECE 1304 Introduction to Electrical and Computer Engineering
응용 전산 및 실습 MATLAB – Chapter 3 행렬연산
Introduction to Programming for Mechanical Engineers (ME 319)
Chapter 2 Creating Arrays.
Unit 1: Matrices Day 1 Aug. 7th, 2012.
L – Modeling and Simulating Social Systems with MATLAB
Chapter 3 Arrays and Vectors
EGR 115 Introduction to Computing for Engineers
Introduction to Programming for Mechanical Engineers (ME 319)
Introduction to Programming for Mechanical Engineers
Cell Arrays Definition Creating Cell Arrays Referencing Cell Arrays
2) Platform independent 3) Predefined functions
Matrix Operations SpringSemester 2017.
Matrices and Arrays.
INTRODUCTION TO BASIC MATLAB
MATLAB DENC 2533 ECADD LAB 9.
Matlab Workshop 9/22/2018.
MATH 493 Introduction to MATLAB
Use of Mathematics using Technology (Maltlab)
Vectors and Matrices I.
Introduction to MATLAB [Vectors and Matrices] Lab 2
Unit 3: Matrices
Matrices and Matrix Operations
Matlab Basic Dr. Imtiaz Hussain
Using Script Files and Managing Data
Announcements P3 due today
Matlab Training Session 2: Matrix Operations and Relational Operators
1.8 Matrices.
Matrix Operations SpringSemester 2017.
1.8 Matrices.
Introduction to Matrices
Presentation transcript:

Introduction to Programming for Mechanical Engineers (ME 319) Lecture 2

Quote of the day “He who has overcome his fears will truly be free.” Aristotle (384 BC – 322 BC)

Quick review of Lecture 1 Work on the Command window by creating variables and performing simple mathematical expressions. Assign proper variables and files names. Assigning a value to a variable. Writing your commands inside a script file. Properly comment words or any other text. Run the script file.

Order of Precedence A scalar variable is a variable that contains a single number. (1×1 matrix) MATLAB uses the symbols + - * / ^ for addition, subtraction, multiplication, right division and exponentiation respectively. \ is used for left division in which denominator comes first. Example: 2\7 = 7/2 = 3.5

Scalar arithmetic operations

Creating Arrays and vectors Command Syntax Description Example The simplest way to create simple vectors, not convenient for complex vectors line = [1 3 5] line = 1 3 5 Where xi = the initial element in the array. xf = the destination element in the array. d = the step size (Default value=1) xi could be bigger than xf, but d should be –ve in this case line = 1:0.4:5 Columns 1 through 9 1.00 1.40 1.80 2.20 2.60 3.00 3.40 3.8 4.20 Columns 10 through 11 4.60 5.00 n = the number of elements in between If n is not provided, the default value is 100 line = linspace (1,5,11) 1.00 1.40 1.80 2.20 2.60 3.00 3.40 3.80 4.20 4.6000 5.0000

Creating Arrays and vectors (contd..) Creates zeros matrix of dimensions i x j zr = zeros(2,2) zr = 0 0 Creates ones matrix of dimensions k x l on = ones(2,3) on = 1 1 1 Creates an identity matrix of dimensions m x m idn = eye(2) idn = 1 0 0 1

Creating Arrays and vectors All variables in MATLAB are arrays. A scalar variable is an array of single element. The array variable is defined by the input when it is assigned. Therefore there is no need to define the size of an array, since it will take the size of the input automatically. You can still perform any operation on any array to change its name, contents, dimensions, or type. You can fill in an array with scalar values, predefined variables, or expressions.

Examples >> A = 5; B = power (A,3); % define two variables A and B >> C = [A, 27, sqrt (B), B - A^2] % define an array C of 4 elements C = 5.0000 27.0000 11.1803 100.0000 >> C(4) = 99 % assign a different value for element 4 5.0000 27.0000 11.1803 99.0000 >> C(6) = 4 % add two elements to the existing C array, the 5th is zero by default 5.0000 27.0000 11.1803 99.0000 0 4.0000 >> C (2:5) % chose elements 2, 3, 4 and 5 ans = 27.0000 11.1803 99.0000 0 >> C (1:2:5) % chose elements 1, 3 and 5 5.0000 11.1803 0 In C(4) and C(6), ‘4’ and ‘6’ are the array indices of matrix ‘C’.

Creating Arrays and vectors (Examples continued) >> D = [linspace(A,B,5); 100:-20:20; % define a matrix D of 5 elements between A and B 1 2 3 4 5] % and another 5 elements between 100 and 20 D = % and another 5 elements 1 2 3 4 5 5 35 65 95 125 100 80 60 40 20 1 2 3 4 5 >> E = [D(2:3,2:4)] % define E that is a submatrix of D E = 80 60 40 2 3 4 >> F = [C(1:3);E(1,:)] % define matrix F that’s a combination of the 1st three elements of C F = % as its 1st row, and the complete 1st row of E as its 2nd row 5.0000 27.0000 11.1803 80.0000 60.0000 40.0000 >> F(2,2) = F(2,1) - F(2,3) + 10*E(2,3) % change element (2,2) of matrix F using the expression to the right F = 80.0000 80.0000 40.0000

Creating Arrays and vectors (Examples continued) >> G = F‘ % define matrix G as the transpose of F G = 5.0000 80.0000 27.0000 80.0000 11.1803 40.0000 >> G (1,:) % Choose the 1st row in matrix G ans = 5 80 >> H = G(1:2,:) % define matrix H as the complete 1st two rows of G H = 27 80 >> I = inv (H) % define matrix I as the inverse of H I = -0.0455 0.0455 0.0153 -0.0028 >> J = eig (I) % define matrix J as the eigen values of the square matrix I J = -0.0581 0.0098

Creating Arrays and vectors Examples (continue): >> J (:,2:3) = [1 2;3 4] % add complete 2nd and 3rd columns to the existing matrix J J = -0.0581 1.0000 2.0000 0.0098 3.0000 4.0000 >> K = [J F] % define matrix K as the combination of J and F K = % setting next to each others -0.0581 1.0000 2.0000 5.0000 27.0000 11.1803 0.0098 3.0000 4.0000 80.0000 80.0000 40.0000 >> L = [F ; J] % define matrix L as the combination of J and F L = % setting over each others 5.0000 27.0000 11.1803 80.0000 80.0000 40.0000 -0.0581 1.0000 2.0000 0.0098 3.0000 4.0000 >> L (1:3,:) = [ ] % delete the complete 1st three rows of L by replacing the L = % rows with the empty matrix notation [ ] >> L (1) = [ ] % delete the 1st element of L by replacing its content with L = % the empty matrix notation [ ] 3 4

Built-in functions for handling arrays Examples (continue): >> length_J = length (J) % check the maximum number of elements in a row or a column length_J = 3 >> [row_J , column_J] = size (J)% return the size of J row_J = 2 column_J = >>M = rand (3) % create a random matrix M of dimensions 3x3 M = 0.9218 0.4057 0.4103 0.7382 0.9355 0.8936 0.1763 0.9169 0.0579 >> N = diag(M) % create a vector of diagonal elements in M N = 0.9218 0.9355 0.0579 >> O = [6 -12 20] O = 6 -12 20 >> P = diag (O) % create a matrix of diagonal P = % elements in O 6 0 0 0 -12 0 0 0 20

Creating Strings and Strings as variables Examples (continue): SYNTAX: >> student_name = 'Robert Smith' student_name = Robert Smith >> student_name (8) ans = S >> student_info = char ('student info=’ , student_name , ‘Grade’, 'B+') student_info = student name Grade B+

Thank you for the attention Any Questions and Suggestions please…