Fortran Program - Calculate the Function

In summary, the program calculates the value of a function F(x) for a range of x values using the formula F(x) = exp(-a x^2) cos bx, where a=13 and b=7. The program contains a mistake in the order in which a and b are set, and it is recommended to set them as double precision at the beginning of the program. Printing out both x and F is also suggested for easier verification of the output.
  • #1
Haleemos
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
 
Physics news on Phys.org
  • #2
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
the program works

but the answers all zero
 
  • #4
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.
 
  • #5


I would first commend the use of Fortran for this calculation, as it is a powerful and efficient language for scientific computing. The program provided seems to be a straightforward implementation of the given function, with appropriate use of implicit declarations and a loop to evaluate the function for a range of values.

One suggestion for improvement would be to add comments or documentation to the code to make it easier for others to understand and potentially modify in the future. Additionally, it may be useful to add some error handling in case the input values for a and b are not appropriate.

Overall, this is a solid program for calculating the given function and can be easily modified for other similar calculations.
 

1. What is Fortran?

Fortran is a programming language designed specifically for scientific and engineering applications. It was first developed in the 1950s and has since undergone several updates, with the current version being Fortran 2018.

2. How do I write a Fortran program to calculate a function?

To write a Fortran program to calculate a function, you will need to first define the function using the appropriate syntax and then use the built-in mathematical functions and operators to perform the necessary calculations. It is also important to declare any necessary variables and use proper indentation and formatting for your code to run correctly.

3. What are some benefits of using Fortran for scientific computing?

Fortran is a highly efficient language, with a long history and extensive library of mathematical functions and subroutines specifically designed for scientific and engineering calculations. It also allows for easy parallelization and optimization, making it a popular choice for high-performance computing.

4. Can I use Fortran on different platforms?

Yes, Fortran is a highly portable language and can be used on a variety of platforms, including Windows, Mac, and Linux. However, some minor changes may be needed for the code to run on different platforms.

5. Are there any resources available for learning Fortran?

Yes, there are many resources available for learning Fortran, including online tutorials, books, and community forums. Many universities also offer courses on Fortran as part of their computer science or engineering programs.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
5K
Back
Top