Fortran program for oscillator using Euler method

Click For Summary

Discussion Overview

The discussion revolves around a Fortran program intended to solve the oscillator problem using the Euler method. Participants are addressing compilation errors and implementation issues within the code, with a focus on array handling and the structure of the program.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant reports an "Unclassifiable statement" error during compilation and seeks assistance in resolving it.
  • Another participant points out that the variable index 'i' is never defined, which could lead to issues in the DO WHILE loop condition.
  • Concerns are raised about the initialization of the array 't', as only 't(1)' is defined, leaving the rest of the array uninitialized.
  • There are suggestions that the logic in the DO WHILE loop should be reversed, with 'h' being defined before incrementing 't'.
  • Participants note that the function 'dx' is defined but not utilized in the program, raising questions about its relevance.
  • One participant expresses confusion regarding the equation for 'dv', suggesting it seems out of place in the context of the Euler method.
  • In the subroutine 'Euler', it is pointed out that 'x' and 'v' should not have indices as they are not declared as arrays within that scope, and 'dv' is not declared as external.
  • Another participant suggests that the error may stem from differences in file format expectations between the compiler and the program file.

Areas of Agreement / Disagreement

Participants generally agree that there are multiple issues with the implementation of the program, but there is no consensus on the specific cause of the compilation error or the best approach to resolve the various problems identified.

Contextual Notes

There are unresolved issues regarding the initialization of arrays, the definition of variables, and the overall structure of the program. The discussion highlights the need for clarity in variable definitions and the use of functions within the program.

koushan
Messages
5
Reaction score
0
I am trying to run a program with fortran. The program is about solving the Oscillator using Euler Method. I am trying to run this code and applying array arguments (as I want to extend it to 3 dimensions afterwards).
When I try to compile, it comes up with an error "Unclassifiable statement at (1)". I don't know how to resolved this error.
This is my code:

program Oscilator
implicit none

real :: h !(Step size)
integer, parameter :: n=100
real, dimension(1:n) :: x,v,t
external dx,dv
integer :: i

x(1) = 0.017
t(1) = 0.0
v(1) = 0.0

write(*,*) 't x v'

do while(t(i) <= n)
h = t(i+1) - t(i)
call Euler(h,x,v)
write(6,'(4(e12.5,3x))') t(i), x(i), v(i)

t(i)=t(i+1)
x(i)=x(i+1)
v(i)=v(i+1)

end do

end program Oscilator

function dx(t)
implicit none
real dx, t
dx = exp(-0.9*t)*(-2.5*exp(0.557*t)-2.8*exp(-0.557*t))
end function dx

function dv(t)
implicit none
real dv, t
dv=-0.2941*exp(-0.343*t)-5.9439*exp(-1.457*t)
end function dv

subroutine Euler(h,x,v)
implicit none
real :: v,x,h,dv
integer :: i

x(i+1) = x(i) + v(i)*h
v(i+1) = v(i) + dv(i)*h


end subroutine Euler
 
Technology news on Phys.org
Please use
Code:
 tags when attaching source files.

IDK about your error, but the program has some serious errors in implementation.

For example, in the main program, you have a DO WHILE construct calculating values for the arrays t,x, and v.
The problem is, the array index i is never defined or calculated, either inside or outside the DO WHILE construct.

The condition for termination (t(i) <= n) can't be met because what value of the t array should be used?  There are no statements which define any of the values of the t array except for t(1) = 0.0.
 
koushan said:
When I try to compile, it comes up with an error "Unclassifiable statement at (1)".
I don't know either about this error. What compiler are you using?

To add to what SteamKing said, here are some more flagrant errors:

koushan said:
do while(t(i) <= n)
h = t(i+1) - t(i)
t(i) has never been defined. And usually, you would do this the other way around: you define h, and then increment t.

koushan said:
write(6,'(4(e12.5,3x))') t(i), x(i), v(i)

t(i)=t(i+1)
x(i)=x(i+1)
v(i)=v(i+1)
If you're only going to print out t, x, and v, there is no need to store them in an array.


koushan said:
function dx(t)
implicit none
real dx, t
dx = exp(-0.9*t)*(-2.5*exp(0.557*t)-2.8*exp(-0.557*t))
end function dx
This function is never used.


koushan said:
dv=-0.2941*exp(-0.343*t)-5.9439*exp(-1.457*t)
This is an extremely strange equation to see in combination with the Euler method. You seem to already know what ##v(t)## is. What problem are you actually trying to solve?

koushan said:
subroutine Euler(h,x,v)
implicit none
real :: v,x,h,dv
integer :: i

x(i+1) = x(i) + v(i)*h
v(i+1) = v(i) + dv(i)*h

end subroutine Euler
x and v are not arrays in the subroutine, so they shouldn't have any index. dv is not declared as external. dv takes a real as an argument, not an integer. i is never defined.
 
Most likely, the error is because you and your compiler have different ideas about whether the program file is in fixed format or free format. Or, you are using an editor which has added some unwanted (and invisible) characters to the file.

But as others have said, there are plenty more problems to fix after have sorted that out!
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 20 ·
Replies
20
Views
3K
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
10K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K