Presentation is loading. Please wait.

Presentation is loading. Please wait.

수치해석 (Numerical Analysis)

Similar presentations


Presentation on theme: "수치해석 (Numerical Analysis)"— Presentation transcript:

1 수치해석 (Numerical Analysis)
일변수 방정식과 함수 (Part 1) 문양세 강원대학교 IT대학 컴퓨터과학전공

2 In this chapter … (1/2) 일변수 방정식과 함수 일변수 방정식(single variable equations)에서 1) 근을 구하는 문제, 2) 최대값과 최소값을 구하는 문제를 다룬다. 일변수 방정식이란? 변수가 하나인 방정식을 의미한다. 즉, 일반적으로 f(x)와 같은 형식으로 변수가 x만 주어지는 방정식을 의미한다. 일변수 방정식의 근을 구하는 문제는 f(x) = 0 꼴의 식을 만족하는 x 값을 찾는 문제라 할 수 있다. ( 0점 찾기(zero crossing localization)라고도 한다.) 저차식(1차, 2차, 3차)인 경우, 인수분해 등의 분석적 방법을 사용한다. But, 고차식인 경우, 비선형 함수(삼각, 지수, 로그 함수)인 경우, 이들 함수들이 복합적으로 섞인 복잡한 방정식인 경우에는 어떻게 하나…  분석적 방법이 어려우므로 수치해석적인 방법(Numerical Method)을 통하여 풀어낸다.

3 극대값(local maximum) 및 최대값(global maximum)
In this chapter … (2/2) 일변수 방정식과 함수 근, 최대값, 최소값, 극대값, 극소값 We will cover … 이분법(bisection method)을 사용한 방정식 풀이 뉴튼-랩슨법(Newton-Raphson Method)을 사용한 방정식 풀이 그 외의 방정식 풀이 방법(할선법, 가상 위치법 등) 극값(extreme value) 찾기 다항식의 인수분해 f(x) x 극대값(local maximum) 및 최대값(global maximum) 극대값(local maximum) 극소값(local minimum) 0점, i.e., 근

4 본론에 들어가기에 앞서서… (1/2) 앞으로 많은 프로그램을 배우게 된다.
일변수 방정식과 함수 앞으로 많은 프로그램을 배우게 된다. 프로그램을 쓰기 위해서는 알고리즘을 기술할 수 있어야 한다. 본 강의에서는 알고리즘 기술은 알고리즘 과목에서 가장 널리 쓰이는 Pascal 형식의 Pseudo Code를 사용하고, 프로그램은 (여러 분이 잘 사용하는, 외부에서도 가장 많이 쓰이는, 많은 다른 언어의 기초로 작용하는) C 언어(혹은 Java)를 사용한다. 그러므로, 본 강의에서는 우선, 알고리즘을 기술하는 방법은 간략하게 소개한다. C/Java 언어의 경우, 기본적인 내용만을 다루므로, 익숙하지 않은 학생이라도 스스로 학습하여 이 기회에 C/Java 언어에 좀 더 실력을 쌓아야 한다.

5 본론에 들어가기에 앞서서… (2/2) 프로그래밍의 경우
일변수 방정식과 함수 프로그래밍의 경우 강의에서는 UNIX(or Linux) 환경에서 C 프로그래밍(+ 안드로이드)을 보여준다. 그러나, 여러 분은 Windows, Linux, Unix 등 어느 환경에서 작업을 해도 상관이 없다. (우리가 배우는 내용은 OS 및 Platform Independent하다.) 다만, 스스로 프로그래밍 할 수 있는 자신의 환경을 구축하기를 (매우 강력하게) 권고한다. UNIX/Linux를 사용한 경험이 있는 학생은 UNIX/Linux 환경을 권장한다. 그렇지 않은 학생은 Windows 환경을 포함한 어느 환경을 사용해도 무방하다. 안드로이드 코드도 제공하므로, 안드로이드 사용도 권장한다. 이론 및 연습 강의에서의 모든 소스는 cs1.kangwon.ac.kr에서 제공한다. 환경 구축에 대한 문의 사항은 …

