0
$\begingroup$

I have an NDSolve expression working in stand alone fashion in a Manipulate. I cannot get the same NDSolve expression working in a Manipulate against a set of data points. My code will plot the data points in the Manipulate. My code will not plot the NDSolve expression in the same Manipulate. Here is my code (Do you see anything wrong?):

ClearAll[t, f2a, x0, v0, a1, f1, m1, c1, k0, k1, e1]

f2a[t_, x0_, v0_, a1_, f1_, m1_, c1_, k0_, k1_, e1_] := 
 xx[t] /. NDSolve[{m1 xx''[t] + 
      c1 xx'[t] + (k1 + (k0 - k1) Exp[-t e1]) xx[t] == 
     a1 Cos[f1 2 \[Pi] t], xx[0] == x0, xx'[0] == v0}, xx, {t, 0, 250}]

Manipulate[
 Show[Plot[f2a[t, x0, v0, a1, f1, m1, c1, k0, k1, e1], {t, 0, 250}, 
   Frame -> True, GridLines -> Automatic, AxesOrigin -> {0, 0}, 
   PlotRange -> {{0.0, 250}, {-20.0, 10.0}}, 
   ImageSize -> {800, 509.3}, 
   PlotStyle -> {Orange, Dashed, Thickness[0.01]}, 
   AspectRatio -> 2.0/\[Pi], 
   FrameLabel -> {Style["Time (months)", Bold, Blue], 
     Style["Amplitude ", Bold, Blue]}, 
   LabelStyle -> Directive[Blue, Large]], pltMale50thIn50th, 
  pltMNoon50th], {{x0, 0.0, 
   Style["Initial (x0)", Blue, Bold, Medium]}, -10.0, 10.0, 1.0, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, {{v0, 0.0, 
   Style["(Initial v0)", Blue, Bold, Medium]}, -20.0, 20.0, 1.0, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, {{a1, 1.0, 
   Style["(Force a1)", Blue, Bold, Medium]}, 0.0, 20.0, 0.10, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, {{f1, 0.0, 
   Style["(Force f1)", Blue, Bold, Medium]}, -0.0, 2.5, 0.10, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, {{m1, 1.0, 
   Style["(Mass m1)", Blue, Bold, Medium]}, 0.10, 5.0, 0.10, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, {{c1, 2.5, 
   Style["( DashPot c1)", Blue, Bold, Medium]}, 0.0, 5.0, 0.10, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, Delimiter, 
 Style["Elasticity k1 + (k0 - k1) exp(-e1 t)", Blue, Bold, 
  Medium], {{k0, 2.0, Style["(Elasticity k0)", Blue, Bold, Medium]}, 
  0.25, 5.0, 0.250, ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, {{k1, 1.0, 
   Style["(Elasticity k1)", Blue, Bold, Medium]}, 0.25, 5.0, 0.25, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, {{e1, 1.0, 
   Style["(Elasticity e1)", Blue, Bold, Medium]}, 0.0, 20.0, 0.25, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, Appearance -> "Labeled"}, 
 ControlPlacement -> Top, Paneled -> True, ImageMargins -> 0]
$\endgroup$
1
  • $\begingroup$ I'd suggest you take some time and read this post. It will give you a sold background to understand how Mathematica works. $\endgroup$
    – ercegovac
    Oct 12, 2017 at 17:57

1 Answer 1

2
$\begingroup$

Plot has HoldAll attribute, which means that your function is not evaluated. To alleviate this , just wrap your function with Evaluate and it will work

ClearAll[t, f2a, x0, v0, a1, f1, m1, c1, k0, k1, e1]

f2a[t_, x0_, v0_, a1_, f1_, m1_, c1_, k0_, k1_, e1_] := 
 xx[t] /. NDSolve[{m1 xx''[t] + 
      c1 xx'[t] + (k1 + (k0 - k1) Exp[-t e1]) xx[t] == 
     a1 Cos[f1 2 \[Pi] t], xx[0] == x0, xx'[0] == v0}, xx, {t, 0, 250}]

Manipulate[
 Plot[Evaluate[f2a[t, x0, v0, a1, f1, m1, c1, k0, k1, e1]], {t, 0, 
   250}, Frame -> True, GridLines -> Automatic, AxesOrigin -> {0, 0}, 
  PlotRange -> {{0.0, 250}, {-20.0, 10.0}}, ImageSize -> {800, 509.3},
   PlotStyle -> {Orange, Dashed, Thickness[0.01]}, 
  AspectRatio -> 2.0/\[Pi], 
  FrameLabel -> {Style["Time (months)", Bold, Blue], 
    Style["Amplitude ", Bold, Blue]}, 
  LabelStyle -> Directive[Blue, Large]], {{x0, 0.0, 
   Style["Initial (x0)", Blue, Bold, Medium]}, -10.0, 10.0, 1.0, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, {{v0, 0.0, 
   Style["(Initial v0)", Blue, Bold, Medium]}, -20.0, 20.0, 1.0, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, {{a1, 1.0, 
   Style["(Force a1)", Blue, Bold, Medium]}, 0.0, 20.0, 0.10, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, {{f1, 0.0, 
   Style["(Force f1)", Blue, Bold, Medium]}, -0.0, 2.5, 0.10, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, {{m1, 1.0, 
   Style["(Mass m1)", Blue, Bold, Medium]}, 0.10, 5.0, 0.10, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, {{c1, 2.5, 
   Style["( DashPot c1)", Blue, Bold, Medium]}, 0.0, 5.0, 0.10, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, Delimiter, 
 Style["Elasticity k1 + (k0 - k1) exp(-e1 t)", Blue, Bold, 
  Medium], {{k0, 2.0, Style["(Elasticity k0)", Blue, Bold, Medium]}, 
  0.25, 5.0, 0.250, ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, {{k1, 1.0, 
   Style["(Elasticity k1)", Blue, Bold, Medium]}, 0.25, 5.0, 0.25, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, 
  Appearance -> "Labeled"}, {{e1, 1.0, 
   Style["(Elasticity e1)", Blue, Bold, Medium]}, 0.0, 20.0, 0.25, 
  ControlType -> Slider, ImageSize -> Medium, 
  DefaultBaseStyle -> {Blue, Bold, Medium}, Appearance -> "Labeled"}, 
 ControlPlacement -> Top, Paneled -> True, ImageMargins -> 0]

which gives

enter image description here

Besides this, you have two unknown variables within show. I removed Show completely.

$\endgroup$
3
  • $\begingroup$ Dear Mister (Professor) ercegovac- I followed your instructions. My NDSolve expression now works inside of my Manipulate. One question remains. How do I restore the data plots you removed? My goal is to hand fit the NDSolve expression against a set of data points (t,y) that I might be able to call upon NonlinearModelFit. Please clarify. Thank You. $\endgroup$
    – OKCarl
    Oct 12, 2017 at 19:16
  • $\begingroup$ Well, it should work if you just wrap them with Show. If there is an issue, try to create a minimum working example to illustrate the problem. Accept this answer if it's resolving your issue. Also, besides the previous link that i left in the comment under your question, I recommend you reading this since you seam to be new to this website as well. $\endgroup$
    – ercegovac
    Oct 12, 2017 at 19:35
  • $\begingroup$ A thousand Thank You's Professor ercegovac! With your guidance I now have a Manipulate exactly to my needs! Thanks Again! $\endgroup$
    – OKCarl
    Oct 12, 2017 at 23:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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