1
$\begingroup$

I wish to manipulate the following for different values of k, using Matheamtica 10:

QQ = NDSolve[{FF''[x] == -k^2*FF[x], FF[0] == 1, FF[Pi/(2 k)] == 0}, 
FF[x], {x, 0, (2 Pi)/k}] 
Plot[FF[x] /. QQ, {x, 0, 2 Pi/k}]

The following attempt examples do not work (return multiple errors)... what does?

Fail 1:

 Manipulate[
 QQ = NDSolve[{FF''[x] == -k^2*FF[x], FF[0] == 1, FF[Pi/(2 k)] == 0}, 
 FF[x], {x, 0, 2Pi/(k)}, MaxStepSize -> 0.001], {k, 0, 5}] 
 Plot[FF[x] /. QQ, {x, 0, 2Pi/(k)}] 

Fail 2:

Manipulate[
Plot[FF[x] /. 
NDSolve[{FF''[x] == -k^2*FF[x], FF[0] == 1, FF[Pi/(2 k)] == 0}, 
FF[x], {x, 0, 2Pi/(k)}, MaxStepSize -> 0.001], {x, 0, 2Pi/(k)}], {k, 0, 5}] 
$\endgroup$
  • $\begingroup$ You cannot have x be a symbolic variable if it is assigned a numeric value. E.g. Plot[.., {x, 0,..}] assigns x to be a number. And k cannot be 0, since you divide by it. $\endgroup$ – Michael E2 May 8 '18 at 12:00
  • $\begingroup$ @Michael E2 2: I wish to manipulate k, and plot over x's. $\endgroup$ – user1611107 May 8 '18 at 12:03
  • $\begingroup$ But Plot assigns numbers to x. That messes up NDSolve, since x is no longer a variable. $\endgroup$ – Michael E2 May 8 '18 at 12:03
  • $\begingroup$ ok, maybe I don't explain myself well: Think of 'k' as eigenvalues. For each 'k' I wish to find numerically the eigenfunction, and then plot it over a range of x's. There one whole function of 'x' for each 'k' mode... $\endgroup$ – user1611107 May 8 '18 at 12:06
  • $\begingroup$ Note that ParametricNDSolve, which is what I'd normally suggest for this sort of problem, doesn't seem to work for boundary value problems. $\endgroup$ – Michael Seifert May 8 '18 at 12:47
2
$\begingroup$

Try this:

Manipulate[
  With[
    { QQ = First @ NDSolve[
        {FF''[x] == -k^2*FF[x], FF[0] == 1, FF[Pi/(2 k)] == 0}
      , FF[x]
      , {x, 0, 2 Pi/(k)}
      ]
    }
  , Plot[FF[x] /. QQ, {x, 0, 2 Pi/(k)}]
  ]
, {k, 0.001, 5}
]

Reply to comment above:

Maybe I don't explain myself well, either :) -- OK, so k = 0 is a problem because of 2Pi/(k), but that's easy to fix. When that is fixed, the reason your second code still fails has nothing to do with k. You're conceiving of there being one function of x, but that's not how the code actually works. First of all Plot holds its arguments. That means the expression it sees is this:

FF[x] /. NDSolve[{FF''[x] == -k^2*FF[x], FF[0] == 1, FF[Pi/(2 k)] == 0}, 
  FF[x], {x, 0, 2Pi/(k)}, MaxStepSize -> 0.001]

Plot then evaluates this expression at several values of x. Take, say, x = 0.123; then Plot tries to evaluate the following, which generates errors:

FF[0.123] /. NDSolve[{FF''[0.123] == -k^2*FF[0.123], FF[0] == 1, FF[Pi/(2 k)] == 0}, 
  FF[0.123], {0.123, 0, 2Pi/(k)}, MaxStepSize -> 0.001]

This doesn't work because there is no independent variable x in the code at this point. The x inside NDSolve needs to be a symbol for NDSolve to work; the x in Plot needs to be set equal to a number for Plot to work. These two x need to be separated in some way for the whole to work.

For instance, changing one of the x to y works, but it's very slow because the BVP is solved hundreds of times per plot:

Manipulate[
 Plot[FF[x] /. 
   NDSolve[{FF''[y] == -k^2*FF[y], FF[0] == 1, FF[Pi/(2 k)] == 0}, 
    FF[x], {y, 0, 2 Pi/(k)}],
  {x, 0, 2 Pi/(k)}],
 {k, 0.001, 5}]

For real efficiency, the following simply connects the steps NDSolve takes, which are usually small enough to make a good graph:

Manipulate[
 ListLinePlot@
  NDSolveValue[{FF''[x] == -k^2*FF[x], FF[0] == 1, FF[Pi/(2 k)] == 0},
   FF, {x, 0, 2 Pi/(k)}],
 {k, 0.001, 5}]
$\endgroup$
  • $\begingroup$ Great answer! Thanks a lot :) $\endgroup$ – user1611107 May 8 '18 at 13:48

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.