The Problem
The Manipulate
and the first Plot
of your code don't work because you use a combination of SetDelayed
(:=
) and ReplaceAll
(/.
) that doesn't behave like you expected.
When you define
g[t_, f0_, f1_, ff1_] := f[t] /. sol[[1]]
and now evaluate
g[2, 1, 1, 1]
you get
f0 + 4 (-3 f0 + 3 f1 - ff1) + 8 (2 f0 - 2 f1 + ff1)
instead of the expacted
5
This happens because (due to the use of :=
) f[t]
on the rhs is first evaluated as f[2]
(giving a + 4 c + 8 d
) and then the replacement (/. sol[[1]]
) is done.
Using Set
instead of SetDelayed
If you replace :=
with =
g2[t_, f0_, f1_, ff1_] = f[t] /. sol[[1]]
then /.
will be performed only once, when you define g2
and whenever you use g2
it is replaced by its rhs with the ReplacedAll
already performed.
g2[2, 1, 1, 1]
5
Using Evaluate
You can also force the evaluation of the right-hand side by using Evaluate
:
g3[t_, f0_, f1_, ff1_] := Evaluate[f[t] /. sol[[1]]]
This way the rhs is evaluated before it is SetDelayed
as the definition of g3
.
Definition@g3
g3[t_, f0_, f1_, ff1_] := f0 + (-3 f0 + 3 f1 - ff1) t^2 + (2 f0 - 2 f1 + ff1) t^3
Using an additional ReplaceAll
You can also use an additional /.
to replace the parameters f0
, f1
, and ff1
with some values
g3[t_g4[t_, pf0_, pf1_, pff1_] := f[t] /. sol[[1]] /. {f0 -> pf0, f1 -> pf1, ff1 -> pff1}
now
g3[2g4[2, 1, 1, 1]
5
This is similar to the last Plot
in your code.