0
$\begingroup$

I want to plot x[t], when i manipulate w from 0.1 to 1.5. Here is what I've attempted, which does not work as expected.

c = 0.05; f = 0.7;
diffeq = {x''[t] + c x'[t] + Sin[x[t]] + F Cos[w t] == 0};
inicond = {x'[0] == 0, x[0] == 1};
eqnlist = Join[diffeq, inicond];
soln := NDSolve[eqnlist, x, {t, 0, 10}]
Manipulate[
            Plot[Evaluate[{x[t] /.soln}, {t, 0, 10}], PlotRange ->All, AxesLabel-> {t, x}], 
          {w, 0.1, 1.5, 0.1}]

What is the appropriate way to plot the numerical solution of a differential equation while varying a parameter?

$\endgroup$
  • $\begingroup$ And what exactly is your code supposed to do? $\endgroup$ – rm -rf Mar 4 '14 at 2:21
  • $\begingroup$ @ rm -rf♦ I want to Plot x[t],versus t, when varying w from 0.1 to 1.5. $\endgroup$ – Lawerance Mar 4 '14 at 2:22
  • $\begingroup$ Can anyone tell me why this post get negative vote? I'm confused and i think everything is clear here. $\endgroup$ – Lawerance Mar 4 '14 at 2:30
  • $\begingroup$ Welcome to M.SE. I edited your question (heavily) to demonstrate how a question can be formatted. In general, using the formatting guidelines suggested in the help section will generate more favorable responses to your questions. $\endgroup$ – bobthechemist Mar 4 '14 at 2:41
  • 2
    $\begingroup$ @Lawerance It's always a good idea to explain what you're trying to do (i.e., what is this equation) rather than just say you want to plot something from a to b. Your question only had code, no context. As it turned out in this case, the problem had nothing to do with the plotting... Imagine yourself in this position — would you like it or be willing to help if someone just said "Here's a bunch of code. It should do what I want, but it doesn't" without telling you what they want it to do? We can't read minds. Hope you try to take these suggestions constructively for your next question :) $\endgroup$ – rm -rf Mar 4 '14 at 2:50
5
$\begingroup$

This is a good case for ParametricNDSolve:

c = 0.05; f = 0.7;
diffeq = x''[t] + c x'[t] + Sin[x[t]] + f Cos[w t] == 0;
inicond = {x'[0] == 0, x[0] == 1};
sol = ParametricNDSolve[{ 
   x''[t] + c x'[t] + Sin[x[t]] + f Cos[w t] == 0, x'[0] == 0, 
   x[0] == 1}, x, {t, 0, 10}, {w}]
Manipulate[
 With[{x1 = x[w] /. sol}, Plot[x1[t], {t, 0, 10}]], {w, 0.1, 1.5}]

Mathematica graphics

$\endgroup$
  • $\begingroup$ Next time, how do I tell if it I need Parametric NDSolve or not? $\endgroup$ – Lawerance Mar 4 '14 at 2:39
  • $\begingroup$ @Lawerance , the documentation will help you out tremendously here. You must provide numerical values for all parameters if using NDSolve, but have the flexibility of varying the parameters with ParametricNDSolve. $\endgroup$ – bobthechemist Mar 4 '14 at 2:43
1
$\begingroup$

bob's answer is the right one, but I thought I would explain a bit about why your code isn't working. The essential problem is that your expression soln won't work with NDSolve since it has a non-numeric parameter, omega. Setting the value of omega in the Manipulate isn't sufficient to avoid this error. If you just evaluate soln you get this:

NDSolve::nlnum: The function value {-0.00014456,-0.841464-0.7 Cos[0.0000937804 omega]} is not a list of numbers with dimensions {2} at {t,x[t],(x^[Prime])[t]} = {0.0000937804,1.,-0.00014456}. >>

(* {{x->InterpolatingFunction[{{0.,0.}},<>]}} *)

This works:

NDSolve[eqnlist /. omega -> 0.1, x, {t, 0, 10}]
(*  {{x->InterpolatingFunction[{{0.,10.}},<>]}} *)

As an alternative to bob's answer, what I'd suggest is that you change your Manipulate to something like this:

Manipulate[ Plot[Evaluate[
   x[t] /. NDSolve[eqnlist /. omega -> oo, x, {t, 0, 10}], {t, 0, 10}], 
   PlotRange -> All, AxesLabel -> {t, x}], {oo, 0.1, 1.5, 0.1}]

It might be slower for more complex differential equations, but it certainly works in your case.

$\endgroup$
  • $\begingroup$ Thanks a lot! Regarding the problem both you and Bob mentioned that NDSolve need the numerical values of all the variables, my question is that when i define "soln", I used ":=", instead of "=". I thought ":=" can hold the function to "Manipualte". $\endgroup$ – Lawerance Mar 4 '14 at 2:59

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