1
$\begingroup$

Suppose that I have a 2nd order, linear ODE and I want to plot the solution I found using DSolve. How can I plot giving specific values of C[1] and C[2]. Let's say I want to plot the solutions for C[1]={1,3,4} and C[2]={1,3}. That makes 6 solutions which I want to plot.

I give this specific example:

eq = y''[x] - 3 y'[x] + 2 y[x] == 7 Sin[4 x];

solution = DSolve[eq, y[x], x]
(* {{y[x] -> 
   E^x C[1] + E^(2 x) C[2] + 7/170 (6 Cos[4 x] - 7 Sin[4 x])}} *)

yy = solution[[1, 1, 2]]
(* E^x C[1] + E^(2 x) C[2] + 7/170 (6 Cos[4 x] - 7 Sin[4 x]) *)

How can I make a list of partial solutions giving some specific values that I choose for C[1] and C[2], let's name it partialsolutions which I later will plot using Plot[partialsolutions, {x,-5,5}] for example.

$\endgroup$
1
  • 1
    $\begingroup$ This is your second question, head to the help centre and read about proper code formatting practices and format your question. $\endgroup$ – Sektor Apr 20 '15 at 10:48
1
$\begingroup$
expression = E^x C[1] + E^(2 x) C[2] + 7/170 (6 Cos[4 x] - 7 Sin[4 x]);

c1list = {.01, .04, .06}; c2list = {.001, .002};
funcs = Flatten@ Table[expression /. {C[1] -> i, C[2] -> j}, {i, c1list}, {j, c2list}];

Plot[funcs, {x, 3, 5}, PlotLegends -> "Expressions"]

enter image description here

You can also do:

replacements = Table[{C[1] -> i, C[2] -> j}, {i, c1list}, {j, c2list}]
Plot[Evaluate[expression /. replacements], {x, 3, 5}, PlotLegends -> "Expressions"]
(* same picture *)
$\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.