How to define periodic function in Fortran?

Viewed 96

How can you define a periodic function in Fortran?

For example, f(x) = exp(-x**2), for –10 < x < 10 with period 20.

1 Answers

You should be fine with using MODULO

f = exp(-(MODULO(x-10,20.)-10.)**2)

x-10 is used to shift the repeating interval to -10,10. Otherwise,MODULO(x, 20.) would be periodic on interval 0,20. Because MODULO retuns values [0,20), the final -10 shifts it to [-10,10)

I assumed that x is real. If it has a different kind or even different type, the other argument to MODULO must be of the same type (20._wp, 20.d0, 20,...) as needed. The final -10. may also have to be adjusted to a higher kind.

Related