Fortran programming to solve ODEs

In summary, a Fortran program using the second order Adams-Bashforth method was written to generate an approximate solution to a first order differential equation with initial condition. The program also includes a function to plot a partial derivative using a CSV file, which can then be imported and plotted in MATLAB.
  • #1
ra_forever8
129
0
Consider the first order differential equation
\[\frac{dy}{dy} = f(t,y) = -16 t^3 y^2\]
with initial condition $y(0)=1$

Using second order Adams-Bashforth method, write a Fortran programming to generate an approximate solution to the problem.

Solution

Fortran:
Program adams
Implicit None 
Real, allocatable :: $y(:),t(:)$
 Real:: yo, tend, h, k1,k2
Integer:: NI, i
Real,external ::f

!Asking to enter the initial vaule of yo, final time and numbers of step
Print*,  'Enter yo,Tend,NI'  
read*, yo,Tend,NI
h= Tend/NI
Print*, 'This gives stepsize h=',h 
allocate (t(0:NI), y(0:NI))

!Initial Conditions
t(0)=0
y(0) = 1

!After using runge kutta method, found out k1 =0 and k2= -16h^3,
   k1=0
   k2= -16*h**3

!we know that y(n+1) =y(n) + h/2(k1+K2)  at n=0
y(1) = y(0) + h/2 *( k1+ k2) 

!  Loop through the number of steps to calculate the following at each step  
do i=2, NI  
  t(i)= i*h  
write(10,*) i, t(i),t(i-1) 
!Second order Adam bashforth for all n 
  y(i)= y(i-1) + (h/2)*(3*f(t(i-1), y(i - 1))- f(t(i-2), y(i-2))) 
 end do
 end program adams

!declaring function
   Real Function f(t,y)
   Real:: t
   Real:: y
   f = - 16*t**3*y**2
   Return   
   End Function f

Now i want to plot this partial derivative ∂f/∂y = |-32 (4t^4 +1)^-1 t^3| against t using fortran to generate a file or something for the MATLAB to plot the graph.
Can someone help me
 
Last edited by a moderator:
Mathematics news on Phys.org
  • #2
If you write your Fortran output as a CSV file then a MATLAB script can import it via the readmatrix() function and then one can plot using MATLAB's plot() function.

https://www.mathworks.com/help/matlab/ref/readmatrix.html

A CSV file is basically a text file with each line representing a row in a spreadsheet and each line containing comma separated (or other character like | or tab) lists of numbers ala 1,2,3,4,5

As an example, y=x^2 in CSV format might look like this:

Code:
1,1
2,4
3,9
4,16
5,25
...

https://www.mathworks.com/help/matlab/ref/plot.html

This should give you enough info to write a simple plot program in MATLAB.
 
  • Like
Likes Greg Bernhardt

FAQ: Fortran programming to solve ODEs

What is Fortran programming and how is it used to solve ODEs?

Fortran (short for Formula Translation) is a programming language commonly used in scientific and engineering applications. It is particularly well-suited for solving complex mathematical problems, such as ordinary differential equations (ODEs). Fortran is used to write code that can solve ODEs numerically, providing a solution to the equations at different points in time.

How does Fortran handle the numerical integration of ODEs?

Fortran uses numerical integration methods, such as the Runge-Kutta method, to solve ODEs. These methods discretize the ODEs into a series of smaller steps, allowing for the calculation of the solution at each time step. Fortran also allows for the use of adaptive step-size control, improving the accuracy and efficiency of the integration process.

What are the advantages of using Fortran for ODE solving?

Fortran is a high-performance language, meaning it can handle complex mathematical computations quickly and efficiently. This makes it a popular choice for solving ODEs, which often involve large sets of equations. Fortran also has built-in functions for handling arrays and matrices, making it easier to implement numerical methods for solving ODEs.

Are there any limitations to using Fortran for ODE solving?

While Fortran is a powerful language for solving ODEs, it does have some limitations. One potential drawback is that it can be difficult to learn for those with no programming experience. Additionally, Fortran is not as widely used as other programming languages, so finding resources and support may be more challenging.

How can I get started with Fortran programming for ODE solving?

If you are interested in using Fortran to solve ODEs, there are many resources available to help you get started. Online tutorials, textbooks, and forums can provide guidance and support. It may also be helpful to have a basic understanding of numerical methods and differential equations before diving into Fortran programming.

Similar threads

Replies
2
Views
995
Replies
7
Views
3K
Replies
4
Views
3K
Replies
2
Views
10K
Replies
36
Views
4K
Replies
1
Views
9K
Replies
15
Views
2K
Back
Top