0% found this document useful (0 votes)
8 views35 pages

Week1.1 - Introduction, Programming, and MATLAB

The document outlines the syllabus for ENGR 3202, focusing on engineering analysis and computation using MATLAB. It covers topics such as structured and modular programming, flowcharting, and logical representation of algorithms. The course emphasizes the importance of programming principles and the use of MATLAB for engineering-oriented numerical calculations.

Uploaded by

youssefsamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views35 pages

Week1.1 - Introduction, Programming, and MATLAB

The document outlines the syllabus for ENGR 3202, focusing on engineering analysis and computation using MATLAB. It covers topics such as structured and modular programming, flowcharting, and logical representation of algorithms. The course emphasizes the importance of programming principles and the use of MATLAB for engineering-oriented numerical calculations.

Uploaded by

youssefsamer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

ENGR 3202

Engineering Analysis and


Computation
Week 1

Introduction,
Programming, and
MATLAB

Abdelaziz Khlaifat
Professor & Chair
Petroleum & Energy Engineering Department
SSE, AUC
Email: [Link]@[Link]
Room: SSE 1006

11
ENGR 3202
Content (Topics / LOs)

• Introduction and Preliminaries (Syllabus)


• Packages and programming (2.1)
• Structured programming (2.2)
• Modular programming (2.3)
• MATLAB (2.5, Appendix B (page 933))

2
ENGR 3202
Introduction and Preliminaries

• Course Syllabus

3
ENGR 3202
2.1. PACKAGES AND PROGRAMMING

Two types of software users:


• USERS - Those who limit themselves to the capabilities found in the
software’s standard mode of operation.
• PROGRAMMERS - Those who go beyond the standard capability of
the tool using one of the two alternatives:
– First, you can look for a different package and see if it is capable of solving the
problem (e.g. Excel VBA, MATLAB, Python, C, C++, Visual Basic, Java,
JavaScript, Fortran 77, Fortran 90, etc.)
– Second, become a “power user” by learning to write MATLAB M-files that allow
you to extend the capabilities of MATLAB.
• How this can be done?
– Course Prerequisites:
– CSCE 1001 (or MENG 2202 or ENGR 2412) and MACT 2141

4
ENGR 3202
2.1. PACKAGES AND PROGRAMMING
2.1.1 Computer Programs
• Computer programs are simply a set of instructions that direct the
computer to perform a certain task.
• Engineers who require high level of programming capabilities need to
perform engineering-oriented numerical calculations.
• Programming topics - covered in prerequisites
– Simple information representation (constants, variables, and type declarations).
– Advanced information representation (data structure, arrays, and records).
– Mathematical formulas (assignment, priority rules, and intrinsic functions).
– Input/output.
– Logical representation (sequence, selection, and repetition).
– Modular programming (functions and subroutines).

5
ENGR 3202
2.1. PACKAGES AND PROGRAMMING
2.1.1 Computer Programs
• Logical representation is the single area that most influences an
algorithm’s coherence and understandability.
• Modular programming contributes greatly to a program’s
organization. In addition, modules provide a means to archive useful
algorithms in a convenient format for subsequent applications.

6
ENGR 3202
2.2 STRUCTURED PROGRAMMING

• Before writing a program:


– Have a thorough understanding of the problem
– Carefully plan an approach for solving it

• While writing a program:


– Know what “building blocks” are available
– Use good programming principles

7
ENGR 3202
2.2 STRUCTURED PROGRAMMING

Flowcharting
• Flowcharting is a method of designing a solution to a problem by using
symbols which indicate actions to be performed.
• It is customary to use geometric figures of certain shapes to indicate
the various kinds of acts.
• In a flowchart these figures are diagrammed with connecting arrows,
indicating a certain sequence for performing the acts in order to solve a
problem.

8
ENGR 3202
2.2 STRUCTURED PROGRAMMING

Algorithm
• An algorithm is a well-defined sequence of acts or procedures to
perform for the solution of a problem in a finite number of steps.
• Therefore, a flowchart is an excellent vehicle to represent an
algorithm.

9
ENGR 3202
2.2 STRUCTURED PROGRAMMING

Flowcharting Symbols

10
ENGR 3202
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation

All programs written in terms of 3 control structures:


• Sequence structures: Programs executed sequentially by
default (step-by-step sequence structure)

• Selection structures: selection provides a means to split


the program’s flow into branches based on the outcome of
a logical condition.
• Repetition structures: implement instructions repeatedly

11
ENGR 3202
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation
Control structures:
• Single-entry/single-exit control structures
– Connect exit point of one control structure to entry point of the next
(control-structure stacking)
– Makes programs easy to build

12
ENGR 3202
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation
Control structures – Sequence
• Each action is in a processing symbol by itself.
• The actions are performed in the sequence (following the
arrows)
• Example: What do you do in the morning?
– Wash face
– Brush teeth
– Take shower
– Comb hair
– Smile in mirror
– Having breakfast

13
ENGR 3202
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation
Control structures – Sequence

14
ENGR 3202
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation
Control structures – Selection
• Selection provides a means to split the program’s flow into
branches based on the outcome of a logical condition.

• When the next statement executed is not the next one in


sequence

• Figure 2.3 shows the two most fundamental ways for


selection control structure.

15
ENGR 3202
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation
Control structures – Selection

IF/THEN

IF/THEN/ELSE

16
ENGR 3202
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation
Control structures – Selection
• Although the IF/THEN and the IF/THEN/ELSE structures are sufficient
to construct any numerical algorithm, two other variants are commonly
used.

• Suppose that the ELSE clause of an IF/THEN/ELSE contains another


