I with Fortran 90 : Simpson's rule

  • Context: Fortran 
  • Thread starter Thread starter fluidistic
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
SUMMARY

The forum discussion focuses on implementing the composite Simpson's rule in Fortran 90 to calculate the integral of the function e^(-x) over the interval [0, 1]. The user initially divides the interval into 100 and 200 subintervals, comparing the results to compute the precision coefficient Q. The correct implementation of the Simpson's rule is emphasized, particularly the handling of floating-point precision and the proper formulation of the integral approximation. Key corrections include ensuring that the endpoint values are not incorrectly factored into the initial approximation and specifying the precision of numeric constants.

PREREQUISITES
  • Fortran 90 programming language
  • Understanding of numerical integration techniques
  • Familiarity with floating-point precision in programming
  • Basic knowledge of mathematical functions and their integration
NEXT STEPS
  • Implement and test the corrected Simpson's rule subroutine in Fortran 90
  • Explore the effects of varying the number of subintervals on integration accuracy
  • Learn about floating-point precision and its impact on numerical computations
  • Investigate alternative numerical integration methods, such as the trapezoidal rule
USEFUL FOR

Students and professionals in computational mathematics, software developers working with numerical methods, and anyone interested in improving their Fortran programming skills for mathematical applications.

  • #31
Is the program in this case a good one to use for simpson's composite rule because it gives a value with a very small difference when entering an ood number compared to an even number when simpson's composite needs only an even number of intervals
 
Technology news on Phys.org
  • #32
bigev234 said:
Is the program in this case a good one to use for simpson's composite rule because it gives a value with a very small difference when entering an ood number compared to an even number when simpson's composite needs only an even number of intervals

I think I would have to disagree with that. The difference is not small at all.

The value of the integral (integrating exp(-x) from 0 to 1) should be 0.63212 to five digits.

When using n=100 intervals, you get that value; if you use n=101 intervals, you get 0.628448. That's about a half a percent difference, which I would say is a huge error for this integral.


For that matter, you can just replace the function given in the program with 1; that is, calculate the integral of 1 from 0 to 1. Of course it should be give 1, right? But the results are:

n=100 ===> integral=1.
n=101 ===> integral=0.990099


The error formula predicts zero error for this type of integral. But for an odd number of intervals it is very close to 1% error--much too large for the integral of a constant. I would say you need to use an even number of subintervals.
 
  • #33
hellloo..how can i make my compoosite simpson fortran code general.i mean...take the function as user's input and solve according..heres the codde..what needs to be changed?.

PROGRAM SIMPSON
C NUMERICAL METHODS: FORTRAN Programs, (c) John H. Mathews 1994
C To accompany the text:
C NUMERICAL METHODS for Mathematics, Science and Engineering, 2nd Ed, 1992
C Prentice Hall, Englewood Cliffs, New Jersey, 07632, U.S.A.
C This free software is complements of the author.
C
C Algorithm 7.2 (Composite Simpson Rule).
C Section 7.2, Composite Trapezoidal and Simpson's Rule, Page 365
C
INTEGER M
REAL A,B,Srule
CHARACTER*60 ANS*1,DFUN,FUN
EXTERNAL F
10 CALL INPUTS(A,B,M,DFUN,FUN)
CALL SIMPRU(F,A,B,M,Srule)
CALL RESULT(A,B,M,Srule,DFUN,FUN)
WRITE(9,*)'WANT TO TRY ANOTHER INTERVAL ? <Y/N> '
READ (9,'(A)') ANS
IF (ANS.EQ.'Y' .OR. ANS.EQ.'y') GOTO 10
END

REAL FUNCTION F(X)
REAL X
F=X/(1+X*X)
RETURN
END

SUBROUTINE PRINTFUN(DFUN,FUN)
CHARACTER*(*) DFUN,FUN
FUN ='X/(1+X*X)'
DFUN='X/(1+X*X) DX'
RETURN
END

