Fortran programming for nonlinear ode

In summary: Print out the Approximate solution... write(10,*) 'x =[', t(0),x(0) do i = 0, NIwrite(10,*) t(i),x(i)end dowrite(10,*) t(NI),x(NI),']'write(10,*) 'y =[', t(0),y(0) do i = 0, NIwrite(10,*) t(i),y(i)end dowrite(10,*) t(NI),y(NI),']'write(10,*) 'z =[', t(0),z(0) do i = 0
  • #1
ra_forever8
129
0
Adapt the fortran programming using second order adams bashforth method to generate a numerical solution of the Lorenz system:
dx/dt =-10x+10y
dy/dy=28x-y-x*z
dz/dt= x*y- (8/3)*z
with initial condition x(0)=y(0)=0, z(0)=2 slightly perturbed. Plot x and z against t runs from 0 to 15, and also z against x for a chosen value of h.

Here is my programming:

Program adamsthree
Implicit None
Real, allocatable :: y(:),t(:),z(:),x(:)
Real:: tend, h, k1,k2,k3,k4,k5,k6
Integer:: NI, i
Real,external ::f
Print*, 'Enter the final time '
read*, Tend
Print*, 'Enter number of timesteps to take'
read*, NI
h= Tend/NI
Print*, 'This gives stepsize h=',h
allocate (t(0:NI), y(0:NI),x(0:NI),z(0:NI))
!Initial Conditions
t(0) = 0
x(0) = 0
y(0) = 0
z(0) = 2
!After using runge kutta method I found out:
!k1 =0 and k2= 27h, k3=-16/3,k4=h-(32/3),k5=0,k6 =-10*h+h
k1 = 0
k2= 27*h
k3=-16/3
k4= h-(32/3)
k5 = 0
k6 = -10*h+h
!we know that y(i+1) =y(i) + h/2(k1+k2),z(i+1) = z(n) + h/2(k3+k4)
! x(i+1) =x(i) + h/2(k5+k6) at i=0

y(1) = y(0) + h/2 *( k1 + k2)
z(1)= z(0) + h/2 *( k3 + k4)
x(1)= x(0) + h/2 *( k5 + k6)
t(1) = h

open(10,file='adams_threeresults.m')
! Loop through the number of steps to calculate the following at each step
do i = 2, NI
t(i) = i*h
!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)))
x(i) = x(i-1) + (h/2)*(3*f(t(i-1), x(i - 1))- f(t(i-2), x(i-2)))
z(i) = y(i-1) + (h/2)*(3*f(t(i-1), z(i - 1))- f(t(i-2), z(i-2)))
end do

!Print out the Approximate solution
write(10,*) 'x =[', t(0),x(0)
do i = 0, NI
write(10,*) t(i),x(i)
end do
write(10,*) t(NI),x(NI),']'

write(10,*) 'y =[', t(0),y(0)
do i = 0, NI
write(10,*) t(i),y(i)
end do
write(10,*) t(NI),y(NI),']'

write(10,*) 'z =[', t(0),z(0)
do i = 0, NI
write(10,*) t(i),z(i)
end do
write(10,*) t(NI),x(NI),']'

write(10,*) "plot(time,'or',x(:,1),x(:,2),'g',z:,1),z(:,2),'r')"
write(10,*) "xlabel('time'),ylabel('y'),legend('x and z','time')"
write(10,*) "plot(x(:,1),x(:,2),'g',z:,1),z(:,2),'r')"
write(10,*) "xlabel('time'),ylabel('y'),legend('x against z')"
close(10)

end program adamsthree

!declaring function
Real Function f(t,y)
Real:: t
Real:: y
f = 28*x-y-x*z
End Function f

Real Function f(t,x)
Real:: t
Real:: x
f = -10*x + 10*y
End Function f

Real Function f(t,z)
Real:: t
Real:: z
f = x*y - (8/3)*z
End Function f

(I am trying to creat a file for mathlab to plot the graph.)

I got these warning:

