Help with solving first order ODE using a simple Fortran code, please

In summary, the conversation revolves around trying to solve a first order ODE using a simple Fortran code. The code involves variables (ki) and (v) that depend on (h), which changes from 100 to 1 with a reduction of -1. The goal is to plot (s) as a function of (h) over a period of time, but the issue lies in getting a single plot instead of a series of plots. The solution involves swapping the loops and resetting the time in the outer loop, with 100 steps in t and 10 steps in h. The conversation also includes a discussion on displaying math using double dollar signs in TeX.
  • #1
joseph2015
14
2
New user has been reminded to use the Template when posting schoolwork problems
I am trying to solve the following first order ODE using a simple Fortran code :

$$ ds/dt=k_i * \sqrt{v}$$

where both (ki) and (v) are variables depending on (h) as follows

$$ k_i=\sqrt{χ/h^2}$$
$$v= \mu h$$

where (μ) and (χ) are constants. (the arbitrary values of each of them can be seen from the code attached).

Note: h changes from 100 to 1 with -1 reduction

Now after writing the code, I would like to plot (s) as a function of (h). but what I will get is not a single plot, but a series of plots depending on the number of timesteps. the issue has to do with loops, but I am not sure how to get a single plot of (s) against (h) as it grows over a period of time.

Fortran:
program height
    implicit none

    double precision ki, t, v, s, a, x, tend, dt
    integer count,h,nstep
    real mu
    REAL, PARAMETER :: Pi = 3.1415927
    tend   = 100
    dt     = 10
    nstep  = tend/dt

    write(*,*) 'end time for the integration: ', tend
    write(*,*) 'integration time step: ', dt
    write(*,*) 'number of time steps: ', nstep

    open(30, file='height.agr')
 
    x      = 1.99
    s      = 1.0e-2
    mu     = 2.34
    t      = 0.0

    write(30,*) s, real(h*1.8e+4)

    ! START to Integrate.

    write(*,*) 'Start time integration (', nstep, ' steps) ...'

    do count = 1, nstep
        ! update the time
        t = t + dt

!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       do h  =100,1,-1
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 
          v  =mu*h
          ki =sqrt((x)/h**2)
          a  = ki*sqrt(v)
          s  = s+dt*a

            write(30,*) s, real(h)
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       end do
!++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    end do

    write(*,*) 'end'
    close(30)
    100  format(d14.8,1x,d14.8)
end program height
 

Attachments

  • sh.png
    sh.png
    5.9 KB · Views: 284
Last edited:
Physics news on Phys.org
  • #2
Your write statement is in an inner loop, so you get 100 writes for every time step.
What's worse, s is incremented 100 times for every time step too.
And the outer loop is done only 10 times.

What is it you want to do ?
 
  • Like
Likes berkeman
  • #3
BvU said:
Your write statement is in an inner loop, so you get 100 writes for every time step.
What's worse, s is incremented 100 times for every time step too.
And the outer loop is done only 10 times.

What is it you want to do ?

Thank you very much for your answer

I am trying to follow the change of s as a function of time and h, then plot s vs h.
for example: if s is a strength of a material, while h is height (from 100 to 1cm), I would like to plot s vs h for 100 years time.

Kind regards,

Joseph
 
  • #4
My guess is you want to swap the loops and reset t to 0 in the outer loop. That way you get s(t) with h as a parameter.
And I'd do 100 steps in t and 10 steps in h.

And with double $ to delimit the ##\TeX## source you get displayed math:
$$ \frac {ds}{dt}=k_i \sqrt v$$ $$ k_i=\sqrt{\chi/h^2}=\frac {\sqrt \chi }{h } $$
 
  • #5
BvU said:
My guess is you want to swap the loops and reset t to 0 in the outer loop. That way you get s(t) with h as a parameter.
And I'd do 100 steps in t and 10 steps in h.

And with double $ to delimit the ##\TeX## source you get displayed math:
$$ \frac {ds}{dt}=k_i \sqrt v$$ $$ k_i=\sqrt{\chi/h^2}=\frac {\sqrt \chi }{h } $$

I will try that, thank you very much
 

1. How do I write a simple Fortran code to solve a first order ODE?

To write a simple Fortran code to solve a first order ODE, you will need to understand the basics of Fortran programming language and the concept of solving ODEs. You can start by defining the ODE and its initial conditions, then use a numerical method such as Euler's method or Runge-Kutta method to solve the ODE. There are also many online resources and tutorials available to help you get started.

2. What is the syntax for solving a first order ODE in Fortran?

The syntax for solving a first order ODE in Fortran may vary depending on the specific method you are using. However, the general structure usually involves defining the ODE, setting up a loop to iterate through the desired time steps, and using appropriate mathematical operations to calculate the solution at each time step. It is important to refer to the documentation of the specific method you are using for the correct syntax.

3. What are some common errors encountered when solving first order ODEs in Fortran?

Some common errors when solving first order ODEs in Fortran include incorrect syntax, incorrect initial conditions, and using an inappropriate numerical method for the given ODE. It is important to carefully check your code for any errors and to make sure that the method you are using is suitable for the ODE you are trying to solve.

4. How can I test the accuracy of my Fortran code for solving first order ODEs?

One way to test the accuracy of your Fortran code for solving first order ODEs is to compare the results with known analytical solutions for simple ODEs. You can also try changing the time step size and comparing the results to see if they converge to the same solution. Additionally, there are various error analysis techniques that can be used to assess the accuracy of your code.

5. Are there any recommended Fortran libraries or modules for solving first order ODEs?

Yes, there are several Fortran libraries and modules available for solving first order ODEs, such as ODEPACK, DLSODE, and LSODE. These libraries offer a range of numerical methods and can be easily integrated into your Fortran code. It is recommended to do some research and choose a library that best suits your specific needs and the type of ODE you are trying to solve.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
955
  • Engineering and Comp Sci Homework Help
Replies
2
Views
827
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Advanced Physics Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Programming and Computer Science
Replies
4
Views
619
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
18
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top