1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Fortran 95 code won't run

  1. Oct 23, 2015 #1
    1. The problem statement, all variables and given/known data. 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!

    3. The attempt at a solution.
    Mod note: Added code tags.
    Code (Fortran):

    program function
    implicit none
      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'
              else pr
             ex:if (abs(x-3)<1e-6 .or. abs(x)<1e-6) then

            else ex

          in: if (abs(x-3)>=1e-6 .and. abs(x-8)<=1e-6) then

        else in
               out:if (abs(x-8)>1e-6 .and. abs(x-10)<1e-6) then

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

    Last edited by a moderator: Oct 23, 2015
  2. jcsd
  3. Oct 23, 2015 #2


    User Avatar
    Staff Emeritus
    Science Advisor
    Homework Helper

    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.
  4. Oct 23, 2015 #3


    Staff: Mentor

    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: ).
  5. Oct 23, 2015 #4
    Thank you very much both SteamKing and Mark44 I really appreciate your help!!,and excuse me for not adding tags around my code
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted