# Dynamic application of several polynomials

I know how to get the 'resulting image' (y) from the application of a certain function (f) (here represented as the coefficients of a polynomial) over a certain interval (x):

x = Range[27000]/27001.;

f = {25.62, -38.43, 21.81}/9;

y = Map[f[[1]]*#^3 + f[[2]]*#^2 + f[[3]]*# &, x];

ListPlot[y]


How do i get the 'resulting images' from the application of several polynomials (represented as its coefficients) over a certain interval (x)?

Considering the representation of those several polynomials to be something like:

polynomials = Map[
{9 + #[[1]] - #[[2]], -#[[1]], #[[2]]} &,
Flatten[Outer[
List,
{23.75, 28.02, 32.29, 36.56, 40.83, 45.1, 49.37, 53.64, 57.91, 62.18},
{13.48, 15.9, 18.33, 20.75, 23.17, 25.6, 28.02, 30.44, 32.87, 35.29}
],
1]
]/9;

-
Guys, thank you guys very much for your solutions. In other forum I've received one suggestion, and I would like your opinion, since I'm far away from Mathematica for about 10 years. Considering process cost and ease of understanding, which you think is the best? Among each of yours and his, which is: y[f_] := (f.#^{3, 2, 1} &) /@ x; ListLinePlot[Evaluate[y /@ polynomials], Frame -> True, Axes -> False, ImageSize -> 500] –  Samuel Siqueira Mar 15 '13 at 14:51
@belisarius? :o) (my comment/question above) –  Samuel Siqueira Mar 15 '13 at 14:52
@bill-s? :o) (my comment/question above) –  Samuel Siqueira Mar 15 '13 at 14:53
@michael-e2? :o) (my comment/question above) –  Samuel Siqueira Mar 15 '13 at 14:54

A similar approach is to define a function

 Clear[x]; p[x_, {a_, b_, c_}] := (a x^3 + b x^2 + c x)/9;


and then build a table of polynomials. Using the variable polynomials from the OPs question, these could be plotted as

 Plot[Table[p[x, polynomials[[i]]], {i, 1, Length[polynomials]}], {x, 0, 1}]


This leads to substantially the same plot as belisarius, but there are a few differences. This uses Plot instead of ListPlot because it's plotting the polynomials instead of the sampled values of the polynomials. As a consequence, the polynomials are being plotted against the values of x rather than against the index of a list of x values.

-
your solution is really flexible and general... thank you very much! –  Samuel Siqueira Mar 15 '13 at 1:42

I'm not sure if I understand the question. Anyway.

To get all the images, just:

y1[f_] := Map[f[[1]]*#^3 + f[[2]]*#^2 + f[[3]]*# &, x];

ListPlot[y1 /@ polynomials]


Edit

Another possible nterpretation of your question is that you are trying to apply the polynomials sequentially, but there is a divergence near the end of the list getting notorious starting around the poly nbr. 26:

x = Range[300]/301.;
y1[f_][x_] := f[[1]]*#^3 + f[[2]]*#^2 + f[[3]]*# &@x;
ListLinePlot[Fold[y1[#2] /@ #1 &, x, polynomials[[1 ;; 25]]], PlotRange -> All]


 ListLinePlot[Fold[y1[#2] /@ #1 &, x, polynomials[[1 ;; 26]]], PlotRange -> All]


-
your answer was really direct and practical... and I meant the first understanding you had... thank you very much! –  Samuel Siqueira Mar 15 '13 at 1:35

Edit

In reply to your comment above, if you want to get the values (images) of all your polynomials at a large number of values of x, then something like this will be fastest:

y = polynomials.(x^# & /@ {3, 2, 1})


If you want to only plot the polynomials, then use Plot, not ListPlot, and the formulas for the polynomials. Each of the three current answers has way (@bill s, @belisarius, and mine). I think you would be the best judge of which has the greatest ease of understanding. I don't think to get the formulas, process speed matters much until you have lots of polynomials.

Original

Here's function that takes a list of coefficients (edit now assuming no constant term) and returns a polynomial function:

polyFn[coeff_List] := Function[{x}, Evaluate@Fold[x (#2 + #1) &, 0, coeff]]


Example:

polyFn[{a, b, c}][x]
(* x (c + x (b + a x)) *)


The function polyFn[{a, b, c}] can be evaluated at many points in one call and it will take advantage of the built-in vectorized functions Plus and Times automatically:

polyFn[{a, b, c}][{1, 2, 3}] // Expand
(* {a + b + c, 8 a + 4 b + 2 c, 27 a + 9 b + 3 c} *)


Ten random coefficents for ten polynomials:

polys = RandomReal[{-1, 1}, {10, 3}];
polynomials = polyFn /@ polys

(* {Function[{x$}, x$ (0.682011 + (0.64716 + 0.179604 x$) x$)],
Function[{x$}, x$ (0.327702 + (0.417207 + 0.832486 x$) x$)],
... omitted ...
Function[{x$}, x$ (0.684578 + (0.196256 + 0.159777 x$) x$)]} *)


And evaluate them all on the same set of inputs:

images = Through[polynomials[Range[0., 2, 0.1]]];


Through takes each function in the list polynomials and applies to Range[..]. Voilà:

ListLinePlot[images]


I am accustomed to polynomials being represented with the coefficients in the other order, from constant to higher degree, so that the index in the list corresponds to the degree of the term (off by one if one starts with a constant term). The correspondence can be convenient, but then one has to change polyFn:

polyFn[coeff_List] := Function[{x}, Evaluate@Fold[#2 + x #1 &, 0, Reverse@coeff]]


Both versions handle arbitrarily long lists of coefficients (i.e., arbitarily high degree polynomials).

And of course you can use Plot with them, as @bill suggests. Use

Plot[Evaluate[Through[polynomials[x]]], {x, 0, 2}]

-
your answer was really complete... thank you very much! –  Samuel Siqueira Mar 15 '13 at 1:40