Fortran Fortran 90, Euler's method help

Click For Summary
The discussion revolves around a program designed to approximate a differential equation using Euler's method. The user initially encounters an issue where the function f(t, x) does not evaluate correctly at the starting values. Specifically, the program fails to set the initial values for t and x before entering the loop that performs the calculations. A suggestion is made to initialize these variables with the user-defined inputs t_0 and x_0 before the loop begins. Implementing this fix resolves the issue, allowing the program to correctly compute the values of the differential equation x' = 2x with the initial condition x(0) = 1 over the interval [0, 1].
fluidistic
Gold Member
Messages
3,931
Reaction score
281
I'm trying to make a program that can approximate a differential equation via the Euler's method.
Here is my program :
Program diff
implicit none

Real :: t_0, x_0, t_f, k_j, h
Real :: t,x
Integer :: n,j

Write(*,*)'Chose t_0 and x_0'
Read(*,*)t_0, x_0
Write(*,*)'Chose t_f'
Read(*,*)t_f

Write(*,*)'Chose n'
Read(*,*)n
h=(t_f-t_0)/n

Do j=1,n
k_j=f(t,x)
x=x+h*k_j
t=t+h

Write(*,*)t,x
end do

Contains
Real Function f(t,x)
real, intent(in) :: x,t
f=2*x
end function

end program

Here I am trying to approximate the solutions of the following differential equation : x'=2x and x(0)=1 in the interval [0,1], using a step h of 0.1. So in my program, I input t_0=0, X_0=1, t_f=1 and n=10.
I know where is the problem of my program. It's in the "do" part. And more exactly here I think "k_j=f(t,x)". For example, the first term should be k_1=f(0,1) so it must be equal to 2. But the program doesn't do it. It doesn't understand that f(t,x) should be evaluated in t=0, x=1 for the first term. How can I fix this? I'm thinking about another "do" part, inside of the one I already have... But can't figure it out. Thanks for your help.
 
Technology news on Phys.org
Hi fluidistic,

It looks to me like you never set the variables equal to the initial values. The is, you had the program ask for t_0 and x_0, but you never actually used those values (except for t_0 to calculate h).

Try putting

Code:
x=x_0
t=t_0

before the do loop.
 
Thanks alphysicist! That works great now.
 
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
Replies
1
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 12 ·
Replies
12
Views
3K