Fortran program for n systems of ODE by Huen Method

Hello Everyone
I have written a Fortran 90 program to solve the single equation of ODE (Ordinary differential equation) by the Huen method. But I need a hint for a system of n ODE in the program.
The program that I have for a single equation is as bellow. Any one can help me fo a system of n ordinary differential equation.

program huen
      implicit none
      real::x,y,k1,k2,f,x0,xn,y0,h
      integer::n,i
      open(unit=7, file='data.txt')
      open(unit=8, file='Output.txt')
      write(*,*)'The value of y with their corresponding x are:'
      write(8,*)'The value of y with their corresponding x are:'
      read(7,*)x0,y0,h,xn
      n=(xn-x0)/h
      x=x0
      y=y0
        do  i=1,n
           k1=f(x,y)
           k2=f(x+h,y+k1*h)
           y=y+(1/2.0)*h*(k1+k2)  
           x=x+h 
           write(*,'(f7.1,f7.4)')x,y
           write(8,'(f7.1,f7.4)')x,y
        end do
       end program
          function f(x,y)
          real::x,y,f
          f=y-x
      end function
2 Likes

Maybe: Is there a good Fortran package for SDE, DDE and ODE?

Welcome to the Fortran discourse forum! Is this an exercise? Because as @alozada indicates, there are several ready-made packages around for that sort of things and Heun’s method (correcting your typo as I type ;)) is definitely not the best around (although as always, it may depend on what you want to do).

1 Like

Yes, it is an exercise. The function that I have used is some example, not specific. My problem is How I can modify this code for the n-system of ODE ( more than one equation)?

1 Like

No problem, it is just that you will need a very different answer then :).

Here are a few hints:

  • x is the independent variable, so leave that alone. Turn y, k1 and k2 into arrays.
  • make the function f return an array instead of a scalar.
  • put the function into a module or make it an internal function. The interface will require this.

I leave further details to you.

1 Like

Thank you for your help. But it is better if you edit the program so that I can understand.

Another hint: do not confuse the variable ‘n’ that you already have in your code (where it represents the number of steps in x) with the variable that will represent the order of the system of ODEs (call the latter ‘m’ or something else other than ‘n’)

Thank you. Do you mean Xn and n?
I mean Xn is the value of x at wich y value is required. I can say xf ( final value of x)

No. In your code you have:

The n in that line is the number of steps of size h. In a later post, you asked “How I can modify this code for the n-system of ODE ( more than one equation)?” This n is quite different.

@Worku1 thanks for the question and welcome to the Forum! If this is a homework that your school gave you, then please mark it with the “Homework” tag. The best way to learn is to try implementing what others have suggested here, simply do your best, implement it in the code, search the internet or this forum how to do individual things, and then when you get stuck, post the code that you have and ask a specific question, and others will help you.