Message Boards Message Boards

Define a function and then use it in an equation?

GROUPS:

I defined a function and then substituted it into an equation in two ways. The first way (eq1, q2, eq2) didn't work, and the second (3, eq3) otherwise.

In[159]:= eq1 = (
  A1 Sqrt[Dc] Sqrt[Dp]
    E^((lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
    Dp]))/((Sqrt[s] + Sqrt[Dp] \[Alpha]1) (-Dc s + 
     Dp (s + \[Theta]c))) - (
  A1 Dc E^((lh Sqrt[s + \[Theta]c])/Sqrt[Dc] - (
    x Sqrt[s + \[Theta]c])/Sqrt[Dc]))/(
  Sqrt[s + \[Theta]c] (-Dc s + Dp (s + \[Theta]c))) + (
  A1 Dc Sqrt[Dp]
    E^((lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
    Dp]) \[Alpha]1)/((Sqrt[s] + Sqrt[Dp] \[Alpha]1) Sqrt[
   s + \[Theta]c] (-Dc s + Dp (s + \[Theta]c))) + (-A2 + 
   pinf (s + \[Theta]c))/(s (s + \[Theta]c)) - (
  Sqrt[Dp] E^((lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
    Dp]) \[Alpha]1 (-A2 + pinf (s + \[Theta]c)))/(
  s (Sqrt[s] + Sqrt[Dp] \[Alpha]1) (s + \[Theta]c)) + 
  E^((Sqrt[s] x)/Sqrt[Dp]) b[1][s] + (
  E^((2 lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
    Dp]) (Sqrt[s] - Sqrt[Dp] \[Alpha]1) b[1][s])/(
  Sqrt[s] + Sqrt[Dp] \[Alpha]1)

Out[159]= (
 A1 Sqrt[Dc] Sqrt[Dp]
   E^((lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
   Dp]))/((Sqrt[s] + Sqrt[Dp] \[Alpha]1) (-Dc s + 
    Dp (s + \[Theta]c))) - (
 A1 Dc E^((lh Sqrt[s + \[Theta]c])/Sqrt[Dc] - (x Sqrt[s + \[Theta]c])/
   Sqrt[Dc]))/(Sqrt[s + \[Theta]c] (-Dc s + Dp (s + \[Theta]c))) + (
 A1 Dc Sqrt[Dp]
   E^((lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
   Dp]) \[Alpha]1)/((Sqrt[s] + Sqrt[Dp] \[Alpha]1) Sqrt[
  s + \[Theta]c] (-Dc s + Dp (s + \[Theta]c))) + (-A2 + 
  pinf (s + \[Theta]c))/(s (s + \[Theta]c)) - (
 Sqrt[Dp] E^((lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
   Dp]) \[Alpha]1 (-A2 + pinf (s + \[Theta]c)))/(
 s (Sqrt[s] + Sqrt[Dp] \[Alpha]1) (s + \[Theta]c)) + 
 E^((Sqrt[s] x)/Sqrt[Dp]) b[1][s] + (
 E^((2 lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
   Dp]) (Sqrt[s] - Sqrt[Dp] \[Alpha]1) b[1][s])/(
 Sqrt[s] + Sqrt[Dp] \[Alpha]1)

In[160]:= q2[x_, s_] := eq1

