0

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?

Dr. belisarius
  • 115,881
  • 13
  • 203
  • 453
Lawerance
  • 517
  • 3
  • 13
  • And what exactly is your code supposed to do? – rm -rf Mar 04 '14 at 02:21
  • @ rm -rf♦ I want to Plot x[t],versus t, when varying w from 0.1 to 1.5. – Lawerance Mar 04 '14 at 02:22
  • Can anyone tell me why this post get negative vote? I'm confused and i think everything is clear here. – Lawerance Mar 04 '14 at 02:30
  • 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. – bobthechemist Mar 04 '14 at 02:41
  • @bobthechemist Thanks, i am now going to read the guideline. – Lawerance Mar 04 '14 at 02:42
  • 2
    @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 :) – rm -rf Mar 04 '14 at 02:50
  • @ rm -rf♦ I posted this question today without context because yesterday when i asked a question with the whole context of the problem, some users thought i was asking them to do the homework for me. – Lawerance Mar 04 '14 at 03:00
  • @Lawerance but it indeed was homework and you hadn't adequately shown where you were stuck. You are not the first person to ask that homework here... probably half your class is lurking on this site. And to be quite honest, the other two deleted questions of yours don't have much context either... In any case, I don't want to get into a protracted conversation about this. I just wanted you to know why you were downvoted and what you can do better the next time :) – rm -rf Mar 04 '14 at 03:17
  • @rm-rf Hey! I learned the meaning of protracted. Let's call it a day. – Dr. belisarius Mar 04 '14 at 03:21
  • @Lawerance I posted yesterday some comment about your homework. Please don't take it personally. There's some kind of agreement on this site about what kind of help we should offer to homework tasks. While asking for some specific detail about Mathematica here for completing your HW is almost always considered OK, asking questions that (one way or another) imply "do it for me" are frowned upon. And that's not a vigilante pose. – Dr. belisarius Mar 04 '14 at 03:28
  • Cont .-- It's just that we've all been there once upon a time and we know that no matter the pressure you can be under, it's better for the student to be able to think out a solution. Not the better, not a perfect one, just a solution that works and can make him/her confident. – Dr. belisarius Mar 04 '14 at 03:29
  • @belisarius I got it. I finally figure out the one i asked you yesterday. Still thank you! – Lawerance Mar 04 '14 at 03:31

2 Answers2

5

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

bobthechemist
  • 19,693
  • 4
  • 52
  • 138
  • Next time, how do I tell if it I need Parametric NDSolve or not? – Lawerance Mar 04 '14 at 02:39
  • @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. – bobthechemist Mar 04 '14 at 02:43
1

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.

Verbeia
  • 34,233
  • 9
  • 109
  • 224
  • 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". – Lawerance Mar 04 '14 at 02:59