• Support PF! Buy your school textbooks, materials and every day products Here!

Fortran programming for nonlinear ode

  • Comp Sci
  • Thread starter ra_forever8
  • Start date
  • #1
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:

Answers and Replies

  • #2
11,807
5,431
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
D H
Staff Emeritus
Science Advisor
Insights Author
15,393
683
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:
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


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
SteamKing
Staff Emeritus
Science Advisor
Homework Helper
12,796
1,667
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 s


ok 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
33,634
5,291
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
SteamKing
Staff Emeritus
Science Advisor
Homework Helper
12,796
1,667
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 Threads on Fortran programming for nonlinear ode

Replies
4
Views
2K
  • Last Post
Replies
5
Views
4K
Replies
0
Views
1K
Replies
5
Views
1K
Replies
3
Views
916
Replies
22
Views
3K
Replies
2
Views
3K
Replies
6
Views
4K
Replies
2
Views
5K
Replies
1
Views
2K
Top