Fortran 90, Euler's method help

In summary, the conversation discusses a program that approximates a differential equation using Euler's method. The program asks for initial values and a step size, but there is an issue with the "do" loop not correctly evaluating the function. The solution is to set the variables equal to the initial values before the loop.
  • #1
fluidistic
Gold Member
3,923
261
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 : [tex]x'=2x[/tex] and [tex]x(0)=1[/tex] 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
  • #2
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.
 
  • #3
Thanks alphysicist! That works great now.
 

What is Fortran 90?

Fortran 90 is a programming language used for scientific and engineering applications. It is an extension of the original Fortran language, with added features such as dynamic arrays, modules, and improved control structures.

What is Euler's method?

Euler's method is a numerical method used for solving ordinary differential equations. It approximates the solution by stepping through small intervals and using the derivative at each point to estimate the next point.

How does Fortran 90 support Euler's method?

Fortran 90 has built-in functions and syntax that make it easier to implement Euler's method. For example, it has a DO loop structure that can be used to iterate through the small intervals, and it supports the use of arrays and mathematical functions for performing calculations.

What are the advantages of using Fortran 90 for Euler's method?

Fortran 90 is a high-performance language, optimized for scientific and engineering calculations. It allows for efficient use of memory and has a wide range of built-in functions and features that make it well-suited for implementing numerical methods like Euler's method.

Are there any limitations to using Fortran 90 for Euler's method?

While Fortran 90 is a powerful language for scientific computing, it may not be as user-friendly as some other programming languages. It also requires knowledge of numerical methods and how to implement them, so some background in mathematics and programming may be necessary.

Similar threads

  • Programming and Computer Science
Replies
4
Views
603
  • Programming and Computer Science
Replies
12
Views
960
  • Programming and Computer Science
Replies
1
Views
749
  • Differential Equations
Replies
1
Views
767
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
Replies
7
Views
2K
  • Programming and Computer Science
Replies
1
Views
940
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Programming and Computer Science
Replies
4
Views
3K
Back
Top