Newsgroup: it.comp.lang.pascal
Titolo: integrals, derivates, and some more!

I have to make a pascal program which must do the following:
    I should define the type polinomio,  (es: 2x^3+3x-4)
    I should resolve the polinomio (find a result giving a value for X)
    I should find the undefined integral of the polinomio
    I should find the derivate of the polinomio...
who can help me in any way?
I need procedures to read poli, a metod to store the poli in a simple way, I
need many good suggestions to make this prg in a clear , simple and fast way
THX
GeX
kah...@hotmail.com



...

In Delphi 3 there is a poly function in the math unit that uses an "open
array"
for the polynomial coefficients, for example,
    poly(x, [1.0, -2.0, 1.0])
can be used to evaluate x^2 -2*X + 1.  You can have an array of
coefficients and use the Slice function (new in Delphi 2) to pass
a specified number of coefficients to poly.
But you asked only about Pascal.  The following is a Turbo Pascal 5.5
program that does much of what you're asking.  It defines a Polynomial
object.  I wrote TakePolynomialDerivative as a separate procedure,
but it could be made a method of a new "class" object in Delphi
(instead of the old "object" definition).
This was a "quick and dirty" example I wrote in 1991 and have
haven't look at it in years.  It is not very elegant.
But, I hope this gets you started.
{$N+,E+}
PROGRAM horner;
  CONST
    MaxRealArraySize = 50;
  TYPE
    RealArray =
      ARRAY[0..MaxRealArraySize] OF RealType;
    Polynomial =
      OBJECT
        degree     :  WORD;
        coefficient:  ^RealArray;       {ARRAY[0..degree]}
        CONSTRUCTOR Init (n:  WORD);
        DESTRUCTOR  Done;
        PROCEDURE   SetCoefficient (i:  WORD; a:  RealType);
        FUNCTION    HornerEvaluate (x:  RealType):  RealType;
      END;
    CONSTRUCTOR Polynomial.Init (n:  WORD);
    BEGIN
      degree := n;
      GetMem (coefficient, (degree+1) * SizeOf(RealType));
    END {Polynomial.Init};
    PROCEDURE   Polynomial.SetCoefficient (i:  WORD; a:  RealType);
    BEGIN
      coefficient^[i] := a
    END {Polynomial.SetCoefficient};
    FUNCTION    Polynomial.HornerEvaluate (x:  RealType):  RealType;
      VAR
        i  :  WORD;
        sum:  RealType;
    BEGIN          {Evaluates polynomial:  ä a[i]*x^i, i = 0..degree}
      sum := coefficient^[degree];
      FOR i := degree-1 DOWNTO 0 DO
        sum := x*sum + coefficient^[i];
      HornerEvaluate := sum
    END {Polynomial.HornerEvaluate};
    DESTRUCTOR  Polynomial.Done;
    BEGIN
      FreeMem (coefficient, (degree+1) * SizeOf(RealType));
    END {Polynomial.Done};
  PROCEDURE TakePolynomialDerivative (          poly:  Polynomial;
                                      VAR derivative:  Polynomial);
    VAR
      i:  WORD;
  BEGIN
    derivative.Init (poly.degree - 1);
    FOR i := 1 TO poly.degree DO
      derivative.SetCoefficient (i-1, i*poly.coefficient^[i])
  END {TakePolynomialDerivative};
  FUNCTION NewtonPolynomialRoot (start:  RealType; p:  polynomial;
                                 MaxIterations:  WORD;
                                 VAR Iterations:  WORD):  RealType;
    VAR
      d :  RealType;
      fn:  RealType;
      i :  WORD;
      q :  Polynomial;
      x :  RealType;
  BEGIN
    TakePolynomialDerivative (p, q);
    x :=  start;
    i := 0;
    REPEAT
      INC (i);
      fn := p.HornerEvaluate(x);
      d  := q.HornerEvaluate(x);
      x := x - fn / d
    UNTIL (i > MaxIterations) OR (ABS(fn) < 1.0E-8);
    q.Done;
    iterations := i;
    NewtonPolynomialRoot := x
  END {NewtonRoot};
  VAR
    i    :  INTEGER;
    loops:  WORD;
    p    :  Polynomial;
    root :  RealType;
    start:  RealType;
    x    :  RealType;
BEGIN
  p.Init (4);
  p.SetCoefficient (4, 4.0);
  p.SetCoefficient (3,-8.0);
  p.SetCoefficient (2, 5.0);
  p.SetCoefficient (1,-1.0);
  p.SetCoefficient (0,-3.0);
  FOR i := -10 TO 10 DO BEGIN
    start := i;
    root := NewtonPolynomialRoot (start,p,100,loops);
    WRITELN (start:5:1, ' ', root:15:9, ' ', loops:4);
  END;
  p.Done;
END.
efg
_________________________________________
Visit the Delphi Math FAQ and Links page, or the
Delphi Math Functions page, as part of
efg's Computer Lab:  
http://infomaster.net/external/efg
Earl F. Glynn                 E-Mail:  
Earl...@att.net
MedTech Research Corporation, Lenexa, KS  USA