I read about statement functions, such as the example:
C(F) = 5.0*(F - 32.0)/9.0
Isn't this the same as:
C = 5.0*(F - 32.0)/9.0
i.e. without the function part, or maybe I'm missing something?
If they're not the same, when do I need to use a statement function?
C = 5.0*(F - 32.0)/9.0
is just assignment to a variable C, it can be anywhere and is evaluated once every time when the program flow reaches it.
C(F) = 5.0*(F - 32.0)/9.0
is a statement function, and can be evaluated any time it is in the scope by, e.g., C(100) which returns approximately 37.8.
From some code
xx(i) = dx*i
f(a) = a*a
do i = 1, nx
x = xx(i)
print *, f(x)
end do
The f(x) in the print statement is evaluated with each new value of x and yields a new value. The value of x is also result of evaluation of the statement function xx on the previous line.
But statement functions are now (in Fortran 95) declared obsolete. Better use internal functions in any new code. E.g.,
program p
implicit none
!declarations of variables x, i, nx, dx
do i = 1, nx
x = xx(i)
print *, f(x)
end do
contains
real function xx(i)
integer, intent(in) :: i
xx = dx*i
end function
real function f(a)
real, intent(in) :: a
f = a*a
end function
end program
Related
I'm trying to implement Newton's method but I'm getting a confusing error message. In my code you'll see I called external with f1 and f2 which I assumes tells the computer to look for the function but it's treating them as variables based on the error message. I've read the stack overflow posts similar to my issue but none of the solutions seem to work. I've tried with and without the external but the issue still persists. Hoping someone could see what I'm missing.
implicit none
contains
subroutine solve(f1,f2,x0,n, EPSILON)
implicit none
real(kind = 2), external:: f1, f2
real (kind = 2), intent(in):: x0, EPSILON
real (kind = 2):: x
integer, intent(in):: n
integer:: iteration
x = x0
do while (abs(f1(x))>EPSILON)
iteration = iteration + 1
print*, iteration, x, f1(x)
x = x - (f1(x)/f2(x))
if (iteration >= n) then
print*, "No Convergence"
stop
end if
end do
print*, iteration, x
end subroutine solve
end module newton
Program Lab10
use newton
implicit none
integer, parameter :: n = 1000 ! maximum iteration
real(kind = 2), parameter :: EPSILON = 1.d-3
real(kind = 2):: x0, x
x0 = 3.0d0
call solve(f(x),fp(x),x0,n, EPSILON)
contains
real (kind = 2) function f(x) ! this is f(x)
implicit none
real (kind = 2), intent(in)::x
f = x**2.0d0-1.0d0
end function f
real (kind = 2) function fp(x) ! This is f'(x)
implicit none
real (kind = 2), intent(in)::x
fp = 2.0d0*x
end function fp
end program Lab10```
You seem to be passing function results to your subroutine and not the functions themselves. Remove (x) when calling solve() and the problem will be resolved. But more importantly, this code is a prime example of how to not program in Fortran. The attribute external is deprecated and you better provide an explicit interface. In addition, what is the meaning of kind = 2. Gfortran does not even comprehend it. Even if it comprehends the kind, it is not portable. Here is a correct portable modern implementation of the code,
module newton
use iso_fortran_env, only: RK => real64
implicit none
abstract interface
pure function f_proc(x) result(result)
import RK
real(RK), intent(in) :: x
real(RK) :: result
end function f_proc
end interface
contains
subroutine solve(f1,f2,x0,n, EPSILON)
procedure(f_proc) :: f1, f2
real(RK), intent(in) :: x0, EPSILON
integer, intent(in) :: n
real(RK) :: x
integer :: iteration
x = x0
do while (abs(f1(x))>EPSILON)
iteration = iteration + 1
print*, iteration, x, f1(x)
x = x - (f1(x)/f2(x))
if (iteration >= n) then
print*, "No Convergence"
stop
end if
end do
print*, iteration, x
end subroutine solve
end module newton
Program Lab10
use newton
integer, parameter :: n = 1000 ! maximum iteration
real(RK), parameter :: EPSILON = 1.e-3_RK
real(RK) :: x0, x
x0 = 3._RK
call solve(f,fp,x0,n, EPSILON)
contains
pure function f(x) result(result) ! this is f(x)
real (RK), intent(in) :: x
real (RK) :: result
result = x**2 - 1._RK
end function f
pure function fp(x) result(result) ! This is f'(x)
real (RK), intent(in) :: x
real (RK) :: result
result = 2 * x
end function fp
end program Lab10
If you expect to pass nonpure functions to the subroutine solve(), then remove the pure attribute. Note the use of real64 to declare 64-bit (double precision) real kind. Also notice how I have used _RK suffix to assign 64-bit precision to real constants. Also, notice I changed the exponents from real to integer as it is multiplication is more efficient than exponentiation computationally. I hope this answer serves more than merely the solution to Lab10.
program prob_1
implicit real*8(a-h, o-z)
f(x) = x**2-cos(x)
df(x) = 2*x+sin(x)
x0 = 0, x1 = 1
do i = 1, 3
if (f((x0+x1)/2) < 0)
x0 = (x0+x1)/2
else
x1 = (x0+x1)/2
end do
print *,"x = ", x
end program
I'm just starting to use Fortran 90.
Now I'm using Code::blocks but I don't know exactly which line the error exists on.
I guess the problem is f((x0+x1)/2) < 0 but don't know actually what is the real error.
what's problem is here?
Be advised that statement functions, the function definitions the OP uses, are obsolescent.
B.3.4 Statement functions
Statement functions are subject to a number of non intuitive restrictions and are a potential source of error because their syntax is easily confused with that of an assignment statement.
The internal function is a more generalized form of the statement function and completely supersedes it.
source: F2018 Standard
Also the notation REAL*8 or anything of that form has never been part of any Fortran standard (see here):
I would suggest to rewrite the code as:
program prob_1
implicit none
double precision :: x1,x0
integer :: i
x0 = 0; x1 = 1
do i = 1, 3
if (f((x0+x1)/2.0D0) < 0) then
x0 = (x0+x1)/2.0D0
else
x1 = (x0+x1)/2.0D0
endif
end do
print *,"x = ", (x0+x1)/2.0D0
contains
function f(x)
double precision, intent(in) :: x
double precision :: f
f = x**2-cos(x)
end function f
function df(x)
double precision, intent(in) :: x
double precision :: df
df = 2.0D0*x+sin(x)
end function df
end program
If you change your program as follows then it will compile:
program prob_1
implicit real*8(a-h, o-z)
f(x) = x**2-cos(x)
df(x) = 2*x+sin(x)
x0 = 0; x1 = 1
do i = 1, 3
if (f((x0+x1)/2) < 0) then
x0 = (x0+x1)/2
else
x1 = (x0+x1)/2
endif
end do
print *,"x = ", x
end program
As mentionned in the comments, you have to add the semicolon ; to separate statements in one line and you have to add the then as well as endif to your if condition.
Hope it helps.
I was using a simple program to compute sinc(x) but forgot to set a return value. gfortran did not complain and the return value is x and not zero. kind of scary ....
Here is my program
implicit none
real :: sinc, x
x = 2.0
print *,'x-sinc(x) ',x,sinc(x)
stop
end
real function sinc(x)
implicit none
real, intent(in) :: x
real :: p
if(abs(x) > 1.e-10) then
p = sin(x)/x
else
p = 1.0
endif
return
end
Again, for any value of x, sinc(x) returns x. I compiled it with no optimization.
I'm relatively new to Fortran and I have an assignment to find quadrature weights and points where the points are the zeros of the nth legendre polynomial (found using Newton's method); I made functions to find the value of Pn(x) and P'n(x) to sub into Newton's method.
However when actually using the functions in my quadrature subroutine it comes back with:
Coursework2a.f90:44.3:
x = x - P(n,x)/dP(n,x)
1
Error: Unclassifiable statement at (1)
Does anybody know any reasons why this statement could be classed as unclassifiable?
subroutine Quadrature(n)
implicit none
integer, parameter :: dpr = selected_real_kind(15) !Double precision
real(dpr) :: P, dP, x, x_new, error = 1, tolerance = 1.0E-6, Pi = 3.141592 !Define Variables
integer, intent(in) :: n
integer :: i
!Next, find n roots. Start with first guess then iterate until error is greater than some tolerance.
do i = 1,n
x = -cos(((2.0*real(i)-1.0)/2.0*real(n))*Pi)
do while (error > tolerance)
x_new = x
x = x - P(n,x)/dP(n,x)
error = abs(x_new-x)
end do
print *, x
end do
end subroutine Quadrature
The line
x = -cos(((2.0*real(i)-1.0)/2.0*real(n))*Pi)
is likely missing a set of brackets around the denominator. As it is, the line divides (2.0*real(i)-1.0) by 2.0, then multiplies the whole thing by real(n). This may be why you get the same root for each loop.
real function p(n,x)
real::n,x
p=2*x**3 !or put in the function given to you.
end function
real function dp(n,x)
real::n,x
dp=6*x**2 !you mean derivative of polynomial p, I guess.
end function
Define function like this separately outside the main program. Inside the main program declare the functions like:
real,external::p, dp
I'm having some troubles to calcule the integral of e^x inside and interval [b.a] using fortran.
I think I'm doing something wrong in the funcion calls. Thanks for helping me.
program trapezium
implicit none
integer :: i, n, b, a
real :: sumation, mean, deltax, f(i), integral
! The value of the integral using the trapezium rule can be found using
! integral = (b - a)*((f(a) +f(b))/2 + sumation_1_n-1 )/n
write(*,*) "type the limits b, a and the number of intervals"
read *, b, a, n
deltax = (b - a)/n
mean = (f(a) + f(b))/2
sumation = 0
do i = 1, n-1
sumation = sumation + f(i)
end do
integral = deltax*(mean + sumation)
write (*,*) "the value of the integral using the trapezoidal method is", integral
end program
function f(x)
real :: f(x)
integer :: x
f(x) = EXP(x)
end function
There are a couple of issues with your code:
f is a function, but at the same time you define an array f(i)
When defining an array of fixed size, the size has to be known at compile time. So real :: f(i) is only valid for a constant i
exp() expects a real variable, not an integer
Integer arithmetic might lead to unexpected results: 1/2 = 0 and not 0.5!
What about (This does not try to fix the maths, though - see my comment):
module functions
contains
function f(x)
implicit none
real :: f
integer,intent(in) :: x
f = EXP(real(x))
end function
end module
program trapezium
use functions
implicit none
integer :: i, n, b, a
real :: sumation, mean, deltax, integral
! The value of the integral using the trapezium rule can be found using
! integral = (b - a)*((f(a) +f(b))/2 + sumation_1_n-1 )/n
write(*,*) "type the limits b, a and the number of intervals"
read *, b, a, n
deltax = real(b - a)/real(n)
mean = (f(a) + f(b))/2
sumation = 0
do i = 1, n-1
sumation = sumation + f(i)
end do
integral = deltax*(mean + sumation)
write (*,*) "the value of the integral using the trapezoidal method is", integral
end program
Note that the use of modules enables the compiler to check for the arguments of the function. Additionally, you do not need to define the return value of the function in the main program.