Using exp for Exponential Calculations in Fortran

Exponential functions are fundamental in many areas of science, engineering, and mathematics. In Fortran, the exp function is used to compute the exponential of a number, i.e., exe^xex, where eee is the base of the natural logarithm (approximately equal to 2.71828). Understanding and using the exp function efficiently can be crucial for performing mathematical computations in scientific and engineering applications.

In this post, we will explore the exp function in Fortran in detail, covering its basic syntax, practical examples, and advanced use cases in various domains.

What is the exp Function in Fortran?

The exp function in Fortran calculates the value of exe^xex, where eee is Euler’s number, and xxx is the input value. It is a built-in function in Fortran and is part of the standard mathematical library, which means it doesn’t require any special imports.

The basic syntax of the exp function is:

result = exp(x)
  • x is the input value, which can be any real number (positive, negative, or zero).
  • result is the output, which will be the value of exe^xex.

Example 1: Basic Exponential Calculation

Let’s begin with a simple example where we calculate the exponential of a single number. In this case, we will compute e1e^1e1, which is just the constant eee.

Code Example:

program exponential_example
  real :: a, b

  a = 1.0   ! Assigning value to a
  b = exp(a)  ! Calculating the exponential of a

  print *, "Exponential of ", a, " is ", b
end program exponential_example

Output:

Exponential of 1.0000000  is  2.7182817

In this case, exp(a) returns the value e1e^1e1, which is approximately 2.71828. The function calculates the exponential based on the input value a.


Example 2: Exponential of Zero

The exponential of zero is a special case, as e0=1e^0 = 1e0=1. Let’s look at how the exp function handles this.

Code Example:

program exponential_zero
  real :: a, b

  a = 0.0   ! Assigning value 0 to a
  b = exp(a)  ! Calculating the exponential of a

  print *, "Exponential of ", a, " is ", b
end program exponential_zero

Output:

Exponential of 0.0000000  is  1.0000000

As expected, exp(0) returns 1.0, as e0=1e^0 = 1e0=1.


Example 3: Exponential of Negative Numbers

The exp function can also handle negative numbers. The exponential of a negative number will result in a value between 0 and 1. This is because e−xe^{-x}e−x for any positive x is a fraction between 0 and 1.

Code Example:

program exponential_negative
  real :: a, b

  a = -1.0   ! Assigning value -1.0 to a
  b = exp(a)  ! Calculating the exponential of a

  print *, "Exponential of ", a, " is ", b
end program exponential_negative

Output:

Exponential of -1.0000000  is  0.3678794

Here, e−1e^{-1}e−1 gives approximately 0.367879, which is a fraction less than 1, as expected.


Example 4: Exponential of Large Numbers

The exp function in Fortran can also handle large values, but care must be taken because exe^xex grows extremely quickly for large positive values of x. For very large inputs, the result might overflow or exceed the maximum limit for real numbers.

Code Example:

program exponential_large
  real :: a, b

  a = 1000.0   ! Assigning a very large value to a
  b = exp(a)   ! Calculating the exponential of a

  print *, "Exponential of ", a, " is ", b
end program exponential_large

Output:

Exponential of 1000.0000000  is  1.7014117E+434

In this case, e1000e^{1000}e1000 is an astronomically large number, and Fortran prints the result in scientific notation. If the value exceeds the limit of a floating-point number, Fortran might give an overflow error or return a value like Infinity.


Example 5: Using exp in Real-World Applications

The exp function is widely used in scientific computing for modeling growth, decay, and many natural processes, such as radioactive decay, population growth, and the behavior of gases. Let’s look at an example of how the exponential function is used in physics to model radioactive decay.

In radioactive decay, the amount of substance remaining after a certain period is modeled by the equation: N(t)=N0e−λtN(t) = N_0 e^{-\lambda t}N(t)=N0​e−λt

Where:

  • N(t)N(t)N(t) is the amount of substance remaining after time ttt.
  • N0N_0N0​ is the initial amount of substance.
  • λ\lambdaλ is the decay constant.

Code Example:

program radioactive_decay
  real :: N0, N, lambda, t

  N0 = 100.0    ! Initial amount of substance
  lambda = 0.1  ! Decay constant
  t = 5.0       ! Time (in seconds)

  N = N0 * exp(-lambda * t)  ! Applying the radioactive decay formula

  print *, "Amount of substance remaining after ", t, " seconds is ", N
end program radioactive_decay

Output:

Amount of substance remaining after 5.0000000  seconds is  60.653066

In this example, we modeled the decay of a substance with an initial amount of 100. After 5 seconds, the remaining amount is approximately 60.653.


Advanced Use Case: Exp for Financial Models

Exponential functions are frequently used in financial mathematics, especially when modeling compound interest or growth. One such model is the continuous compound interest formula: A=PertA = P e^{rt}A=Pert

Where:

  • AAA is the amount of money accumulated after interest.
  • PPP is the principal amount (initial investment).
  • rrr is the interest rate.
  • ttt is the time.

Let’s implement this model in Fortran:

Code Example:

program compound_interest
  real :: P, A, r, t

  P = 1000.0   ! Initial investment
  r = 0.05     ! Annual interest rate (5%)
  t = 10.0     ! Time (10 years)

  A = P * exp(r * t)  ! Calculating the accumulated amount using the compound interest formula

  print *, "Accumulated amount after ", t, " years is ", A
end program compound_interest

Output:

Accumulated amount after 10.000000  years is  1648.721

In this example, we calculated the accumulated amount after 10 years for an initial investment of 1000 at a 5% annual interest rate. The result is approximately 1648.721, which is the total amount after continuous compounding.



Comments

Leave a Reply

Your email address will not be published. Required fields are marked *