Why Does My Fortran Subroutine Return the Same Value?

  • Context: Fortran 
  • Thread starter Thread starter chrissi.<3
  • Start date Start date
  • Tags Tags
    Code Errors Fortran
Click For Summary

Discussion Overview

The discussion revolves around a Fortran subroutine that consistently returns the same value despite varying input parameters. Participants explore potential debugging strategies, code structure, and the handling of mathematical functions within the subroutine.

Discussion Character

  • Technical explanation
  • Debugging
  • Exploratory

Main Points Raised

  • One participant describes the issue where the subroutine returns the same value of CPREV regardless of different inputs for A, B, LAMDA1, and LAMDA2.
  • Another participant suggests adding a write statement to print the values of A, B, LAMDA1, and LAMDA2 for debugging purposes.
  • A different participant notes that the lines "CPREV=CHISQ" and "CPREV=0" seem odd, as CHISQ is not utilized further.
  • After some modifications, the original poster mentions that removing the line "CPREV=CHISQ" did not resolve the issue.
  • One participant questions the COMMON statement's structure, suggesting it may not align with the main program's definition.
  • Another participant discovers that the program fails to handle the exponential function correctly for certain values, indicating that it works only when LAMDA1 and LAMDA2 are less than 0.01.
  • In a later post, the original poster reiterates the problem and requests the full program for better assistance.
  • Finally, the original poster indicates that they have resolved the issue independently.

Areas of Agreement / Disagreement

Participants express various debugging strategies and observations, but there is no consensus on the root cause of the issue until the original poster resolves it independently.

Contextual Notes

Some participants note potential limitations in the handling of mathematical functions and the structure of the COMMON statement, but these points remain unresolved in the discussion.

chrissi.<3
Messages
4
Reaction score
0
Hii,

Having trouble with some code I am writing, where a subroutine returns the same value regardless of different variable inputs.

...
C Calculate Yth and Chi values. Compute sum of Chi-squared.
Subroutine Chisquared(CPREV,CHISQ)
Real TIME(17),COUNTS(17),ERROR(17),YTH(17),CHI(17)
Real A,B,LAMDA1,LAMDA2,CHISQ,CPREV
Common TIME,ERROR,COUNTS,YTH,A,B,LAMDA1,LAMDA2

CPREV=CHISQ
CPREV=0

Do K=1,17
YTH(K)=A*EXP(-LAMDA1*TIME(K))+B*EXP(-LAMDA2*TIME(K))
End Do

Do L=1,17
CHI(L)=(COUNTS(L)-YTH(L))/((ERROR(L)**2))
End Do

Do M=1,17
CPREV=CPREV+CHI(M)
End Do

Return
End
...

The user inputs A, B, LAMDA1 and LAMDA2 but it always returns the same value of CPREV.
Help please!
 
Technology news on Phys.org
Looks like you need some basic debug. Suggest you first add a write statement in your subroutine to print out the values of A, B, LAMDA1 and LAMDA2 to confirm the values are what you expect.
 
Without seeing all of the code debugging this is just guessing, but the first two lines

CPREV=CHISQ
CPREV=0

look a bit strange, considering that is the only thing you do with CHISQ.
 
Thanks...

I've tried adding a write statement and it seems to have no problems with using the same variables (as input by the user in the main program).

I've also got rid of that line of code CPREV=CHISQ.

Still gives me the same value :confused:
 
C Calculate Yth and Chi values. Compute sum of Chi-squared.
Subroutine Chisquared(CPREV,CHISQ)
Real TIME(17),COUNTS(17),ERROR(17),YTH(17),CHI(17)
Real A,B,LAMDA1,LAMDA2,CHISQ,CPREV
Common TIME,ERROR,COUNTS,YTH,A,B,LAMDA1,LAMDA2

I haven't done fortran coding in a while but should not the COMMON staement be:
Common TIME(17),ERROR(17),COUNTS(17),YTH(17),A,B,LAMDA1,LAMDA2

and similar to one you have in your main part of the program?

Just guessing that maybe TIME is a different variable then TIME(17)
 
After throughly writing each step of the calculation (for YTH, CHI and CPREV), I found that the program was unable to handle the exponential function for certain values.

So it works ok now... as long as LAMDA1 and LAMDA2 have values less than 0.01
 
chrissi.<3 said:
Hii,

Having trouble with some code I am writing, where a subroutine returns the same value regardless of different variable inputs.

...
C Calculate Yth and Chi values. Compute sum of Chi-squared.
Subroutine Chisquared(CPREV,CHISQ)
Real TIME(17),COUNTS(17),ERROR(17),YTH(17),CHI(17)
Real A,B,LAMDA1,LAMDA2,CHISQ,CPREV
Common TIME,ERROR,COUNTS,YTH,A,B,LAMDA1,LAMDA2

CPREV=CHISQ
CPREV=0

Do K=1,17
YTH(K)=A*EXP(-LAMDA1*TIME(K))+B*EXP(-LAMDA2*TIME(K))
End Do

Do L=1,17
CHI(L)=(COUNTS(L)-YTH(L))/((ERROR(L)**2))
End Do

Do M=1,17
CPREV=CPREV+CHI(M)
End Do

Return
End
...

The user inputs A, B, LAMDA1 and LAMDA2 but it always returns the same value of CPREV.
Help please!

You need to post the entire program. This little piece needs values
passed in COMMON.
 
Ive solved it now

Thanks anyway : )
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 59 ·
2
Replies
59
Views
12K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 7 ·
Replies
7
Views
4K
  • · Replies 16 ·
Replies
16
Views
2K