SUBROUTINE SIMPRU(F,A,B,M,Srule)
INTEGER K,M
REAL A,B,H,Sum,SumEven,SumOdd,Srule,X
EXTERNAL F
H=(B-A)/(2*M)
SumEven=0
DO K=1,(M-1)
X=A+H*2*K
SumEven=SumEven+F(X)
ENDDO
SumOdd=0
DO K=1,M
X=A+H*(2*K-1)
SumOdd=SumOdd+F(X)
ENDDO
Sum=H*(F(A)+F(B)+2*SumEven+4*SumOdd)/3
Srule=Sum
RETURN
END

SUBROUTINE XSIMPRU(F,A,B,M,Srule)
C This subroutine uses labeled DO loop(s).
INTEGER K,M
REAL A,B,H,Sum,SumEven,SumOdd,Srule,X
EXTERNAL F
H=(B-A)/(2*M)
SumEven=0
DO 10 K=1,(M-1)
X=A+H*2*K
SumEven=SumEven+F(X)
10 CONTINUE
SumOdd=0
DO 20 K=1,M
X=A+H*(2*K-1)
SumOdd=SumOdd+F(X)
20 CONTINUE
Sum=H*(F(A)+F(B)+2*SumEven+4*SumOdd)/3
Srule=Sum
RETURN
END

SUBROUTINE INPUTS(A,B,M,DFUN,FUN)
INTEGER I,M
REAL A,B
CHARACTER*(*) DFUN,FUN
CALL PRINTFUN(DFUN,FUN)
DO 10 I=1,18
WRITE(9,*)' '
10 CONTINUE
WRITE(9,*)' SIMPSON`S RULE IS USED TO COMPUTE AN APPROXIMATION'
WRITE(9,*)' '
WRITE(9,*)'FOR THE VALUE OF THE DEFINITE INTEGRAL:'
WRITE(9,*)' '
WRITE(9,*)' '
WRITE(9,*)' B'
WRITE(9,*)' /'
WRITE(9,*)' | ',DFUN
WRITE(9,*)' /'
WRITE(9,*)' A'
WRITE(9,*)' '
WRITE(9,*)' '
WRITE(9,*)'ENTER THE LEFT ENDPOINT A = '
READ(9,*) A
WRITE(9,*)' '
WRITE(9,*)'ENTER THE RIGHT ENDPOINT B = '
READ(9,*) B
WRITE(9,*)' '
WRITE(9,*)'THE NUMBER OF SUBINTERVALS USED IS 2*M'
WRITE(9,*)' ENTER THE NUMBER M = '
READ(9,*) M
WRITE(9,*)' '
RETURN
END

SUBROUTINE RESULT(A,B,M,Srule,DFUN,FUN)
INTEGER I,M
REAL A,B,Srule
CHARACTER*(*) DFUN,FUN
CALL PRINTFUN(DFUN,FUN)
DO 10 I=1,18
WRITE(9,*)' '
10 CONTINUE
WRITE(9,*)' ',B
WRITE(9,*)' /'
WRITE(9,*)' |'
WRITE(9,*)Srule,' ~ | ',DFUN
WRITE(9,*)' |'
WRITE(9,*)' /'
WRITE(9,*)' ',A
WRITE(9,*)' '
WRITE(9,*)' '
WRITE(9,*)'AN APPROXIMATE VALUE FOR THE DEFINITE INTEGRAL OF'
WRITE(9,*)' '
WRITE(9,*)' '
WRITE(9,*)'F(X) = ',FUN
WRITE(9,*)' '
WRITE(9,*)' '
WRITE(9,*)'TAKEN OVER [',A,' ,',B,' ] WAS FOUND.'
WRITE(9,*)' '
WRITE(9,*)'SINCE M = ',M,', THERE WERE ',2*M,' SUBINTERVALS.'
WRITE(9,*)' '
WRITE(9,*)'THE SIMPSON RULE APPROXIMATION IS ',Srule
WRITE(9,*)' '
RETURN
END
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
1
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 20 ·
Replies
20
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K