I to solve ODE with rk4 y" + 2y =0 in fortran

In summary, the conversation discussed solving an ordinary differential equation of order two using the RK4 method in FORTRAN. The equation was initially stated to be $y'' + 2y = 0$, but later clarified to be $y'' + 2y = 0$ with $y(0)=1$ and $y'(0)=0$. The conversation also mentioned the importance of not taking advantage of the fact that $y=\cos(\sqrt(2)x)$ is a solution of the equation.
  • #1
mlouky
5
0
How can i solve y" + 2y =0 with RK4 and their program fortran
please I néed your helpe
 
Technology news on Phys.org
  • #2
mlouky said:
How can i solve y" + 2y =0 with RK4 and their program fortran
please I néed your helpe
my ey is y"+2y weith y(x) = cos(2x)
voila mon progam
Program second_ordr
implicit none
integer, parameter :: n=21 ! number of base points
double precision, external :: f
double precision, dimension(1:n) :: x, y, dy ! x, y, y'
integer i

open (unit=22,file="result.dat")

! boundary values
x(1) = 0.0
y(1) = 0.0
x(n) = 1.0
y(n) = 1.0

! assumptions for y'(1) - use dy(1) and dy(2) here only as a storage
dy(1) = 1
dy(2) = 0

write(*,100)
do i=1,n
write (*,101) x(i), y(i), dy(i)
end do

100 format(5x, 'x',11x, 'y',11x, 'dy')
101 format(3(1pe12.4))
stop
end program second_ordr

function f(x,y,dy)
implicit none
double precision f, x, y, dy
f = 0.55*cos(0.55*x)
end function f

subroutine rk4_2d(f,x,y,dy,n)
implicit none
double precision f
integer n
double precision, dimension(1:n) :: x, y, dy
integer i
double precision h,k11,k12,k21,k22,k31,k32,k41,k42

do i=2,n
h = x(i)-x(i-1)
k11 = h*dy(i-1)
k12 = h*f(x(i-1),y(i-1),dy(i-1))
k21 = h*(dy(i-1)+k12/2.0)
k22 = h*f(x(i-1)+h/2.0, y(i-1)+k11/2.0, dy(i-1)+k12/2.0)
k31 = h*(dy(i-1)+k22/2.0)
k32 = h*f(x(i-1)+h/2.0, y(i-1)+k21/2.0, dy(i-1)+k22/2.0)
k41 = h*(dy(i-1)+k32)
k42 = h*f(x(i-1)+h,y(i-1)+k31,dy(i-1)+k32)

y(i) = y(i-1) + (k11 + 2.0*(k21+k31) + k41)/6.0
dy(i) = dy(i-1)+ (k12 + 2.0*(k22+k32) + k42)/6.0
end do
end subroutine rk4_2d
 
  • #3
mlouky said:
my ey is y"+2y weith y(x) = cos(2x)
y(x) = cos(2x) is not a solution of y'' + 2y = 0.
mlouky said:
voila mon progam

Mod note: Added [ code ] and [ /code ] tags, below.
Fortran:
Program second_ordr
implicit none
integer, parameter :: n=21  ! number of base points
double precision, external :: f
double precision, dimension(1:n) :: x, y, dy  ! x, y, y'
integer i

open (unit=22,file="result.dat")

! boundary values
x(1) =  0.0
y(1) =  0.0
x(n) =  1.0
y(n) =  1.0

! assumptions for y'(1) - use dy(1) and dy(2) here only as a storage
dy(1) =  1
dy(2) =  0

write(*,100)
do i=1,n
  write (*,101)  x(i),  y(i),  dy(i)
end do

100 format(5x,  'x',11x,  'y',11x,  'dy')
101   format(3(1pe12.4))
stop
end program second_ordr

function f(x,y,dy)
implicit none
  double precision f, x, y, dy
  f = 0.55*cos(0.55*x)
  end function f

subroutine rk4_2d(f,x,y,dy,n)
implicit none
double precision f
integer n
double precision, dimension(1:n) :: x, y, dy
integer i
double precision h,k11,k12,k21,k22,k31,k32,k41,k42

