Errors with fortran code, help

In summary, the subroutine returns the same value for different input variables, but with a little debugging it can be fixed.
  • #1
chrissi.<3
4
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
  • #2
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.
 
  • #3
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.
 
  • #4
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:
 
  • #5
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)
 
  • #6
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
 
  • #7
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.
 
  • #8
Ive solved it now

Thanks anyway : )
 

What are the most common errors encountered when writing Fortran code?

The most common errors in Fortran code include syntax errors, logic errors, and runtime errors. Syntax errors occur when the code does not follow the proper syntax rules of the language. Logic errors occur when the code produces incorrect results due to a mistake in the program's logic. Runtime errors occur during program execution and can be caused by a variety of factors, such as incorrect input or memory issues.

How can I debug my Fortran code?

To debug Fortran code, you can use a debugger tool or add print statements throughout the code to track the program's execution. You can also use a code editor with built-in debugging features, such as breakpoints and variable inspection.

Why am I getting a "segmentation fault" error in my Fortran code?

A "segmentation fault" error occurs when a program tries to access memory that it does not have permission to access. This can happen due to a variety of reasons, such as using uninitialized variables, accessing arrays out of bounds, or memory leaks. It is essential to carefully check your code for these types of errors to avoid "segmentation fault" errors.

What is the best way to organize my Fortran code?

There is no one "right" way to organize Fortran code, as it often depends on the project's specific needs and personal preferences. However, some best practices include keeping the code concise and readable, using consistent formatting and naming conventions, and breaking the code into modular subroutines or functions.

Where can I find resources to help me learn and troubleshoot Fortran code?

There are many resources available online to help you learn and troubleshoot Fortran code. Some useful resources include online tutorials, forums, and documentation from the Fortran language website. You can also seek help from experienced Fortran programmers or join a community of Fortran developers for support and collaboration.

Similar threads

  • Programming and Computer Science
Replies
4
Views
589
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
3K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
6
Views
2K
Back
Top