Download as pdf or txt
Download as pdf or txt
You are on page 1of 17

Computational Method Course

CHEG 220
Week_2_Lec_2
Programming with MATLAB

Previously
in CHEG220
Mathematical description of an
engineering problem
Construction of numerical
algorithms
Programming

Outline

Designing and developing programs


Relational Operators and Logical Variables
Logical operators and Functions
Conditional Statements
Loops and Switch Structures
Application to programming

Designing and developing programs


(1)
Construct MATLAB programs to solve complex problems
Structure and manage the design of MATLAB programs

We need a general and efficient systematic approach

Designing and developing programs


(2)
Algorithms and control structures
An Algorithm is an ordered sequence of instructions that performs
desired task in a finite amount of time. An algorithm has the ability to
alter the order of its instructions using control structures.
Algorithmic operations can be sequential
instructions , or iterative instructions

instructions,

conditional

Designing and developing programs


(3)
Sequential operations
*Compute the perimeter p and the area A of a triangle
whose sides are a, b and c*
a=input (Enter the value of side a: );
b=input (Enter the value of side b: );
c=input (Enter the value of side c: );
p= a + b + c
A= sqrt((p/2)*(p/2 - a)* (p/2 b)*(p/2 c));
disp (The perimeter is:)
disp(p)
disp (The area is:)
disp(A)

Designing and developing programs


(4)
Conditional operations
*Compute the square root b if a real number a*
a=input (Enter the value of a: );
if a>= 0
b=sqrt(a);
else
b=sqrt(abs(a));
end
disp (The square root is:)
disp(b)

Designing and developing programs


(5)
Iterative operations
*Determine how many terms are required for the sum of the series
10k2-4k+2, k=1,2,3, to exceed 20,000. What is the sum for this many
terms?*
total=0;
k=0;
while total < 20000
k= k+ 1;
total=total+10*k2-4*k+2;
end
disp (The number of terms :)
disp (k)
disp (The sum is:)
disp (total)

Designing and developing programs


(6)
Structured Programming
It is a technique for designing programs in which a hierarchy of
modules is used.
In MATLAB these modules can be built-in or user-defined functions.
Structured programs are:
Easy to write, understand and maintain
Reusable codes that can be used for other applications
Easy to debug and their modules can be tested separately

Designing and developing programs


(7)
Top-down design

It is a method for creating structured programs. Its purpose is to describe the aim
of a program at a very high level initially and then partition it into more detailed
levels until the program structure become enough understood to be coded.
The process of top-down design consists of the following steps:
Define the problem mathematically
Specify the input
Specify the output
Use a simpler set of data to work out the solution steps by hand
Write a program
Run it
Compare the solution with your hand solution
Run the program with your input data

Designing and developing programs


(8)
An example of top-down design
(This example can be found in the presentation 2 of week 1)

Main Program
Input A,B
Output X
X=A-1*B

Matrix Inverse
give A
get A-1

Matrix Determinant
give A
get det (A)

Matrix Product
give A-1 and B
get X

Designing and developing programs


(9)
Good habits when writing a program include:
Proper selection of variable names and functions to reflect the
quantities they represent. Example: call the function inversing
matrices INV_MATRIX
Use of comments within the program. Example: here we
compute the matrix determinant
Divide your program to a main part and many functions or
modules to make it easy to write, understand and maintain

Relational Operators and Logical Variables (1)


MATLAB has 6 relational operators to make comparison between arrays:
<
Less than
<=
Less than or equal to
>
Greater than
>=
Greater than or equal to
==
Equal to
~=
Not equal to
The single = sign is the assignment, or replacement operator in MATLAB
The result of the comparison is 0 (false) or 1 (true)

Relational Operators and Logical Variables (2)


The results of comparison using the relational operators can be used as variable.
Example 1 : a=4 and b=7
typing z = (a<b) : z=1
typing z = (a==b) :
z=0
Example 2 : x[6,3,9], y[14,2,9]
typing z = x(x<y) :
z=6
Example 3 : (arithmetic operator have precedence over the relational operators.
Parentheses can be used to change the order of the precedence )
typing z =5>2+7 : z=0
typing z =(5>2) +7 : z=8

Relational Operators and Logical Variables (3)


The results of comparison using the relational operators can be used as variable.

Example 4 : (The relational operators have equal precedence among themselves. They
are evaluated in order from left to right )
z = 5>3 ~= 1
z = (5>3) ~= 1
z=0

Relational Operators and Logical Variables (4)


The logical class
z = (2==3) is a logical variable. It is a data type and MATLAB class
Logical variable such z may have only the value 1 (true) and 0 (false)

The logical function


B = logical (A) returns a logical array B. A is a numeric array
D = double (C) returns a numeric array D. C is a logical array

Questions
In three words, define a well written MATLAB program

You might also like