Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 2 Numerical Methods for Engineering MECN 3500 Department of Mechanical Engineering Inter American University of Puerto Rico Bayamon Campus Dr.

Similar presentations


Presentation on theme: "Lecture 2 Numerical Methods for Engineering MECN 3500 Department of Mechanical Engineering Inter American University of Puerto Rico Bayamon Campus Dr."— Presentation transcript:

1 Lecture 2 Numerical Methods for Engineering MECN 3500 Department of Mechanical Engineering Inter American University of Puerto Rico Bayamon Campus Dr. Omar E. Meza Castillo omeza@bayamon.inter.edu http://www.bc.inter.edu/facultad/omeza

2 Lecture 2 MECN 3500 Inter - Bayamon 2 Tentative Lectures Schedule TopicLecture Mathematical Modeling and Engineering Problem Solving 1 Introduction to Matlab 2 Numerical Error Root Finding System of Linear Equations Least Square Curve Fitting Polynomial Interpolation Numerical Integration Ordinary Differential Equations

3 Lecture 2 MECN 3500 Inter - Bayamon MATLAB Programming and Software 3

4 Lecture 2 MECN 3500 Inter - Bayamon  To introduce you to how the computer is used as a tool to obtain numerical solutions. Thermal Systems Design Universidad del Turabo 4 Course Objectives

5 Lecture 2 MECN 3500 Inter - Bayamon 4 Packages and Programming  Computer programs are merely a set of instructions that direct the computer to perform a certain task.  The programs consist of: Simple information representation (constants, variables, and type declarations). Simple information representation (constants, variables, and type declarations). Advanced information representation (data structure, arrays, and records). Advanced information representation (data structure, arrays, and records). Mathematical formulas (assignment, priority rules, and intrinsic functions). Mathematical formulas (assignment, priority rules, and intrinsic functions). Input/output. Input/output. Logical representation (sequence, selection, and repetition). Logical representation (sequence, selection, and repetition). Modular programming (functions and subroutines). Modular programming (functions and subroutines).

6 Lecture 2 MECN 3500 Inter - Bayamon 5 Structured Programming  Structured programming is a set of rules that prescribe good style habits for the programmer. Although structured programming is flexible enough to allow considerable creativity and personal expression, its rules impose enough constraints to render the resulting codes far superior to unstructured versions.  In particular, the finished product is more elegant and easier to understand.  A key idea behind structured programming is that any numerical algorithm can be composed using the three fundamental control structures: sequence, selection, and repetition.  To keep this description generic, we will employ flowcharts and pseudocode.

7 Lecture 2 MECN 3500 Inter - Bayamon 6 Flowchart  A flowchart is a visual or graphical representation of an algorithm

8 Lecture 2 MECN 3500 Inter - Bayamon 7 Pseudocode An alternative approach to express an algorithm that bridges the gap between flowcharts and computer code is called pseudocode. This technique uses code-like statements in place of the graphical symbols of the flowchart. We have adopted some style conventions for the pseudocode in this class. Keywords such as IF, DO, INPUT, etc., are capitalized, whereas the conditions, processing steps, and tasks are in lowercase. Additionally, the processing steps are indented. Thus the keywords form a “sandwich” around the steps to visually define the extent of each control structure. An alternative approach to express an algorithm that bridges the gap between flowcharts and computer code is called pseudocode. This technique uses code-like statements in place of the graphical symbols of the flowchart. We have adopted some style conventions for the pseudocode in this class. Keywords such as IF, DO, INPUT, etc., are capitalized, whereas the conditions, processing steps, and tasks are in lowercase. Additionally, the processing steps are indented. Thus the keywords form a “sandwich” around the steps to visually define the extent of each control structure.

9 Lecture 2 MECN 3500 Inter - Bayamon Logical Representation  Sequence: The sequence structure expresses the trivial idea that unless you direct it otherwise, the computer code is to be implemented one instruction at a time. As in Figure, the structure can be expressed generically as a flowchart or as pseudocode. 8 Figure 1

10 Lecture 2 MECN 3500 Inter - Bayamon Logical Representation  Selection: In contrast to the step-by-step sequence structure, selection provides a means to split the program’s flow into branches based on the outcome of a logical condition. Figure 2 and 3 show the two most fundamental ways for doing this.  The single-alternative decision, or IF/THEN structure (Figure 2), allows for a detour in the program flow if a logical condition is true. If it is false, nothing happens and the program moves directly to the next statement following the ENDIF. 9

11 Lecture 2 MECN 3500 Inter - Bayamon Logical Representation 10 Figure 2

12 Lecture 2 MECN 3500 Inter - Bayamon Logical Representation  The double-alternative decision, or IF/THEN/ELSE structure (Figure 3), behaves in the same manner for a true condition. However, if the condition is false, the program implements the code between the ELSE and the ENDIF. 11 Figure 3

13 Lecture 2 MECN 3500 Inter - Bayamon Logical Representation 12 Figure 4

14 Lecture 2 MECN 3500 Inter - Bayamon Logical Representation  The CASE structure is a variant on this type of decision making (Figure 5). Rather than testing individual conditions, the branching is based on the value of a single test expression. Depending on its value, different blocks of code will be implemented. In addition, an optional block can be implemented if the expression takes on none of the prescribed values (CASE ELSE). 13 Figure 5

15 Lecture 2 MECN 3500 Inter - Bayamon Logical Representation  Repetition: Repetition provides a means to implement instructions repeatedly. The resulting constructs, called loops, come in two “flavors” distinguished by how they are terminated.  The first and most fundamental type is called a decision loop because it terminates based on the result of a logical condition. Figure 6 shows the most generic type of decision loop, the DOEXIT construct, also called a break loop. This structure repeats until a logical condition is true. 14

16 Lecture 2 MECN 3500 Inter - Bayamon Logical Representation 15 Figure 6

17 Lecture 2 MECN 3500 Inter - Bayamon Logical Representation  The break loop in Figure 6 is called a logical loop because it terminates on a logical condition. In contrast, a count-controlled or DOFOR loop (Figure 7) performs a specified number of repetitions, or iterations 16 Figure 7

18 Lecture 2 MECN 3500 Inter - Bayamon 18

19 Lecture 2 MECN 3500 Inter - Bayamon Algorithm for Roots of a Quadratic 19 Example 2.1 Problem Statement: The roots of a quadratic equation. Problem Statement: The roots of a quadratic equation. Can be determined with the quadratic formula Develop an algorithm that does the following: Develop an algorithm that does the following:

20 Lecture 2 MECN 3500 Inter - Bayamon Algorithm for Roots of a Quadratic 20

21 Lecture 2 MECN 3500 Inter - Bayamon Algorithm for Roots of a Quadratic 21

22 Lecture 2 MECN 3500 Inter - Bayamon Homework2  www.bc.inter.edu/facultad/omeza www.bc.inter.edu/facultad/omeza Omar E. Meza Castillo Ph.D. 22


Download ppt "Lecture 2 Numerical Methods for Engineering MECN 3500 Department of Mechanical Engineering Inter American University of Puerto Rico Bayamon Campus Dr."

Similar presentations


Ads by Google