0
$\begingroup$

Whenever I open the file I get the following as an error

ReplaceAll::reps: {Sol} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing.

but as soon as I hit shift+enter the errors go away and the code runs as intended.

Here's a condensed form of the code:

ks = 100 ;
kd = 10;
g = 9.81; 
l = 0.1;
Sol = NDSolve[{

xab'[t] == xbb[t],
yab'[t] == ybb[t],
xbb'[t] == -ks*(1 - (l/((xab[t] - 0)^2 + (yab[t] - 0)^2)^(.5)))*(xab[t] - 0)-ks*(1 - (l/((xab[t])^2 + (yab[t])^2)^(.5)))*(xab[t]) - kd*(xbb[t] - 0) - kd*(xbb[t]),
ybb'[t] ==  -ks*(1 - (l/((xab[t] - 0)^2 + (yab[t] - 0)^2)^(.5)))*(yab[t] - 0) - ks*(1 - (l/((xab[t])^2 + (yab[t])^2)^(.5)))*(yab[t]) - kd*(ybb[t] - 0) - kd*(ybb[t] - g),

xab[0] == 0.1, yab[0] ==  0, xbb[0] == 0, ybb[0] == 0},

{xab, xbb, yab, ybb}, {t, 0, 100}];

Manipulate[
ListLinePlot[{{0, 0}, {Part[xab[t] /. Sol, 1], 
Part[yab[t] /. Sol, 1]}, {1.2, 0}},
PlotMarkers -> Automatic, PlotRange -> {{0, 1.2}, {1, -10}}, 
PlotStyle -> Directive[Grey, Thick], PlotTheme -> "Detailed"], {t, 
0, 10}]

I used to have Evaluate within the Manipulate command, but read that these errors occur because mathematica is trying to evaluate the function symbollically and then numerically, but I'm still getting an error and not sure why?

$\endgroup$

closed as off-topic by m_goldberg, LCarvalho, MarcoB, gwr, rcollyer Nov 30 '17 at 21:59

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question arises due to a simple mistake such as a trivial syntax error, incorrect capitalization, spelling mistake, or other typographical error and is unlikely to help any future visitors, or else it is easily found in the documentation." – m_goldberg, LCarvalho, MarcoB, gwr, rcollyer
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    $\begingroup$ You're most likely getting this error because the visible manipulate is evaluated instantly but it doesn't have Sol because, for this, you first need to evaluate the NDSolve calculation. Check out the option SaveDefinition of Manipulate. $\endgroup$ – halirutan Nov 22 '17 at 2:22
  • $\begingroup$ Awesome, I just used this and it worked well. Thank you! $\endgroup$ – c12uel Nov 22 '17 at 11:29
2
$\begingroup$

You need to add the solving of NDSolve to the initialization so that when Manipulate runs, it has the sol value.

In addition, you can't use t as slider variable and also use it for the solution of ODE in NDSolve. So I changed that. There are other issues you have, but at least this get rid of the error you mentioned

Manipulate[
 ListLinePlot[
  {{0, 0}, {Part[(xab[t] /. sol) /. t -> t0, 1], 
    Part[(yab[t] /. sol) /. t -> t0, 1]}, {1.2, 0}},
  PlotMarkers -> Automatic, 
  PlotRange -> {{0, 1.2}, {1, -10}},
  PlotStyle -> Directive[Gray, Thick],
  PlotTheme -> "Detailed"],
 {t0, 0, 10}, (*changed from t to t0 so not conflict*)

 Initialization :>
  (
   ks = 100;
   kd = 10;
   g = 9.81;
   l = 0.1;
   sol = NDSolve[{xab'[t] == xbb[t], yab'[t] == ybb[t], 
      xbb'[t] == -ks*(1 - (l/((xab[t] - 0)^2 + (yab[t] - 
                   0)^2)^(.5)))*(xab[t] - 0) - 
        ks*(1 - (l/((xab[t])^2 + (yab[t])^2)^(.5)))*(xab[t]) - 
        kd*(xbb[t] - 0) - kd*(xbb[t]), 
      ybb'[t] == -ks*(1 - (l/((xab[t] - 0)^2 + (yab[t] - 
                   0)^2)^(.5)))*(yab[t] - 0) - 
        ks*(1 - (l/((xab[t])^2 + (yab[t])^2)^(.5)))*(yab[t]) - 
        kd*(ybb[t] - 0) - kd*(ybb[t] - g), xab[0] == 0.1, yab[0] == 0,
       xbb[0] == 0, ybb[0] == 0}, {xab, xbb, yab, ybb}, {t, 0, 100}]
   )
 ]
$\endgroup$
  • $\begingroup$ Hmmm, so I know this works, but is there a way to do it where I can use other functions with sol such as plot? When I try, it looks like it only works with that one function, manipulate. $\endgroup$ – c12uel Nov 22 '17 at 11:24
  • $\begingroup$ @c12uel yes you can. sol actually is global. From any other cell, just type sol and you'll see it is there. So you can use sol from outside Manipulate. The variables inside the initialization section as they are in above, are actually all global variables. $\endgroup$ – Nasser Nov 22 '17 at 11:37

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