How to Use FORTRAN Do Loops for Solving Equations - Step by Step Guide"

In summary: Thank you.In summary, the conversation discusses a problem with looping values in Fortran code and suggests moving a statement out of the loop and adding a new statement to solve the issue.
  • #1
McAfee
96
1
Hey guys,

I'm new to fortran and I'm having a slight problem. I put my code below as reference. I first set up a my known values. Then I used an equation to solve for v. After I solve for v I then computed c which involves v.

What I'm now trying to do is to loop the value I got for c back into the equation [c=((r*t)/(p+(a/(v**2))))+b]. So the c would become v and compute a new number.

Any ideas on how I could accomplish that? Thanks for help in advance.

Code:
 program homework
      open (2, file='output.txt')
      a=366*10**3
      b=0.0429
      p=100
      t=300
      r=8.3145



      DO 1, i=0,5

      v=(r*t)/p
      c=((r*t)/(p+(a/(v**2))))+b



      write(2,*) c
      
      
      
      
   1  continue
      
      
      
      
      end program
 
Technology news on Phys.org
  • #2
First: move the statement "v=(r*t)/p" out of the loop (just before the "do" statement.
Then, after the write statement, add this statement: v=c

That's all it takes.
 
  • #3
Hi everyone;
I want to know whether a call statement is used in a do loop. For example;

do 100 ii = 1, 10

call subroutine getsum(ii,20,XX,JJ)

100 continue

Here XX and JJ are parameters.
 
  • #4
selmayda,
Please start a new thread with your question. Tacking an unrelated question onto an existing thread is known as "hijacking."
 
  • #5
homework

I would like to commend you for taking the initiative to learn FORTRAN and for seeking help when encountering a problem. It shows your dedication to understanding and solving problems using programming.

To use FORTRAN do loops for solving equations, follow these steps:

1. Define your known values: In your code, you have already defined the values for a, b, p, t, and r. This is a good start.

2. Set up the equation to be solved: In your case, you are solving for v using the equation v=(r*t)/p. This is also already in your code.

3. Use a do loop to iterate through different values of v: In your code, you have used a do loop to iterate through the values of i from 0 to 5. This is a good start, but you need to modify it to iterate through different values of v instead.

4. Within the do loop, calculate c using the equation: c=((r*t)/(p+(a/(v**2))))+b. This will give you a different value of c for each iteration of the do loop.

5. Use the value of c as the new v and repeat the do loop: To do this, you can assign the value of c to v before the next iteration of the do loop. This will use the new value of v to calculate a new value of c, and the process will continue until the do loop reaches the end.

6. Output the values: In your code, you have used the write statement to output the value of c to a file. This is a good way to keep track of the values and compare them.

Overall, your approach seems correct. However, you need to modify your do loop to iterate through different values of v and assign the value of c to v before the next iteration. I hope this helps and good luck with your FORTRAN journey!
 

Related to How to Use FORTRAN Do Loops for Solving Equations - Step by Step Guide"

1. What is FORTRAN and why is it used for solving equations?

FORTRAN (short for Formula Translation) is a programming language commonly used for numerical and scientific computing. It is specifically designed for solving mathematical equations and is often used in fields such as physics, engineering, and astronomy. Its efficient and fast processing capabilities make it a popular choice for solving complex calculations.

2. What are do loops in FORTRAN and how do they work for solving equations?

Do loops, also known as do-while or do-until loops, are a type of control structure in FORTRAN that allows for repeated execution of a block of code. They are commonly used for solving equations by iteratively updating the values of variables until a desired result is achieved. This allows for efficient and accurate approximation methods for solving equations.

3. How do I write a FORTRAN do loop for solving equations?

To write a FORTRAN do loop, you first need to define the starting and ending values for the loop, as well as the increment value. Then, you can use the DO keyword followed by the loop index variable and the loop parameters (e.g. DO i = 1, 10, 2). Inside the loop, you can include the equations and calculations needed to solve the problem. The loop will continue to run until the final condition is met.

4. Can FORTRAN do loops be nested for more complex equations?

Yes, FORTRAN do loops can be nested, meaning that one loop can be placed inside another. This allows for more complex equations with multiple variables and conditions to be solved efficiently. However, it is important to carefully consider the logic and structure of the nested loops to avoid infinite loops or errors in the calculations.

5. Are there any common mistakes to avoid when using FORTRAN do loops for solving equations?

One common mistake is forgetting to update the loop index variable within the loop, which can result in an infinite loop. It is also important to properly define the loop parameters to avoid errors in the calculations. Additionally, it is recommended to use debugging tools or print statements to check the values of variables and ensure the accuracy of the results.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
985
  • Programming and Computer Science
Replies
26
Views
3K
  • Programming and Computer Science
2
Replies
59
Views
9K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
22
Views
4K
Back
Top