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

In summary, the code provided is attempting to solve Rayleigh's differential equation using Rungen-Kutta 4th order analytic method. However, it is encountering an error due to exceeding array bounds. This can be easily fixed by starting the array indices at 0 instead of 1.
  • #1
Metalman9
1
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
  • #2
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.
 
  • #3
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)
 
  • #4
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.
 
  • #5


It is difficult to determine the exact cause of the error without seeing the full code and understanding the specific problem you are trying to solve. However, one potential issue could be related to how you are defining and using the arrays X, Y1, and Y2. It may be helpful to double check the size of these arrays and make sure they are properly initialized before the DO loops. It could also be helpful to print out the values of these arrays at each iteration to see if they are changing as expected. Additionally, it may be beneficial to use a debugger or step through the code to identify where the error is occurring. Overall, carefully checking your code and data inputs can help you identify and resolve the issue.
 

1. What is the Runge-Kutta 4 order method?

The Runge-Kutta 4 order method is a numerical method used to solve ordinary differential equations. It is a popular method because it offers a good balance between accuracy and computational efficiency.

2. How does the Runge-Kutta 4 order method work?

The Runge-Kutta 4 order method works by using four different estimates of the derivative at different points within the interval of interest. These estimates are then combined in a weighted average to provide a more accurate approximation of the solution to the differential equation.

3. What are the advantages of using the Runge-Kutta 4 order method?

The Runge-Kutta 4 order method is advantageous because it is relatively simple to implement, has a high degree of accuracy, and is applicable to a wide range of differential equations. It also allows for the estimation of multiple points within the interval, providing a more accurate overall solution.

4. What are some common errors encountered when using the Runge-Kutta 4 order method?

One common error that can occur when using the Runge-Kutta 4 order method is the “Array bounds exceeded” error. This can happen when the step size used in the method is too large, causing the array used to store the solutions to exceed its size limit.

5. How can the “Array bounds exceeded” error be avoided when using the Runge-Kutta 4 order method?

To avoid the “Array bounds exceeded” error, it is important to choose an appropriate step size for the method. This can be done by considering the stability and accuracy requirements of the differential equation being solved. Additionally, using adaptive step sizes or implementing error control techniques can also help prevent this error.

Similar threads

  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
4
Views
6K
  • Programming and Computer Science
Replies
8
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Replies
47
Views
581
  • Programming and Computer Science
Replies
2
Views
788
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
Back
Top