Fortran: mistake in writting this formulas

Click For Summary

Discussion Overview

The discussion revolves around identifying and resolving syntax errors in Fortran code related to the formulation of two equations, V1 and V2. Participants explore potential issues with the code structure and formatting, particularly in the context of Fortran's syntax rules.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant expresses uncertainty about their formulas and requests feedback on potential mistakes.
  • Another participant suggests that unbalanced parentheses might be the issue and recommends breaking down the assignment into simpler statements.
  • A different participant points out that line continuation in Fortran may not be recognized if the statement is spread across multiple lines without proper formatting.
  • It is mentioned that in free format Fortran, an '&' character is needed at the end of a line to indicate continuation, although some compilers may allow continuation based on certain conditions.
  • One participant indicates that they have attempted to use the '&' character but still encounter a syntax error, expressing confusion about the source of the problem.
  • Another participant questions the meaning of "different shape of functions" mentioned by the original poster, indicating a lack of clarity in the discussion.

Areas of Agreement / Disagreement

Participants do not reach a consensus on the specific syntax error in the code. Multiple competing views on potential formatting issues and solutions are presented, and the discussion remains unresolved.

Contextual Notes

Limitations include potential misunderstandings of Fortran's syntax rules, the impact of code formatting on compilation, and the specific nature of the syntax error that remains unidentified.

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
2K
  • · Replies 5 ·
Replies
5
Views
2K