


*******************************************************************************
**                                                                           **
**      Programmet ber{knar alla r|tter (reella och komplexa) till ett       **
**      N:te-gradspolynom med komplexa koefficienter. (N-max = 10)           **
**                                                                           **
**                n        n-1                                               **
**      P(z) = a z  + a   z    + ... + a z + a                               **
**              n      n-1              1     0                              **
**                                                                           **
**      Avbrott sker om                                                      **
**         1) Abs (Z1-Z0) < EPS       ==>   Roten funnen = Z1                **
**         2) ITER > MAX         L}ngsam konvergens   ==>   Avbrott          **
**                                                                           **
**      Programmet s{tter EPS till 1E-7 och MAX till 30                      **
**                                                                           **
**      Anv{nd metod {r NEWTON-RAPHSONS metod:  z1 = z0 - P(z0)/P'(z0)       **
**      V{rdet av P(z0) och P'(z0) ber{knas med hj{lp av HORNERS SCHEMA.     **
**                                                                           **
**      F{ltet A(0:10) inneh}ller polynomets komplexa koefficienter.         **
**      F{ltet B(1:10) inneh}ller de komplexa koefficienterna till poly-     **
**      nomet Q(z),                                                          **
**                  d{r P(Z) = (z-z0)*Q(z) + P(z0)                           **
**      Koefficienterna till Q(z) erh}lles med hj{lp av HORNERS SCHEMA.      **
**                                                                           **
**      N{r f|rsta roten erh}llits med NEWTON-RAPHSONS metod, divideras den  **
**      bort (s.k. deflation). Kvotpolynomet  = Q(z).                        **
**      Proceduren upprepas d{refter med koefficienterna till Q(z) som       **
**      indata.                                                              **
**                                                                           **
**      Som startapproximation anv{nds i samtliga fall STARTV = 1+i          **
**      Z0 {r f|reg}ende approximation till roten.                           **
**      Z1 {r senast ber{knade approximation till roten                      **
**      F0 = P(Z0)                                                           **
**      FPRIM0 = P'(Z0)                                                      **
**                                                                           **
*******************************************************************************


      COMPLEX      A(0:10), B(1:10), Z0, Z1, STARTV
      INTEGER      N, I, ITER, MAX
      REAL         EPS

      DATA  EPS/1E-7/, MAX /30/, STARTV /(1,1)/

*******************************************************************************
!

*******************************************************************************
20    WRITE(6,*)  'Ange polynomets gradtal'
      READ (5,*) N
      IF (N .GT. 10) THEN
         WRITE(6,*) 'Polynomets gradtal f}r inte vara st|rre {n 10'
      GOTO 20

      WRITE (6,*) 'Ge polynomets koefficienter, som komplexa konstanter'
      WRITE (6,*) 'H|gstagradskoefficienten f|rst'

      DO 30 I = N, 0, -1
          WRITE (6,*) 'A(' , I, ') = '
          READ  (5,*)  A(I)
30    CONTINUE

      WRITE (5,*) '    R|tterna {r','            Antal Iterationer'

*******************************************************************************

40    IF (N GT 0) THEN

C     ******     Finn n{sta rot     ******
          Z0 = (0,0)
          ITER = 0
          Z1 = STARTV
C         +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

50        IF (ABS(Z1-Z0) .GE. EPS) THEN 

C         ++++++ Forts{tt iterera tills tv} efterf|ljande r|tter ++++++
C         ++++++ {r tillr{ckligt n{ra varandra.                  ++++++

              ITER = ITER + 1
              IF (ITER .GT. MAX) THEN
C             ------   F|r m}nga iterationer  ==> Avbrott  ------
                  WRITE (6,*) 'F|r m}nga iterationer.'
                  WRITE (6,*) 'Senaste approximationen till roten {r ',Z1
              GOTO 200
              ENDIF

              Z0 = Z1

              HORNER (N, A, B, Z0, F0, FPRIM0)

C             ++++++   NEWTON-RAPHSONS METOD                 ++++++

              Z1 = Z0 - F0/FPRIM0
          GOTO 50
          ENDIF

C         +++++++++++++++++++++++++++++++++++++++++++++++++++++++++

100       WRITE (6,*) Z1, '         ',Iter
C         ****** Roten funnen. Dividera bort den och s|k n{sta rot ******

          N = N - 1
          FOR I = 0 TO N DO
              A(I) = B(I+1)
 
      GOTO 40
      ENDIF

*******************************************************************************

200   END
!







      SUBROUTINE  HORNER(N, A, B, Z, F, FPRIM)
******    Parametrarnas betydelse - se huvudprogrammet   ******
******    BI och CI hj{lpvariabler.                      ******

      INTEGER N, I
      COMPLEX A(1:10), B(0:10), Z, F, FPRIM, BI, CI

      BI = A(N)
      B(N) = BI
      CI = BI

      DO 60  I = N-1, 1, -1
             BI = A(I) + Z*BI
             CI = BI + Z*CI
             B(I) = BI
C            ++++++ BI = B(I) f|r ber{kning av P(Z) ++++++
C            ++++++ CI  f|r ber{kning av P'(Z)      ++++++
60    CONTINUE

      F = A(0) + Z*BI
      FPRIM = CI		

      RETURN
      END
******   SLUT  HORNERS SCHEMA    ******

