0% found this document useful (0 votes)
15 views13 pages

수치해석 Supporting File Lecture2

Uploaded by

wjdalscks0204
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)
15 views13 pages

수치해석 Supporting File Lecture2

Uploaded by

wjdalscks0204
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
You are on page 1/ 13

Lecture 2.

Programming and Software

Computer program: a set of instructions that direct the computer to


perform a certain task
Structured Programming
• Well-organized, well-structured algorithm:
easy debug, test, and update → Good style habits for the programmer
• The three fundamental control structures:
Sequence, Selection, Repetition
Flow chart: Visual or graphical representation of an algorithm
Useful for expressing and communicating algorithms
Useful in planning, unraveling, or communicating the logic of
your own or other program excellent pedagogical tools
Pseudocode: Code-like statements
IF, THEN, ELSE, ENDIF, FOR, WHILE, INPUT, etc.
Easy programming, modification of codes,
sharing with others

Numerical Methods for Chemical Engineers


Flow chart: a visual or graphical representation of an algorithm
a series of block, arrows, etc.

Numerical Methods for Chemical Engineers


Sequence: Computer code is to be implemented in one instruction
at a time

Numerical Methods for Chemical Engineers


Selection(I): Split of the program’s flow into branches based on the
outcome of a logical condition

Single-alternative structure
(IF / END)

IF condition
True block;
END Double-alternative structure
(IF / ELSE / END)

IF condition
True block;
ELSE
False block;
END

Numerical Methods for Chemical Engineers


MATLAB relational operator:

== : equal to (a == 0)
~= : not equal to (b ~= 1)
< : less than (c < 2)
> : greater than (d > 3)
<= : less than or equal to (e <= 4)
>= : greater than or equal to (f >= 5)

Numerical Methods for Chemical Engineers


IF / END IF / ELSE / END

Numerical Methods for Chemical Engineers


Selection(II):
Multialternative structure
(IF / ELSEIF / ELSE / END)
IF condition1
Block1;
ELSEIF condition2
Block2;
ELSEIF condition3 Case structure
Block3; (SWITCH / CASE )
ELSE
Block4;
END

SWITCH Test Expression


CASE Value1,
Block1
CASE Value2,
Block2
CASE Value3,
Block3
CASE Value4,
Block4
OTHERWISE,
Statement
END

Numerical Methods for Chemical Engineers


IF / ELSEIF / ELSE / END SWITCH / CASE / END

Numerical Methods for Chemical Engineers


Repetition: iteration of instructions

WHILE loop

WHILE (condition)
Block2
END
The count-controlled or
False
FOR construct
True

FOR i= start, step, finish


Block
END

Numerical Methods for Chemical Engineers


WHILE FOR

Numerical Methods for Chemical Engineers


Example(1): Roots of a quadratic equation
ax2 + bx + c = 0
Input a,b,c
Initialize r1,r2,i1,i2 Pseudocode:
INPUT a, b, c
r1 = 0: r2=0: i1=0: i2=0
a=0 ? IF a=0 THEN
Yes IF b  0 THEN
r1= -c / b
No
b0? ELSE
discr = b*b-4*a*c No DISPLAY “Trivial Solution”
ENDIF
Yes
Trivial Soln. ELSE
r1= -c/b discr= b*b – 4*a*c
discr  0 ? IF discr  0 THEN
No r1= (- b + SQRT(discr)) / (2*a)
Yes r2=(- b - SQRT(discr)) / (2*a)
r1=-b/(2a) ELSE
r2=r1 r1=(-b+SQRT(discr)/(2a) r1=-b / (2*a)
i1=SQRT(ABS(discr)/(2a) r2=(-b-SQRT(discr)/(2a) r2= r1
i2=-i1 i1 = SQRT(ABS(discr)) / (2*a)
i2 = -i1
ENDIF
Output
ENDIF
r1,r2,i1,i2 DISPLAY r1,r2,i1,i2

Numerical Methods for Chemical Engineers


Exercise 1
1 2 1 3 1 4
exp(x)  1 + x + x + x + x +
2! 3! 4!

x=1에서 지수함수와 Taylor series (10 terms,


20terms)에 의한 값을 비교하시오.

Exercise 2

f(x) = sin(10x) + cos(3x)

x의 범위가 0~5 일 때 위 함수의 개형을 도시하


고 f(x)=0이 되게 하는 근이 몇 개인지 찾으시오.
(Data 저장 방법 ?)

Numerical Methods for Chemical Engineers


Numerical Methods for Chemical Engineers

You might also like