warning 767 - The argument T has not been used
warning 298 - Variable X has been used without being given an initial value
warning 298 - Variable Z has been used without being given an initial value
warning 767 - The argument T has not been used
warning 298 - Variable Y has been used without being given an initial value
warning 767 - The argument T has not been used
warning 298 - Variable X has been used without being given an initial value
warning 298 - Variable Y has been used without being given an initial value
*** Multiple definition of symbol: _F
\\ndrive\tg813934\.do_not_delete\desktop.xp\Project 1\lgotemp@.obj
(\\NDRIVE\TG813934\.DO_NOT_DELETE\DESKTOP.XP\PROJECT 1\ADAMSTHREE.F95)
and,
\\ndrive\tg813934\.do_not_delete\desktop.xp\Project 1\lgotemp@.obj
(\\NDRIVE\TG813934\.DO_NOT_DELETE\DESKTOP.XP\PROJECT 1\ADAMSTHREE.F95)
*** Compilation abandoned
Compilation failed.
 
Last edited:
Physics news on Phys.org
  • #2
please edit your post and place code tags around it to preserve indentation.

Based on your warning you have several unintialized variables.

You need to add statements assigning values to them:

x=3.0
...
 
  • #3
Can your please tell me which are my unintialized variables in the code.
How did you get x=3
...
and where to add this statements
Would you please clarify specifically
 
  • #4
ra_forever8 said:
Can your please tell me which are my unintialized variables in the code.
*Read* the output from your compiler! It's right there, in black and white:
ra_forever8 said:
I got these warning:

warning 767 - The argument T has not been used
warning 298 - Variable X has been used without being given an initial value
warning 298 - Variable Z has been used without being given an initial value
warning 767 - The argument T has not been used
warning 298 - Variable Y has been used without being given an initial value
warning 767 - The argument T has not been used
warning 298 - Variable X has been used without being given an initial value
warning 298 - Variable Y has been used without being given an initial value



ra_forever8 said:
How did you get x=3
I think that was a "for example" rather than "use x=3" kind of statement.


You need to initialize all variables before you use them. If you don't you will get garbage results.

That you have uninitialized variables was not the reason your program failed to compile. The problem is that you have defined function f twice. How is the compiler ever going to make sense of that?
 
  • #5
All three of your function routines have the same name 'f', which is confusing if not poor programming. Each function routine contains programming errors in the number or names of the variables used in defining the calculations within the routines.
 
  • #6
Program adamsthree
Implicit None
Real, allocatable :: y(:),t(:),z(:),x(:)
Real:: tend, h, k1,k2,k3,k4,k5,k6
Integer:: NI, i
Real,external ::f,r,s
Print*, 'Enter the final time '
read*, Tend
Print*, 'Enter number of timesteps to take'
read*, NI
h= Tend/NI
Print*, 'This gives stepsize h=',h
allocate (t(0:NI), y(0:NI),x(0:NI),z(0:NI))
!Initial Conditions
t(0) = 0
x(0) = 0
y(0) = 0
z(0) = 2
!After using runge kutta method, I found out k1 =0 and k2= 27h, k3=-16/3,k4=h-(32/3),k5=0,k6 = -10*h+h
k1 = 0
k2= 27*h
k3=-16/3
k4= h-(32/3)
k5 = 0
k6 = -10*h+h
!we know that y(n+1) =y(n) + h/2(k1+K2) at n=0
y(1) = y(0) + h/2 *( k1 + k2)
z(1)= z(0) + h/2 *( k3 + k4)
x(1)= x(0) + h/2 *( k5 + k6)
t(1) = h

open(10,file='adams_threeresults.m')
! Loop through the number of steps to calculate the following at each step
do i = 2, NI
t(i) = i*h
!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)))
x(i) = x(i-1) + (h/2)*(3*r(t(i-1), x(i - 1))- r(t(i-2), x(i-2)))
z(i) = y(i-1) + (h/2)*(3*s(t(i-1), z(i - 1))- s(t(i-2), z(i-2)))
end do