IF/THEN. For such cases, the ELSE and the IF can be combined in the
IF/THEN/ELSEIF structure shown in Fig. 2.4a.

17
ENGR 3202
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation
Control structures – Selection
Notice how in Fig. 2.4a there
is a chain or “cascade” of
decisions:
• The first one is the IF
statement, and each
successive decision is an
ELSEIF statement.
• Going down the chain, the
first condition
encountered that tests
true will cause a branch to
its corresponding code
block followed by an exit
of the structure.
• At the end of the chain of
conditions, if all the
conditions have tested
false, an optional ELSE
block can be included.

18
ENGR 3202
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation
Control structures – Selection
• The CASE structure is a variant on this type of decision making (Fig.
2.4b).

• 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).

19
ENGR 3202
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation
Control structures – Selection

20
ENGR 3202
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation
Control structures – Selection

21
ENGR 3202
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation
Control structures – 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 2.5 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.

22
ENGR 3202
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation
Control structures – Repetition

It is not necessary to have two blocks


in this structure.
• If the first block is not included, the
structure is sometimes called a
pretest loop because the logical
test is performed before anything
occurs.
• Alternatively, if the second block is
omitted, it is called a posttest loop.
• Because both blocks are included,
the general case in Fig. 2.5 is
sometimes called a midtest loop.
23
ENGR 3202 DOEXIT - Fortran 90 Excel VBA
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation
Control structures – Repetition
• This course algorithms are directly implemented in MATLAB, therefore
the break loop can be simulated with the WHILE structure as shown
later in this topic (see Sec. 2.5).

• The break loop in Fig. 2.5 is called a logical loop because it terminates
on a logical condition.

• In contrast, a count-controlled or DOFOR loop (Fig. 2.6) performs a


specified number of repetitions, or iterations.

24
ENGR 3202
2.2 STRUCTURED PROGRAMMING
2.2.1 Logical Representation
The count-controlled loop
Control structures – Repetition
works as follows.
• The index (i) is a variable
that is set at an initial value
of start.
• The program then tests
whether the index is less
than or equal to the final
value, finish. If so, it executes
the body of the loop, and
then cycles back to the DO
statement.
• When the index is greater
than the final value (finish),
the computer automatically

MATLAB
exits the loop and transfers
control to the line following
the ENDDO statement
25
ENGR 3202
2.2 STRUCTURED PROGRAMMING

26
ENGR 3202
2.2 STRUCTURED PROGRAMMING

27
ENGR 3202
2.2 STRUCTURED PROGRAMMING

How the program works:


1. The user is prompted to enter the coefficients a, b, and c of the quadratic
equation .

2. The discriminant D=b2−4ac is calculated.

3. Based on the value of D:

 If D>0, there are two real roots.

 If D=0, there is one real root (a repeated root).

 If D<0, there are two complex conjugate roots.

Run the program by entering the coefficients, and it will display the roots
accordingly.
28
ENGR 3202
2.2 STRUCTURED PROGRAMMING

% MATLAB Program to find the roots of a quadratic equation by AK

% Input coefficients
a = input('Enter the coefficient a: ');
b = input('Enter the coefficient b: ');
c = input('Enter the coefficient c: ');

% Calculate the discriminant


D = b^2 - 4*a*c;

% Check if the discriminant is positive, zero, or negative


if D > 0
% Two real roots
root1 = (-b + sqrt(D)) / (2*a);
root2 = (-b - sqrt(D)) / (2*a);
fprintf('The roots are real and different: %.2f and %.2f\n', root1, root2);
elseif D == 0
% One real root
root = -b / (2*a);
fprintf('The root is real and the same: %.2f\n', root);
else
% Complex roots
realPart = -b / (2*a);
imagPart = sqrt(-D) / (2*a);
fprintf('The roots are complex: %.2f + %.2fi and %.2f - %.2fi\n', realPart, imagPart, realPart, imagPart);
end

29
ENGR 3202
2.2 STRUCTURED PROGRAMMING

30
ENGR 3202
2.3 MODULAR PROGRAMMING

• Modular programming contributes greatly to a program’s


organization. In addition, modules provide a means to archive useful
algorithms in a convenient format for subsequent applications.
• Computer programs can be divided into small subprograms, or
modules, that can be developed and tested separately. This approac is
called modular programming.
• The most important attribute of modules:
– They be as independent and self-contained as possible.

– They are typically designed to perform a specific, well-defined function and have
one entry and one exit point.

– They are usually short (generally 50 to 100 instructions in length) and highly
focused.

31
ENGR 3202
2.3 MODULAR PROGRAMMING

• Two types of procedures are commonly employed high-level


languages such as Fortran 90 or C: functions and subroutines:
– Function returns a single result,

– Subroutine returns several.

• Excel macros and MATLAB functions are designed to receive some


information, perform a calculation, and return results.

32
ENGR 3202
2.3 MODULAR PROGRAMMING

Modular programming advantages:


• The use of small, self-contained units makes the underlying logic
easier to devise and to understand for both the developer and the user.
• Development is facilitated because each module can be perfected in
isolation (good for large projects).
• Modular design also increases the ease with which a program can be
debugged and tested because errors can be more easily isolated.
• Program maintenance and modification are simplified. Maintain your
own library of useful modules for later use in other programs.

33
ENGR 3202
2.3 MODULAR PROGRAMMING

• MATLAB subprograms are called functions:


– User-defined function
– Built-in-Functions
– Creating a function file
– Structure of a function file
– Logical and global variables
– Saving a function file
– Using a user-defined function
– Script files versus function files
– Anonymous functions

34
ENGR 3202
Questions

THANK You 

ENGR 3202 35

You might also like