MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Generate polynomial of specified degree


In article <dgtuja$6ni$1 at smc.vnet.net>, dh <dh at metrohm.ch> wrote:

> it is not too hard:
> 
> f[x_Symbol,n_Integer]:= Array[c,{n+1},0].x^Range[0,n]

Personally, I think it is better to separate parameters from variables, 
and also to localise the coefficient name:

  f[n_Integer,c_:c][x_Symbol]:= With[{p = Range[0, n]}, (c /@ p) . x^p]
  f[0, c_:c][x_Symbol]:= c[0]

Now try

  f[0][x]
  f[3][x]
  f[3,a][x]

Sometimes using pure functions is advantageous:

  Clear[f]
  f[n_Integer,c_:c] := Function[x, (c /@ Range[0, n]) . x^Range[0, n]]
  f[0, c_:c]:= Function[x, c[0]]

As a particular advantage of this syntax, try

  f[2]'[x]

Cheers,
Paul



> Renan wrote:
> > Hello,
> > 
> > Sorry if this may seem a silly question, but I readed the Mathematica
> > help and found no references.
> > 
> > Is there any function that can generate a polynomial of a given degree?
> > 
> > e.g.
> > 
> > For degree 1, f[x] would return a x + b
> > For degree 2, f[x] would return a x^2 + b x + c (quadratic equation)
> > For degree 3, f[x] would return a x^3 + b x^2 + c x + d (quadratic equation)
> > 
> > Thanks,
> > Renan - Canoas, RS, Brazil
> >

_______________________________________________________________________
Paul Abbott                                      Phone:  61 8 6488 2734
School of Physics, M013                            Fax: +61 8 6488 1014
The University of Western Australia         (CRICOS Provider No 00126G)    
AUSTRALIA                               http://physics.uwa.edu.au/~paul


  • Prev by Date: Re: Vectorization
  • Next by Date: Re: Problem in loading the Statistics package.
  • Previous by thread: Re: Generate polynomial of specified degree
  • Next by thread: Re: Generate polynomial of specified degree