Why Does My Fortran 95 Code for Calculating a Piecewise Function Fail?

In summary, the conversation is about a person trying to make their Fortran95 code run, but encountering an error with the square root function. The problem is related to the arithmetic of the assignment and the person is seeking assistance in creating a program that can calculate a function with different branches for different input values. The code can be simplified by removing labels.
  • #1
NicolasPan
21
2

Homework Statement

. [/B]Hello all ! I am currently trying to make my fortran95 code run in vain.I would really appreciate it if someone could lend my a hand since I am a starter in programming.So the problem goes like that:Create a program capable of calculating the following function: f(x)=1/x when x<3 , x+2(x)^2 when 3≤x≤8 and finally sqrt(x-10) when x>8
The problem is that my code will pop up a window 'Error' and it is indicated that the problem is with the square root .Thanks in advance!

The Attempt at a Solution

.[/B]
Mod note: Added code tags.
Fortran:
program function
implicit none
integer::st=0
real::x,y1,y2,y3
  print*,'Welcome to the function  y=1/x , x<3 &
& 2.*x**2.+x   3<=x<=8               and sqrt(x-10),x>8 '
  print*,'input to x'
  read (*,*,iostat=st) x
        pr:if (st /=0) then
          print*,'possible error'
     stop
          else pr
         ex:if (abs(x-3)<1e-6 .or. abs(x)<1e-6) then
      
      y1=1./x
        print*,y1

        else ex

      in: if (abs(x-3)>=1e-6 .and. abs(x-8)<=1e-6) then
        y2=(2*x)**2+x
        print*,y2

    else in
    
           out:if (abs(x-8)>1e-6 .and. abs(x-10)<1e-6) then
           print*,'impossible'
           stop

  
        else out
           br:if (abs(x-10)>=1e-6) then
        
             y3=sqrt(x-10)
        print*,y3
        
        
        end if br
        end if out
        end if in
        end if ex
        end if pr
        end program




 
Last edited by a moderator:
Physics news on Phys.org
  • #2
NicolasPan said:

Homework Statement

. [/B]Hello all ! I am currently trying to make my fortran95 code run in vain.I would really appreciate it if someone could lend my a hand since I am a starter in programming.So the problem goes like that:Create a program capable of calculating the following function: f(x)=1/x when x<3 , x+2(x)^2 when 3≤x≤8 and finally sqrt(x-10) when x>8
The problem is that my code will pop up a window 'Error' and it is indicated that the problem is with the square root .Thanks in advance!

The Attempt at a Solution

.
[/B]
Mod note: Added code tags.
Fortran:
program function
implicit none
integer::st=0
real::x,y1,y2,y3
  print*,'Welcome to the function  y=1/x , x<3 &
& 2.*x**2.+x   3<=x<=8               and sqrt(x-10),x>8 '
  print*,'input to x'
  read (*,*,iostat=st) x
        pr:if (st /=0) then
          print*,'possible error'
     stop
          else pr
         ex:if (abs(x-3)<1e-6 .or. abs(x)<1e-6) then
    
      y1=1./x
        print*,y1

        else ex

      in: if (abs(x-3)>=1e-6 .and. abs(x-8)<=1e-6) then
        y2=(2*x)**2+x
        print*,y2

    else in
  
           out:if (abs(x-8)>1e-6 .and. abs(x-10)<1e-6) then
           print*,'impossible'
           stop        else out
           br:if (abs(x-10)>=1e-6) then
      
             y3=sqrt(x-10)
        print*,y3
      
      
        end if br
        end if out
        end if in
        end if ex
        end if pr
        end program



When including source code with your post, please include the source code inside [ CODE ] [ /CODE ] tags (omit the spaces before and after the word CODE.

Your problems with this program stem from the arithmetic of the assignment, not so much the code.

You want to evaluate the following expressions:

1. f(x)=1/x when x<3 ,
2. x+2(x)^2 when 3≤x≤8 and
3. sqrt(x-10) when x>8

Do you notice anything about expressions 1 and 3 and their respective ranges of x which might cause a problem in evaluation? Did you add any statements to your program which might detect these problems and alert the user?

The way you have coded expression 2 will evaluate f = x + (2*x)2 rather than f = x + 2 x2, which is a significant difference.

Also, the first statement in your code, PROGRAM FUNCTION, might cause confusion in the compiler, since FUNCTION is a reserved word in Fortran. Better to rename the program to something besides FUNCTION.
 
  • #3
I have added code tags around your code. Please use them, as SteamKing had described in his reply.

Your code can be greatly simplified as follows:
  1. Ask user for an input value
  2. If input value < 3, use first branch of function definition; i.e., f(x) = 1/x. Be sure to check for improper input to that function.
  3. Else if 3 <= input value <= 8, use second branch of function definition.
  4. Else if input value > 8, use third branch.
  5. Display calculated function value.
You really need to get rid of all those labels (e.g. in: ).
 
  • #4
Thank you very much both SteamKing and Mark44 I really appreciate your help!,and excuse me for not adding tags around my code
 

Related to Why Does My Fortran 95 Code for Calculating a Piecewise Function Fail?

1. Why is my Fortran 95 code not running?

There could be a variety of reasons why your Fortran 95 code is not running. Some common causes include syntax errors, incompatible compiler, incorrect file paths, or missing libraries.

2. How do I debug my Fortran 95 code?

To debug your Fortran 95 code, you can use a debugger tool such as GDB or a built-in debugger in your IDE. You can also use print statements or logging to track the execution of your code and identify any errors.

3. Why am I getting a segmentation fault when running my Fortran 95 code?

A segmentation fault can occur when your program tries to access memory that it is not allowed to. This can be caused by a variety of reasons such as a null pointer, out-of-bounds array access, or uninitialized variables. Use a debugger or print statements to locate the source of the error.

4. How do I check for errors in my Fortran 95 code?

You can use the "list" command in GDB to view your code line by line and check for any syntax errors. You can also use compiler flags such as -Wall or -Werror to enable warnings and errors, which can help identify potential issues.

5. Can I convert my Fortran 95 code to a different programming language?

Yes, there are tools available that can help convert Fortran 95 code to other languages such as C or Python. However, it is important to note that the resulting code may not have the same functionality or efficiency as the original Fortran code, and manual adjustments may be necessary.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
951
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
239
  • Programming and Computer Science
Replies
4
Views
771
  • Programming and Computer Science
Replies
5
Views
3K
Back
Top