TAYLOR
!Program name: TAYLOR
!Taylor
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)+(h^2)/2*[-y+(x^2)*y)
!Y(n+1) <=> X+h
program taylor
real :: x, y, h, exact, error
integer :: i
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*(-x*y)+(0.5*(h**2))*(-y+(x**2)*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.60526 error=0.00163
!for
h=0.1 y(3)= 0.01256 error=-0.00015
end do
Print
*,
h=0.05
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*(-x*y)+(0.5*(h**2))*(-y+(x**2)*y)
exact=exp((-(x+h)**2)/2)
error=exact-y
print *, i, x+h, y,exact, error
!for
h=0.05 y(1)=0.60621 error=0.000317
!for
h=0.05 y(3)=0.011143 error=-0.000034
end do
stop
Comentários
Postar um comentário