(*-----------------------------------------------------------------------------------------*)
In[]:=
ClearAll["`*"];Remove["`*"];
In[]:=
sol[P_]:=NDSolve[{y''[x]+Sin[x]y[x]^3==-1,y[-Pi]==0,y'[-Pi]==P},y,{x,-Pi,Pi}]
In[]:=
FindRoot[y[Pi]/.sol[P],{P,0.7}]
NDSolve
:Initial condition P is not a number or a rectangular array of numbers.
ReplaceAll
:NDSolveSin[x]
3
y[1]
+
′′
y
[x]-1,y[-π]0,
′
y
[-π]P,y,{x,-π,π} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing.
Out[]=
{P0.730366}
(*-----------------------------------------------------------------------------------------*)
In[]:=
ClearAll["`*"];Remove["`*"];
In[]:=
sol1[P_?NumericQ]:=NDSolve[{y''[x]+Sin[x]y[x]^3==-1,y[-Pi]==0,y'[-Pi]==P},y,{x,-Pi,Pi}]
In[]:=
FindRoot[y[Pi]/.sol1[P],{P,0.7}]
ReplaceAll
:{sol1[P]} is neither a list of replacement rules nor a valid dispatch table, and so cannot be used for replacing.
Out[]=
{P0.730366}
(*-----------------------------------------------------------------------------------------*)
In[]:=
ClearAll["`*"];Remove["`*"];
In[]:=
sol2[P_?NumericQ]:=First[y[Pi]/.NDSolve[{y''[x]+Sin[x]y[x]^3==-1,y[-Pi]==0,y'[-Pi]==P},y,{x,-Pi,Pi}]]
In[]:=
FindRoot[sol2[P],{P,0.7}]
Out[]=
{P0.730366}
(*---Anotherway:--------------------------------------------------------------------------------------*)
In[]:=
ClearAll["`*"];Remove["`*"];
In[]:=
sol4=ParametricNDSolve[{y''[x]+Sin[x]y[x]^3==-1,y[-Pi]==0,y'[-Pi]==P},y,{x,-Pi,Pi},{P}];
In[]:=
Plot[y[x][Pi]/.sol4,{x,0.5,1.2}]
Out[]=
In[]:=
FindRoot[(y[x][Pi]/.sol4),{x,0.7}]
Out[]=
{x0.730366}
(*---------------------------------------------------------------------------------------------*)