Help with Adams-Bashforth program

In summary, the conversation discusses a Fortran95 program that is being used to solve a first order differential equation. The program uses Euler and Adams-Bashforth methods to calculate the values of Y for different values of X. The conversation also mentions issues with the code and suggests posting the code directly instead of in a zip file. After discussing some possible errors in the code, it is determined that the program is working, but has issues with oscillation at certain step sizes.
  • #1
ognik
643
2
Hi - trying to write a Fortran95 program to solve a trivial 1st order DE (dy/dx=-xy), the idea being that once it works it can be used for different forms of f(x,y). I use Euler to get the starting point, then A-B's 2 step formula for the next 2 starting points. It is when I apply the A-B 4-step formula that I get obviously wrong values for Y(n+1) (using debug to compare with expected values). I've looked at this every which way until my head hurts, can't see what I'm doing wrong in the program? (code in attached zip file)
 

Attachments

  • AB-4step-9.zip
    1.2 KB · Views: 221
Technology news on Phys.org
  • #2
It would be helpful if you posted the code right here, rather than posting it in a Zip file that we 1) have to download, and 2) open in some editor. If you have a question about code, it behooves you to make life as easy as possible for those providing help.

If you do this, please enclose your code with BBcode [ code ] and [ /code ] tags (without the extra spaces).
 
  • #3
Understood thanks. -I seem to loose the code's indenting when pasting, can you advise there a way to keep that?
 
  • #4
