I want to add a symbolic function and its second derivative. The symbolic function is given by DSolve:
sol = DSolve[y''[x] + w^2 y[x] == A Cos[q x], y[x], x]
(* {{y[x] -> -((A Cos[q x])/(q^2 - w^2)) + C[1] Cos[w x] +
C[2] Sin[w x]}} *)
p[x_, q_, w_, A_] = y[x] /. sol[[1]]
(* -((A Cos[q x])/(q^2 - w^2)) + C[1] Cos[w x] + C[2] Sin[w x] *)
g[x_, q_, w_, A_] = p''[x, q, w, A] + w^2 p[x, q, w, A]
(* {((y'')[x] -> (A q^2 Cos[q x])/(q^2 - w^2) -
w^2 C[1] Cos[w x] - w^2 C[2] Sin[w x]) +
w^2 (-((A Cos[q x])/(q^2 - w^2)) + C[1] Cos[w x] + C[2] Sin[w x])} *)
I want Mathematica to combine like terms in g[x,q,w,A]
when displayed, and I want to be able to evaluate g[1,2,3,4]
to get a single numeric value, like so:
N[p[1, 2, 3, 4]]
(* -0.332917 - 0.989992 C[1] + 0.14112 C[2] *)
Which is very different from the form of the result given by g[1,2,3,4]
, as it contains (y''[x])[1]-> ...
:
g[1, 2, 3, 4]
(* {((y''[x])[1] -> -((16 Cos[2])/5) - 9 C[1] Cos[3] - 9 C[2] Sin[3]) + 9 ((4 Cos[2])/5 + C[1] Cos[3] + C[2] Sin[3])} *)
How do I make sure g
works like a regular symbolic function?
DSolve
, sosol
contains two undetermined constantsC[1]
andC[2]
$\endgroup$p
(So it acted as a regular function) but didn't apply the rule given byp''[x,w,q,A]
so it stayed a rule that would not simplify with the other expression. Replacing the definition ofg
withg[x_, q_, w_, A_] = y''[x] + w^2 p[x, q, w, A] /. p''[x]
solved the problem as it applied the rule for y''[x]. $\endgroup$