Matlab Programming for Engineers

Slides:



Advertisements
Similar presentations
CS107 Introduction to Computer Science Lecture 3, 4 An Introduction to Algorithms: Loops.
Advertisements

ITEC113 Algorithms and Programming Techniques
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
General Computer Science for Engineers CISC 106 Lecture 21 Dr. John Cavazos Computer and Information Sciences 04/10/2009.
Computer Science 1620 Loops.
CS107 Introduction to Computer Science Loops. Instructions Pseudocode Assign values to variables using basic arithmetic operations x = 3 y = x/10 z =
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
Branches and Loops Selim Aksoy Bilkent University Department of Computer Engineering
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Program Design and Development
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chapter 4 Loops and Character Manipulation Dr. Ali Can Takinacı İstanbul Technical University Faculty of Naval Architecture and Ocean Engineering İstanbul.
Lecture 12 Another loop for repetition The while loop construct © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Loops are MATLAB constructs that permit us to execute a sequence of statements more than once. There are two basic forms of loop constructs: i. while.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
CSCI 130 for Loops Chapter 7 - A. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence.
EPSII 59:006 Spring Topics Using TextPad If Statements Relational Operators Nested If Statements Else and Elseif Clauses Logical Functions For Loops.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. C How To Program - 4th edition Deitels Class 05 University.
Selection Programming EE 100. Outline introduction Relational and Logical Operators Flow Control Loops Update Processes.
Matlab Programming for Engineers Dr. Nidal Farhat Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
MATLAB FUNDAMENTALS: CONTROL STRUCTURES – LOOPS HP 100 – MATLAB Wednesday, 10/1/2014
For Loops 1 ENGR 1181 MATLAB 8. For Loops and Looped Programming in Real Life Looping within programs has long been a useful tool for completing mundane.
ENGR 1320 Final Review - Programming Major Topics: – Functions and Scripts – Vector and Matrix Operations in Matlab Dot product Cross product – Plotting.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Loops CS 103 February 19, 2007 Presented by Nate.
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Matlab Programming for Engineers Dr. Bashir NOURI Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data.
MATLAB for Engineers 4E, by Holly Moore. © 2014 Pearson Education, Inc., Upper Saddle River, NJ. All rights reserved. This material is protected by Copyright.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
Addison Wesley is an imprint of © 2010 Pearson Addison-Wesley. All rights reserved. Engineering Computation with MATLAB Second Edition by David M. Smith.
CSCI 171 Presentation 4. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
Introduction to Loops For Loops. Motivation for Using Loops So far, everything we’ve done in MATLAB, you could probably do by hand: Mathematical operations.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 5A Repetition (Concepts)
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
Conditional Loops CSIS 1595: Fundamentals of Programming and Problem Solving 1.
ENG College of Engineering Engineering Education Innovation Center 1 Basic For Loops in MATLAB Programming in MATLAB / Chapter 6 Topics Covered:
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 2 Wednesday 15 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Chapter 8: MuPAD Programming I Conditional Control and Loops MATLAB for Scientist and Engineers Using Symbolic Toolbox.
Digital Image Processing Lecture 6: Introduction to M- function Programming.
Repetition Statements (Loops) The do while Loop The last iteration structure in C++ is the do while loop. A do while loop repeats a statement or.
Introduction to Computer Sciences References: [1] Fortran 95/2003 for Scientists and Engineers (3e) by Stephen J. Chapman. [2] Using Fortran 90 by Chester.
EGR 115 Introduction to Computing for Engineers Loops and Vectorization – Part 1 Monday 13 Oct 2014 EGR 115 Introduction to Computing for Engineers.
Why Repetition? Read 8 real numbers and compute their average REAL X1, X2, X3, X4, X5, X6, X7, X8 REAL SUM, AVG READ *, X1, X2, X3, X4, X5, X6, X7, X8.
Finishing up Chapter 5. Will this code enter the if statement? G=[30,55,10] if G
Control Structures WHILE Statement Looping. S E Q C E N U E REPITITION …a step or sequence of steps that are repeated until some condition is satisfied.
CSE123 - Lecture 4 Structured Programming- Loops.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
Flow control. Conditionals if condition do this stuff end if condition do this stuff else do this stuff end if condition do this stuff elseif condition.
CS 170 – INTRO TO SCIENTIFIC AND ENGINEERING PROGRAMMING.
ECE Application Programming
Matlab Programming for Engineers
Matlab Training Session 4: Control, Flow and Functions
Chapter 4 MATLAB Programming
CS1371 Introduction to Computing for Engineers
Engineering Problem Solving with C++, Etter/Ingber
Loops CS140: Introduction to Computing 1 Savitch Chapter 4 Flow of Control: Loops 9/18/13 9/23/13.
Lecture 4 MATLAB programming (2)
MATLAB – Basic For Loops
Types of Flow of Control
Repetition Control Structure
Chapter 8: More on the Repetition Structure
Communication and Coding Theory Lab(CS491)
CMPT 102 Introduction to Scientific Computer Programming
Vectors and Matrices In MATLAB a vector can be defined as row vector or as a column vector. A vector of length n can be visualized as matrix of size 1xn.
Repetition Structures
REPETITION Why Repetition?
Presentation transcript:

