# Solving a system of generated equations?

I would like to generate a function in the following form, where the number of terms can be specified arbitrarily:

(* cVal + cVal x + cVal x^2 + cVal x^3 *)


I've tried things along this line:

cList = Block[{n = 1}, Table[cVal[i], {i, 2 (n + 1)}]]

(* {cVal, cVal, cVal, cVal} *)

xList = Block[{n = 1}, Table[x^(i - 1), {i, 2 (n + 1)}]]

(* {1, x, x^2, x^3} *)

cList.xList

(* cVal + x cVal + x^2 cVal + x^3 cVal *)


Is there a better way to construct this result from cList.xList ? I tried the following but it doesn't work

y[x_] := cList.xList


Since the length of y should change in my program if I change the value of n, how would I go about making this into a function of x?

So I want a function

y[x_] := cVal + cVal x + cVal x^2 + cVal x^3


and then it's derivative with respect to x would be

y'[x_] := cVal  + 2 cVal x + 3 cVal x^2


Now given the following conditions (for n=1, 4 conditions are necessary, more conditions would be needed if n is increased), say

y = r; y' = S; y = t; y' = u;


I would like to solve for the cVal's. For this case, to solve for cVal, cVal, cVal, cVal. So notice that for y=r, which means x=0, then plugging that in gives that cVal = r and so on.

Can anyone show me where I'm going wrong, or suggest a better approach?

• y[x_,n_]:=Array[c,n].x^Range[0,2*n+1] might be a start on what you want. Oct 27, 2014 at 4:05
• Should the solve part be automated? If so it's quite tricky.
– Öskå
Oct 30, 2014 at 18:40

c[n_?IntegerQ] := Array[cVal, 2 n + 2, 0]                  (* Unknowns *)
y[n_?NumericQ, x1_] := c[n].x^Range[0, 2 n + 1] /. x -> x1 (*function  value*)
dy[n_?NumericQ, x1_] := D[y[n, x], x] /. x -> x1           (*derivative*)

(*Now the solving function*)
res[n_, pts_, vals_] :=
Solve[Flatten@
MapThread[{y[n, #1] == #2[], dy[n, #1] == #2[]} &, {pts, vals}],
c@n]

(*Usage*)
pts = {x1, x2};                 (*x values*)
vals = {{y1, dy1}, {y2, dy2}};  (*function and derivative for each x value*)
res[1, pts, vals] // FullSimplify But please note that your system is linear and can be solved with LinearSolve[ ]:

y[n_?NumericQ]  := x^Range[0, 2 n + 1]   (*function*)
dy[n_?NumericQ] := D[y[n], x]            (*derivative*)
mat[n_, pts_]   := Flatten[{y[n], dy[n]} /. # & /@ Thread[x -> pts], 1]

(* Usage *)
pts  = {x1, x2};                       (*x values*)
vals = {{y1, dy1}, {y2, dy2}};         (*function and derivative for each x value*)
LinearSolve[mat[1, pts], Flatten@vals] // Together