Fortran Program - Calculate the Function

  • Comp Sci
  • Thread starter Haleemos
  • Start date
  • #1
2
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
 

Answers and Replies

  • #2
DrClaude
Mentor
7,389
3,653
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.
 
  • #3
2
0
the program works

but the answers all zero
 
  • #4
DrClaude
Mentor
7,389
3,653
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.
 

Related Threads on Fortran Program - Calculate the Function

Replies
3
Views
2K
  • Last Post
Replies
5
Views
4K
Replies
0
Views
1K
Replies
5
Views
1K
Replies
7
Views
2K
Replies
2
Views
3K
Replies
6
Views
4K
Replies
1
Views
2K
Replies
22
Views
3K
Top