1. Not finding help here? Sign up for a free 30min tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Fortran programming for nonlinear ode

  1. Dec 1, 2013 #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: Dec 1, 2013
  2. jcsd
  3. Dec 1, 2013 #2

    jedishrfu

    Staff: Mentor

    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
    ...
     
  4. Dec 1, 2013 #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
     
  5. Dec 1, 2013 #4

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    *Read* the output from your compiler! It's right there, in black and white:


    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?
     
  6. Dec 1, 2013 #5

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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.
     
  7. Dec 1, 2013 #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
     
  8. Dec 1, 2013 #7

    Mark44

    Staff: Mentor

    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.
     
  9. Dec 1, 2013 #8

    SteamKing

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Fortran programming for nonlinear ode
  1. Fortran Programming (Replies: 5)

Loading...