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

In summary: Uart for the advice!In summary, the conversation discusses a Fortran program for calculating the roots of an equation using Newton's method. The program encountered an error due to the use of INTENT(IN) statements, but after removing them, it was able to run successfully.
  • #1
Belgium 12
43
0
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
 
Technology news on Phys.org
  • #2


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.
 
  • #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
 

1. What is a Fortran program?

Fortran is a high-level programming language used primarily for scientific and engineering applications. It was developed in the 1950s and is known for its efficiency and use in numeric computation.

2. How does the program calculate the roots?

The program uses a mathematical algorithm, such as the Newton-Raphson method, to iteratively approximate the roots of a given equation. It will continue to refine its approximation until it reaches a desired level of accuracy.

3. What type of equations can be solved using this program?

This program is designed to solve polynomial equations, which are equations with one or more variables raised to non-negative integer powers. It can solve both real and complex roots.

4. Can this program handle multiple equations at once?

No, this particular program is designed to solve one equation at a time. However, multiple equations can be solved by running the program multiple times, each time inputting a different equation.

5. Are there any limitations to this program?

Like any program, there are limitations to what this Fortran program can do. It may have difficulty solving very large or complex equations, or it may not be able to accurately solve equations with very small or very large roots. It is important to understand the capabilities and limitations of any program before using it for a particular task.

Similar threads

  • Programming and Computer Science
Replies
4
Views
607
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
2
Views
947
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
20
Views
1K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
5
Views
7K
Back
Top