Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
9 views2 pages

Assignment

The document contains two Fortran programs that simulate an oscillator using different numerical methods: Euler's Method and the Runge-Kutta 4th Order (RK4) Method. Both programs calculate the position and velocity of the oscillator over time, writing the results to three separate data files. Each program initializes parameters, iterates through a specified number of iterations, and updates the state of the oscillator accordingly.

Uploaded by

munna524407
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views2 pages

Assignment

The document contains two Fortran programs that simulate an oscillator using different numerical methods: Euler's Method and the Runge-Kutta 4th Order (RK4) Method. Both programs calculate the position and velocity of the oscillator over time, writing the results to three separate data files. Each program initializes parameters, iterates through a specified number of iterations, and updates the state of the oscillator accordingly.

Uploaded by

munna524407
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

By Euler's Method :-

program oscillator
implicit none
real :: dxdt, dvdt, k, l, v, x, t, h = 0.001
integer :: i, niter = 25000

dxdt(x, v, t) = v
dvdt(x, v, t) = -x - 0.2 * v ! w = 1, b = 0.2

x = 1
v = 0
t = 0

open(unit=21, file="oscillator1.dat", status="replace")


open(unit=22, file="oscillator2.dat", status="replace")
open(unit=23, file="oscillator3.dat", status="replace")

write(21,*)x, t
write(22,*)v, t
write(23,*)x, v
do i = 1, niter
k = dxdt(x, v, t)
l = dvdt(x, v, t)

x = x + h * k
v = v + h * l
t = t + h
write(21,*)x, t
write(22,*)v, t
write(23,*)x, v
enddo
close(21)
close(22)
close(23)
end program oscillator

By RK4 Method:-

program oscillator
implicit none
real :: dxdt, dvdt, k1, l1, v, x, t, h = 0.001,hh
integer :: i, niter = 25000
real :: k2, k3, k4, l2, l3, l4

dxdt(x, v, t) = v
dvdt(x, v, t) = -x - 0.2 * v ! w = 1, b = 0.2
hh = h/2
x = 1
v = 0
t = 0

open(unit=21, file="oscillator1.dat", status="replace")


open(unit=22, file="oscillator2.dat", status="replace")
open(unit=23, file="oscillator3.dat", status="replace")

write(21,*)x, t
write(22,*)v, t
write(23,*)x, v
do i = 1, niter
k1 = dxdt(x, v, t)
l1 = dvdt(x, v, t)

k2 = dxdt(x + hh * k1, v + hh * l1, t + hh)


l2 = dvdt(x + hh * k1, v + hh * l1, t + hh)

k3 = dxdt(x + hh * k2, v + hh * l2, t + hh)


l3 = dvdt(x + hh * k2, v + hh * l2, t + hh)

k4 = dxdt(x + h * k3, v + h * l3, t + h)


l4 = dvdt(x + h * k3, v + h * l3, t + h)

x = x + (h/6) * (k1 + 2 * k2 + 2 * k3 + k4)


v = v + (h/6) * (l1 + 2 * l2 + 2 * l3 + l4)
t = t + h
write(21,*)x, t
write(22,*)v, t
write(23,*)x, v
enddo
close(21)
close(22)
close(23)
end program oscillator

You might also like