I have a numerically solved a differential equation which has an equilibrium value and a starting point, both included as a parameter. I want to plot the difference between the times it takes to come into a certain proximity of the equilibrium value starting from +/- starting parameter. I'll show you the code and hopefully things get more clear:
bsp[c_, d_] := NDSolve[{x'[t] == -x[t] + d, x[0] == c}, x, {t, 0, 30}]
(* defines the differential eqn *)
bspdiff[d_] := Module[{d1 = d, func, func2, diff, par, x, t, t1},
func := x /. First[bsp[par, d1]];
func2 := x /. First[bsp[-par, d1]];
Plot[
Abs[(t /. FindRoot[func[t] == 1.01*d1, {t, 2}]) - (t1 /. FindRoot[func2[t1] == 0.99*d1,
{t1, 2}])],
{par, 1, 5}]]
As you see, I define two functions, one for the positive, one for the negative value of the starting point. Then I use FindRoot
to obtain the times at which they are within 1% of the equilibrium value and deduct those two. I want to plot this result as a function of the starting point. What I get upon executing this module is an empty plot and:
FindRoot::nlnum: The function value {-2.02+x\$70015[2.]} is not a list of numbers with dimensions {1} at {t\$70015} = {2.}. >>
ReplaceAll::reps: {FindRoot[func\$70015[t\$70015]==1.01 2,{t\$70015,2}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
FindRoot::nlnum: The function value {-1.98+x\$70015[2.]} is not a list of numbers with dimensions {1} at {t1\$70015} = {2.}. >>
ReplaceAll::reps: {FindRoot[func2\$70015[t1\$70015]==0.99 2,{t1\$70015,2}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
FindRoot::nlnum: The function value {-1.98+x\$70015[2.]} is not a list of numbers with dimensions {1} at {t1\$70015} = {2.}. >>
General::stop: Further output of FindRoot::nlnum will be suppressed during this calculation. >>
ReplaceAll::reps: {FindRoot[func2\$70015[t1$70015]==0.99 2.,{t1\$70015,2.}]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing. >>
General::stop: Further output of ReplaceAll::reps will be suppressed during this calculation. >>
When I however execute this manually for single values, it works:
func := x /. First[bsp[2, 1]];
func2 := x /. First[bsp[-2, 1]];
Abs[(t /. FindRoot[func[t] == 1.01, {t, 2}]) - (t1 /.
FindRoot[func2[t1] == 0.99, {t1, 2}])]
yields a numerical output. What I do not understand - where is my mistake/why does it not work? Or is there a better way to do this? I would be very happy about any help and suggestions.