Fortran program for oscillator using Euler method

In summary, the conversation discusses the attempt to run a program written in Fortran to solve the Oscillator using Euler Method. The program includes array arguments and functions for calculating dx and dv. However, there are errors in the code, such as an unclassifiable statement and undefined variables, which need to be resolved. Additionally, there are some fundamental issues with the implementation of the program that need to be addressed.
  • #1
koushan
5
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
  • #2
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.
 
  • #3
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.
 
  • #4
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!
 
  • #5


As a scientist, it is important to carefully examine the code and identify any possible errors or issues. In this case, the error "Unclassifiable statement at (1)" indicates that there is a problem with the syntax or structure of the program. It is likely that there is a missing or misplaced statement that is causing this error.

One possible issue that stands out is the use of the external functions "dx" and "dv". These functions are not properly declared in the program, which could be causing the error. In Fortran, external functions must be declared before they can be used in the main program. To resolve this issue, the functions should be declared with the "external" statement before the program begins.

Another possible issue is the use of the arrays in the program. In Fortran, arrays are typically declared with a specific size, such as "real, dimension(1:n)". However, in the program, the arrays "x", "v", and "t" are declared without a specific size. This could be causing issues when trying to access elements of the array. To fix this, the arrays should be declared with a specific size that matches the number of elements being used in the program.

In addition, it is important to carefully check the logic of the program and ensure that all variables are properly declared and initialized. It is possible that there are other errors or issues that may be contributing to the error message.

Overall, it is important to carefully review the code and make necessary adjustments to ensure that it is syntactically correct and logically sound. With these changes, the program should be able to compile and run successfully.
 

Related to Fortran program for oscillator using Euler method

1. What is Fortran?

Fortran is a programming language used primarily for scientific and engineering applications. It was developed in the 1950s and has been widely used for creating numerical and scientific computing programs.

2. What is an oscillator?

An oscillator is a physical or mathematical system that exhibits periodic or repetitive motion. Examples include a pendulum, a spring-mass system, or an electronic circuit.

3. What is the Euler method?

The Euler method is a numerical method for solving differential equations. It involves approximating the solution of a differential equation by taking small time steps and using the derivative of the function at each time step to calculate the next value.

4. What does the Fortran program for an oscillator using Euler method do?

The Fortran program for an oscillator using Euler method solves the differential equation that describes the motion of an oscillator using the Euler method. It calculates the position and velocity of the oscillator at regular time intervals, allowing for the visualization of its motion over time.

5. How can I use the Fortran program for an oscillator using Euler method in my research?

You can use the Fortran program for an oscillator using Euler method to simulate and analyze the behavior of an oscillator in various conditions. This can help in understanding and predicting the behavior of physical systems and can be applied to a wide range of fields such as physics, engineering, and mathematics.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
684
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
2
Views
922
  • Programming and Computer Science
Replies
20
Views
2K
  • Programming and Computer Science
Replies
1
Views
956
  • Programming and Computer Science
Replies
8
Views
1K
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
Back
Top