Fortran: mistake in writting this formulas

In summary: This will make the code easier to read and debug. Additionally, using a parameter for the value of 6.2831853 will make the code more flexible.
  • #1
Petar Mali
290
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
  • #2
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)
 
  • #3
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[itex]\pi[/itex], so a reasonable name for the parameter would be TwoPI.
 
  • #4
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.
 
  • #5
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.
 
  • #6
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 [tex]V_1[/tex] and [tex]V_2[/tex]. I don't understand where is a problem...
 
Last edited:
  • #7
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 [itex]V_1[/itex] and [itex]V_2[/itex].
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.
 

Related to Fortran: mistake in writting this formulas

1. What is the most common mistake in writing Fortran formulas?

The most common mistake in writing Fortran formulas is missing or incorrect syntax, such as forgetting to include parentheses or using incorrect operators.

2. How can I avoid making mistakes when writing Fortran formulas?

To avoid making mistakes when writing Fortran formulas, it is important to carefully check the syntax and use proper indentation to make the code more readable. Additionally, using a compiler or debugger can help identify and fix any errors.

3. What are some common errors that can occur when writing Fortran formulas?

Some common errors that can occur when writing Fortran formulas include using incorrect data types, forgetting to initialize variables, and using incorrect or missing control structures.

4. How can I troubleshoot errors in Fortran formulas?

To troubleshoot errors in Fortran formulas, it is helpful to use a debugger or compiler, as well as carefully reviewing the code for any syntax or logic errors. Additionally, breaking down the code into smaller parts and testing each part separately can help identify the source of the error.

5. Are there any resources available to help with writing Fortran formulas?

Yes, there are many resources available to help with writing Fortran formulas, such as online forums, tutorials, and documentation. Additionally, there are books and courses specifically focused on learning and mastering Fortran programming.

Similar threads

  • Programming and Computer Science
2
Replies
41
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
1K
  • General Math
Replies
5
Views
880
  • Programming and Computer Science
Replies
4
Views
963
  • Programming and Computer Science
Replies
2
Views
1K
  • Differential Equations
Replies
2
Views
1K
  • Calculus and Beyond Homework Help
Replies
3
Views
730
  • Introductory Physics Homework Help
Replies
1
Views
1K
  • Introductory Physics Homework Help
Replies
1
Views
318
  • Differential Equations
Replies
5
Views
1K
Back
Top