# Opening notebook immediately produces error message [closed]

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?

• 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. – halirutan Nov 22 '17 at 2:22
• Awesome, I just used this and it worked well. Thank you! – c12uel Nov 22 '17 at 11:29

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

• 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. – c12uel Nov 22 '17 at 11:24
• @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. – Nasser Nov 22 '17 at 11:37