!Print out the Approximate solution
write(10,*) 'x =[', t(0),x(0)
do i = 0, NI
write(10,*) t(i),x(i)
end do
write(10,*) t(NI),x(NI),']'

write(10,*) 'y =[', t(0),y(0)
do i = 0, NI
write(10,*) t(i),y(i)
end do
write(10,*) t(NI),y(NI),']'

write(10,*) 'z =[', t(0),z(0)
do i = 0, NI
write(10,*) t(i),z(i)
end do
write(10,*) t(NI),x(NI),']'

write(10,*) "plot(time,'or',x(:,1),x(:,2),'g',z:,1),z(:,2),'r')"
write(10,*) "xlabel('time'),ylabel('y'),legend('x and z','time')"
write(10,*) "plot(x(:,1),x(:,2),'g',z:,1),z(:,2),'r')"
write(10,*) "xlabel('time'),ylabel('y'),legend('x against z')"
close(10)

end program adamsthree

!declaring function
Real Function f(t,y)
Real:: t
Real:: y
f = 28*x-y-x*z
End Function f

Real Function r(t,x)
Real:: t
Real:: x
r = -10*x + 10*y
End Function r

Real Function s(t,z)
Real:: t
Real:: z
s = x*y - (8/3)*z
End Function sok i changed it but still got warning:
warning 767 - The argument T has not been used
warning 298 - Variable X has been used without being given an initial value
warning 298 - Variable Z has been used without being given an initial value
warning 767 - The argument T has not been used
warning 298 - Variable Y has been used without being given an initial value
warning 767 - The argument T has not been used
warning 298 - Variable X has been used without being given an initial value
warning 298 - Variable Y has been used without being given an initial value
Creating executable: \\ndrive\tg813934\.do_not_delete\desktop.xp\Project 1\adamsthree.EXE
 
  • #7
Please use a [code] tag at the start of your code, and a [/code] tag after the last line.
All of your functions have the same problem, which is the source of the warnings you're getting. In your function definitions, you are saying that t and y are the parameters, but when you calculate a value, you are using x and y. You need to use the same variables as are in your list of parameters. The compiler is doing you a favor by telling you that t is not being used. This will cause your functions to produce incorrect results.
Code:
!declaring function
 Real Function f(t,y)
 Real:: t
 Real:: y
 f = 28*x-y-x*z
 End Function f
 
  • #8
You fixed the function routine names, but you still miss the point. If you define a function r(t,x) and the value of r depends on x and y, that is an error of logic at a minimum. You need to examine all of your function definitions for consistency. This is not hard.
 

Related to Fortran programming for nonlinear ode

1. What is Fortran programming?

Fortran is a high-level programming language commonly used in scientific and engineering applications. It was developed in the 1950s and is designed for numerical and scientific computing, making it well-suited for solving mathematical problems such as nonlinear ordinary differential equations (ODEs).

2. What is an ODE?

An ODE, or ordinary differential equation, is a mathematical equation that describes how a variable changes over time. Nonlinear ODEs are those that do not follow a linear relationship between the variables and their derivatives, making them more complex to solve.

3. How is Fortran used to solve nonlinear ODEs?

Fortran has built-in functions and libraries for solving mathematical problems, including nonlinear ODEs. Programmers can write code using these functions to solve the equations numerically, using methods such as Euler's method or Runge-Kutta methods.

4. What are the advantages of Fortran for solving ODEs?

Fortran is a fast, efficient, and reliable language, making it well-suited for solving complex mathematical problems like nonlinear ODEs. It also has a long history in scientific computing, with a large community of support and a vast array of libraries and tools available for numerical computation.

5. Are there any drawbacks to using Fortran for ODEs?

One potential drawback of using Fortran for solving ODEs is its syntax, which can be more complex and less user-friendly compared to other languages. Additionally, Fortran's main use is in scientific and engineering applications, so it may not have the breadth of features and libraries available for other types of programming tasks.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
938
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
898
  • Engineering and Comp Sci Homework Help
Replies
1
Views
959
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
867
Back
Top