1 / 7

Ch4homework 4-4

Ch4homework 4-4. 환경공학과 20071464 백하비. (H.W. and Exam. 2009 of W.Q. and E.H.) 다른 초기값을 사용하여 근을 구하여라. Newton- Rapnson 법의 프로그래밍 계산 순서는 아래와 같다 . 단 , 함수. 와 도함수. 는 사용자 정의의 모듈로서 작성된다 . Newton-Raphson 법의 모듈의 순서도를 다음 그림에 나타내었다. 프로그램 예제로서. 의 근을 구하는 경우의 FORTRAN 프로그램을

camdyn
Download Presentation

Ch4homework 4-4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Ch4homework4-4 환경공학과 20071464 백하비

  2. (H.W. and Exam. 2009 of W.Q. and E.H.) 다른 초기값을 사용하여 근을 구하여라.

  3. Newton-Rapnson법의 프로그래밍 계산 순서는 아래와 같다. 단, 함수 와 도함수 는 사용자 정의의 모듈로서 작성된다. Newton-Raphson법의 모듈의 순서도를 다음 그림에 나타내었다. 프로그램 예제로서 의 근을 구하는 경우의 FORTRAN 프로그램을 프로그램 F4.2, C 프로그램을 프로그램 C4.2에 나타내었다. 실제의 계산에서는 을 기억할 필요가 없기 때문에, 수렴된 결과만을 주 프로그램에 반환하고 있다 . 또, 사용자 정의의 모듈 이름은, 이 Newton-Raphson법의 모듈의 인수가 되도록 하였다. 이에 따라, 사용자는 적당한 모듈 이름으로 프로그래밍이 가능하다. 또, 1개의 주 프로그램 안에서 복수의 비선형 방정식의 해를 구하는 것이 가능해진다. 그림 Newton-Raphson법의 순서도

  4. --------- F 4.2 Newton-Raphson법에 따른 비선형 방정식의 해법 --------- c <주 프로그램> implicit double precision (a-h, o-z) external userf c *** 단계 1 자료 입력 *** write(6,*) ' 초기값 x0 = ' read(5,*) x0 write(6,*) ' 수렴 판정 정수 = ' read(5,*) e write(6,*) ' 최대 반복 회수 n_max = ' read(5,*) nmax c *** 단계 2 newton-raphson법의 계산 *** call s_newt(n,x0,e,nmax,userf) c *** 단계 3 계산 결과의 출력 *** write(6,10) ' 반복 회수:n= ',n,' 해:x(n)= ',x0 10 format(1h,a11,i4,1h,a8,f10.7) stop end

  5. c < newton-raphson법의 모듈 > subroutine s_newt(n,x0,e,nmax,func) c n : 스칼라 반복 회수 (out) c x0 : 스칼라 초기값(해) (in/out) c e : 스칼라 수렴 판정 정수 (in) c nmax : 스칼라 최대 반복 회수 (in) c func : 사용자 정의 함수 (in) implicit double precision (a-h, o-z) n=0 xn=x0 100 if(n.lt.nmax) then call func(fun,dfun,xn) x0=xn-fun/dfun n=n+1 if(abs(xn-x0).gt.e) then xn=x0 go to 100 end if end if return end

  6. c < 사용자 정의 함수와 그 미분의 subroutine > subroutine userf(fn,dfn,x) c fn : 스칼라 방정식 (out) c dfn : 스칼라 방정식의 미분 (out) c x : 스칼라 입력 변수 (in) implicit double precision(a-h,o-z) x2=x*x fn=x2*x-1.0 dfn=3.0*x2 return end ----------------------------------------------------------------- -------- C 4.2 Newton-Raphson법에 따르는 비선형 방정식의 해법 -------- /* <주 프로그램> */ #include "smp.h" main() { void USERF(double *, double *, double); double x0,e; int n, NMAX; /*** 단계 1 자료 입력 ***/ printf("\n 초기값 x0 = "); scanf("%lf",&x0); printf("\n 수렴 판정 정수 epsilon = "); scanf("%lf",&e); printf("\n 최대 반복 회수 NMAX = "); scanf("%d",&NMAX);

  7. /*** 단계 2 Newton-Raphson법의 계산 ***/ S_NEWT(&n,&x0,e,NMAX,USERF); /*** 단계 3 계산 결과의 출력 ****/ printf("\n 반복 회수:N=%d 해:X(N)=%10.7lf",n,x0); } /* < Newton-Raphson법의 모듈 > */ void S_NEWT(int *n,double *x0,double e,intNMAX,double (*FUNC)()) /* n : 스칼라 반복 회수 (Out) x0 : 스칼라 초기값(해) (In/Out) e : 스칼라 수렴 판정 정수 (In) NMAX : 스칼라 최대 반복 회수 (In) FUNC : 사용자 정의 함수 (In) */ { double fun,dfun,xn,ans; for(*n=0,xn=*x0;*n<NMAX;){ (*FUNC)(&fun,&dfun,xn); *x0=xn-fun/dfun; (*n)++; if(fabs(xn-*x0)>e) xn=*x0>0; else break; }} /* < 사용자 정의 함수와 그 미분 > */ void USERF(double *fn,double *dfn, double x) /* fn : 스칼라 방정식 (Out) dfn : 스칼라 방정식의 미분 (Out) x : 스칼라 입력 변수 (In) */ { double x2; x2=x*x; *fn=x2*x-1.0; *dfn=3.0*x2; return; } ------------------------------------------------------------------

More Related