numerical computing - burapha universitykrisana/886307/handout/00-intro.… · general introduction...
TRANSCRIPT
Numerical ComputingK. Chinnasarn
[email protected]://www.facebook.com/groups/student.k/
Before Class• Turn OFF your cell phone(s)• Open your Notebook, Pen or Pencil
for writing• Then, open your mind for discussion• Listen, Think, Ask, Write
15/1/17 Informatics@BUU 2
Expected Learning Outcome1. บณฑตมความสามารถในการประยกตความรทางดานคอมพวเตอรและคณตศาสตรไดอยาง
เหมาะสมกบผลการเรยนรทคาดหวงอยางถกหลกวชา
2. บณฑตมความสามารถในการวเคราะหปญหาและเลอกวธการแกปญหาเพอใหไดคำตอบทเหมาะสม
3. บณฑตมความสามารถในการออกแบบ ตดตง และประเมน ระบบคอมพวเตอร กระบวนการ องคประกอบ หรอโปรแกรม ทตอบสนองตอความตองการ
4. บณฑตมความสามารถในการทำงานรวมกบผอนเพอบรรลเปาประสงคของการทำงานรวมกน
5. บณฑตมความเขาใจในวชาชพ จรยธรรม กฎหมาย ความปลอดภยและประเดนทางสงคม และหนาทความรบผดชอบของตนเอง
6. บณฑตมความสามารถในการสอสารกบผคนทหลากหลายไดอยางมประสทธภาพ
7. บณฑตมความสามารถในการใชเทคนคใหมๆ และเครองมอทจำเปนสำหรบการสรางตวแบบการคำนวณตางๆ
15/1/17 Informatics@BUU 3
พอคาหยกการเรยนในมหาวทยาลย
Numerical C => 3 (2-2-5)Mathematics => 3 (3-0-6)
15/1/17 Informatics@BUU 4
Why we have to learn:• English?• Mathematics?• Calculus?• Programming
15/1/17 Informatics@BUU 5
What is Computer Science?• Computer science is a discipline that
spans theory and practice. – It requires thinking both in abstract
terms and in concrete terms. – The practical side of computing can be
seen everywhere. • Computer science also has strong
connections to other disciplines.Source: https://www.cs.mtu.edu/~john/whatiscs.html
15/1/17 Informatics@BUU 6
What is Computer Science?• Computer Science is practiced by
mathematicians, scientists and engineers. – Mathematics, the origins of Computer
Science, provides reason and logic. – Science provides the methodology for
learning and refinement. – Engineering provides the techniques for
building hardware and software.Source: https://www.cs.mtu.edu/~john/whatiscs.html
15/1/17 Informatics@BUU 7
Numerical Computing/Analysis
• is the study of algorithms that use numerical approximation (as opposed to general symbolic manipulations) for the problems of mathematical analysis.
15/1/17 Informatics@BUU 8
Numerical Computing: Cont• Numerical Methods are techniques by
which mathematical problems are formulated so that they can be solved with arithmetic and logic operations. (Steven C. Chapra)
15/1/17 Informatics@BUU 9
Informatics@BUU10
Scenario I: Resolution Enhancement
Low-Res.
High-Res.
15/1/17
General introductionThe overall goal of the field of numerical analysis is the design
and analysis of techniques to give approximate but accurate solutions to hard problems, the variety of which is suggested by the following.
• Advanced numerical methods are essential in making numerical weather prediction feasible.
• Computing the trajectory of a spacecraft requires the accurate numerical solution of a system of ordinary differential equations.
• Car companies can improve the crash safety of their vehicles by using computer simulations of car crashes. Such simulations essentially consist of solving partial differential equations numerically.
• Airlines use sophisticated optimization algorithms to decide ticket prices, airplane and crew assignments and fuel needs. Historically, such algorithms were developed within the overlapping field of operations research.
• Insurance companies use numerical programs for actuarial analysis.15/1/17 Informatics@BUU 11
Areas of study• Computing values of functions• Interpolation, extrapolation, and
regression• Solving equations and systems of
equations• Solving eigenvalue or singular value
problems• Optimization• Evaluating integrals• Differential equations
15/1/17 Informatics@BUU 12
Gradingผลการเรยนร* วธการประเมน สปดาหทประเมน
สดสวนของการ
ประเมนผล
1.2 การเขาชนเรยน การตรงตอเวลาตลอดภาคการศกษา
5%
4.4, 5.2
การมสวนรวมในชนเรยน ซงตองแสดงถง ความรบผดชอบในการกระทำของตนเองและรบผดชอบงานในกลม ตลอดถงการรวมแสดงความคดเหนในประเดนตาง ๆ โดยแสดงผลขอสรปในรปแบบของคาตวเลขหรอคาทางสถต (Mindmup)
ตลอดภาคการศกษา
5%
2.1-2, 2.8, 3.1, 3.3 Assignment/งานกลม 4 - 5 คนตลอดภาคการศกษา
25%
2.1-2, 2.8, 3.1, 3.3 สอบทฤษฎกลางภาค 9 30%2.1-2, 2.8, 3.1, 3.3 สอบทฤษฎปลายภาค 18 35%
เชคช&อเขาเรยนทกคร3 ง / สาย 3 คร3 ง เทากบขาด 1 คร3 ง / ขาดเกน 3 คร3 ง หมดสทธ= สอบ
15/1/17 Informatics@BUU 13
เกณฑการวดผลการเรยนร
• 0 – 49 => F• 50 – 54 => D• 55 – 59 => D+• 60 – 64 => C• 65 – 69 => C+• 70 – 74 => B• 75 – 79 => B+• 80 – 100 => A
15/1/17 Informatics@BUU 14
Scientific Methods
Source: Gordana DODIG- CRNKOVIC, Scientific Methods in Computer Science, Department of Computer Science Mälardalen University, Västerås, Sweden
15/1/17 Informatics@BUU 15
What is Computer Science?• The discipline of Computing is the
systematic study of algorithmic processes that describe and transform information: their theory, analysis, design, efficiency, implementation, and application
• Computer Science is the study of phenomena related to computers
• Computer Science is the study of information structures
15/1/17 Informatics@BUU 16
What is Computer Science?• Computer Science is the study and
management of complexity• Computer Science is the mechanization of
abstraction• Computer Science is a field of study that
is concerned with theoretical and applied disciplines in the development and use of computers for information storage and processing, mathematics, logic, science, and many other areas
15/1/17 Informatics@BUU 17
Scientific Methods of Computer Science• Real-world phenomena understanding• Mathematical Modelling• Theoretical Computer Science• Experimental Computer Science• Computer Simulation
15/1/17 Informatics@BUU 18
310201Fundamental Programming
K.Chinnasarn, Ph.D. [email protected]
Informatics@BUU 20
What is programming?• Given a problem:
– Find an algorithm to solve a problem.
– Express that algorithm in a way that the computer can execute it.
15/1/17
Informatics@BUU 21
Algorithms• In simple terms, an algorithm is a sequence
of instructions to solve a problem, such that:
– Each instruction is unambiguous, and is something the computer can do.
– After an instruction is finished, there is no ambiguity about which instruction is to be executed next.
– Execution finishes in a finite number of steps.– The description of the algorithm is finite.
• Think of the computer as a meticulous moron.
15/1/17
Informatics@BUU 22
Computers• computers are data
processing devices• main input device:
keyboard• main output device:
display• processor performs
operations on input data to produce output data
Display
Keyboard
MemoryDisk
Processor
15/1/17
Computers: Von Neumann machine
Informatics@BUU 23
Input
Keyboard
Memory DiskProcessor
Disk
Process Output
Display
15/1/17
24
ความสมพนธขององคประกอบระบบคอมพวเตอร
Software
Control
Control
Inst
ruct
ion
Set
Data Information
15/1/17 Informatics@BUU
25
ประเภทของชดคาส&งในภาษาโปรแกรม• คาส&งรบขอมล (Input)• คาส&งแสดงผลขอมล (Output)• คาส&งประมวลผลขอมล หรอ การกาหนดคาตวแปร
(Assignment statement)• คาส&งเปรยบเทยบคา (Condition or Selection)• คาส&งกระทาซา (Iteration or Looping)
15/1/17 Informatics@BUU
26
ตวอยางลาดบของการเขยนโปรแกรม
โปรแกรม run01.c โปรแกรม run02.c
main() {
int I=0;while (I<3) {
printf(“\nRunning No. %d”,I);I++;
}}
main() {
int I=0;while (I<3) {
I++;printf(“\nRunning No. %d”,I);
}}
ผลลพธโปรแกรม run01.c ผลลพธโปรแกรม run02.c
Running No. 0Running No. 1Running No. 2
Running No. 1Running No. 2Running No. 3
15/1/17 Informatics@BUU
27
การออกแบบข3นตอนวธสาหรบการเขยน
โปรแกรม (Algorithms)• การเขยนข3นตอนวธแบบมาตรฐาน
• การเขยนชดคาส&งเทยม (Pseudo Code)
• การเขยนผงงาน (Flowchart)
15/1/17 Informatics@BUU
28
การเขยนข3นตอนวธแบบมาตรฐานAlgorithm : ช%อข )นตอนวธInput : ………………………………………….Output : ………………………………………..begin
{ สวนคาอธบายอลกอรธม หรอ Comment of Algorithm }if condition then
………………………………….. else
…………………………………..endifcase variable of
condition 1 : ……………………….condition 2 : ……………………….condition N : ……………………….
endcasefor start_condition to stop_condition [step……………] do
…………………………………..endforwhile condition do
…………………………………..endwhilerepeat
…………………………………..until condition
end15/1/17 Informatics@BUU
29
ตวอยางท& 1 จงออกแบบข3นตอนวธเพ&อคานวณหา
พ3นท&สามเหล&ยม Algorithm : Triangle_AreaInput : ความยาวฐาน และ ความสงของสามเหล%ยม ซ%งอาจเปนเลขจานวนเตม
หรอแบบจานวนจรงกได แตตองไมเปนเลขท%มคาลบOutput : พ)นท%สามเหล%ยมbegin
{ ทดสอบวาความยาวฐานมคานอยกวาศนยหรอไม }if ความยาวฐานนอยกวาศนย then
แสดงขอความแสดงความผดพลาดและเลกการทางานendif{ ทดสอบวาความสงมคานอยกวาศนยหรอไม }if ความสงนอยกวาศนย then
แสดงขอความแสดงความผดพลาดและเลกการทางานendifกาหนดใหพ)นท%สามเหล%ยมเปนศนยพ)นท%สามเหล%ยม=ความยาวฐาน * ความสง * (1/2)คนคาพ)นท%สามเหล%ยมใหแกผเรยก
end
15/1/17 Informatics@BUU
30
ตวอยางท& 2 จงออกแบบข3นตอนวธเพ&อจานวนเพศ
ชายและเพศหญงจากจานวนคนท3งหมด 10 คน
15/1/17 Informatics@BUU
31
ตวอยางท& 2 จงออกแบบข3นตอนวธเพ&อจานวนเพศ
ชายและเพศหญงจากจานวนคนท3งหมด 10 คนAlgorithm : Count_SexInput : เพศ โดย M เปนเพศชาย และ F เปนเพศหญงOutput : จานวนเพศชาย และจานวนเพศหญงbegin
กาหนดจานวนเพศชายเปนศนยกาหนดจานวนเพศหญงเปนศนยกาหนดจานวนนบเปนหนงwhile จานวนนบนอยกวาหรอเทากบสบ do
if เพศ = ‘M’ thenจานวนเพศชาย=จานวนเพศชาย+1
elseจานวนเพศหญง=จานวนเพศหญง+1
endifจานวนนบ=จานวนนบ+1
endwhileแสดงผลจานวนเพศชาย และจานวนเพศหญง
end15/1/17 Informatics@BUU
32
การใชรหสเทยม (Pseudocode)
• วธการเขยนอลกอรทมอกวธท%สะดวก และ แกไขงายคอการใชรหสเทยม
• รหสเทยม เปนคาส%งท%มลกษณะการเขยนใกลเคยงกบภาษาองกฤษ แตมโครงสรางเกอบจะเปนภาษาโปรแกรม เชน– เร%มตน ใหมคาวา BEGIN จบลงใหใช END– อาน เขยนขอมลใช READ และ PRINT – การทดสอบเง%อนไขใช IF , ELSE , ELSEIF – การทาซ)าใช WHILE , DO ENDWHILE เปนตน
310322 C ProgrammingDepartment of Computer Science15/1/17 Informatics@BUU
33
การเขยนชดคาส&งเทยม (Pseudo Code) การรบขอมล และการแสดงผล• read var1, var2, …• print var1, var2, …การเปรยบเทยบระหวาง 2 คา• if (condition) then• true statement• else• false statement• endifทางเลอกหลายทาง• case variable of• a : a-statement• b : b-statement• endcase
การทางานแบบวนซาWHILEWhile (condition) do
statementEndwhile
Repeat-Untilrepeat
statementuntil (condition)
Computationvariable=expression/constant15/1/17 Informatics@BUU
34
จงเขยนชดคาส&งเทยม เพ&อคานวณหาพ3นท
สามเหล&ยม
Input : ความยาวฐาน แทนดวย base: ความสง แทนดวย height
Output : พ)นท%สามเหล%ยม แทนดวย areaBegin
1. Read base, height2. Compute
area=(1/2)*base*height3. Print area
End15/1/17 Informatics@BUU
35
ตวอยางท& 4 จงเขยนอลกอรธม
• จงเขยนอลกอรธม หรอชดคาส&งเทยม เพ&อรบขอมลช&อ และอาย
ของพนกงาน 15 คน ถาอายนอยกวา 20 ใหพมพขอความ
‘Child’ ถาอาย 20 ปข3นไปใหพมพขอความ ‘Adult’ และนบจานวนคนท&อายนอยกวา 20 ป
15/1/17 Informatics@BUU
36
Pseudo-code
Input : ช%อ แทนดวย nameอาย แทนดวย age
Output : จานวนคนอายนอยกวา 20 แทนดวย less20Begin
1. Repeat 2. read name, age คนท% i3. if age<20
ใหแสดงผล name และ ‘Child’เพ%มจานวนพนกงานท%อายนอยกวา 20
else ใหแสดงผล name และ ‘Adult’
endif4. until (จานวนพนกงาน 15 คน)
End.
15/1/17 Informatics@BUU
37
ข3นตอนวธAlgorithm Compare_Age_AlgoInput ช%อ และ อายOutput จานวนคนอายนอยกวา 20begin count = less20 = 0
While (count<15) doread name, age
If (age<20) thenprint name, ‘Child’
compute less20=less20+1Else
print name, ‘Adult’Endif
compute count=count+1EnddoPrint less20
End.15/1/17 Informatics@BUU
38
การเขยนผงงาน (Flowchart)
• เปนเคร%องมอท%ใชเขยนอลกอรทมรปแบบหน%งซ%งเขาใจงาย จะใชรปภาพเปนสญลกษณแสดงการทางานดงน
310322 C ProgrammingDepartment of Computer Science
แสดงเสนทางการไหลของงาน (flowlines)สญลกษณแสดงจดเร%มตน หรอส)นสด
สญลกษณนาเขาหรอแสดงผล (Input/Output)สญลกษณแสดงการประมวลผล (Processing)
15/1/17 Informatics@BUU
39
การเขยนผงงาน (Flowchart)
310322 C ProgrammingDepartment of Computer Science
สญลกษณแสดงการตดสนใจ (Decision)
สญลกษณแสดงจดเช%อมตอภายในหนเดยวกน(Connection)
สญลกษณแสดงโปรแกรมยอย (Predefined Process)
สญลกษณบรรทดแรกของกลมคาส%งทาซา (Repetition)
15/1/17 Informatics@BUU
40
การเขยนผงงาน (Flowchart)
310322 C ProgrammingDepartment of Computer Science
สญลกษณจดเช%อมจดในหนาอ%น
สญลกษณเอกสาร
สญลกษณหนวยเกบขอมล เชน ดสก
15/1/17 Informatics@BUU
4115/1/17 Informatics@BUU
42
ลกษณะของผงงานท&ด
• มทางเขาหรอจดเร&มตน และทางออกหรอจดส3นสดเพยงทางเดยว
เทาน3น
• ลาดบข3นตอนการทางานควรจะเร&มจากบนลงลาง หรอจากซายไปขวา
• ในสญลกษณใดๆ มทางออกเพยงทางเดยว ยกเวนสญลกษณแสดง
การตดสนใจ หรอทางเลอกสามารถมทางออกไดอยางนอยสองทาง
• เสนทางในผงงานควรชดเจน เปนระเบยบ
• ขอความหรอคาส&งใดๆท&อยในสญลกษณควรส3น กระชบ ได
ใจความ และสามารถเขาใจไดงาย15/1/17 Informatics@BUU
43
การจดภาพในผงงาน จะมอย 3 รปแบบ
• การจดผงงานตามลาดบ (Sequence)
• การจดผงงานตามทางเลอก (Selection)
• การจดผงงานการทางานแบบวนซา (Iteration)
15/1/17 Informatics@BUU
44
การจดผงงานตามลาดบ (Sequence)
Function A
Function B
Function C
15/1/17 Informatics@BUU
45
การจดผงงานตามทางเลอก (Selection)
Condition A
Function B Function C
A=b A=c
Condition X
Function A Function D
X=b X=c
Function B Function C
X=a X=d
การจดผงงานตามทางเลอกการทางานแบบมาตรฐาน การจดผงงานการเลอกตดสนใจทางาน
กบทางเลอกท&เปนไปไดมากกวา 2 ทางเลอก
15/1/17 Informatics@BUU
46
การจดผงงานการทางานแบบวนซา
(Iteration)
Condition AFunction B
Function C
A=True
A=False
ผงงานการทางานแบบวนซ3 าของ Do-While Loop
15/1/17 Informatics@BUU
47
จงเขยนผงงาน เพ&อคานวณหาพ3นท&สามเหล&ยม
area=height*base*(1/2)
Start
read base, height
print base, height, area
Stop15/1/17 Informatics@BUU
48
• จงเขยนผงงานเพ&อรบขอมลช&อ และอาย ของ
พนกงาน 15 คน ถาอายนอยกวา 20 ใหพมพ
ขอความ ‘Child’ ถาอาย 20 ปข3นไปใหพมพ
ขอความ ‘Adult’ และนบจานวนคนท&อายนอย
กวา 20 และอาย 20 ข3นไป
15/1/17 Informatics@BUU
49
Begin
Count=0,
less20=0,
more20=0
count<15
age<20
print name, 'Child' print name, 'Adult'
less20=1ess20+1 more20=more20+1
count=count+1
True
End
False
print less20, more20
read name, age
FalseTrue
15/1/17 Informatics@BUU
50
สญลกษณท&ใชในการประมวลผล
สญลกษณ ความหมาย
+ บวก- ลบ* คณ/ หาร^ ยกกาลง
310322 C ProgrammingDepartment of Computer Science
สญลกษณ ความหมาย
> มากกวา< นอยกวา
>= มากกวาหรอเทากบ<= นอยกวาหรอเทากบ<> ไมเทากบ
&& และ
|| หรอ
15/1/17 Informatics@BUU
51
ตารางคาความจรงทางตรรกศาสตร
P Q P AND Q P OR Q NOT P
T T T T F
T F F T F
F T F T T
F F F F T
15/1/17 Informatics@BUU
Informatics@BUU 52
Languages• processors perform very simple operations• most programs are written in high-level
languages (C++, Visual Basic, COBOL, etc) • each high-level language statement translates
to several machine code statements• a compiler converts the high-level language
statements to machine code statements• programs are written in a high-level language
as they are easier to code and understand
15/1/17
53
แนวคดเก&ยวกบหลกการเขยนโปรแกรม
(Programming Paradigm)• Imperative paradigm : การทางานของโปรแกรมตามลาดบของชดคาส&ง First do this and next do that
• Functional paradigm : อธบายฟงกชนทางคณตศาสตรและใชผลลพธน3นเพ&อทางานตอไป
• Logic paradigm : เนนงานโปรแกรมดานปญญาประดษฐโดยพจารณากฎความสมพนธตางๆ
• Object-oriented paradigm : เปนการรวมเอาตวแปรและกระบวนการทางานท&เก&ยวของกนเขาไวดวยกนเรยกวา
Object และ เนนการสงขอมลระหวาง Object15/1/17 Informatics@BUU
Informatics@BUU 54
Language Translators: Interpreter
Program
Statement+Data
Interpreter
Machine
language
statementStatement execution
15/1/17
Informatics@BUU 55
Language Translators (Compilers)Step1: Convert program (Compile Time)
Step2: Execute program (Run time)
Computer
programCompiler
Machine
language
Program
Machine
language
program
Data
Program Output
15/1/17
Informatics@BUU 56
Program Development cycle• Design• Edit: notepad++, tools• Interpreter: Octave >
15/1/17
Informatics@BUU 57
Software Development• when you code a program in Octave, it is easy
to make mistakes that cause compiler errors• but this task is only a small part of s/w
development process • before coding a program, it must be designed• if the design is going to work, the task the
program will perform must be understood • the Study Guide describes a 5-step software
development process
15/1/17
Informatics@BUU 58
Software Development• task analysis and specification
• analysis the task(s) the program must perform and produce a written statement of the requirements
• algorithm design• develop the logic that the program will perform to
accomplish it’s task(s)• coding• testing
• it must do what you want (no bugs) - verification• it must do what the client wants - validation
• maintenance15/1/17
Informatics@BUU 59
Software Development• by “maintenance” we mean:
• fixing bugs – distributed as Service Release• making improvements - eg. Win95 user interface• changes/extensions - eg. Win98 support for USB
• around 75% of programming time is spent maintaining software – so it’s important to use simple designs that are easy to understand
• important: coding is just a small part of the software development process - analysis, design and testing are more important stepsin the process
15/1/17
Informatics@BUU 60
SDLC• Study Guide talks about a software
development life cycle - SDLC • idea is that each version of a program
involves a rotation through the 5 stagesanalysis and specification
algorithm design
codingtesting
maintenance
15/1/17
Informatics@BUU 61
Summary• in 886307 you will design mathematical
equation & code Octave programs• a program is a piece of software• computers input, process and output data • coding is a small part of development process• analysis, design and testing more important• use simple designs to reduce maintenance• Study Guide describes a 5-step SDLC
15/1/17