Task

Create a program that finds and outputs the roots of a given function, range and (if applicable) step width.

The program should identify whether the root is exact or approximate.



For this task, use:      ƒ(x)   =   x3 - 3x2 + 2x 

 
PROGRAM ROOTS_FUNCTION
 
!VAR E,X,STP,VALUE,S%,I%,LIMIT%,X1,X2,D
 
FUNCTION F(X)
    F=X*X*X-3*X*X+2*X
END FUNCTION
 
BEGIN
  X=-1
  STP=1.0E-6
  E=1.0E-9
  S%=(F(X)>0)
 
  PRINT("VERSION 1: SIMPLY STEPPING X")
  WHILE X<3.0 DO
    VALUE=F(X)
    IF ABS(VALUE)<E THEN
        PRINT("ROOT FOUND AT X =";X)
        S%=NOT S%
      ELSE
        IF ((VALUE>0)<>S%) THEN
          PRINT("ROOT FOUND AT X =";X)
          S%=NOT S%
        END IF
    END IF
    X=X+STP
  END WHILE
 
  PRINT
  PRINT("VERSION 2: SECANT METHOD")
  X1=-1.0
  X2=3.0
  E=1.0E-15
  I%=1
  LIMIT%=300
  LOOP
    IF I%>LIMIT% THEN
       PRINT("ERROR: FUNCTION NOT CONVERGING")
       EXIT
    END IF
    D=(X2-X1)/(F(X2)-F(X1))*F(X2)
    IF ABS(D)<E THEN
      IF D=0 THEN
         PRINT("EXACT ";)
       ELSE
         PRINT("APPROXIMATE ";)
      END IF
      PRINT("ROOT FOUND AT X =";X2)
      EXIT
    END IF
    X1=X2
    X2=X2-D
    I%=I%+1
  END LOOP
END PROGRAM
 