Why Is My Euler Method in Fortran Not Updating Yn Correctly?

Click For Summary
SUMMARY

The forum discussion addresses a coding issue in a Fortran implementation of the Euler method for solving ordinary differential equations (ODEs). The user encounters a problem where the variable Yn(2) is incorrectly initialized to zero instead of using the value from Yn1(1). Key recommendations include moving the assignment of deltat and ts1 outside the loop and ensuring proper initialization of the tn array. Additionally, it is advised to structure the Euler loop within a time step loop to ensure accurate calculations at each time step.

PREREQUISITES
  • Understanding of the Euler method for numerical solutions of ODEs
  • Familiarity with Fortran programming language
  • Knowledge of variable initialization and scope in programming
  • Basic concepts of iterative loops and time-stepping in numerical methods
NEXT STEPS
  • Review Fortran array initialization techniques
  • Learn about structuring loops in numerical algorithms
  • Explore debugging techniques for Fortran programs
  • Study the implementation of time-stepping methods in numerical analysis
USEFUL FOR

This discussion is beneficial for Fortran programmers, numerical analysts, and anyone implementing the Euler method for solving differential equations who seeks to troubleshoot common coding issues.

sayellow
Messages
1
Reaction score
0
Euler Method in Fortran - HELP!

Using Euler method I want to calculate the equation below, deltat=0.3, 10 times.

Problem: When n=1, Yn1(1) is calculated right. But, in the following steps it should assume that Yn(2)=Yn1(1) and the program is assuming Yn(2)=0, as well as Yn(3)=0,...and so on
Please, can someone help me?

Program Exercise1
!
! This Program will solve the ODE bellow using Euler Method ...
!
! dy(t)
! ______ = 2y(t) + t^2 + t
! dt
!
! Declaration of Variables
!
Implicit none
!
!
Real:: deltat1

Integer:: ts1,n,time
Real:: Yn1(1:11),Yn(1:11),tn(1:11)

Yn(1)=1
do n=1,ts1
deltat=0.3
ts1=10

tn(n)=(deltat*(n-1))+tn(n)

Yn1(n+1)=Yn(n)+deltat*(2*Yn(n)+(tn(n)**2)+tn(n))
Yn(n)=Yn1(n+1)
write (*,*) Yn1(n)

enddo

n=n+1

End
 
Technology news on Phys.org


first move the assignment statements for deltat and ts1 outside of your loop. You also have not initialize tn(0).

I am very uneasy with your incrementing your time variable in step with the Euler method iterations.

I think you should put the Euler loop inside of a time step loop. So you want to iterate a Euler loop at EACH time step.

You may want to look closely at the result of your time step calculation, is it producing what you want it to?
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 4 ·
Replies
4
Views
9K
  • · Replies 26 ·
Replies
26
Views
4K
  • · Replies 5 ·
Replies
5
Views
10K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 17 ·
Replies
17
Views
9K
Replies
2
Views
2K