Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran 90, Euler's method help

  1. May 4, 2008 #1


    User Avatar
    Gold Member

    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'

    Write(*,*)'Chose n'

    Do j=1,n

    end do

    Real Function f(t,x)
    real, intent(in) :: x,t
    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.
  2. jcsd
  3. May 4, 2008 #2


    User Avatar
    Homework Helper

    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 (Text):

    before the do loop.
  4. May 4, 2008 #3


    User Avatar
    Gold Member

    Thanks alphysicist! That works great now.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook