1. Limited time only! Sign up for a free 30min personal 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!

Unresolved external symbol eror in fortran:solution?

  1. Dec 22, 2011 #1
    Mod note: Added code tags and formatted code.

    hello...i have been given an assignment which needs to be done on fortran90....i have come up with my code but it isnt working for me..

    there are four parrts each specifying the values of M1 and angle for which M2 is to be calculated using SECANTS method..

    here is the codde with simplified comments
    Code (Text):

    !here i have declared the arrays
    !note that there are four parts of this question so i have declared two variables with four values each therefore combinining those parts

    !the dimensions of Function f,values of initial gueses x1,x2 and x3 have been declared arbitarily since i do not know the number of iterations needed to come up with the final solution

       
    dimension m1(1:4),rad(1:4),theeta(1:4),f(1:20),x1(1:20),x2(1:20),x3(1:20)
    data (m1(i),i=1,4)/1.0,1.0,1.5,1.5/
    data (theeta(i),i=1,4)/10.0,20.0,10.0,20.0/

    do i=1,4
       rad(i)=theeta(i)*3.142/180
    enddo


    ! here i have started the outer loop so that for each set of M1 and Rad the inner loop shouldd run and calculate the value of M2(taken as x3)
    !for  e.g for M1=1.0 and rad=10.0 this code should calculate the value for M2 but since i do not know how many iterations will be done i have assigned an arbitary value of 10 in the inner loop

    do i=1,4
       do  j=1,20
           
          !here i have set the tolerance
          tol=.00001

          !here is the function
          F(x)=rad(i)-(6.**(1./2.))*(atan((((x(j))**2.)-1.)/6.)**(1./2.))-((atan((((m1(i))**2.)-1.)/6.)**(1./2.)))+(atan((((x(j))**2.)-1.)**(1./2.))-(atan((((m1(i))**2.)-1.)**(1./2.))))

          ! here are the two values of initial guesses(which will keep interchanging at each iteration..which i have set as 10 as a guess)

          x1(j)=2.0
          x2(j)=1.5

          !here is secants formula

    10   x3(j)=(x1(j)*f(x2(j))-x2(j)*f(x1(j)))/(f(x2(j))-f(x1(j)))

          print*,'m1',m1(i),'rad',rad(i),'x1',x1(j),'x2',x2(j),'x3',x3(j)
          x1(j)=x2(j)

          x2(j)=x3(j)  

          if (abs (x1(j)-x2(j)).lt.tol) goto 20

          goto 10

    20   print*,'The value of m2 for part(a) is',x3(j)

        end do

    end do

    end

    all well in compiling but gives the following eror when i build it


    Linking...
    sec.obj : error LNK2001: unresolved external symbol _X@4
    sec.exe : fatal error LNK1120: 1 unresolved externals
    Error executing link.exe.
    sec.exe - 2 error(s), 0 warning(s)


    WHERE HAVE I GONE WRONG?

    please help

    thank you


    i have attached the question paper for your reference
     
    Last edited: Dec 28, 2011
  2. jcsd
  3. Dec 22, 2011 #2

    Mark44

    Staff: Mentor

    I see several problems.
    1. You need to write an actual Fortran function to calculate your function values. You can't just put in the formula of your function as F(x) = ....
    2. The two linker errors are from x in the array expression F(x). You have declarations for x1, x2, and x3, but none for just plain x.
     
  4. Dec 22, 2011 #3
    What I can see in the line 3 it is not necessary to write the array in the form f(1:4) if you need an array with four spaces you can declare it just like this f(4) and as Mark44 said you have to define your function out of the program. Here I write an example:

    PROGRAM DIFFERENTIAL_EQUATIONS_SYSTEM

    WRITE (*,*) 'WRITE A, B, N, ALF1 AND ALF2'

    ....
    ....
    ....
    WRITE (10,*) T,W2
    WRITE (*,*) T,W2

    W1=W1+H*F1(T,W1,W2)
    W2=W2+H*F2(T,W1,W2)
    T=A+I*H

    END DO
    END

    FUNCTION F1(T,W1,W2)
    REAL W1,W2,T
    F1=W2
    RETURN
    END

    FUNCTION F2(T,W1,W2)
    REAL W1,W2,T
    F2=-(9.8/10.0)*w1
    RETURN
    END


    You can see that I use to functions. For example look at function F1 it has the form F1(T,W1,W2) that means that the function reacives 3 values T, W1 and W2, the function takes this 3 values and returns a value F1 the same for F2. Hope this help you
     
  5. Dec 22, 2011 #4



    thank you sir for such a prompt reply...but i fail to understand how to take out the function F(x) and the formula of secant method out of the loop?...
     
  6. Dec 22, 2011 #5
    You probably mean to define F(x) as a Fortran "statement function". Statement functions must be declared before the first executable line in your program, i.e. before your "do i=1,4" statement.

    F seems to be a function of 3 variables: rad(i), x(j), and m1(i). So it would be declared as something like:

    F(a,b,c)=a-(6.**(1./2.))*(atan((((b)**2.)-1.)/6.)**(1./2.))-((atan((((c)**2.)-1.)/6.)**(1./2.)))+(atan((((b)**2.)-1.)**(1./2.))-(atan((((c)**2.)-1.)**(1./2.))))

    and then referenced in your line labeled 10 as, for example, "F(rad(i), x2(j), m1(i))"
     
  7. Dec 22, 2011 #6

    Redbelly98

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    Moderator's note: thread moved from "Other Sciences > Computing & Technology > Programming & Comp Sci" to the Homework & Coursework Questions forums.

    Normal forum rules regarding Homework Help now apply.
     
  8. Dec 23, 2011 #7
    ok i have done the corrections and good enough the unresolved error is gone..but when i execute it gives me a math error...puzzzled and distressed!!!!

    heres the corrected code


    Code (Text):

        !here i have declared the arrays
        !note that there are four parts of this question so i have declared two variables with four values each therefore combinining those parts

        !the dimensions of Function f,values of initial gueses x1,x2 and x3 have been declared arbitarily since i do not know the number of iterations needed to come up with the final solution

         real f(4,20,4)
         dimension m1(1:4),rad(1:4),theeta(1:4),x1(1:20),x2(1:20),x3(1:20)
         
          data (m1(i),i=1,4)/1.0,1.0,1.5,1.5/

          data (theeta(i),i=1,4)/10.0,20.0,10.0,20.0/

         
         
          do i=1,4

       rad(i)=theeta(i)*3.142/180
        enddo


             ! here i have started the outer loop so that for each set of M1 and Rad the inner loop shouldd run and calculate the value of M2(taken as x3)
          !for  e.g for M1=1.0 and rad=10.0 this code should calculate the value for M2 but since i do not know how many iterations will be done i have assigned an arbitary value of 10 in the inner loop



     F(rad,x,m1)=a-(6.**(1./2.))*(atan((((b)**2.)-1.)/6.)**(1./2.))-((atan((((c)**2.)-1.)/6.)**(1./2.)))+(atan((((b)**2.)-1.)**(1./2.))-(atan((((c)**2.)-1.)**(1./2.))))




           do i=1,4
       
             
        do  j=1,20
           
       
                     
          !here i have set the tolerance
           tol=.00001

           

         !here is the function
     

     


          ! here are the two values of initial guesses(which will keep interchanging at each iteration..which i have set as 10 as a guess)
      x1(j)=2.0
           x2(j)=1.5


         !here is secants formula

    10 x3(j)=(x1(j)*F(rad(i), x2(j), m1(i))-x2(j)*F(rad(i), x2(j), m1(i)))/(F(rad(i), x2(j), m1(i))-F(rad(i), x2(j), m1(i)))
    print*,'m1',m1(i),'rad',rad(i),'x1',x1(j),'x2',x2(j),'x3',x3(j)
    x1(j)=x2(j)
    x2(j)=x3(j)  
    if (abs (x1(j)-x2(j)).lt.tol) goto 20
    goto 10
    20 print*,'The value of m2 for part(a) is',x3(j)

          end do

          end do

          end
     
    Last edited by a moderator: Dec 29, 2011
  9. Dec 23, 2011 #8
    Are you using double precision arithmetic for your computations? Convergence can be impossible with a tight tolerance in single precision.

    You might try and stop the program after a single iteration and write out some results. Then hand calculate the results to see if you are calculating what you intended.
     
  10. Dec 23, 2011 #9
    Over the years I learned to never write out a long complicated expression with many parentheses. It is too easy to make an error. Instead, I would break up the expression. Here's an example:

    f=a+b+(c*d)/e

    where a through e are somewhat complicated.

    I would write:

    f = a
    f=f+b
    p=c*d
    p=p/e
    f=f+p
     
  11. Dec 23, 2011 #10
    thank you lawrence for your replies..but still not sucess..pls have a look again and try spotting the error...
     
  12. Dec 23, 2011 #11

    Mark44

    Staff: Mentor

    Please repost your code with a [ code ] tag at the top and a [ /code ] tag at the bottom but don't include the extra spaces.

    Also, your code is very poorly formatted, with random indentation that makes it difficult to follow. Using an archaic programming style with line numbers and gotos doesn't help, either.
    Comments
    You have declared a three-dimensional array variable named f in this line
    Code (Text):
    real f(4,20,4)
    but you use what I believe is a different variable named F in this line
    Code (Text):
    F(rad,x,m1)=a-(6.**(1./2.))*(atan((((b)**2.)-1.)/6.)**(1./2.))-((atan((((c)**2.)-1.)/6.)**(1./2.)))+(atan((((b)**2.)-1.)**(1./2.))-(atan((((c)**2.)-1.)**(1./2.))))
     
    Despite the name you gave it, F is not a function. I would suggest writing an actual function, as Casco suggested in an earlier post.

    Furthermore, on the left side you have rad, x, and m1, but on the right side you have a, b, and c. The variables need to match.

    When you do this, simplify the formula of your function. Why are you writing 6.**(1./2.)? This can be written more simply as 6.**(0.5), or better yet, as SQRT(6.0).

    Also, instead of doing one big calculation for your function, break it up into smaller pieces
    Your value of 3.142 for [itex]\pi[/itex] is a crude approximation. Any calculations you do using this value will be incorrect in the 3rd decimal place.
     
    Last edited: Dec 23, 2011
  13. Dec 28, 2011 #12
    thanks to all of you for your help.
    i was given the question(the link of the attachment is mentioned above)..i have managed to do the first part,having generated the values of M2 foor each combination of theeta and m1.

    how do i do the second part which requires me to generate a table.?



    heres the CODE for the 1st part(tried my utmost to make it seem readable)

    Code (Text):
    code                        


    !here i have declared the arrays
    !note that there are four parts in the first part of his question so i have declared two variables with four values each therefore combinining those parts


    real formula
    r eal rad,m1,theeta

    dimension m1(1:4),rad(1:4),theeta(1:4)

    data (m1(j),j=1,4)/1.0,1.0,1.5,1.5/

    data (theeta(j),j=1,4)/10.0,20.0,10.0,20.0/


    do i=1,4

    rad(i)=theeta(i)*3.142/180.0
    enddo

    tol=.00001

    do j=1,4

    x1=2.0
    x2=1.5


    10 x3=(x1*formula(rad(j),x2,m1(j))-x2*formula(rad(j),x1,m1(j)))/(formula(rad(j),x2,m1(j))-formula(rad(j),x1,m1(j)))

    print*,x1,x2,x3

    x1=x2
    x2=x3


    if (abs (x1-x2).lt.tol) goto 20

    goto 10

    20 print*,'The Root is',x3


    end do

    end
    !-------------------------------------------------------------------------------------------------------------------------------

    REAL FUNCTION formula(rad,x,m1)
    REAL rad,x,m1


    Formula=rad-sqrt(6.0)*(atan(sqrt((x**2.0-1.0)/6.0))-atan(sqrt((m1**2.0-1.0)/6.0)))+(atan(sqrt(x**2.0-1.0))-atan(sqrt(m1**2.0-1.0)))


    RETURN
    END

    i have attached the question paper for your reference
     

    Attached Files:

  14. Dec 28, 2011 #13

    Mark44

    Staff: Mentor

    I would make a table with two columns, one for values of M, and one for values of δ. For each row in the table WRITE an M value and the δ value.

    There should be 40 rows in the table.
     
  15. Dec 29, 2011 #14

    Redbelly98

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    The quickest way to make a table with the code you have already written is to redimension the arrays to have 40 values, and give theeta(j) the values 1.0, 2.0, ..., 40.0; the array values of m(j) would all be the same, 1.0. You can make a do-loop to assign the values.

    I have two other comments that you might find useful:

    1. Have you checked that the values your program already calculated satisfy the equation given in the problem statement? You should do a hand-calculation with the M1 and M2 values you get, and make sure the result is the given theta.

    2. I noticed that your value of pi is less accurate than the tolerance parameter of 0.00001 you are using. This might or might not be important. However, I myself have gotten into the habit of including the following line of code near the beginning of my programs:
    ppii = 4.0*atan(1.0)​
    This has 3 benefits:
    i. Using this equation automatically gives "pi" the maximum possible accuracy.
    ii. There is virtually no chance of erroneously typing the wrong digit of pi somewhere.
    iii. By naming it "ppii", I avoid potential conflict in case there is already a predefined "pi" as some programming packages have.
     
  16. Dec 29, 2011 #15

    Redbelly98

    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    Note to polka129: please start a new discussion thread to ask a new question.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Unresolved external symbol eror in fortran:solution?
  1. Pearson symbol (Replies: 7)

  2. Fortran 90 (Replies: 1)

  3. Fortran code (Replies: 1)

  4. Searching on Fortran (Replies: 13)

  5. Scheme symbol (Replies: 5)

Loading...