Skip to main content
added 1166 characters in body
Source Link

What I wrote to combine them is this:

    Program RK4
    implicit none

    real k1,k2,k3,k4,h,t,R
    integer i,n
    real a
    read*,n,h 


    t=0
    R=0

    Do i=1,n

      k1=h*(1/a(t))

      k2=h*(1/a(t+h/2.0))

      k3=h*(1/a(t+h/2.0))

      k4=h*(1/a(t+h))

      t=t+h

      R=R+(k1+2*k2+2*k3+k4)*(1/6.0)

      write(*,*)t,R

    End Do

    end program

    !-----------------------------------------

    SUBROUTINE Scale_Factor(h,n,t,a)
      implicit none
      real t,a,k1,k2,k3,k4,h,g
      integer i,n

      t=0
      a=0.001


    Do i=1,n

       k1=h*g(a)

       k2=h*g(a+k1/2.0)

       k3=h*g(a+k2/2.0)

       k4=h*g(a+k3)

       t=t+h

       a=a+(k1+2*k2+2*k3+k4)*(1/6.0)

       write(*,*)t,a

    END DO
    END SUBROUTINE

    !-------------------------
    FUNCTION g(a)
      implicit none
      real a,g
      g=sqrt((1.0/a)+(1.0/a**2)) 
    END FUNCTION

But I know it is not correct.

What I wrote to combine them is this:

    Program RK4
    implicit none

    real k1,k2,k3,k4,h,t,R
    integer i,n
    real a
    read*,n,h 


    t=0
    R=0

    Do i=1,n

      k1=h*(1/a(t))

      k2=h*(1/a(t+h/2.0))

      k3=h*(1/a(t+h/2.0))

      k4=h*(1/a(t+h))

      t=t+h

      R=R+(k1+2*k2+2*k3+k4)*(1/6.0)

      write(*,*)t,R

    End Do

    end program

    !-----------------------------------------

    SUBROUTINE Scale_Factor(h,n,t,a)
      implicit none
      real t,a,k1,k2,k3,k4,h,g
      integer i,n

      t=0
      a=0.001


    Do i=1,n

       k1=h*g(a)

       k2=h*g(a+k1/2.0)

       k3=h*g(a+k2/2.0)

       k4=h*g(a+k3)

       t=t+h

       a=a+(k1+2*k2+2*k3+k4)*(1/6.0)

       write(*,*)t,a

    END DO
    END SUBROUTINE

    !-------------------------
    FUNCTION g(a)
      implicit none
      real a,g
      g=sqrt((1.0/a)+(1.0/a**2)) 
    END FUNCTION

But I know it is not correct.

added 421 characters in body
Source Link

I want to solve this integral using RK4 by coding in Fortran:

$$R=∫1/a(t) dt → dR/dt=1/a(t) =f(t)$$

Initial point: t=0 (or a=0.001) and R=0

And I have to get a(t) by solving another differential equation: $$da/dt=1/a+1/a^2 =g(a)$$

Initial point: t=0 and a=0.001

I wrote this code to get a(t):

    PROGRAM RK4
      implicit none
      real h,t
      integer n
      read*,h,n
      call Scale_Factor(h,n,t,a)
    END PROGRAM

    !---------------------------------------------

    SUBROUTINE Scale_Factor(h,n,t,a)
      implicit none
      real t,a,k1,k2,k3,k4,h,g
      integer i,n

      t=0
      a=0.001


    Do i=1,n

       k1=h*g(a)

       k2=h*g(a+k1/2.0)

       k3=h*g(a+k2/2.0)

       k4=h*g(a+k3)

       t=t+h

       a=a+(k1+2*k2+2*k3+k4)*(1/6.0)

       write(*,*)t,a

    END DO
    END SUBROUTINE

    !-------------------------
    FUNCTION g(a)
      implicit none
      real a,g
      g=sqrt((1.0/a)+(1.0/a**2)) 
    END FUNCTION

And I have another similar program for solving the first integral. But I need to use a(t) that this program produces to solve the integral. and I do not know how to do itcombine them in a single program.

I want to solve this integral using RK4 by coding in Fortran:

$$R=∫1/a(t) dt → dR/dt=1/a(t) =f(t)$$

Initial point: t=0 (or a=0.001) and R=0

And I have to get a(t) by solving another differential equation: $$da/dt=1/a+1/a^2 =g(a)$$

Initial point: t=0 and a=0.001

I wrote this code to get a(t):

    PROGRAM RK4
      implicit none
      real h,t
      integer n
      read*,h,n
      call Scale_Factor(h,n,t,a)
    END PROGRAM

    !---------------------------------------------

    SUBROUTINE Scale_Factor(h,n,t,a)
      implicit none
      real t,a,k1,k2,k3,k4,h,g
      integer i,n

      t=0
      a=0.001


    Do i=1,n

       k1=h*g(a)

       k2=h*g(a+k1/2.0)

       k3=h*g(a+k2/2.0)

       k4=h*g(a+k3)

       t=t+h

       a=a+(k1+2*k2+2*k3+k4)*(1/6.0)

       write(*,*)t,a

    END DO
    END SUBROUTINE

    !-------------------------
    FUNCTION g(a)
      implicit none
      real a,g
      g=sqrt((1.0/a)+(1.0/a**2)) 
    END FUNCTION

And I have another similar program for solving the first integral. But I need to use a(t) that this program produces to solve the integral. I do not know how to do it.

I want to solve this integral using RK4 by coding in Fortran:

$$R=∫1/a(t) dt → dR/dt=1/a(t) =f(t)$$

Initial point: t=0 (or a=0.001) and R=0

And I have to get a(t) by solving another differential equation: $$da/dt=1/a+1/a^2 =g(a)$$

Initial point: t=0 and a=0.001

I wrote this code to get a(t):

    PROGRAM RK4
      implicit none
      real h,t
      integer n
      read*,h,n
      call Scale_Factor(h,n,t,a)
    END PROGRAM

    !---------------------------------------------

    SUBROUTINE Scale_Factor(h,n,t,a)
      implicit none
      real t,a,k1,k2,k3,k4,h,g
      integer i,n

      t=0
      a=0.001


    Do i=1,n

       k1=h*g(a)

       k2=h*g(a+k1/2.0)

       k3=h*g(a+k2/2.0)

       k4=h*g(a+k3)

       t=t+h

       a=a+(k1+2*k2+2*k3+k4)*(1/6.0)

       write(*,*)t,a

    END DO
    END SUBROUTINE

    !-------------------------
    FUNCTION g(a)
      implicit none
      real a,g
      g=sqrt((1.0/a)+(1.0/a**2)) 
    END FUNCTION

And I have another similar program for solving the first integral. But I need to use a(t) that this program produces to solve the integral and I do not know how to combine them in a single program.

added 421 characters in body
Source Link

I want to solve this integral using RK4 by coding in Fortran:

$$R=∫1/a(t) dt → dR/dt=1/a(t) =f(t)$$

Initial point: t=0 (or a=0.001) and R=0

$$k_1=hf(t_0,R_0 )=h 1/(a(t_0))$$ $$k_2=hf(t_0+h/2,R_0+k_1/2)=h 1/(a(t_0+h/2))$$ $$k_3=hf(t_0+h/2,R_0+k_2/2)=h 1/(a(t_0+h/2))$$ $$k_4=hf(t_0+h,R_0+k_3 )=h 1/a(t_0+h)$$ $$R_1=R_0+ 1/6(k_1+2k_2+2k_3+k_4)$$

And I have to get a(t) by solving another differential equation: $$da/dt=1/a+1/a^2 =g(a)$$

Initial point: t=0 and a=0.001

$$k_1=hg(t_0,a_0 )$$ $$k_2=hg(t_0+h/2,a_0+k_1/2)$$ $$k_3=hg(t_0+h/2,a_0+k_2/2)$$ $$k_4=hg(t_0+h,a_0+k_3 )$$ $$a_1=a_0+ 1/6(k_1+2k_2+2k_3+k_4)$$ I wrote this code to get a(t):

    PROGRAM RK4
      implicit none
      real h,t
      integer n
      read*,h,n
      call Scale_Factor(h,n,t,a)
    END PROGRAM

    !---------------------------------------------

    SUBROUTINE Scale_Factor(h,n,t,a)
      implicit none
      real t,a,k1,k2,k3,k4,h,g
      integer i,n

      t=0
      a=0.001


    Do i=1,n

       k1=h*g(a)

       k2=h*g(a+k1/2.0)

       k3=h*g(a+k2/2.0)

       k4=h*g(a+k3)

       t=t+h

       a=a+(k1+2*k2+2*k3+k4)*(1/6.0)

       write(*,*)t,a

    END DO
    END SUBROUTINE

    !-------------------------
    FUNCTION g(a)
      implicit none
      real a,g
      g=sqrt((1.0/a)+(1.0/a**2)) 
    END FUNCTION

I can write the code of RK4 method in Fortran butAnd I have problem in combining these two Rk4 programsanother similar program for solving the first integral. But I need to write a code that solves the second differential equation and producesuse a(t) and then uses it in solvingthat this program produces to solve the first integral. Can anyone help me?I do not know how to do it.

I want to solve this integral using RK4 by coding in Fortran:

$$R=∫1/a(t) dt → dR/dt=1/a(t) =f(t)$$

Initial point: t=0 (or a=0.001) and R=0

$$k_1=hf(t_0,R_0 )=h 1/(a(t_0))$$ $$k_2=hf(t_0+h/2,R_0+k_1/2)=h 1/(a(t_0+h/2))$$ $$k_3=hf(t_0+h/2,R_0+k_2/2)=h 1/(a(t_0+h/2))$$ $$k_4=hf(t_0+h,R_0+k_3 )=h 1/a(t_0+h)$$ $$R_1=R_0+ 1/6(k_1+2k_2+2k_3+k_4)$$

And I have to get a(t) by solving another differential equation: $$da/dt=1/a+1/a^2 =g(a)$$

Initial point: t=0 and a=0.001

$$k_1=hg(t_0,a_0 )$$ $$k_2=hg(t_0+h/2,a_0+k_1/2)$$ $$k_3=hg(t_0+h/2,a_0+k_2/2)$$ $$k_4=hg(t_0+h,a_0+k_3 )$$ $$a_1=a_0+ 1/6(k_1+2k_2+2k_3+k_4)$$

I can write the code of RK4 method in Fortran but I have problem in combining these two Rk4 programs. I need to write a code that solves the second differential equation and produces a(t) and then uses it in solving the first integral. Can anyone help me?

I want to solve this integral using RK4 by coding in Fortran:

$$R=∫1/a(t) dt → dR/dt=1/a(t) =f(t)$$

Initial point: t=0 (or a=0.001) and R=0

And I have to get a(t) by solving another differential equation: $$da/dt=1/a+1/a^2 =g(a)$$

Initial point: t=0 and a=0.001

I wrote this code to get a(t):

    PROGRAM RK4
      implicit none
      real h,t
      integer n
      read*,h,n
      call Scale_Factor(h,n,t,a)
    END PROGRAM

    !---------------------------------------------

    SUBROUTINE Scale_Factor(h,n,t,a)
      implicit none
      real t,a,k1,k2,k3,k4,h,g
      integer i,n

      t=0
      a=0.001


    Do i=1,n

       k1=h*g(a)

       k2=h*g(a+k1/2.0)

       k3=h*g(a+k2/2.0)

       k4=h*g(a+k3)

       t=t+h

       a=a+(k1+2*k2+2*k3+k4)*(1/6.0)

       write(*,*)t,a

    END DO
    END SUBROUTINE

    !-------------------------
    FUNCTION g(a)
      implicit none
      real a,g
      g=sqrt((1.0/a)+(1.0/a**2)) 
    END FUNCTION

And I have another similar program for solving the first integral. But I need to use a(t) that this program produces to solve the integral. I do not know how to do it.

Source Link
Loading