OK - I had 'replace tabs with spaces' set in my editor, code follows:
Code:
program AB4step
   !Summary: Apply Adams-Bashforth 4 step method to dy/dx = f(x,y) = -xy given y(0)=1
   implicit none
   integer, parameter  :: dp = selected_real_kind(15, 307)
   real(kind=dp)       :: xN,yN,xNplus1,yNplus1,xNminus1,yNminus1,exact,diff
   real(kind=dp)       :: fN, fNminus1, fNminus2, fNminus3, FUNC
   real, parameter  :: lim=4.0
   real  :: h
   integer         :: iter, numsteps
   integer, parameter  :: n=60     
   character(len=n )  :: stars=REPEAT('*',n)
   !----- header ----   
   print *, " "
   print 10, 'Program AB-4step starts, integrating from 0.0 to ', lim
   print *, 'Enter the number of steps to use (zero or negative to stop)'
   read  (*,*) numSteps  ! ensure integer number os steps, adjust h accordingly
   print *, stars
   print *, " "
   print 20, 'Iter.','X(n+1)','F(n)  ', 'Y(n+1)  ', 'Exact  ','Diff  '
   !----- end header ----   
   do while (numSteps.GT.0)     ! allows trials of diff. values of step size h
   !----- Initialise ----   
     h=lim/numSteps
     yNminus1=1.0  ! y(0)  starting point, n=0
     xNminus1=0.0  ! x=0
     fNminus2=0.0
     fNminus1=FUNC(xNminus1, yNminus1)
     yN=yNminus1+(h*fNminus1)  ! using Eulers 1-step to get 2nd starting value, n=1
     xN=xNminus1+h
   !----- Calculate next 2 points using AB 2step (n = 2,3) --------   
     Do iter=1,2   
       fN=FUNC(xN,yN)
       exact=exp(-(xN**2)/2.0)
       diff=exact-yN
       print 50, iter, xN, fN, yN, exact, diff
       yNplus1=yN+(h*((1.5*fN)-(0.5*fNminus1)))
       xNplus1=xN+h
       yNminus1=yN
       xNminus1=xN
       fNminus3=fNminus2
       fNminus2=fNminus1
       fNminus1=fN
       yN=yNplus1
       xN=xNplus1   
     end do
   !----- Calculate rest of points using AB 4 step (n = 4 onward --------   
     Do iter=iter,numsteps   
       fN=FUNC(xN, yN)
       exact=exp(-(xN**2)/2.0)
       diff=exact-yN
       print 50, iter, xN, fN, yN, exact, diff
       yNplus1=yN+((h/24.0)*((55.0*fN)-(59.0*fNminus1)+(37.0*fNminus2)-(9.0*fNminus3)))
       xNplus1=xN+h
       yNminus1=yN
       xNminus1=xN
       fNminus3=fNminus2
       fNminus2=fNminus1
       fNminus1=fN
       yN=yNplus1
       xN=xNplus1   
     end do
     print *, 'The step size used above was ',h
     print *, 'Enter another number of steps value (zero or negative to stop)'
     read  (*,*) numSteps
   end do
!--- admin ----
   print *, 'User selected end'   
   print *, stars  
10   format (A, F8.3)
20   format (A, 5X, 5(A,8X))
50   format (I2, 5X, F8.4, 2X, 4(F15.9, 3X))
end program AB4step
!---------Functions -----------
function FUNC (x, y)
integer, parameter      :: dp = selected_real_kind(15, 307)
real(kind=dp)           :: x, y, FUNC
   FUNC=-x*y
end function
 
  • #5
Here's what I think might be going on. You are using n as if it were a variable, but you have declared it as a parameter, which means that its value doesn't change. In the line that starts with "Do iter = 1, 2" your comment above talks about AB 2step (n = 2, 3). After that loop is finished, iter will be 3, but during the loop iter's value will be 1 in the first iteration and 2 in the second iteration.

In the next loop that starts with "Do iter = iter, numsteps" the comment above says (n = 4 onward), but iter's value starts with 3, not 4.

Also, in the code below, it's possible that you might not be implementing the algorithm correctly.
Code:
Do iter=iter,numsteps  
       fN=FUNC(xN, yN)
       exact=exp(-(xN**2)/2.0)
       diff=exact-yN
       print 50, iter, xN, fN, yN, exact, diff
       yNplus1=yN+((h/24.0)*((55.0*fN)-(59.0*fNminus1)+(37.0*fNminus2)-(9.0*fNminus3)))
       xNplus1=xN+h
       yNminus1=yN
       xNminus1=xN
       fNminus3=fNminus2
       fNminus2=fNminus1
       fNminus1=fN
       yN=yNplus1
       xN=xNplus1  
     end do[/quote]
What I would do is verify that the first loop is producing the right numbers, and if so, calculate by hand what the first one or two iterations of the loop above should be producing, and compare these values with what your code is producing.
 
  • #6
Embarrassed, me. The program is working, but it turns out the algorithm oscillates above a certain step-size. Sorry for any hassle.
 

1. What is Adams-Bashforth program used for?

Adams-Bashforth program is a numerical method used for approximating the solutions of ordinary differential equations (ODEs). It is commonly used in the field of computational fluid dynamics for solving problems related to fluid flow, heat transfer, and chemical reactions.

2. How does Adams-Bashforth program work?

The Adams-Bashforth program is an explicit multistep method, which means that it uses a combination of previous function values to approximate the next value. It uses a polynomial interpolation technique to predict the next value of the dependent variable based on the known values of the function at previous time steps.

3. What are the advantages of using Adams-Bashforth program?

The Adams-Bashforth program has several advantages, including its simplicity, efficiency, and accuracy. It also has the ability to handle stiff equations, which are equations with rapidly changing solutions. Additionally, it is a self-starting method, meaning that it does not require an initial condition to be specified for the first time step.

4. What are the limitations of Adams-Bashforth program?

One of the main limitations of the Adams-Bashforth program is that it is an explicit method, which means that its stability is limited by the step size. This can lead to instability and inaccurate results if the step size is too large. It also requires a significant amount of memory and computational power for solving complex problems.

5. How can I improve the accuracy of my Adams-Bashforth program?

To improve the accuracy of Adams-Bashforth program, you can use a higher order method, such as the Adams-Bashforth-Moulton method, which uses a combination of the Adams-Bashforth and Adams-Moulton methods. You can also decrease the step size, which will result in a more accurate approximation but will increase the computational time. Additionally, ensuring that the initial conditions and boundary conditions are accurate can also improve the accuracy of the program.

Similar threads

  • Programming and Computer Science
Replies
4
Views
619
  • Programming and Computer Science
Replies
32
Views
2K
  • Programming and Computer Science
Replies
2
Views
898
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Sticky
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
13
Views
4K
  • Programming and Computer Science
Replies
6
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
Back
Top