# Fortran Program - Calculate the Function

• Comp Sci

## Homework Statement

F(x) = exp(-a x^2) cos bx
for -100 =< x >= 100 , a=13 and b=7.

...

## The Attempt at a Solution

program main

implicit double precision (A-H,O-Z)
implicit integer (I-N)

DO I = -100,100
X = I * 1D0

F = DEXP(-A * X**2) * DCOS(B * X)

A = 13
B = 7

PRINT*,F
ENDDO

STOP
END

## Answers and Replies

Related Engineering and Comp Sci Homework Help News on Phys.org
DrClaude
Mentor
Have you tested the program to see if it works?

Because it will not give the right result. Check the order in which you are doing things.

the program works

but the answers all zero

DrClaude
Mentor
but the answers all zero
Are you sure about that? Because it is not the case when I run it.

You only made one mistake, which is that a and b are set too late. And they shouldn't be inside the loop: set them at the begining of the program. Might as well also set them directly as double precision
Code:
a = 13.d0
b = 7.d0
Also, instead of multiplying by 1D0, set I to double precision using
Code:
X = dble(I)
This is more efficient as there will not be an unnecessary multiplication every time.

I also suggest that you print out both x and F, which makes it easier to check the output and plot the results.