Matlab Programming for Engineers Introduction to Matlab Matlab Basics Branching Statements Loops User Defined Functions Additional Data Types Input/Output Functions Simulink Toolbox Important Toolboxes (if time is available) Dr. Bashir NOURI

Logical Arrays and Vectorization The Matlab Profiler OBJECTIVES The while Loop The for Loop Logical Arrays and Vectorization The Matlab Profiler

THE while LOOP Structure: The Code Block is repeated until the expression is false. If it is false, the program executes the first statement after the end.

THE while LOOP Example: Write a program that calculates the average, and the standard deviation, for a set of values (X), entered by the user.

THE while LOOP Solution:

THE while LOOP Solution (modified):

THE while LOOP Solution (modified):

Repeats a block of statements (Body) specified number of times. THE for LOOP Structure: Repeats a block of statements (Body) specified number of times. Legal Examples: Matlab generates an array by evaluating this expression column by column.

THE for LOOP Legal Examples:

THE for LOOP Example (The Factorial Function): N! = N * (N-1) * (N-2) * … * 3 * 2 * 1

THE for LOOP Example (The Factorial Function): N! = N * (N-1) * (N-2) * … * 3 * 2 * 1

THE for LOOP Example, calculating the day of year:

THE for LOOP Example: Use THE for LOOP to calculate the average and standard deviation for any (n) values (modify the previous while program).

THE for LOOP Details of operation: Indent the bodies of loops (automatically done in Matlab). Don’t modify the loop index within the body of a loop. Preallocating arrays. recall: arr = 1:4; arr(7) = 8;  arr == [1 2 3 4 0 0 8]. (i.e. Matlab automatically extend/change the size of the array. It is better to preallocate the array before THE for LOOP to make the program much more faster.

Details of operation: (4. Vectorizing Arrays) THE for LOOP Details of operation: (4. Vectorizing Arrays) Faster

THE for LOOP The break and continue Statements: The break statement terminates the execution of a loop and passes control to the next statement after the end of the loop, example:

The break and continue Statements: THE for LOOP The break and continue Statements: The continue statement terminates the current step of the loop and return the control to the top of the loop, example: continue

THE for LOOP Nesting Loops: ii jj product Output

Different loop index variables THE for LOOP Nesting Loops: Different loop index variables ii jj product Output

THE for LOOP Nesting Loops: The break/continue statements they apply to the current loop, example:

Logical array and vectorization: THE for LOOP Logical array and vectorization: Double array Logical array

Logical array and vectorization: THE for LOOP Logical array and vectorization: Logical arrays can serve as a mask for arithmetic operations. The specified operation will be applied to the selected elements and not to the remaining elements, example: Will take the square root of all elements for which the logical array b is true and leave all the other elements in the array unchanged

Logical array and vectorization: Example 1: THE for LOOP Logical array and vectorization: Example 1: Calculate the sqrt of all the elements of array (a) > 5. Faster

THE for LOOP Logical array and vectorization: Example 2: Calculate the sqrt of all the elements of array (a) > 5, and the square of the remaining (unselected by the previous operation).

THE for LOOP The Matlab Profiler: Used to identify the parts of the program that consume most execution time.

THE for LOOP The Matlab Profiler:

THE for LOOP Example: (Projectile Motion) General equation: When the ball hits the ground

THE for LOOP Example: (Projectile Motion) General equation: When the ball hits the ground

THE for LOOP Example: (Projectile Motion) General equation: When the ball hits the ground

THE for LOOP Example: (Commands and Functions)

Home Work Solve the following problems: 4.[16, 19, 21]