3
$\begingroup$

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

(* cVal[1] + cVal[2] x + cVal[3] x^2 + cVal[4] x^3 *)

I've tried things along this line:

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

(* {cVal[1], cVal[2], cVal[3], cVal[4]} *)

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

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

cList.xList

(* cVal[1] + x cVal[2] + x^2 cVal[3] + x^3 cVal[4] *)

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[1] + cVal[2] x + cVal[3] x^2 + cVal[4] x^3

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

y'[x_] := cVal[2]  + 2 cVal[3] x + 3 cVal[4] 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[0] = r; y'[0] = S; y[1] = t; y'[1] = u;

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

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

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

1 Answer 1

2
$\begingroup$
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[[1]], dy[n, #1] == #2[[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

Mathematica graphics


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
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.