Fortran: mistake in writting this formulas

Click For Summary
SUMMARY

The discussion focuses on syntax errors encountered in Fortran code involving complex mathematical formulas for variables V1 and V2. Users suggest that the errors may stem from unbalanced parentheses and recommend breaking down the assignment statements into simpler components using intermediate variables. Additionally, the use of a parameter for the constant 6.2831853, representing 2π, is advised for clarity and maintainability. The conversation highlights the importance of proper line continuation techniques in Fortran, especially in older versions.

PREREQUISITES
  • Understanding of Fortran syntax and structure
  • Familiarity with mathematical operations in programming
  • Knowledge of line continuation methods in Fortran
  • Experience with debugging techniques for code errors
NEXT STEPS
  • Learn about Fortran line continuation rules in both fixed and free formats
  • Explore best practices for breaking down complex expressions in Fortran
  • Investigate the use of parameters in Fortran for constants like 2π
  • Review common syntax errors in Fortran and how to resolve them
USEFUL FOR

This discussion is beneficial for Fortran programmers, software developers working with mathematical computations, and anyone troubleshooting syntax errors in their Fortran code.

Petar Mali
Messages
283
Reaction score
0
Do I made a mistake in writting this formulas. I don't see them. Tnx for your answer.

V1=(Kps*(1-ra**2)*(1-cos(6.2831853*y(1))))/(6.2831853**2*
(1+ra**2+2*ra*
cos(6.2831853*y(1)/2))**2)

V2=(Kps*ra*(1-ra**2)*(1-cos(6.2831853*y(1)))*
sin(6.2831853*y(1)/2))/(6.2831853/2*(1+ra**2+2ra*
cos(6.2831853*y(1)/2))**3)+(Kps*(1-ra**2)*sin(6.2831853*y(1)))/
(6.2831853*(1+ra**2+2*ra*cos(6.2831853*y(1)/2))**2)
 
Technology news on Phys.org
Fortran says to me that I made syntax error here
V1=(Kps*(1-ra**2)*(1-cos(6.2831853*y(1))))/(6.2831853**2*
(1+ra**2+2*ra*
cos(6.2831853*y(1)/2))**2)
 
Petar Mali said:
Fortran says to me that I made syntax error here
V1=(Kps*(1-ra**2)*(1-cos(6.2831853*y(1))))/(6.2831853**2*
(1+ra**2+2*ra*
cos(6.2831853*y(1)/2))**2)

What was the syntax error?

I'm guessing that you have unbalanced parentheses. If that's the problem, you could print out this line of code on paper, and then connect each left parenthesis with its matching right parenthesis. You shouldn't end up with any unmatched parentheses.

A better way to go would be to break up this complicated assignment statement into at least three or four assignment statements using intermediate variables. The final assigment statement would use only the intermediate variables.

Also, you should use a parameter for 6.2831853. That seems to be 2\pi, so a reasonable name for the parameter would be TwoPI.
 
If the statement is really spread out over three lines like you wrote it here, your compiler may not be recognizing second (etc.) line as a "continuation" of the first one, but instead is trying to compile each line as a separate statement.

In the old days of fixed-format Fortran, all statements had to start in position 7 of the line. You indicated a continuation line by putting an additional character in position 6. Most people used a + or a * or a digit for this purpose:

Code:
      V1=(Kps*(1-ra**2)*(1-cos(6.2831853*y(1))))/(6.2831853**2*
     +(1+ra**2+2*ra*
     +cos(6.2831853*y(1)/2))**2)

In this example, the + signs at the beginning of lines 2 and 3 are not part of the statement. They simply flag the line as a continuation of the first one.

I haven't used modern versions of Fortran, so I don't know if this is an issue any more.
 
If your source code is in "free format", you need an & character at the end of a line to say the next line is a continuation.

You can also put a & character at the start of the continuation line if you want.

IIRC some Fortan compilers assume that if a line that ends with an operator (e.g. + - * / ) or "punctiation" (e.g. a comma), then the next line is a continuation, and you don't need to use the & character. Your code seems to be written using that convention, but that isn't standard fortran, and if your compiler supports it you probably need to do something to switch the option on.
 
I'll try to print code and look for unbalanced brackets, but that looks just fine for me. I put symbol & in the end of some problematic lines and that didn't work neither. And in program I get just one mistake and that mistake is syntax one. Program worked just fine with different shape of functions V_1 and V_2. I don't understand where is a problem...
 
Last edited:
Petar Mali said:
I'll try to print code and look for unbalanced brackets, but that looks just fine for me.
Did you actually do this?
Petar Mali said:
I put symbol & in the end of some problematic lines and that didn't work neither. And in program I get just one mistake and that mistake is syntax one.
What is the syntax error? Knowing this would help us diagnose the problem.
Petar Mali said:
Program worked just fine with different shape of functions V_1 and V_2.
I have no idea what you mean by "different shape of functions."
Petar Mali said:
I don't understand where is a problem...

I still recommend that you break up this complicated assignment statement into at least three separate assigment statements using intermediate variables, and then use these intermediate variables for the final assignment statement.
 

Similar threads

  • · Replies 41 ·
2
Replies
41
Views
6K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 26 ·
Replies
26
Views
1K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 5 ·
Replies
5
Views
2K