Comp Sci Fortran Program - Calculate the Function

AI Thread Summary
The Fortran program aims to calculate the function F(x) = exp(-a x^2) cos(bx) for specified values of a and b. Users identified that the program outputs all zeros due to the incorrect placement of variable assignments for a and b, which should be defined outside the loop. It is recommended to declare a and b as double precision at the start and to convert I to double precision directly for efficiency. Additionally, printing both x and F is suggested for easier output verification and plotting. Proper adjustments will ensure the program functions correctly and produces the expected results.
Haleemos
Messages
2
Reaction score
0

Homework Statement



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


Homework Equations


...

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
 
Physics news on Phys.org
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
 
Haleemos said:
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 beginning 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.
 

Similar threads

Replies
5
Views
2K
Replies
2
Views
6K
Replies
10
Views
2K
Replies
7
Views
2K
Replies
3
Views
7K
Replies
6
Views
5K
Back
Top