Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to tabulate a function

Status
Not open for further replies.

ferry2

Technical User
May 6, 2011
20
RO
Hi, guys!
I have to write a console application that tabulate a function in a given interval. This is the code I wrote:

program Calculate_Function

real, dimension(0:200) :: x, y

real :: a, b, h, n

character(len=32) :: file_name

print *, "Input the name of the file: "

read *, file_name

open(unit = 5, file = file_name, action = "write", status = "new")

print *, "Input range of the interval:"
print *, "a = "
read *, n

print *, "b = "
read *, b

print *, "Input step h = "

read *, h

write(unit = 5, fmt = *) " x(i)", " y(x(i))"

do a = n, b, h
y(a) = x(a)** + 2*sin(x(a)) + 4
write(unit = 5, fmt = *) x(a), y(a)
end do

close(unit = 5)

end program Calculate_Function

I don't know how to do the "do" loop so that it calculates the value of the function of each step.
 
- you cannot use a real value as loop index. This feature often exists as extension but don't use it !
- you cannot use "a" (a real value) as array index : this is illegal
- a syntax like "x(a)** +" has no sense even if "a" is integer
- do not open file with an input unit less than 10 : the small unit values are often associated to predefined files. For instance, 5 is normally the standard input and 6 the standard output. Writing on the unit 5 will be problematic !
- indicating the status NEW means that you will be unable to overwrite an existing file, for instance created at a previous run. This is possibly what you want but ...
- about the algorithm, you do not need to store intermediate values in arrays, even if this is possible of course. In addition, you have to solve a small problem because you give the interval bounds and the step size : because of rounding errors , there is no chance to deduce an exact number intervals.. It is better to ask for the number of steps either in place of "b" or in place of "h".

Try something like the following :

Code:
program Calculate_Function

implicit none

real    :: a,b,x,y
integer :: i,n

character(len=32) :: file_name

print *, "Input the name of the file: "

read *, file_name

open(unit = 15, file = file_name, action = "write")

print *, "Input range of the interval:"
print *, "a = "
read *, a

print *, "b = "
read *, b

print *, "number of steps = "

read *, n

write(unit = 15, fmt = *) "    x(i)", "          y(x(i))"

do i = 0,n
   x=a+i*(b-a)/n
   y = x**2 + 2*sin(x) + 4
   write(unit = 15, fmt = *) x, y
end do

close(unit = 15)

end program Calculate_Function

With as demonstration :

Code:
[lcoul@localhost test]$ ifort t38.f90
[lcoul@localhost test]$ ./a.out
 Input the name of the file: 
f.txt
 Input range of the interval:
 a = 
1
 b = 
10
 number of steps = 
5
[lcoul@localhost test]$ more f.txt
     x(i)          y(x(i))
   1.000000       6.682941    
   2.800000       12.50998    
   4.600000       23.17262    
   6.400000       45.19310    
   8.200000       73.12146    
   10.00000       102.9120

François Jacq
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top