"Unexpected Array Reference" in Fortran 77
I'm relatively new to programming, and I'm currently trying to write a program using Fortran 77 that will use the Runge-Kutta method of equation solving to determine the temperature of a spherical ball at a certain time value. Anyways, the equation is fine, doesn't seem to be causing any problems, it seems to be the programming of the RK method itself.
When I tell it to compile, it presents repeated er开发者_运维百科rors about an "unexpected array reference". If anyone can give me some pointers about where I'm going wrong, would be much appreciated. I'll post up the code and the results below:
PROGRAM RKSubroutine
IMPLICIT NONE
DIMENSION t(3), y(3)
func=(-2.2067E-12)*((y**4)-(81E8));
y0=1200;
h1=240;
a=0;
b=480;
func is name of function to be evaluated
a & b are the limits of integration
y0 is the initial condition
h1 is the stepsize
t=[a];
y=[y0];
i=1;
while t(i)<b
h=h1
k1=feval(func,t(i), y(i));
k2=feval(func,t(i)+h/2,y(i)+k1*h/2);
k3=feval(func,t(i)+h/2,y(i)+k2*h/2);
k2=feval(func,t(i)+h,y(i)+k3*h);
y1=y(i)+(k1+2*k2+2*k3+k4)*h/6;
i=i+1;
t(i)=t(i-1)+h1;
stop
end
After a little modification following bradys suggestion below, I'm now only getting the error, seemingly no matter the value of t's dimension:
`In file RK.f:21
while t(i)<b
1
Error: Unexpected array reference at (1)
Many thanks!
It looks like you didn't declare any of your variables. The type of each variable is declared by the first character of the variable name. And unless you explicitly assign a dimension (or size) to a variable it is considered to be a scalar variable. Since you didn't declare anything the compiler doesn't know that t
and y
are supposed to be arrays (hence the "unexpected array reference" warning messages).
I highly recommend you use IMPLICIT NONE
if you can (not sure if it's standard in Fortran 77). Either way always declare every variable you use. In your case you need to declare t
and y
with the proper size:
DIMENSION t(n), y(n)
where you replace n with an integer that represents the actual size. Keep in mind the behavior of the program is undefined if you access the array out of bounds. Where undefined typically means your program will crash.
精彩评论