6 Declaration STATEMENTS procedure name(argument: type)
Pseudocode Language 일변수 방정식과 함수 Declaration STATEMENTS procedure name(argument: type) variable := expression informal statement begin statements end {comment} if condition then statement [else statement] for variable := initial value to final value statement while condition statement procname(arguments) Not defined in book: return expression

7 procedure procname(arg: type)
일변수 방정식과 함수 Declares that the following text defines a procedure named procname that takes inputs (arguments) named arg which are data objects of the type type. Example: procedure maximum(L: list of integers) [statements defining maximum…]

8 variable := expression
일변수 방정식과 함수 An assignment statement evaluates the expression, then reassigns the variable to the value that results. Example: v := 3x+7 (If x is 2, changes v to 13.) In pseudocode, the expression might be informal: x := the largest integer in the list L

9 Informal Statement 일변수 방정식과 함수 Sometimes we may write an informal statement, if the meaning is still clear and precise: “swap x and y.” Keep in mind that real programming languages never allow this. (궁극적으로는 알고리즘을 쓰고 이를 구현해야 한다.)

10 Groups a sequence of statements together:
begin statements end 일변수 방정식과 함수 Groups a sequence of statements together: begin statement statement … statement n end Allows sequence to be used like a single statement. (한 문장인양..) Might be used: After a procedure declaration. In an if statement after then or else. In the body of a for or while loop.

11 Not executed (does nothing).
{ comment } 일변수 방정식과 함수 Not executed (does nothing). Natural-language text explaining some aspect of the procedure to human readers. (Reader의 이해 도모) Also called a remark in some real programming languages. Example: {Note that v is the largest integer seen so far.}

12 If condition then statement
일변수 방정식과 함수 Evaluate the propositional expression condition. If the resulting truth value is true, then execute the statement; otherwise, just skip on ahead to the next statement. (조건이 true일 때만 문장을 수행한다.) Variant: if cond then stmt1 else stmt2 Like before, but iff truth value is false, executes stmt2.

13 while condition statement
일변수 방정식과 함수 Evaluate the propositional expression condition. If the resulting value is true, then execute statement. Continue repeating the above two actions over and over until finally the condition evaluates to false; then go on to the next statement. (조건이 true인 한 문장을 반복하여 수행한다.)

14 for var := initial to final stmt
일변수 방정식과 함수 Initial is an integer expression. Final is another integer expression. Repeatedly execute stmt, first with variable var := initial, then with var := initial+1, then with var := initial+2, etc., then finally with var := final. What happens if stmt changes the value that initial or final evaluates to? For can be exactly defined in terms of while, like so: begin var := initial while var  final begin stmt var := var end end

15 Various real programming languages refer to procedures as
procedure(argument) 일변수 방정식과 함수 A procedure call statement invokes the named procedure, giving it as its input the value of the argument expression. Various real programming languages refer to procedures as functions (since the procedure call notation works similarly to function application f(x)), or as subroutines, subprograms, or methods.

16 Max Procedure in Pseudocode
일변수 방정식과 함수 Write “finding maximum number” in pseudo-code. procedure max(a1, a2, …, an: integers) v := a1 {largest element so far} for i := 2 to n {go thru rest of elems} if ai > v then v := ai {found bigger?} {at this point v’s value is the same as the largest integer in the list} return v

17 We are now … 이분법(bisection method)을 사용한 방정식 풀이
뉴튼-랩슨법(Newton-Raphson Method)을 사용한 방정식 풀이 그 외의 방정식 풀이 방법(할선법, 가상 위치법 등) 극값(extreme value) 찾기 다항식의 인수분해

