Fortran Program - Calculate the Function

Click For Summary

Discussion Overview

The discussion revolves around a Fortran program intended to calculate the function F(x) = exp(-a x^2) cos(bx) over the range -100 to 100, with parameters a=13 and b=7. Participants are addressing issues related to the program's implementation and output.

Discussion Character

  • Homework-related, Technical explanation, Debate/contested

Main Points Raised

  • One participant presents a Fortran program to compute the function F(x) but questions the output.
  • Another participant suggests that the program will not yield the correct results due to the order of operations in the code.
  • A different participant claims that the program works but notes that the output is all zeros.
  • One participant challenges the claim of all zeros, stating that their own execution of the program produces different results.
  • Suggestions are made to improve the program by moving the initialization of parameters a and b outside the loop and setting them as double precision from the start.
  • It is proposed to change the way X is assigned to improve efficiency by using double precision directly instead of multiplying by 1D0.
  • There is a recommendation to print both x and F to facilitate output verification and plotting.

Areas of Agreement / Disagreement

Participants express differing views on the functionality of the program, with some asserting it works while others claim it does not produce the expected results. There is no consensus on the correctness of the output or the program's implementation.

Contextual Notes

Participants highlight potential issues with variable initialization and efficiency in the code, but the discussion does not resolve these concerns definitively.

Who May Find This Useful

This discussion may be useful for individuals interested in Fortran programming, particularly in the context of numerical computations and debugging code related to mathematical functions.

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 ·
Replies
5
Views
2K
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 10 ·
Replies
10
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 6 ·
Replies
6
Views
5K