Unresolved external symbol eror in fortran:solution?

  • Comp Sci
  • Thread starter polka129
  • Start date
  • Tags
    Symbol
In summary, the conversation discusses an assignment involving FORTRAN90 and a code that calculates values for M2 using the SECANTS method. The code has a few issues, such as not including an actual FORTRAN function to calculate the function values and not declaring the variable x. Two linker errors are also mentioned. The conversation includes a sample code with two functions, F1 and F2, which take in three values and return a value.
  • #1
polka129
43
0
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 isn't 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:
!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:
Physics news on Phys.org
  • #2
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.
 
  • #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
 
  • #4
Casco said:
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




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?...
 
  • #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))"
 
  • #6
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.
 
  • #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:
	!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:
  • #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.
 
  • #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
 
  • #10
thank you lawrence for your replies..but still not sucess..pls have a look again and try spotting the error...
 
  • #11
polka129 said:
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
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.
polka129 said:
!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

Comments
You have declared a three-dimensional array variable named f in this line
Code:
real f(4,20,4)
but you use what I believe is a different variable named F in this line
Code:
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:
  • #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:
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
 

Attachments

  • mati fortran question paper.jpg
    mati fortran question paper.jpg
    26.3 KB · Views: 708
  • #13
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.
 
  • #14
polka129 said:
how do i do the second part which requires me to generate a table.?

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.
 
  • #15
Note to polka129: please start a new discussion thread to ask a new question.
 

What is an unresolved external symbol error in Fortran?

An unresolved external symbol error in Fortran occurs when the compiler cannot find the definition for a function, subroutine, or variable that is used in a program. This can happen if the necessary library or module is not linked properly.

What causes an unresolved external symbol error in Fortran?

There are several possible causes for this error, including misspelling the name of a function or variable, not including the necessary library or module in the compilation, or using a function or subroutine that is not defined in the program or in any linked libraries.

How can I fix an unresolved external symbol error in Fortran?

To fix this error, you should first check for any spelling mistakes in the names of functions or variables. Then, make sure that all necessary libraries or modules are linked correctly. If the error persists, you may need to check the documentation for the function or subroutine to ensure it is being used correctly.

Can an unresolved external symbol error be caused by incompatible libraries or modules?

Yes, it is possible for an unresolved external symbol error to be caused by using incompatible libraries or modules. This can happen if the versions of the libraries or modules are not compatible with the compiler being used.

Is there a way to prevent unresolved external symbol errors in Fortran?

Yes, there are a few ways to prevent unresolved external symbol errors in Fortran. First, make sure to double check all spelling and syntax when writing a program. Additionally, regularly update and maintain all linked libraries and modules to ensure compatibility with the compiler being used. Finally, carefully read the documentation for any functions or subroutines being used to ensure they are being used correctly.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
875
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
Replies
5
Views
354
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
985
  • Engineering and Comp Sci Homework Help
Replies
22
Views
2K
  • Introductory Physics Homework Help
Replies
6
Views
774
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
702
Back
Top