Fortran programming to solve ODEs

AI Thread Summary
The discussion focuses on implementing the second-order Adams-Bashforth method in Fortran to solve the first-order differential equation \(\frac{dy}{dy} = -16 t^3 y^2\) with the initial condition \(y(0)=1\). The provided Fortran program initializes parameters, computes the solution iteratively, and defines a function for \(f(t,y)\). Additionally, there is a request for assistance in generating a CSV file from the Fortran output to facilitate plotting in MATLAB. The conversation emphasizes the ease of importing CSV data into MATLAB for visualization. The overall goal is to create an effective workflow for solving ODEs and visualizing results.
ra_forever8
Messages
106
Reaction score
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
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
Thread 'Video on imaginary numbers and some queries'
Hi, I was watching the following video. I found some points confusing. Could you please help me to understand the gaps? Thanks, in advance! Question 1: Around 4:22, the video says the following. So for those mathematicians, negative numbers didn't exist. You could subtract, that is find the difference between two positive quantities, but you couldn't have a negative answer or negative coefficients. Mathematicians were so averse to negative numbers that there was no single quadratic...
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...
Thread 'Unit Circle Double Angle Derivations'
Here I made a terrible mistake of assuming this to be an equilateral triangle and set 2sinx=1 => x=pi/6. Although this did derive the double angle formulas it also led into a terrible mess trying to find all the combinations of sides. I must have been tired and just assumed 6x=180 and 2sinx=1. By that time, I was so mindset that I nearly scolded a person for even saying 90-x. I wonder if this is a case of biased observation that seeks to dis credit me like Jesus of Nazareth since in reality...

Similar threads

Replies
2
Views
1K
Replies
7
Views
3K
Replies
4
Views
3K
Replies
0
Views
1K
Replies
2
Views
10K
Replies
15
Views
3K
Back
Top