Runge-Kutta 4 order method. Severe 161: Array bounds exceeded.

Click For Summary

Discussion Overview

The discussion revolves around a coding issue encountered while implementing the Runge-Kutta 4th order method to solve Rayleigh's differential equation in Fortran. Participants are addressing an "array bounds exceeded" error that arises during execution.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their implementation of the Runge-Kutta method and the specific error encountered regarding array bounds.
  • Another participant points out that Fortran array indices start at 1, indicating that accessing x(0), y1(0), and y2(0) is out of bounds.
  • A different participant suggests modifying the array declarations to start at zero, which could simplify the code and avoid the out-of-bounds issue.
  • There is a mention of a potential follow-up question regarding the output of the WRITE statement, hinting at further complications in the code execution.

Areas of Agreement / Disagreement

Participants generally agree that the out-of-bounds error is due to the use of zero indexing in a language that typically starts indexing at one. However, there is no consensus on the best approach to resolve the issue, as different solutions are proposed.

Contextual Notes

The discussion highlights the importance of understanding array indexing in Fortran, as well as the implications of modifying array bounds on the overall code logic.

Metalman9
Messages
1
Reaction score
0
So here I am trying to solve Rayleigh's differential equation using Rungen-Kutta 4th order analytic method.

This is what the exercise gives me: X(0 to 40), Y1(0)=0.1, Y2(0)=0, H=0.1, μ=0.5

This is what I ve written and gives me this error:

ssd.png


Code:
	IMPLICIT REAL*8(A-K,O-Z)
	DIMENSION X(401)
	DIMENSION Y1(401)
	DIMENSION Y2(401)

	
	F2(X,Y1,Y2)=0.5D0*Y2-(0.5D0/3.0D0*(Y2**3))-Y1

	H=0.1D0
	X(0)=0
	Y1(0)=0.01D0
	Y2(0)=0


	DO I=1,400
	X(I)=X(I-1)+H
	END DO


	DO I=0,400

	K1=H*Y2(I)
	L1=H*F2(X(I),Y1(I),Y2(I))
	K2=H*(Y2(I)+L1/2.0D0)
	L2=H*F2(X(I)+H/2.0D0,Y1(I)+K1/2.0D0,Y2(I)+L1/2.0D0)
	K3=H*(Y2(I)+L2/2.0D0)
	L3=H*F2(X(I)+(H/2.0D0),Y1(I)+(K2/2.0D0),Y2(I)+(L2/2.0D0))
	K4=H*(Y2(I)+L3)
	L4=H*F2(X(I)+H,Y1(I)+K3,Y2(I)+L3)
	
	Y1(I+1)=Y1(I)+(1.0D0/6.0D0)*(K1+2.0D0*K2+2.0D0*K3+K4)
	Y2(I+1)=Y2(I)+(1.0D0/6.0D0)*(K1+2.0D0*K2+2.0D0*K3+K4)
	END DO
	
	WRITE(*,30)X(I),Y1(I),Y2(I)
30	FORMAT(2X,F8.6,2X,F8.6,2X,F8.6)
	

	STOP
	END

Any ideas why am I exceeding array bounds?
 
Technology news on Phys.org
Array indices in Fortran start at 1. You are setting and accessing x(0), y1(0), and y2(0). That index of 0 is out of bounds.
 
You can easily make the array indices start at zero, which may be easier than fixing your index-manipulating code so that it doesn't use a zero index value.

Code:
	DIMENSION X(0:401)
	DIMENSION Y1(0:401)
	DIMENSION Y2(0:401)
 
Your next question will probably be "why doesn't the WRITE statement output the correct values" :smile:

To answer that yourself, print out the value of I after the DO loop. If I remember the FORTRAN standard correctly, it won't be equal to 400.
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 4 ·
Replies
4
Views
8K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 13 ·
Replies
13
Views
6K