In[161]:= eq2 = \[Alpha]1 q2[lh, s] - 
\!\(\*SuperscriptBox[\(q2\), \*
TagBox[
RowBox[{"(", 
RowBox[{"1", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[lh, s] == 0

Out[161]= \[Alpha]1 ((
    A1 Sqrt[Dc] Sqrt[Dp]
      E^((lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
      Dp]))/((Sqrt[s] + Sqrt[Dp] \[Alpha]1) (-Dc s + 
       Dp (s + \[Theta]c))) - (
    A1 Dc E^((lh Sqrt[s + \[Theta]c])/Sqrt[Dc] - (
      x Sqrt[s + \[Theta]c])/Sqrt[Dc]))/(
    Sqrt[s + \[Theta]c] (-Dc s + Dp (s + \[Theta]c))) + (
    A1 Dc Sqrt[Dp]
      E^((lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
      Dp]) \[Alpha]1)/((Sqrt[s] + Sqrt[Dp] \[Alpha]1) Sqrt[
     s + \[Theta]c] (-Dc s + Dp (s + \[Theta]c))) + (-A2 + 
     pinf (s + \[Theta]c))/(s (s + \[Theta]c)) - (
    Sqrt[Dp] E^((lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
      Dp]) \[Alpha]1 (-A2 + pinf (s + \[Theta]c)))/(
    s (Sqrt[s] + Sqrt[Dp] \[Alpha]1) (s + \[Theta]c)) + 
    E^((Sqrt[s] x)/Sqrt[Dp]) b[1][s] + (
    E^((2 lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
      Dp]) (Sqrt[s] - Sqrt[Dp] \[Alpha]1) b[1][s])/(
    Sqrt[s] + Sqrt[Dp] \[Alpha]1)) == 0

In[162]:= 
q3[x_, s_] := (
  A1 Sqrt[Dc] Sqrt[Dp]
    E^((lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
    Dp]))/((Sqrt[s] + Sqrt[Dp] \[Alpha]1) (-Dc s + 
     Dp (s + \[Theta]c))) - (
  A1 Dc E^((lh Sqrt[s + \[Theta]c])/Sqrt[Dc] - (
    x Sqrt[s + \[Theta]c])/Sqrt[Dc]))/(
  Sqrt[s + \[Theta]c] (-Dc s + Dp (s + \[Theta]c))) + (
  A1 Dc Sqrt[Dp]
    E^((lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
    Dp]) \[Alpha]1)/((Sqrt[s] + Sqrt[Dp] \[Alpha]1) Sqrt[
   s + \[Theta]c] (-Dc s + Dp (s + \[Theta]c))) + (-A2 + 
   pinf (s + \[Theta]c))/(s (s + \[Theta]c)) - (
  Sqrt[Dp] E^((lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
    Dp]) \[Alpha]1 (-A2 + pinf (s + \[Theta]c)))/(
  s (Sqrt[s] + Sqrt[Dp] \[Alpha]1) (s + \[Theta]c)) + 
  E^((Sqrt[s] x)/Sqrt[Dp]) b[1][s] + (
  E^((2 lh Sqrt[s])/Sqrt[Dp] - (Sqrt[s] x)/Sqrt[
    Dp]) (Sqrt[s] - Sqrt[Dp] \[Alpha]1) b[1][s])/(
  Sqrt[s] + Sqrt[Dp] \[Alpha]1)

In[163]:= eq3 = \[Alpha]1 q3[lh, s] - 
\!\(\*SuperscriptBox[\(q3\), \*
TagBox[
RowBox[{"(", 
RowBox[{"1", ",", "0"}], ")"}],
Derivative],
MultilineFunction->None]\)[lh, s] == 0 // Simplify // Normal

Out[163]= True

enter image description here

enter image description here

POSTED BY: Zhonghui Ou
Answer
10 days ago

The first method above is not a good way to define a function. SetDelayed ( := ) has attribute HoldAll. Here is a simpler example that demonstrates the issue:

In[92]:= eq = x;
In[93]:= f[x_] := eq
In[94]:= DownValues[f]
Out[94]= {HoldPattern[f[x_]] :> eq}

Notice that f will evaluate to 'eq', not 'x' as you might have expected. Mathematica will then replace eq with it's OwnValue ('x') following this evaluation. The symbol 'x' is not the function's input. If we then try to evaluate f[3], we see what surprised you:

In[95]:= f[3]
Out[95]= x

So, you should always use the 2nd method for defining your functions in order to ensure that the patterns you expect to match actually do match.

In[96]:= f2[x_] := x

In[97]:= DownValues[f2]

Out[97]= {HoldPattern[f2[x_]] :> x}

In[98]:= f2[3]

Out[98]= 3
POSTED BY: Chad Knutson
Answer
9 days ago

Dear Chad, Thanks for you reply.

With best regards, Jacques

POSTED BY: Zhonghui Ou
Answer
5 days ago

Group Abstract Group Abstract