Fortran Fortran: mistake in writting this formulas

Click For Summary
The discussion revolves around a syntax error encountered in Fortran while writing two complex mathematical formulas, V1 and V2. The user is unsure about the source of the error and seeks clarification. Suggestions include checking for unbalanced parentheses, which could be causing the issue. It is recommended to simplify the code by breaking the complex assignment into multiple statements using intermediate variables. Additionally, the use of a parameter for the constant 6.2831853 (suggested to be renamed to TwoPI) is advised for clarity. The conversation touches on the formatting requirements of Fortran, particularly regarding line continuation, and the need to ensure that the code adheres to the compiler's expectations. Despite attempts to fix the issue, the user remains confused about the specific syntax error, indicating that the program previously worked with different function shapes.
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 1 ·
Replies
1
Views
2K
  • · Replies 26 ·
Replies
26
Views
853
  • · Replies 4 ·
Replies
4
Views
1K