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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simpson 1/3 Program Errors 1

Status
Not open for further replies.

Inexperience

Programmer
Nov 23, 2014
5
US
I am very new to fortran and I have been assigned to write a Simpsons 1/3 function. Below is what I have come up with but it is outputting the area as 0. All help is greatly appreciated.


Program test
IMPLICIT NONE

REAL :: n, simp13, Simpson13
INTEGER, DIMENSION(10) :: V
INTEGER :: h

V = (/4,5,6,7,8,9,10,11,12,13/)
h = 1
n = 9

Simp13=Simpson13(V,h,n)

WRITE (*,*) 'The area is:', Simp13

Pause
End Program test
!*******************************************
REAL FUNCTION Simpson13 (V,h,n)
IMPLICIT NONE

INTEGER, INTENT(IN) :: V(n)
INTEGER, INTENT(IN) :: h
Real :: i,Vodd,Veven,n

Vodd=0
Veven=0

DO i=1,((n-2)/2)
Vodd=Vodd+V(2*i-1)
END DO

Do i=1,((n-2)/2)
Veven=Veven+V(2*i)
END DO

Simpson13=(h/3)*(V(1)+4*Vodd+2*Veven+V(n))

END FUNCTION Simpson13
 
There are a couple of problems.

First, the index to the array needs to be an integer variable; so, you need to declare n an integer.

Second, you need to know about integral division...in the various programming languages that I know, 1/2=0, 2/3=0, 4/7=0 5/2=2 7/3=2, 9/2=4 ....get the picture?

The last line in your listing shows: h/3, but h=1 and, so, h/3=0.

So, you need to need to be aware of typecasting and operand promotion and that kind of things when you put together your mathematical expressions.

 
So I made the changes but I am still having outputs of zero.
Anymore help?

This is what I have now...
Program test
IMPLICIT NONE

REAL :: Simpson13
INTEGER, DIMENSION(7) :: V
INTEGER :: h,n,simp13

V = (/1.000,1.167,1.333,1.500,1.667,1.833,2.000/)
h = 0.1667
n = 6.0

Simp13=Simpson13(V,h,n)

WRITE (*,*) 'The area is:', Simp13

Pause
End Program test
!*******************************************
REAL FUNCTION Simpson13(V,h,n)
IMPLICIT NONE

INTEGER, INTENT(IN) :: V,h,n
REAL :: Vodd, Veven, i

Vodd=0
Veven=0

DO i=1,((n-2)/2)
Vodd=Vodd+V(2*i)
END DO

Do i=1,((n-2)/2)
Veven=Veven+V(2*i+1)
END DO

Simpson13=(h/3.)*(V(1)+4*Vodd+2*Veven+V(n+1))

END FUNCTION Simpson13
 
There still several problems with the code...you really need to be more careful and consistent.

First, you have declared array V to an integer, yet, you assign real numbers to it.

You have declared h to an integer, but you assign not only a real to it, but a number less than 1, which will then be truncated to zero!

You have declared simp13 to be an integer, yet, you expect it to catch the returning value from a real function (truncation will happen).

You really need to make sure you declare all these variables correctly no only in the main program but also in the function and make sure the list of arguments matches the parameters you are going to pass...for example, V in the function is not declared as an array! (and should be real, too).

Inside the function, the loop variable i needs to be an integer variable, too.

 
So I declared n as an integer in the function and declared the h division to produce an actual value but it is still producing a 0 value.
Anymore advice?

This is what I have now...
Program test
IMPLICIT NONE

REAL :: Simpson13
INTEGER, DIMENSION(7) :: V
INTEGER :: h,n,simp13

V = (/1.000,1.167,1.333,1.500,1.667,1.833,2.000/)
h = 0.1667
n = 6.0

Simp13=Simpson13(V,h,n)

WRITE (*,*) 'The area is:', Simp13

Pause
End Program test
!*******************************************
REAL FUNCTION Simpson13(V,h,n)
IMPLICIT NONE

INTEGER, INTENT(IN) :: h,n
INTEGER, DIMENSION(n+1), INTENT(IN) :: V
REAL :: Vodd, Veven, i

Vodd=0
Veven=0

DO i=1,((n-2)/2)
Vodd=Vodd+V(2*i)
END DO

Do i=1,((n-2)/2)
Veven=Veven+V(2*i+1)
END DO

Simpson13=(h/3.)*(V(1)+4*Vodd+2*Veven+V(n+1))

END FUNCTION Simpson13
 
did you see my post, though? I did not think it was going to get through as it seemed the site went into some kind of maintainance mode...
 
Yes I did I corrected the declaration portion. Then made sure the variables matched from the main program to the function and it now puts out a value. It is not correct so it is a math issue which I will hunt down. Thank you for the help it is greatly appreciated!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top