Error while calculating arc length of curve in Fortran

In summary, the conversation was about calculating the length of a curve using a program that would divide the curve into 20 line segments and calculate the length of each segment using a function. The output of the program was not giving the expected result and the conversation included suggestions on how to improve the code, such as using a function to calculate the y values and avoiding hard-coding constants in the code.
  • #1
NicolasPan
21
2
The program must calculate the length of the curve of ƒ=3.1*x^2-5.3/x between x=1/2 and x=3/2.The legth should be calculated as the sum of n line segments starting with n=1 and ending with n=20.

I really can't find why the result I'm getting is wrong.Thanks in advance
I am giving you the code below:

Fortran:
program pr2_ex2
implicit none
integer::x
double precision::dy,dx !dy=the height of the linear part & dx=the length of the linear part
double precision::x1,x2,s !f=f(x) the function,x=the values which can be given to f
double precision::length 
  print*,"Please enter the function's starting point"
  read*,x1
  print*,"Please enter the function's ending  point"
  read*,x2
length=0
  s=0
do x=2,21
   dx=((x*abs(x2-x1)-(x-1)*abs(x2-x1))/(20))
   dy=(3.1*(x*abs(x2-x1)/20)**2-(5.3*20/x*abs(x2-x1)))-(3.1*((x-1)*abs(x2-x1)/20)**2-(5.3*20/(x-1)*abs(x2-x1)))
   length=sqrt((dx**2)+(dy**2))
   s=length+s
   end do
   print*,s
end program
 
Last edited:
Physics news on Phys.org
  • #2
What is the result you're getting that you say is wrong?

"Lenght" being a misspelling of "length"?
 
  • #3
Well if for x1=0.5 and for x2=1.5 I am getting 104.3853... That can't be right since I've checked it in wolframalpha and it says 13
 
  • #4
The program must calculate the length of the curve of ƒ=3.1*x^2-5.3/x between x=1/2 and x=3/2.The leght should be calculated as the sum of n line segments starting with n=1 and ending with n=20.
EDIT: My reading of this is that it needs a program to print out 20 answers, one answer for each value of n.

I can see your program prints only one answer, right at the end, so that might be where you are going wrong?

How would you explain to someone what n is in this situation, to show you understand what this calculation is about?
 
Last edited:
  • #5
NascentOxygen said:
My reading of this is that your program will print out 20 answers, one answer for each value of n.

I can see your program prints only one answer, right at the end, so that might be where you are going wrong?

How would you explain to someone what n is in this situation, to show you understand what this calculation is about?
Well I think the curve's x's from x1 to x2 can be divided to 20 equidistant x's with every n to be used to get to the next x.Xn=abs(x2-x1)*x/20 and X(n-1)=(x-1)*(abs(x2-x1)/20).Therefore if I subtract Xn,X(n-1),I will be getting the distance between consecutive X's.
 
  • #6
NicolasPan said:
Well I think the curve's x's from x1 to x2 can be divided to 20 equidistant x's with every n to be used to get to the next x.Xn=abs(x2-x1)*x/20 and X(n-1)=(x-1)*(abs(x2-x1)/20).Therefore if I subtract Xn,X(n-1),I will be getting the distance between consecutive X's.
That would be the case for n=20. But, for example, when n=16 then there would be 16 segments spanning the path between those same two endpoints. That's what I think. The greater the value of n, the closer to the true length will be your approximation.
 
  • #7
I am aware of that,although that can't be the problem the bigger the n the more my sum goes up.And the problem states that it should be able to work with just n=20,which doesn't in my case.Therefore I assume the problem is associated with my code.Nevermind thanks for your suggestion
 
  • #8
NicolasPan said:
dy=(3.1*(x*abs(x2-x1)/20)**2-(5.3*20/x*abs(x2-x1)))-(3.1*((x-1)*abs(x2-x1)/20)**2-(5.3*20/(x-1)*abs(x2-x1)))
When you write 5.3*20/x*abs(x2-x1), it means
$$
5.3 \times \frac{20}{x} \times \left| x_2 - x_1 \right|
$$
which is not what you want.

