본문 바로가기
학부 강의 노트/포트란 프로그래밍 (보충예제)

포트란 기본 예제8 (순환구조 DO WHILE loop)

by Dr. STEAM 2022. 6. 9.
반응형

순환구조의 종류

1. DO (횟수 제어 순환구조)

    (구조명 : ) do [var = 초기값, 끝값, (증분)]

                          실행문

                      end do

do 이후 [ ] 내용이 없는 구조도 가능

 

2. DO WHILE (조건적 순환구조)

    (구조명 : )  do while (조건)

                           실행문

                      end do 

 

 

 

!! f(x) = x^3 + 4x^2 - 10 = 0 근을 bisection method (이분법) 로 계산하는 프로그램(2)

program bisection
    implicit none
    integer :: i, maxit    ! 4바이트 정수형 선언
    real(kind=16) :: a, b, x, p, fa, fb, fp, tol, f    ! 16바이트 실수형 선언

    f(x) = ( x + 4 ) * x * x - 10       !   문함수로서 프로시저 내에서의 한줄 함수식을 표현
10  print *, ' type a and b : '
    read *, a, b
!! 이하 이분법 계산과정    
    if ( a > b ) then 
        x = a
        a = b
        b = x
    end if
    fa = f(a)
    fb = f(b)
    if ( fa * fb > 0.0 ) then 
        print *, 'f(a) and f(b) have same signs ! ' 
        goto 10
    else
        print *, ' Input tolerance '
        read *, tol
        print *, ' Input maximum number of iterations ' 
        read *, maxit
    end if


loop : do while (abs(fp) > 1.0e-20 .and. (b-a)/2 < tol .and. i <= maxit ) 
            i = i + 1    
            print *, ' ** p, i, f(p) ' , p, i, f(p)
            
inner :     if (fa * fp > 0) then        ! 구조명 inner 인 순환구조 시작
                a = p         
                fa = fp    
            else        
                b = p         
                fb = p    
            end if inner        
            p = a + (b-a)/2 
            fp = f(p)
          end do loop    
end program bisection


       


    

DO WHILE 순환구조

구조명 : do while (조건)

           실행문들 ...

              end do 구조명

 

조건의 자료값이 참일때만 실행

728x90
반응형