18 이분법(Bisection Method) 개요 (1/2)
Motivation: 연속 함수의 경우, 실근의 전후에서 함수 값은 서로 다른 부호를 갖는다. (단, 중근의 경우 예외가 있으며, 이는 추후 다루기로 한다.) 이분법 개요 어떤 구간의 두 경계 값에서 함수 값의 부호에 변화가 있는지 검사한다. 부호에 변화가 있다면, 그 구간 내에 근이 존재한다는 의미이다. 따라서, (좀 더 정확한 근을 구하기 위하여) 해당 구간을 반으로 나누어 두 개의 새로운 구간을 만든다. 두 구간 중에서 부호의 변화가 있는 구간을 찾아낸다. 상기 과정을 원하는 정밀도까지 반복한다.

19 이분법 개요 (2/2) 구간 분할: 중간 값을 취하는 방법을 사용한다.
Bisection Method 구간 분할: 중간 값을 취하는 방법을 사용한다. 두 값 xl 과 xh 사이에 근이 존재할 때, 중간 값 xm은 다음과 같이 구한다. f(x) Xm Xm’ x Xl Xh Xl’ Xh’ f(xm)f(xh)와 f(xm)f(xl)을 조사하여 음수 값을 갖는 경우를 다음 구간으로 사용한다. In Computer Science, we call this method as “binary search.”

20 이분법 알고리즘 procedure bisection(xl, xh, e: real numbers)
Bisection Method procedure bisection(xl, xh, e: real numbers) { xl is a left bound value of the range having a root.} { xh is a right bound value of the range having a root.} { e is an allowable error value.} while (xh − xl) > e begin xm := (xh + xl) / 2; {get a medium value} if f(xm)f(xh) = 0 then return xm; {xm is a root!} else if f(xm)f(xl) < 0 then xh := xm; else if f(xm)f(xh) < 0 then xl := xm; else break; { something wrong  cannot find the root.} end return xm;

21 이분법 프로그램 (1/2) 대상 함수: argi.c, argf.c #include <stdio.h>
Bisection Method 대상 함수: #include <stdio.h> #include <stdlib.h> #include <math.h> float f(float); // evaluation of f(x) main(int argc, char *argv[]) { int i = 1; float xh, xl, xm, e; if(argc < 4) { printf("Usage: %s xh xl e\n", argv[0]); exit(0); } xh = (float)atof(argv[1]); // ascii to float function xl = (float)atof(argv[2]); e = (float)atof(argv[3]); printf("xh = %.10f\n", xh); printf("xl = %.10f\n", xl); printf("e = %.10f\n", e); argi.c, argf.c

