Como substituir a expressão no ninho?
Dec 11 2020
Preciso calcular a seguinte função com substituição em recorrência
f[x0_, y0_]:= (s1 + s2)/t1 /. {s1 ->
NDSolveValue[{x''[t] + x[t] == 0., y''[t] + x[t]^2 y[t] == 0.,
x[0.] == x0, x'[0.] == 0., y[0.] == y0, y'[0.] == 0.},
y, {t, 0, t1}][t1], s2 -> NDSolveValue[{x''[t] + x[t] == 0, y''[t] + x[t]^2 y[t] == 0.,
x[0.] == x0, x'[0.] == 0., y[0.] == y0, y'[0.] == 1.},
y, {t, 0, t1}][t1]} /. {t1 -> Take[Reap[
NDSolve[{x''[t] + x[t] == 0., x[0.] == x0, x'[0.] == 0.,
WhenEvent[x'[t] > 0., {Sow[t], "StopIntegration"}]},
x, {t, 0., 100.},
MaxStepSize -> 0.001]], {2, -1}][[1]][[1]][[1]]}
Mas encontro um erro que diz "NDSolveValue: Endpoint t1 em {t, 0., t1} não é um número real".
A expressão chave é
NDSolveValue[{x''[t] + x[t] == 0, y''[t] + x[t]^2 y[t] == 0.,
x[0.] == 1., x'[0.] == 0., y[0.] == 1., y'[0.] == 0.},
y, {t, 0, Re[t1]}][Re[t1]] /. {t1 -> Take[Reap[
NDSolve[{x''[t] + x[t] == 0, x[0.] == 1., x'[0.] == 0.,
WhenEvent[x'[t] > 0., {Sow[t], "StopIntegration"}]},
x, {t, 0., 100.},
MaxStepSize -> 0.001]], {2, -1}][[1]][[1]][[1]]}
que vem o erro. Como posso resolver esse problema?
Respostas
3 MichaelE2 Dec 12 2020 at 08:26
Faça a t1
reposição na s1,s2
substituição, agrupando com parênteses:
ff[x0_, y0_] := s1 + s2 /. (
{s1 :>
NDSolveValue[{x''[t] + x[t] == 0., y''[t] + x[t]^2 y[t] == 0.,
x[0.] == x0, x'[0.] == 0., y[0.] == y0, y'[0.] == 0.},
y, {t, 0, t1}][t1],
s2 :> NDSolveValue[{x''[t] + x[t] == 0,
y''[t] + x[t]^2 y[t] == 0., x[0.] == x0, x'[0.] == 0.,
y[0.] == y0, y'[0.] == 1.}, y, {t, 0, t1}][t1]} /. {t1 ->
NDSolveValue[{x''[t] + x[t] == 0., x[0.] == x0, x'[0.] == 0.,
WhenEvent[x'[t] > 0., {"StopIntegration"}]},
Indexed[x["Domain"], {1, -1}], {t, 0., 100.},
MaxStepSize -> 0.001]}
);
ff[-1, -3]
(* -2.54137 *)
Alternativo:
ff[x0_, y0_] :=
Block[{t1 =
NDSolveValue[{x''[t] + x[t] == 0., x[0.] == x0, x'[0.] == 0.,
WhenEvent[x'[t] > 0., {"StopIntegration"}]},
Indexed[x["Domain"], {1, -1}], {t, 0., 100.},
MaxStepSize -> 0.001]},
s1 + s2 /.
{s1 :>
NDSolveValue[{x''[t] + x[t] == 0., y''[t] + x[t]^2 y[t] == 0.,
x[0.] == x0, x'[0.] == 0., y[0.] == y0, y'[0.] == 0.},
y, {t, 0, t1}][t1],
s2 :> NDSolveValue[{x''[t] + x[t] == 0,
y''[t] + x[t]^2 y[t] == 0., x[0.] == x0, x'[0.] == 0.,
y[0.] == y0, y'[0.] == 1.}, y, {t, 0, t1}][t1]}
];
O que significa um erro “Não é possível encontrar o símbolo” ou “Não é possível resolver o símbolo”?
George Harrison ficou chateado por suas letras de 'Hurdy Gurdy Man' de Donovan não terem sido usadas