You should create a function that given x, returns f(x), instead of hard-coding it many times. Also, do not use x as an integer index for the loop. It looks too much like the mathematical variable x. Use something like i instead.
 
  • #9
NicolasPan said:
The program must calculate the length of the curve of ƒ=3.1*x^2-5.3/x between x=1/2 and x=3/2.The legth should be calculated as the sum of n line segments starting with n=1 and ending with n=20.

I really can't find why the result I'm getting is wrong.Thanks in advance
I am giving you the code below:

Fortran:
program pr2_ex2
implicit none
integer::x
double precision::dy,dx !dy=the height of the linear part & dx=the length of the linear part
double precision::x1,x2,s !f=f(x) the function,x=the values which can be given to f
double precision::length
  print*,"Please enter the function's starting point"
  read*,x1
  print*,"Please enter the function's ending  point"
  read*,x2
length=0
  s=0
do x=2,21
   dx=((x*abs(x2-x1)-(x-1)*abs(x2-x1))/(20))
   dy=(3.1*(x*abs(x2-x1)/20)**2-(5.3*20/x*abs(x2-x1)))-(3.1*((x-1)*abs(x2-x1)/20)**2-(5.3*20/(x-1)*abs(x2-x1)))
   length=sqrt((dx**2)+(dy**2))
   s=length+s
   end do
   print*,s
end program
Nicholas, your code is chock full of what are disparagingly called "magic" numbers, constants that are hard-coded. The number of subintervals should be a variable that the user enters, and not sprinkled throughout in your loop ending value and your assignment statements for dx and dy.

As already suggested, your code would be a lot more readable if you used a function to calculate the y values.

Fortran:
function f(x)
   double precision :: f
   double precision ::x
 
   f = 3.1 * x**2 - 5.3/x
   return
end function

The only change to your main program would be to insert a declaration for the function up near the top:
Fortran:
program pr2_ex2
implicit none
double precision:: f

To use the function, write an expression of the form f(x1).

One more thing -- you really shouldn't use x as your loop control variable. Use i or j for that.
 
Last edited:
  • #10
Thank you for your answers! I will be using your advice for sure in my code and see how it goes
 
  • #11
I put together some Python code to do this calculation. With n = 20 and n = 100 subintervals, I got 13.3059 in the first four decimal places.
 

1. What is the cause of the error while calculating arc length of curve in Fortran?

The most common cause of this error is an incorrect or incomplete formula or algorithm used for calculating the arc length. This could be due to a mistake in the code or a lack of understanding of the mathematical concepts involved.

2. How can I fix the error while calculating arc length of curve in Fortran?

To fix this error, it is important to carefully review the formula or algorithm used for calculating the arc length. Check for any mistakes in the code and make sure all variables and parameters are properly defined and used. It may also be helpful to consult with a colleague or reference material to ensure the accuracy of the calculations.

3. Can the error while calculating arc length of curve in Fortran be caused by incorrect input values?

Yes, incorrect input values can also lead to this error. Make sure all input values are within the appropriate range and format required for the calculation. It is also important to check for any potential data errors or typos in the input values.

4. Is there a specific Fortran compiler that is more prone to this error?

No, the occurrence of this error is not dependent on the Fortran compiler being used. It is more likely to be caused by issues with the code or input values rather than the compiler itself.

5. Can using a different programming language prevent this error from occurring?

Not necessarily. While some programming languages may have more built-in functions for calculating arc length or may be easier to use for certain calculations, the occurrence of this error is not exclusive to Fortran. It is important to have a thorough understanding of the mathematical concepts involved in order to accurately calculate arc length regardless of the programming language being used.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
888
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
999
  • Engineering and Comp Sci Homework Help
Replies
5
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
2K
  • Calculus and Beyond Homework Help
Replies
1
Views
734
  • Calculus and Beyond Homework Help
Replies
2
Views
842
  • Engineering and Comp Sci Homework Help
Replies
11
Views
5K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
Back
Top