|
-
Sep 9th, 2006, 02:08 PM
#1
Thread Starter
Lively Member
numeric analysis from vb to fortran
I have been writing in VB for a long time and need to get strong in FORTRAN quick (I've never used FORTRAN). I wrote a program in VB that integrates (using numeric analysis) x^2 from 0 to 1 (which should be 0.3333) and it works fine. As the number of iterations through the loop increases, the result becomes more and more accurate.
I tried to run the same program in FORTRAN with slight syntax changes and I get a very weird result. If I have the loop iterate 1000 times I get a result of 0.332828. If i run the loop 10 000 times I get 0.33293. If i run the loop 1 million times I get 0.3386. If i run the loop 1 billion times I get 3.05E-05. As I increase the number of iterations the accuracy decreases. The strange thing is that the answer is getting bigger for a while and then smaller the more times the loop runs. I should be getting better accuracy with more iterations. The answer seems to hover around an accurate value until I run a lot of iterations and then it plummets.
I apologize for putting a FORTRAN question in a VB forum but the FORTRAN forums I could find weren't very good and I figure most math/engineering people have had a lot of experience with FORTRAN. Thanks for any help you can give.
Here is the vb code and then the FORTRAN code for the same program:
Sub CalcIntegral()
Dim i As Long
Dim dxval As Double, Lower As Double, Upper As Double
Dim dx As Double, x As Double, y As Double
'Set upper and lower limits for integration
Upper = 1
Lower = 0
'dxval represents the number of segments used in the integration
dxval = 1000000000
'dx represents the width of each segment
dx = (Upper - Lower) / dxval
x = Lower
'A For…Next loop calculates the area of each segment and adds up all the areas as a Riemann sum
For i = 1 To dxval
y = y + (x ^ 2 * dx)
x = x + dx
Next i
msgbox y
End Sub
NOW THE FORTRAN VERSION---------------------------------
PROGRAM ERIC
IMPLICIT NONE
real,parameter :: lower=0.0,upper=1.0
real :: dx,x,y,dxval
integer :: i
dxval=1000000000
y=0.0
dx=(upper-lower)/dxval
x=0.0
DO i = 1,dxval, 1
y = y + ((x**2)*dx)
x = x + dx
END DO
write(*,*) y
END PROGRAM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|