Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Hi,Below is a fortran program to calculate the roots of an

  1. Aug 9, 2011 #1
    Hi,

    Below is a fortran program to calculate the roots of an equation by newton's method.
    I compile the program with the free compiler g95:
    g95 -c newton.f95 then g95 newton.f95 -o newton.exe

    When I run the program newton.exe I get the error can't assign value INTENT(IN)::x to

    x=x-fx/g(x).It goes wrong at the do loop.When I change x1=x-fx/g(x) I get six times
    the same result.
    4 33.
    What goes wrong with the program.I'm not a specialist.

    Thank you for help


    ! Numerical Mathematics and Computing, Fifth Edition
    ! Ward Cheney & David Kincaid
    ! Brooks/Cole Publ. Co.
    ! Copyright (c) 2003. All rights reserved.
    ! For educational use with the Cheney-Kincaid textbook.
    ! Absolutely no warranty implied or expressed.
    !
    ! Section 3.2
    !
    ! File: newton.f90
    !
    ! Sample Newton's method program using subroutine newton(f,fp,x,m)
    !
    program main
    integer, parameter :: dp = kind (1d0)
    real (kind = dp) :: x, true
    integer :: m

    interface
    function f(x)
    integer, parameter :: dp = kind(1d0)
    real (kind=dp), intent(in) :: x
    end function f
    function g(x)
    integer, parameter :: dp = kind (1d0)
    real (kind=dp), intent(in) :: x
    end function g
    end interface

    m = 6
    x = 4.0_dp
    call newton(f,g,x,m)
    end program main

    subroutine newton(f,g,x,m)
    interface
    function f(x)
    integer, parameter :: dp = kind(1d0)
    real (kind=dp), intent(in) :: x
    end function f
    function g(x)
    integer, parameter :: dp = kind(1d0)
    real (kind =dp), intent(in) :: x
    end function g
    end interface
    integer, parameter :: dp = kind(1d0)
    real (kind=dp), intent(in) :: x
    integer, intent(in) :: m

    print *, "n x f(x)"
    fx = f(x)
    print *, 0, x, fx
    do n = 1,m
    x = x - fx/g(x)
    fx = f(x)
    print "(i2, f20.16, f20.16)", n, x, fx
    end do
    end subroutine newton

    function f(x)
    integer, parameter :: dp = kind (1d0)
    real (kind = dp) , intent(in) :: x
    f = ((x - 2.0_dp)*x + 1.0_dp)*x - 3.0_dp
    end function f

    function g(x)
    integer, parameter :: dp = kind (1d0)
    real (kind = dp), intent(in) :: x
    g = (3.0_dp*x - 4.0_dp)*x + 1.0_dp
    end function g
     
  2. jcsd
  3. Aug 9, 2011 #2

    uart

    User Avatar
    Science Advisor

    Re: Fortran

    Because Fortran passes all parameters by reference the "intent" modifier was added to the language to help prevent unwanted side effects to passed variables. Intent(in) simply informs the compiler that the variable should not be modified during the subroutine or function.

    Since your newton subroutine declares "x" with intent(in) it is not allowed to modify it. Either remove the intent(in) statement and write the main program in such a way that it is immune to any side effects in x, or keep intent(in) and use a new local variable for x within the subroutine.

    BTW. The return type of the functions f and g, as well as that of some other variables like fx, doesn't seem to have been declared. You should be really wary of using Fortrans implicit typing.
     
  4. Aug 10, 2011 #3
    Fortran

    Hi,

    Thanks to Uart for his help.I changed the program with your suggestions and it works fine.
    Here is the program:!


    ! Numerical Mathematics and Computing, Fifth Edition
    ! Ward Cheney & David Kincaid
    ! Brooks/Cole Publ. Co.
    ! Copyright (c) 2003. All rights reserved.
    ! For educational use with the Cheney-Kincaid textbook.
    ! Absolutely no warranty implied or expressed.
    !
    ! Section 3.2
    !
    ! File: newton.f90
    !
    ! Sample Newton's method program using subroutine newton(f,fp,x,m)
    !
    program main
    integer, parameter :: dp = kind (1d0)
    real (kind = dp) :: x, true
    integer :: m

    interface
    function f(x)
    integer, parameter :: dp = kind(1d0)
    real (kind=dp), intent(in) :: x
    end function f
    function g(x)
    integer, parameter :: dp = kind (1d0)
    real (kind=dp), intent(in) :: x
    end function g
    end interface

    m = 6
    x = 4.0_dp
    call newton(f,g,x,m)
    end program main

    subroutine newton(f,g,x,m)
    interface
    function f(x)
    integer, parameter :: dp = kind(1d0)
    real (kind=dp) :: x
    end function f
    function g(x)
    integer, parameter :: dp = kind(1d0)
    real (kind =dp) :: x
    end function g
    end interface
    integer, parameter :: dp = kind(1d0)
    real (kind=dp) :: x
    integer, intent(in) :: m

    print *, "n x f(x)"
    fx = f(x)
    print *, 0, x, fx
    do n = 1,m
    x = x - fx/g(x)
    fx = f(x)
    print "(i2, f20.16, f20.16)", n, x, fx
    end do
    end subroutine newton

    function f(x)
    integer, parameter :: dp = kind (1d0)
    real (kind = dp) , intent(in) :: x
    f = ((x - 2.0_dp)*x + 1.0_dp)*x - 3.0_dp
    end function f

    function g(x)
    integer, parameter :: dp = kind (1d0)
    real (kind = dp), intent(in) :: x
    g = (3.0_dp*x - 4.0_dp)*x + 1.0_dp
    end function g

    I removed the INTENT(IN) instructions in the subprogramma newton.

    Thanks
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Hi,Below is a fortran program to calculate the roots of an
  1. Programming in fortran (Replies: 5)

  2. Root programming (Replies: 3)

  3. Root programming (Replies: 3)

Loading...