- #1
koushan
- 5
- 0
Hi everybody,
I am programming a new code for a problem.
The problem is numerically solving the Simple Harmonic Motion using the Euler method. This approach is just an approximate solution and not a exact solution, however when I run the code successfully and plot my data, it comes up as an exact solution and the plot is exactly the same as sinusoidal harmonic motion. (while I am expecting to detect error!)
I'm really not sure where I am going wrong here. Any help would be appreciated. Thank you!
Here is my code:
program SHM_Euler
implicit none
real, dimension(100) :: x, v, t
real, dimension(100) :: a
integer :: i
real :: dt !(Step Size)
x(1) = 0.01745
t(1) = 0.00
dt = 0.1
v(1)=0
write (*,*) ' t x(t) v(t) '
do i=1, 100
t(i+1) = t(i) + dt
v(i) = -0.0546*sin(3.1305*t(i)) !Velocity
a(i)=-0.1710*cos(3.1305*t(i)) !Acceleration
x(i+1) = x(i) + v(i)*dt
v(i+1) = v(i) + a(i)*dt
write(6,'(3(e12.5,3x))') t(i+1), x(i+1), v(i+1)
t(i) = t(i+1)
x(i) = x(i+1)
v(i)=v(i+1)
end do
end program SHM_Euler
I am programming a new code for a problem.
The problem is numerically solving the Simple Harmonic Motion using the Euler method. This approach is just an approximate solution and not a exact solution, however when I run the code successfully and plot my data, it comes up as an exact solution and the plot is exactly the same as sinusoidal harmonic motion. (while I am expecting to detect error!)
I'm really not sure where I am going wrong here. Any help would be appreciated. Thank you!
Here is my code:
program SHM_Euler
implicit none
real, dimension(100) :: x, v, t
real, dimension(100) :: a
integer :: i
real :: dt !(Step Size)
x(1) = 0.01745
t(1) = 0.00
dt = 0.1
v(1)=0
write (*,*) ' t x(t) v(t) '
do i=1, 100
t(i+1) = t(i) + dt
v(i) = -0.0546*sin(3.1305*t(i)) !Velocity
a(i)=-0.1710*cos(3.1305*t(i)) !Acceleration
x(i+1) = x(i) + v(i)*dt
v(i+1) = v(i) + a(i)*dt
write(6,'(3(e12.5,3x))') t(i+1), x(i+1), v(i+1)
t(i) = t(i+1)
x(i) = x(i+1)
v(i)=v(i+1)
end do
end program SHM_Euler