22 이분법 프로그램 (2/2) while((xh - xl) > e) { xm = (xh + xl) / 2.0;
Bisection Method while((xh - xl) > e) { xm = (xh + xl) / 2.0; if((f(xm)*f(xh)) == (float)0) break; else if((f(xm)*f(xl)) < (float)0) xh = xm; else if((f(xm)*f(xh)) < (float)0) xl = xm; else { printf(“Something worng --> cannot find the root.\n”); break; } printf("[Iteration %02d]: The root is %.10f <with error %.10f>\n", i++, xm, xh-xl); float f(float x) { return ((float)log(x + 5.0) + x); //

23 프로그램 실행 결과 Bisection Method

24 안드로이드 프로그래밍 – 코드(일부) Bisection Method

25 안드로이드 프로그래밍 – 실행결과 Bisection Method

26 다른 함수의 예와 실행 결과 (1/2) Bisection Method 대상 함수: 이분법 알고리즘(프로그램) 자체는 동일하며, 단지 함수 f(x)만 다음과 같이 달리하면 된다. 참고: pow(x, y) = xy

27 다른 함수의 예와 실행 결과 (2/2) Bisection Method

28 안드로이드 프로그래밍 – 코드(일부) Bisection Method

29 안드로이드 프로그래밍 – 실행결과 Bisection Method

30 이분법 - 재귀 알고리즘 (recursive algorithm)
Bisection Method procedure bisection(xl, xh, e: real numbers) { xl is a left bound value of the range having a root.} { xh is a right bound value of the range having a root.} { e is an allowable error value.} xm := (xh + xl) / 2; {get a medium value} if f(xm)f(xh) = 0 then return xm; else if f(xm)f(xl) < 0 then xh := xm; else if f(xm)f(xh) < 0 then xl := xm; else break; {something wrong  cannot find the root.} if (xh − xl)  e then return xm else return bisection(xh, xl, e);

31 이분법 - 재귀 프로그램(1/2) 대상 함수: #include <stdio.h>
Bisection Method 대상 함수: #include <stdio.h> #include <stdlib.h> #include <math.h> int i = 1; float f(float); // evaluation of f(x) void bisection(float, float, float); // recursive function main(int argc, char *argv[]) { float xh, xl, e; if(argc < 4) { printf("Usage: %s xh xl e\n", argv[0]); exit(0); } xh = (float)atof(argv[1]); xl = (float)atof(argv[2]); e = (float)atof(argv[3]); printf("xh = %.10f\n", xh); printf("xl = %.10f\n", xl); printf("e = %.10f\n", e); bisection(xh, xl, e);

32 이분법 - 재귀 프로그램(2/2) void bisection(float xh, float xl, float e) {
Bisection Method void bisection(float xh, float xl, float e) { float xm; xm = (xh + xl) / 2.0; if(f(xm) == (float)0) return; else if((f(xm)*f(xl)) < (float)0) xh = xm; else if((f(xm)*f(xh)) < (float)0) xl = xm; else { printf(“Something worng --> cannot find the root.\n”); exit(-1); } printf("[Recursion %02d]: The root is %.10f <with error %.10f>\n", i++, xm, xh-xl); if((xh - xl) <= e) return; else bisection(xh, xl, e); float f(float x) return ((float)log(x + 5.0) + x); //

33 재귀 프로그램 - 실행 결과 Bisection Method

34 안드로이드 프로그래밍 – 코드(일부) Bisection Method

35 안드로이드 프로그래밍 – 실행결과 Bisection Method

36 We are now … 이분법(bisection method)을 사용한 방정식 풀이
Newton-Raphson Method 이분법(bisection method)을 사용한 방정식 풀이 뉴튼-랩슨법(Newton-Raphson Method)을 사용한 방정식 풀이 그 외의 방정식 풀이 방법(할선법, 가상 위치법 등) 극값(extreme value) 찾기 다항식의 인수분해

37 뉴튼-랩슨(Newton-Raphson) 방법 이전에
Newton-Raphson Method 미분 그까이껏~ 고딩 시절에 다 배운 것인데… 뭘~ 더구나, 1학년때 Calculus 열심히 공부해서… 별 걱정 없을 껄~ 여러분의 기억력을 믿지만, 그래도 … Back to the Future 미분(differentiation)의 정의를 복습하고, 몇 가지 중요한 함수들에 대한 도함수(derivative)를 살펴본다. 뉴튼 랩슨 방법의 이론적 Background에 해당하는 테일러 정리(Tayler’s Theorem)에 대해서 살펴본다.

38 미분과 도함수 (1/10) Newton-Raphson Method 정의: 함수 f(x)에서 x가 a와 다른 값을 가지면서, a에 한없이 가까워질 때, f(x)의 값이 일정한 값 에 한없이 가까워지면, x  a일 때, f(x)는 에 수렴한다 하고, 와 같이 나타낸다. 그리고, 이때 를 f(x)의 (a에 대한) 극한(값)이라 한다. 예제:

39 미분과 도함수 (2/10) Newton-Raphson Method 정의: 함수 f(x)에서 xa일 때 f(x)의 값이 한없이 커지면, xa일 때 f(x)는 양의 무한대로 발산한다 하고, 와 같이 나타낸다. 그리고, 이때 f(x)의 극한은 라 한다. 정의: 함수 f(x)에서 xa일 때 f(x)의 값이 음수로서, 그 절대값이 한없이 커지면, xa일 때 f(x)는 음의 무한대로 발산한다 하고, 와 같이 나타낸다. 그리고, 이때 f(x)의 극한은 라 한다. 예제:

40 미분과 도함수 (3/10) Newton-Raphson Method 정의: f가 실수 집합 X상에 정의된 함수일 때, 이면, f는 a에서 연속이라 한다. 또한, f가 X의 모든 점에 대해서 연속이면 f는 X (위)에서 연속이라 한다. 예제 연속 함수의 예: 연속 함수가 아닌 예:

41 미분과 도함수 (4/10) Newton-Raphson Method 정의: f가 실수 집합 X상에 정의된 함수라 하자. 만일, 이 존재하면, f는 a에서 미분가능(differentiable)하다고 한다. 또한, f(a)를 a에서 f의 도함수(derivative)라 부른다. 그리고, X에 있는 모든 점에서 도함수를 갖는 함수를 X 위에서 미분가능하다고 하며, a에서 f의 도함수는 (a, f(a)) 그래프에 대한 접선의 기울기에 해당한다.  다음 페이지 그래프 참조

42 미분과 도함수 (5/10) 정리: 만일 f가 a에서 미분가능하다면, f는 a에서 연속이다. 접선은 기울기 f(a)를 갖는다.
Newton-Raphson Method 접선은 기울기 f(a)를 갖는다. 정리: 만일 f가 a에서 미분가능하다면, f는 a에서 연속이다.

43 미분과 도함수 (6/10) Newton-Raphson Method Rolle의 정리: 함수 f가 폐구간 [a,b]에서 연속이고 개구간 (a,b)에서 미분가능하다고 하자. 이때, 만일 f(a) = f(b)이면, f(c)=0이 되는 한 점 c가 (a,b) 상에 존재한다.

44 미분과 도함수 (7/10) Newton-Raphson Method 평균값의 정리: 함수 f가 [a,b]에서 연속이고 (a,b)에서 미분가능하다면, 가 되는 수 c가 (a,b) 상에 존재한다. 평행선 기울기 기울기

45 미분과 도함수 (8/10) Newton-Raphson Method 미분법의 기본공식

46 미분과 도함수 (9/10) Newton-Raphson Method 합성함수의 미분법 삼각함수의 미분법

47 미분과 도함수 (10/10) Newton-Raphson Method 지수함수의 미분법 로그함수의 미분법

48 테일러 정리 (Tayler’s Theorem)
Newton-Raphson Method 함수 f와 f의 도함수들인 f, f, …, f(n)이 [a,b]에서 연속이고 f(n)이 (a,b)에서 미분가능하다면, 다음 식을 만족하는 수 cn+1이 존재한다. 테일러 정리를 사용한 Approximation Formulas

49 뉴튼-랩슨 방법 개요 (1/6) 이분법의 단점 뉴튼-랩슨 방법 뉴튼-랩슨 방법의 유도
Newton-Raphson Method 이분법의 단점 근이 존재하는 구간을 미리 알고 있어야 한다. 지정된 구간에 근이 두 개 있는 경우를 해결하지 못한다. ... 뉴튼-랩슨 방법 다음 근의 값(xi+1)을 현재 근의 값(xi), 함수 값, 도함수 값을 사용하여 정한다. 즉, 을 사용한다. 뉴튼-랩슨 방법의 유도 테일러 정리에서 유도할 수 있다. 도함수의 정의에 의해 유도할 수 있다.

50 뉴튼-랩슨 방법 개요 (2/6) Newton-Raphson Method 테일러 정리에서 유도 테일러 정리에서 두 번째 항까지만을 고려한 Approximation Formula는 이다. 그런데, 근이 되는 점 x에서 f(x)=0이므로, 좌변을 0으로 놓고 정리하면 가 된다.

51 뉴튼-랩슨 방법 개요 (3/6) Newton-Raphson Method 도함수 정의를 사용한 유도 – 방법 1 점 (a, f(a))에서의 접선 방정식은 기울기가 f(a)이므로, 과 같이 나타낼 수 있다. 그런데, 근이 되는 점 x에서 f(x)=0이므로, f(x)를 0으로 놓고 정리하면 가 된다.

52 뉴튼-랩슨 방법 개요 (4/6) Newton-Raphson Method 도함수 정의를 사용한 유도 - 방법 2 점 (a, f(a))에서의 접선 방정식은 다음과 같이 구할 수 있다. 여기서, y=0으로 놓고 정리하면 다음과 같이 근을 구할 수 있다. 가 된다.

53 뉴튼-랩슨 방법 개요 (5/6) 뉴튼-랩슨법으로 근을 찾아가는 과정 (접선의 x절편을 찾아나가는 과정) 기울기 = f(xi)
Newton-Raphson Method 뉴튼-랩슨법으로 근을 찾아가는 과정 (접선의 x절편을 찾아나가는 과정) 기울기 = f(xi) 기울기 = f(xi+1) 기울기 = f(xi+2)

54 뉴튼-랩슨 방법 개요 (6/6) 뉴튼-랩슨법의 장점: 수렴 속도가 매우 빨라서 빠른 시간 내에 근을 찾을 수 있다.
Newton-Raphson Method 뉴튼-랩슨법의 장점: 수렴 속도가 매우 빨라서 빠른 시간 내에 근을 찾을 수 있다. 뉴튼-랩슨법의 문제점: 근을 찾지 못하는 경우가 있다.

55 뉴튼-랩슨 방법 개요 (6/6) Newton-Raphson Method

56 뉴튼-랩슨 방법 알고리즘 procedure newton(xi, e: real numbers)
Newton-Raphson Method procedure newton(xi, e: real numbers) { xi is an initial value, i.e., a starting point} { e is an allowable error value.} while |f(xi)| > e xi := xi – f(xi)/f(xi); {get a next value} return xi;

57 뉴튼-랩슨 방법 프로그램 (1/2) 대상 함수: #include <stdio.h>
Newton-Raphson Method 대상 함수: #include <stdio.h> #include <stdlib.h> #include <math.h> float f(float); // evaluation of f(x) float f_prime(float); // evaluation of f’(x) main(int argc, char *argv[]) { int i = 1; float xi, e; if(argc < 3) { printf("Usage: %s xi e\n", argv[0]); exit(0); } xi = (float)atof(argv[1]); // ascii to float function e = (float)atof(argv[2]); printf("xi = %.10f\n", xi); printf("e = %.10f\n", e);

58 뉴튼-랩슨 방법 프로그램 (2/2) while(fabs(f(xi)) > e) {
Newton-Raphson Method while(fabs(f(xi)) > e) { xi = xi - f(xi)/f_prime(xi); printf("[Iteration %02d]: The root is %.10f <with error %.10f>\n", i++, xi, fabs(f(xi)); } float f(float x) { return ((float)log(x + 5.0) + x); // float f_prime(float x) return (1.0/(x+5.0) + 1.0); //

59 프로그램 실행 결과 Newton-Raphson Method

60 안드로이드 프로그래밍 – 코드(일부) Bisection Method

61 안드로이드 프로그래밍 – 실행결과 Bisection Method

62 다른 함수의 예와 실행 결과 (1/2) Newton-Raphson Method 대상 함수: 이분법 알고리즘(프로그램) 자체는 동일하며, 단지 함수 f(x)와 f(x)만 다음과 같이 달리하면 된다.

63 다른 함수의 예와 실행 결과 (2/2) Newton-Raphson Method

64 안드로이드 프로그래밍 – 코드(일부) Bisection Method

65 안드로이드 프로그래밍 – 실행결과 Bisection Method


Download ppt "수치해석 (Numerical Analysis)"

Similar presentations


Ads by Google