Fortran: Lesson 4

Lesson topics


View Demos
# 1 # 2 # 3 # 4 # 5 # 6
Download Demos
# 1 # 2 # 3 # 4 # 5 # 6

Demo 5

								        program maximum
c       This program finds the maximum value of a function on a grid.
c       You may replace f(x) with your own function.
c       The user inputs endpoints of an interval, and a grid size h;
c       the program looks at all values of f(x) from x = a to x = b
c       in steps of h, and picks the largest of these as the maximum.
        real a, b, h, x, y, xmax, fmax, f
        f(x) = cos(x) + 3 * sin(2 * x)

        print *, "Enter the left endpoint :"
        read *, a
        print *, "Enter the right endpoint :"
        read *, b
        print *, "Enter the gridsize :"
        read *, h

        x = a
        xmax = a
        fmax = f(a)        
        do while (x .lt. b)
                y = f(x)
                if (y .gt. fmax) then
                        xmax = x
                        fmax = y
                end if
                x = x + h
        end do

        print *
        print *, "The maximum value was found at x = ", xmax
        print *, "The function value there is ", fmax
        print *
        print *, "Auf Wiedersehen"
        end