How to fix the Euler method error?

In summary: It's the intermediate step values that I think you want to examine to see how well Euler's method is working.
  • #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
 
Technology news on Phys.org
  • #2
It would help if you described the details of the SHM problem.

Where did the expressions for v(i) and a(i) come from?

I have a problem with the indexing of your t, x, v, and a variables. Using the i+1 index means that when i = 100, you are calculating variables of index = 101, when the maximum dimension is only 100. This is careless programming.

It's not necessarily outside the realm of possibility that a numerical method like Euler could give exact solutions to a certain differential equation.
 
  • Like
Likes 1 person
  • #3
koushan said:
v(i) = -0.0546*sin(3.1305*t(i)) !Velocity
a(i)=-0.1710*cos(3.1305*t(i)) !Acceleration

It seems to me that you are assuming a sinusoidal solution here. You should start with the differential equation for SHM which gives you

a(i) = -c*x(i)

where c is a constant that characterizes the system, e.g. for a mass on a spring c = k/m where k is the spring constant and m is the mass. Then find v(i+1) and x(i+1) as you've done.

Also, these statements should not be there:

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

They replace the current values of t, x, and v with the new ones that you have just calculated. The net effect is to shift each data point "down" one position in the arrays, wiping out the initial values that you set at the beginning of the program.

And as SteamKing noted, don't try to go past the ends of your arrays!
 
Last edited:
  • Like
Likes 1 person
  • #4
Simple Harmonic Motion (SHM) is a sinusoidal motion in time and demonstrates a single resonant frequency. In this equation position is x=A Cos(wt) which A is an amplitude and w is a frequency. Similarly, we can derive velocity and acceleration by differentiating from x (v=dx/dt & a=dv/dt). I put an amount for amplitude and frequency.

You are right. I have changed index to real, dimension(0:100) :: x, v, t and t(i) = t(i-1) + dt and it was run successful but I still have the same problem.
 
  • #5
The replace statement is a part of Euler method that substitute an old value to a new one.
 
  • #6
You need to "replace old values with new ones" if you are storing them (one after the other) in the same variables, without using arrays, so you can advance from one step to the next. However, you're using arrays which contain the results for all steps, and you advance from one step to the next by incrementing the array index.

It's the difference between doing something like this for a trivial constant-velocity problem (without using an array):

Code:
x = 0
t = 0
dt = 0.1
v = 5

do i = 1, 100
    write (*, *) t, x
    t_next = t + dt
    x_next = x + v * dt
    t = t_next
    x = x_next
end do

versus this (using an array):

Code:
x(1) = 0
t(1) = 0
dt = 0.1
v = 5

do i = 1, 100
    write (*, *) t(i), x(i)
    t(i+1) = t(i) + dt
    x(i+1) = x(i) + v * dt
end do

The practical difference between the two versions is that at the end of the first one, you have only the final values of x and t available for future calculations; but at the end of the second one, you have all the values for the intermediate steps available in the arrays.
 

What is the Euler method in Fortran?

The Euler method is a numerical integration technique used to approximate solutions to ordinary differential equations. It is commonly implemented in Fortran, a programming language often used in scientific and engineering applications.

How does the Euler method work?

The Euler method approximates the solution to an ordinary differential equation by breaking it down into smaller steps and using a linear approximation to estimate the solution at each step. The smaller the steps, the more accurate the approximation will be.

What are the advantages of using the Euler method in Fortran?

One advantage of using the Euler method in Fortran is its simplicity and ease of implementation. It is also a relatively fast method compared to other more complex numerical integration techniques. Additionally, Fortran is a high-performance language, making it well-suited for scientific computing tasks.

What are the limitations of the Euler method?

The Euler method is a first-order method, meaning that its accuracy is limited by the size of the steps used. If the steps are too large, the approximation may deviate significantly from the true solution. It also struggles with stiff differential equations, which have rapidly changing solutions.

How can I improve the accuracy of the Euler method in Fortran?

To improve the accuracy of the Euler method, you can decrease the step size used in the approximation. However, this will increase the computational cost. Alternatively, you can use higher-order methods, such as the Runge-Kutta method, which offer greater accuracy but are more complex to implement.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
3K
  • Programming and Computer Science
Replies
2
Views
885
Replies
7
Views
2K
  • Programming and Computer Science
Replies
4
Views
587
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
1
Views
933
  • Programming and Computer Science
Replies
22
Views
3K
  • Differential Equations
Replies
1
Views
757
  • Programming and Computer Science
Replies
1
Views
735
Back
Top