Sign up ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

This question already has an answer here:

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}]
            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?

share|improve this question

marked as duplicate by Mr.Wizard Jan 27 at 22:00

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

And what exactly is your code supposed to do? – R. M. Mar 4 '14 at 2:21
@ rm -rf♦ I want to Plot x[t],versus t, when varying w from 0.1 to 1.5. – Lawerance Mar 4 '14 at 2:22
Can anyone tell me why this post get negative vote? I'm confused and i think everything is clear here. – Lawerance Mar 4 '14 at 2: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 4 '14 at 2:41
@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 :) – R. M. Mar 4 '14 at 2:50

2 Answers 2

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}]
 With[{x1 = x[w] /. sol}, Plot[x1[t], {t, 0, 10}]], {w, 0.1, 1.5}]

Mathematica graphics

share|improve this answer
Next time, how do I tell if it I need Parametric NDSolve or not? – Lawerance Mar 4 '14 at 2: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 4 '14 at 2:43

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.

share|improve this answer
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 4 '14 at 2:59

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