Function Repository Resource:

SolutionRulesToFunctions

Source Notebook

Convert a rule between expressions that depend on one or more variables to a pure function rule

Contributed by: E. Chan-López & Héctor Argote Morales

ResourceFunction["SolutionRulesToFunctions"][var[params]expr]

converts the expr to a Function defining var for values of params.

Details

ResourceFunction["SolutionRulesToFunctions"] threads over lists.

Examples

Basic Examples (3) 

Convert a rule y[t]a x[t]:

In[1]:=
ResourceFunction["SolutionRulesToFunctions"][y[t] -> a x[t]]
Out[1]=

Convert multiple rules:

In[2]:=
ResourceFunction[
 "SolutionRulesToFunctions"][{y[t] -> a x[t], z[t] -> c b w[t]}]
Out[2]=

Convert a rule with a fixed parameter:

In[3]:=
ResourceFunction["SolutionRulesToFunctions"][
 sol[x, 0, t] -> Sinc[x - t]]
Out[3]=

Scope (1) 

Use SolutionRulesToFunctions with more complicated Rules:

In[4]:=
ResourceFunction["SolutionRulesToFunctions"][
 y[x, \[Tau]] -> \[Theta][x, \[Tau]] w[x, \[Tau]]]
Out[4]=
In[5]:=
ResourceFunction["SolutionRulesToFunctions"]@
 ResourceFunction["SolutionRulesToFunctions"][x[t][\[Alpha]] -> s[t]]
Out[5]=

Applications (4) 

Use SolutionRulesToFunctions with the results of DSolve:

In[6]:=
eqn = y'[x] + y[x] == a Sin[x];
DSolve[{eqn, y[0] == 0}, y[x], x]
Out[7]=
In[8]:=
ResourceFunction["SolutionRulesToFunctions"][%]
Out[8]=

Verify the solution:

In[9]:=
ReplaceAll[Part[%, 1]][eqn]
Out[9]=

Solve an initial value problem for an inhomogeneous linear hyperbolic system with constant coefficients:

In[10]:=
eqns = {D[u[x, t], t] == D[v[x, t], x] + 1, D[v[x, t], t] == -D[u[x, t], x] - 1};
ic = {u[x, 0] == Cos[x]^2, v[x, 0] == Sin[x]};
sol = DSolve[{eqns, ic}, {u[x, t], v[x, t]}, {x, t}] // FullSimplify
Out[12]=

Use SolutionRulesToFunctions on the solution:

In[13]:=
ResourceFunction["SolutionRulesToFunctions"][sol]
Out[13]=

Verify the solutions:

In[14]:=
FullSimplify@ReplaceAll[Part[%, 1]][eqns]
Out[14]=

Use SolutionRulesToFunctions with an initial value problem for the heat equation:

In[15]:=
heqn = D[u[x, t], t] == D[u[x, t], {x, 2}];
ic = u[x, 0] == E^(-x^2);
sol = DSolve[{heqn, ic }, u[x, t], {x, t}]
Out[17]=
In[18]:=
ResourceFunction["SolutionRulesToFunctions"][%]
Out[18]=

Verify the solution:

In[19]:=
Simplify@ReplaceAll[Part[%, 1]][heqn]
Out[19]=

Use SolutionRulesToFunctions on the Euler-Lagrange equations for a rolling wheel on an inclined plane. In this problem, the translational coordinate y and rotational coordinate θ are assumed to be independent, and constraints are used to determine the Lagrange multiplier for obtaining the equations of motion in terms of the independent coordinates.

The equation of motion for the translational coordinate and its initial conditions:

In[20]:=
eqny = {2/3 m g Sin[\[Alpha]] - m y''[t] == 0};
In[21]:=
icvy = {y[0] == y0, y'[0] == v0};

The solution for equation of motion using DSolve:

In[22]:=
Simplify@DSolve[Join[eqny, icvy], y[t], t]
Out[22]=

Apply SolutionRulesToFunctions:

In[23]:=
ResourceFunction["SolutionRulesToFunctions"][Part[%, 1]]
Out[23]=

Verify the solution:

In[24]:=
Part[ReplaceAll[%][eqny], 1]
Out[24]=

Use the resource function SymbolToSubscript to format some parts of the solution as subscripts:

In[25]:=
ResourceFunction["SymbolToSubscript"][
 ResourceFunction["SolutionRulesToFunctions"][
  Simplify@DSolve[Join[eqny, icvy], y[t], t]]]
Out[25]=

The equation of motion for the rotational coordinate and its initial conditions:

In[26]:=
eqn\[Theta] = {1/3 m R g Sin[\[Alpha]] - 1/2 m R^2 \[Theta]''[t] == 0};
In[27]:=
icv\[Theta] = {\[Theta][0] == \[Theta]0, \[Theta]'[0] == \[Omega]0};

The solution for this equation of motion using DSolve:

In[28]:=
Simplify@DSolve[Join[eqn\[Theta], icv\[Theta]], \[Theta][t], t]
Out[28]=
In[29]:=
ResourceFunction["SolutionRulesToFunctions"][Part[%, 1]]
Out[29]=

Verify the solution:

In[30]:=
Part[ReplaceAll[%][eqn\[Theta]], 1]
Out[30]=

Use SymbolToSubscript to format some parts of the solution as subscripts:

In[31]:=
ResourceFunction["SymbolToSubscript"][
 ResourceFunction["SolutionRulesToFunctions"][
  Simplify@DSolve[Join[eqn\[Theta], icv\[Theta]], \[Theta][t], t]]]
Out[31]=

Properties and Relations (1) 

Use SolutionRulesToFunctions with the resource function RuleReverse:

In[32]:=
ResourceFunction["SolutionRulesToFunctions"][
 ResourceFunction["RuleReverse"][x[t][\[Alpha]] -> s[t]]]
Out[32]=

Publisher

Ramón Eduardo Chan López

Version History

  • 1.0.0 – 09 August 2022

Source Metadata

Related Resources

License Information