Ecrire un code fortran pour calculer la dérivée en un point d'une fonction

Published: 27 février 2017

DMCA.com Protection Status

Exemple de comment programmer une dérivée en un point en fortran 90:

program derivative_function

implicit none

integer, parameter :: pr = selected_real_kind(15,3)

real(pr) :: x,h
real(pr) :: der_estimate, previous_der_estimate

real(kind=16) :: f

integer :: i,n

x = 2.0
h = 1.0

n = 10

do i = 1, 10

    der_estimate = ( f(x+h) - f(x) ) / h

    if( i == 1) write(6,*) h, der_estimate
    if( i > 1) write(6,*) h, der_estimate, der_estimate - previous_der_estimate

    previous_der_estimate = der_estimate

    h = h / 10.0

end do

end program derivative_function

!----------------------------------------------------------------------------------------!

real(kind=16) function f(x)

implicit none

integer, parameter :: pr = selected_real_kind(15,3)

real(pr), intent(in) :: x

f = x**2.0

end function f

!----------------------------------------------------------------------------------------!

donne

1.0000000000000000        5.0000000000000000     
0.10000000000000001        4.1000000000000014      -0.89999999999999858     
1.0000000000000000E-002   4.0099999999998914       -9.0000000000109992E-002
1.0000000000000000E-003   4.0009999999996992       -9.0000000001921876E-003
1.0000000000000000E-004   4.0001000000078335       -8.9999999186574087E-004
1.0000000000000001E-005   4.0000100000270322       -8.9999980801280799E-005
1.0000000000000002E-006   4.0000010006480116       -8.9993790206577273E-006
1.0000000000000002E-007   4.0000000911533098       -9.0949470177292824E-007
1.0000000000000002E-008   3.9999999756901152       -1.1546319456101628E-007
1.0000000000000003E-009   4.0000003309614831        3.5527136788005009E-007

Dans cet exemple la dérivée de la fonction $f(x) = x^2$ en x = 2 est 4.

Références