### EULER

!Program name: EULER

!Euler’s method for differential equation dy/dx=-xy

!The exact solution: y=exp(-x^2/2)

!Interval X=0 to X=3 where y(0)=1

!y(n+1)=y(n)+h*(-xy)

!Y(n+1) <=> X+h

program euler

real :: x, y, h, exact, error

integer :: i

func(x,y)=-x*y

h=0.1

nstep=3./h ! 30 steps

y=1.   !y(0)=1

do i=0,nstep

x=i*h   !It guarantees interval from 0 to 3

y=y+h*func(x,y)

!Y(n+1)=Y(n)+ h* f(x,y)

exact=exp((-(x+h)**2)/2)

error=exact-y

print *, i, x+h, y, exact, error

!x+h is associated with Yn+1. Then x+h = Xn+1

!for h=0.1   y(1)=0.6281     error=0.0216

!for h=0.1   y(3)= 0.00803   error=0.00308

!the error decreases as x increases

end do

Print *,

!smaller h and higher number of steps gives small error

h=0.05

!number of steps increases as h decreases

nstep=3./h  !60 steps

y=1.   !y(0)=1

do i=0,nstep

x=i*h    !It guarantees interval from 0 to 3

y=y+h*func(x,y)

!Y(n+1)=sum Y(n)+ h*sum f(x,y)

exact=exp((-(x+h)**2)/2)

error=exact-y

print *, i, x+h, y,exact, error

!for h=0.05  y(1)=0.6169     error=0.0104

!for h=0.05  y(3)=0.0094    error=0.0016

!the error decreases as x increases

!the error decreases as h decreases

end do

print *,

y=1.

h=0.1

do i=0,10  !10 steps

x=i

y=y+h*func(x,y)

!Y(n+1)=Y(n)+ h*f(x,y)

exact=exp((-(x+h)**2)/2)

error=exact-y

print *, i, x+h, y, exact, error

!the error decreases as x increases

end do

print *,

y=1.

h=0.05

do i=0,10  !10 steps

x=i

y=y+h*func(x,y)

!Y(n+1)=Y(n)+ h*f(x,y)

exact=exp((-(x+h)**2)/2)

error=exact-y

print *, i, x+h, y, exact, error

!Smaller number of steps gives higher error!

!the error decreases as x increases

end do

end program euler