do i=2,n
  h  = x(i)-x(i-1)
  k11 = h*dy(i-1)
  k12 = h*f(x(i-1),y(i-1),dy(i-1))
  k21 = h*(dy(i-1)+k12/2.0)
  k22 = h*f(x(i-1)+h/2.0, y(i-1)+k11/2.0, dy(i-1)+k12/2.0)
  k31 = h*(dy(i-1)+k22/2.0)
  k32 = h*f(x(i-1)+h/2.0, y(i-1)+k21/2.0, dy(i-1)+k22/2.0)
  k41 = h*(dy(i-1)+k32)
  k42 = h*f(x(i-1)+h,y(i-1)+k31,dy(i-1)+k32)

  y(i) = y(i-1) + (k11 + 2.0*(k21+k31) + k41)/6.0
  dy(i) = dy(i-1)+ (k12 + 2.0*(k22+k32) + k42)/6.0
end do
end subroutine rk4_2d
 
  • #4
Note - you're not suppose to take advantage of the fact that the ODE can be solved, y = c1 sin(sqrt(2) t) + c2 cos(sqrt(2) t).

RK4 is supposed to used with an ODE. Instead of using y and x, it may help to use y for position and t for time. y(t) is a position, y'(t) is a velocity, and y"(t) is an acceleration. For RK4, the ODE should be written as y" = -2y. The initial condition is y(0) = 0, y'(0) = 1, and y"(0) = 0. For a given y"(t), y'(t), y(t), RK4 calculates y'(t+Δt), y(t+Δt), and then y"(t+Δt) = -2 y(t+Δt), t = t+Δt, and the process continues for each time step Δt.
 
Last edited:
  • #5
Marrk44

I explain exactly what I want
I have an ordinary differential equation of order two y "(x) = -2cos (2x)
I am looking to solve numerically the RK4 method to write a program in FORTRAN
with y (0) = 1 and y '(0) = 0
thank you very much
 
  • #6
thanks rcgldr
 
  • #7
mlouky said:
I explain exactly what I want
I have an ordinary differential equation of order two y "(x) = -2cos (2x)
I am looking to solve numerically the RK4 method to write a program in FORTRAN
with y (0) = 1 and y '(0) = 0
thank you very much
That is not what you wrote in your opening post. There you wrote that you want to solve $y'' + 2y = 0$, but you did not specify initial conditions. In your second post you wrote that you want to solve $y'' + 2y = 0$ with $y(x) = cos(2x)$ and boundary conditions $y(0)=0, y(1)=1$. As Mark noted, $y(x) = cos(2x)$ is not a solution of $y'' + 2y = 0$.

So which is it?

If you truly are to numerically solve $y''+2y=0, y(0)=1, y'(0)=0$ using RK4, you should not be taking advantage of the fact that $y=\cos(\sqrt(2)x)$ is the solution.
 
  • #8
ok I understand thank you
 

FAQ: I to solve ODE with rk4 y" + 2y =0 in fortran

1. How do I solve a first-order ordinary differential equation using the Runge-Kutta method (rk4) in Fortran?

To solve a first-order ODE using rk4 in Fortran, you will need to first define the initial conditions, the differential equation itself, and the step size. Then, you can use a series of loops to calculate the values of the dependent variable at each time step using the rk4 algorithm. Finally, you can print out or plot the results to see the solution.

2. What is the rk4 method and how does it work?

The Runge-Kutta method (rk4) is a numerical method for solving ordinary differential equations. It works by approximating the solution at each time step using a weighted average of several intermediate values calculated using the derivative of the function at different points. This method is more accurate than other numerical methods and is widely used in scientific computing.

3. Can I use rk4 to solve higher-order differential equations?

Yes, you can use rk4 to solve higher-order differential equations by converting them into a system of first-order equations. This can be done by introducing new variables to represent the higher-order derivatives and then solving the resulting system using rk4 as described in the first question.

4. Are there any limitations to using rk4 to solve ODEs in Fortran?

While the rk4 method is a powerful tool for solving ODEs, it does have some limitations. For example, it may not be as accurate for stiff or highly oscillatory problems. Additionally, the choice of step size can affect the accuracy of the solution, so it may require some trial and error to find the optimal step size.

5. Can I use pre-existing Fortran libraries or modules for rk4 instead of coding it myself?

Yes, there are many Fortran libraries and modules available that implement the rk4 method for solving ODEs. These libraries may offer additional features and optimizations that can improve the accuracy and speed of the solution. However, it is still important to understand how the rk4 method works and how to implement it in case you encounter any issues or need to customize the code for your specific problem.

Similar threads

Replies
36
Views
4K
Replies
25
Views
1K
Replies
2
Views
1K
Replies
3
Views
2K
Replies
8
Views
2K
Replies
6
Views
1K
Replies
6
Views
3K
Back
Top