# Homework Help: Unresolved external symbol eror in fortran:solution?

1. Dec 22, 2011

### polka129

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

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
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

! 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)))

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

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

WHERE HAVE I GONE WRONG?

thank you

i have attached the question paper for your reference

Last edited: Dec 28, 2011
2. Dec 22, 2011

### 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.

3. Dec 22, 2011

### Casco

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. Dec 22, 2011

### polka129

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. Dec 22, 2011

### obafgkmrns

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. Dec 22, 2011

### Redbelly98

Staff Emeritus
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. Dec 23, 2011

### polka129

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)

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

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

! 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

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
8. Dec 23, 2011

### LawrenceC

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. Dec 23, 2011

### LawrenceC

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. Dec 23, 2011

### polka129

thank you lawrence for your replies..but still not sucess..pls have a look again and try spotting the error...

11. Dec 23, 2011

### 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.
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):

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 $\pi$ is a crude approximation. Any calculations you do using this value will be incorrect in the 3rd decimal place.

Last edited: Dec 23, 2011
12. Dec 28, 2011

### polka129

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

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

enddo

tol=.00001

do j=1,4

x1=2.0
x2=1.5

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
!-------------------------------------------------------------------------------------------------------------------------------

RETURN
END

i have attached the question paper for your reference

#### Attached Files:

• ###### mati fortran question paper.jpg
File size:
26.3 KB
Views:
160
13. Dec 28, 2011

### 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.

14. Dec 29, 2011

### Redbelly98

Staff Emeritus
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. Dec 29, 2011

### Redbelly98

Staff Emeritus